3 * UCC (University [of WA] Computer Club) Electronic Accounting System
4 * - Cokebank (Basic Version)
6 * bank.c - Actual bank database
8 * This file is licenced under the 3-clause BSD Licence. See the file COPYING
20 USER_TYPE_NORMAL = 0x00,
21 USER_TYPE_COKE = 0x01,
22 USER_TYPE_WHEEL = 0x02,
27 static int GetUnixID(const char *Username);
35 int Bank_GetUserByName(const char *Username)
39 uid = GetUnixID(Username);
41 // Expensive search :(
42 for( i = 0; i < giBank_NumUsers; i ++ )
44 if( gaBank_Users[i].UnixID == uid )
51 int Bank_GetUserBalance(int ID)
53 if( ID < 0 || ID >= giBank_NumUsers )
56 return gaBank_Users[ID].Balance;
59 int Bank_AlterUserBalance(int ID, int Delta)
62 if( ID < 0 || ID >= giBank_NumUsers )
66 gaBank_Users[ID].Balance += Delta;
69 fseek(gBank_File, ID*sizeof(gaBank_Users[0]), SEEK_SET);
70 fwrite(&gaBank_Users[ID], sizeof(gaBank_Users[0]), 1, gBank_File);
75 int Bank_SetUserBalance(int ID, int Value)
78 if( ID < 0 || ID >= giBank_NumUsers )
82 gaBank_Users[ID].Balance = Value;
85 fseek(gBank_File, ID*sizeof(gaBank_Users[0]), SEEK_SET);
86 fwrite(&gaBank_Users[ID], sizeof(gaBank_Users[0]), 1, gBank_File);
91 int Bank_GetMinAllowedBalance(int ID)
93 if( ID < 0 || ID >= giBank_NumUsers )
96 switch( gaBank_Users[ID].Flags & FLAG_TYPEMASK )
98 case USER_TYPE_NORMAL: return 0;
99 case USER_TYPE_COKE: return -2000;
100 case USER_TYPE_WHEEL: return -10000;
101 case USER_TYPE_GOD: return INT_MIN;
107 * \brief Create a new user in our database
109 int Bank_AddUser(const char *Username)
112 int uid = GetUnixID(Username);
114 // Can has moar space plz?
115 tmp = realloc(gaBank_Users, (giBank_NumUsers+1)*sizeof(gaBank_Users[0]));
116 if( !tmp ) return -1;
120 gaBank_Users[giBank_NumUsers].UnixID = uid;
121 gaBank_Users[giBank_NumUsers].Balance = 0;
122 gaBank_Users[giBank_NumUsers].Flags = 0;
124 if( strcmp(Username, ">liability") == 0 ) {
125 gaBank_Users[giBank_NumUsers].Flags = USER_TYPE_GOD; // No minium
129 fseek(gBank_File, giBank_NumUsers*sizeof(gaBank_Users[0]), SEEK_SET);
130 fwrite(&gaBank_Users[giBank_NumUsers], sizeof(gaBank_Users[0]), 1, gBank_File);
139 // Unix user dependent code
140 // TODO: Modify to keep its own list of usernames
142 char *Bank_GetUserName(int ID)
146 if( ID < 0 || ID >= giBank_NumUsers )
149 if( gaBank_Users[ID].UnixID == -1 )
150 return strdup(">sales");
152 if( gaBank_Users[ID].UnixID == -2 )
153 return strdup(">liability");
155 pwd = getpwuid(gaBank_Users[ID].UnixID);
156 if( !pwd ) return NULL;
158 return strdup(pwd->pw_name);
161 static int GetUnixID(const char *Username)
165 if( strcmp(Username, ">sales") == 0 ) { // Pseudo account that sales are made into
168 else if( strcmp(Username, ">liability") == 0 ) { // Pseudo acount that money is added from
174 pwd = getpwnam(Username);
175 if( !pwd ) return -1;