realm.c (raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
#include <u.h> #include <libc.h> #include <stdio.h> #include "util.h" #include "user.h" #include "universe.h" #include "realm.h" Realm* create_realm(char* name) { Realm* self; char path[64]; scat(path, "realms/"); scat(path, name); if (create(path, OREAD, 0755) < 0) { return nil; } else { self = malloc(sizeof(Realm)); scpy(name, self->name, 32); self->max = 4; self->password = 0; self->universe = create_universe(); return self; } } Realm* parse_realm(char* name) { Realm* self; FILE* f; char path[64]; char file[64]; char buf[256]; scat(path, "realms/"); scat(path, name); scpy(path, file, 64); scat(file, "/realm"); f = fopen(file, "r"); if (f != nil) { if (fgets(buf, 256, f)) { self = malloc(sizeof(Realm)); sscanf(buf, "%hu %llu", &(self->max), &(self->password)); fclose(f); } else { return nil; } } else { return nil; } scpy(path, file, 64); scat(file, "/universe"); self->universe = parse_universe(path); return self; } Realm* find_realm(UserInfo* table, char* name) { UserInfo* u = table; int i; for (i = 0; i < 64; i++) { if (slen(u->name) > 0 && u->realm != nil && scmp(u->realm->name, name)) return u->realm; u++; } return nil; } void save_realm(Realm* self) { FILE* f; char path[64]; char file[64]; scat(path, "realms/"); scat(path, self->name); scpy(path, file, 64); scat(file, "/realm"); f = fopen(file, "w"); if (f != nil) { fprintf(f, "%hu %llu", self->max, self->password); fclose(f); save_universe(self->universe, self->name); } } void destroy_realm(Realm* self) { if (self != nil) { if (self->universe != nil) { destroy_universe(self->universe); } free(self); self = nil; } } |