added MIT license and created headers and code files for all the types

This commit is contained in:
Iris Lightshard 2021-07-04 23:44:58 -06:00
parent 0cc1835a02
commit 2d4f1f5ec4
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
17 changed files with 203 additions and 75 deletions

8
LICENSE Normal file
View file

@ -0,0 +1,8 @@
Copyright 2021 Derek Stevens
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

12
aux.c Normal file
View file

@ -0,0 +1,12 @@
#include <u.h>
#include <libc.h>
#include "aux.h"
Aux* create_aux(FileType t) {
Aux* self = (Aux*)malloc(sizeof(Aux));
self->type = t;
self->data = nil;
self->count = 0;
return self;
}

9
aux.h Normal file
View file

@ -0,0 +1,9 @@
typedef enum { CTL = 1, USERS, CARTS, SLOT, DATA, REALMS, UNIVERSE } FileType;
typedef struct Aux {
FileType type;
char* data;
int count;
} Aux;
Aux* create_aux(FileType t);

View file

@ -1,6 +1,6 @@
#!/bin/sh
clang-format -i ./*.c
clang-format -i ./*.c ./*.h
mk clean
mk o.xrxs

0
cart.c Normal file
View file

7
cart.h Normal file
View file

@ -0,0 +1,7 @@
typedef struct Cart {
char name[32];
char* cart_rom;
char* cart_txt_data;
char* cart_sprite_data;
char* cart_audio_data;
} Cart;

12
command.h Normal file
View file

@ -0,0 +1,12 @@
typedef enum {
LOGIN = 208176873,
LOAD = 5626172,
CREATE = 7083959236,
PROTECT = 295480618573,
ENTER = 195024746,
LEAVE = 207662601,
LOGOUT = 7702552890,
SAVE = 5962355,
RESET = 218931595,
UNLOAD = 8325026851
} Command;

2
err.h Normal file
View file

@ -0,0 +1,2 @@
#define EUNAME "username is already taken"
#define EUFULL "users table is full"

0
realm.c Normal file
View file

8
realm.h Normal file
View file

@ -0,0 +1,8 @@
typedef struct Universe Universe;
typedef struct Realm {
char name[32];
ushort max;
uvlong password;
Universe* universe;
} Realm;

0
universe.c Normal file
View file

11
universe.h Normal file
View file

@ -0,0 +1,11 @@
typedef unsigned int unit;
typedef struct Atom {
char name[16];
char value[64];
} Atom;
typedef struct Universe {
uint count;
Atom** data;
} Universe;

0
user.c Normal file
View file

8
user.h Normal file
View file

@ -0,0 +1,8 @@
typedef struct Cart Cart;
typedef struct Realm Realm;
typedef struct UserInfo {
char name[32];
uvlong password;
Cart* cart;
Realm* realm;
} UserInfo;

92
util.c Normal file
View file

@ -0,0 +1,92 @@
#include <u.h>
#include "util.h"
uvlong hash(char* str) {
uvlong h;
uchar* p;
h = 0;
for (p = (unsigned char*)str; *p != '\0'; p++)
h = 37 * h + *p;
return h; // or, h % ARRAY_SIZE;
}
char clca(char c) {
return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c;
} /* char to lowercase */
char cuca(char c) {
return c >= 'a' && c <= 'z' ? c - ('a' - 'A') : c;
} /* char to uppercase */
int slen(char* s) {
int i = 0;
while (s[i] && s[++i]) {
;
}
return i;
} /* string length */
char* st__(char* s, char (*fn)(char)) {
int i = 0;
char c;
while ((c = s[i]))
s[i++] = fn(c);
return s;
}
char* stuc(char* s) { return st__(s, cuca); } /* string to uppercase */
char* stlc(char* s) { return st__(s, clca); } /* string to lowercase */
char* scpy(char* src, char* dst, int len) {
int i = 0;
while ((dst[i] = src[i]) && i < len - 2)
i++;
dst[i + 1] = '\0';
return dst;
} /* string copy */
int scmp(char* a, char* b) {
int i = 0;
while (a[i] == b[i])
if (!a[i++])
return 1;
return 0;
} /* string compare */
char* scsw(char* s, char a, char b) {
int i = 0;
char c;
while ((c = s[i]))
s[i++] = c == a ? b : c;
return s;
} /* string char swap */
char* scat(char* dst, const char* src) {
char* ptr = dst + slen(dst);
while (*src)
*ptr++ = *src++;
*ptr = '\0';
return dst;
} /* string cat */
int ssin(char* s, char* ss) {
int a = 0, b = 0;
while (s[a]) {
if (s[a] == ss[b]) {
if (!ss[b + 1])
return a - b;
b++;
} else
b = 0;
a++;
}
return -1;
} /* string substring index */
char* ccat(char* dst, char c) {
int len = slen(dst);
dst[len] = c;
dst[len + 1] = '\0';
return dst;
}

15
util.h Normal file
View file

@ -0,0 +1,15 @@
typedef unsigned long long uvlong;
uvlong hash(char* str);
char clca(char c);
char cuca(char c);
int slen(char* str);
char* st__(char* str, char (*fn)(char c));
char* stuc(char* str);
char* stlc(char* str);
char* scpy(char* src, char* dst, int len);
int scmp(char* a, char* b);
char* scsw(char* str, char a, char b);
char* scat(char* dst, const char* src);
int ssin(char* str, char* substr);
char* ccat(char* dst, char c);

92
xrxs.c
View file

@ -6,84 +6,21 @@
#include <stdio.h>
#include <dirent.h>
#include <libString.h>
#include <mp.h>
#include <libsec.h>
#include "err.h"
#include "command.h"
#include "util.h"
#include "aux.h"
#include "cart.h"
#include "universe.h"
#include "realm.h"
#include "user.h"
/* clang-format off */
char clca(char c) { return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c; } /* char to lowercase */
char cuca(char c) { return c >= 'a' && c <= 'z' ? c - ('a' - 'A') : c; } /* char to uppercase */
int slen(char *s) { int i = 0; while(s[i] && s[++i]) { ; } return i; } /* string length */
char *st__(char *s, char (*fn)(char)) { int i = 0; char c; while((c = s[i])) s[i++] = fn(c); return s; }
char *stuc(char *s) { return st__(s, cuca); } /* string to uppercase */
char *stlc(char *s) { return st__(s, clca); } /* string to lowercase */
char *scpy(char *src, char *dst, int len) { int i = 0; while((dst[i] = src[i]) && i < len - 2) i++; dst[i + 1] = '\0'; return dst; } /* string copy */
int scmp(char *a, char *b) { int i = 0; while(a[i] == b[i]) if(!a[i++]) return 1; return 0; } /* string compare */
char *scsw(char *s, char a, char b) { int i = 0; char c; while((c = s[i])) s[i++] = c == a ? b : c; return s; } /* string char swap */
char *scat(char *dst, const char *src) { char *ptr = dst + slen(dst); while(*src) *ptr++ = *src++; *ptr = '\0'; return dst; } /* string cat */
int ssin(char *s, char *ss) { int a = 0, b = 0; while(s[a]) { if(s[a] == ss[b]) { if(!ss[b + 1]) return a - b; b++; } else b = 0; a++; } return -1; } /* string substring index */
char *ccat(char *dst, char c) { int len = slen(dst); dst[len] = c; dst[len + 1] = '\0'; return dst; }
/* clang-format on */
int chatty9p = 1;
static char Ebad[] = "something bad happened";
static char Enomem[] = "no memory";
static char Euname[] = "username is already taken";
static Tree* tree;
typedef enum {
LOGIN = 208176873,
LOAD = 5626172,
CREATE = 7083959236,
PROTECT = 295480618573,
ENTER = 195024746,
LEAVE = 207662601,
LOGOUT = 7702552890,
SAVE = 5962355,
RESET = 218931595,
UNLOAD = 8325026851
} Command;
typedef enum { CTL = 1, USERS, CARTS, SLOT, DATA, REALMS, UNIVERSE } FileType;
typedef struct Aux Aux;
struct Aux {
FileType type;
char* data;
int count;
};
Aux* create_aux(FileType t) {
Aux* self = (Aux*)malloc(sizeof(Aux));
self->type = t;
self->data = nil;
self->count = 0;
return self;
}
typedef struct UserInfo UserInfo;
struct UserInfo {
char name[32];
uvlong password;
char realm[32];
char cart[32];
};
static UserInfo users_table[64];
uvlong hash(char* str) {
uvlong h;
uchar* p;
h = 0;
for (p = (unsigned char*)str; *p != '\0'; p++)
h = 37 * h + *p;
return h; // or, h % ARRAY_SIZE;
}
void xrxs_attach(Req* r) {
/* As it is, once the user detaches, they will stay in the table
* until the server restarts. We have to figure out some way
@ -92,6 +29,7 @@ void xrxs_attach(Req* r) {
*/
int i = 0;
int l = 0;
int vacancy = 0;
char* usr;
char* username = r->ifcall.uname;
usr = username;
@ -102,15 +40,20 @@ void xrxs_attach(Req* r) {
for (i = 0; i < 64; i++) {
usr = users_table[i].name;
if (scmp(usr, username)) {
respond(r, Euname);
respond(r, EUNAME);
return;
}
if (*usr == 0) {
scpy(username, usr, l + 1);
vacancy = 1;
break;
}
}
respond(r, nil);
if (vacancy) {
respond(r, nil);
} else {
respond(r, EUFULL);
}
}
void login(char* uname, char* password) {
@ -129,7 +72,8 @@ int logout(char* uname) {
if (scmp(uname, users_table[i].name)) {
*(users_table[i].name) = 0;
users_table[i].password = 0;
*(users_table[i].realm) = 0;
/* free cart */
/* free realm */
return 1;
}
}