Removed heap abuse from server (now uses sendf)
[tpg/opendispense2.git] / src / cokebank_basic / bank.c
index d78f90f..55a1957 100644 (file)
 #include <pwd.h>
 #include "common.h"
 
-enum {
-       FLAG_TYPEMASK    = 0x03,
-       USER_TYPE_NORMAL = 0x00,
-       USER_TYPE_COKE   = 0x01,
-       USER_TYPE_WHEEL  = 0x02,
-       USER_TYPE_GOD    = 0x03
-};
-
 // === PROTOTYPES ===
-static int     GetUnixID(const char *Name);
+static int     GetUnixID(const char *Username);
 
 // === GLOBALS ===
 tUser  *gaBank_Users;
@@ -32,13 +24,16 @@ tUser       *gaBank_Users;
 FILE   *gBank_File;
 
 // === CODE ===
-int Bank_GetUserByUnixID(int UnixID)
+int Bank_GetUserByName(const char *Username)
 {
-        int    i;
+        int    i, uid;
+       
+       uid = GetUnixID(Username);
+       
        // Expensive search :(
        for( i = 0; i < giBank_NumUsers; i ++ )
        {
-               if( gaBank_Users[i].UnixID == UnixID )
+               if( gaBank_Users[i].UnixID == uid )
                        return i;
        }
 
@@ -53,6 +48,14 @@ int Bank_GetUserBalance(int ID)
        return gaBank_Users[ID].Balance;
 }
 
+int Bank_GetUserFlags(int ID)
+{
+       if( ID < 0 || ID >= giBank_NumUsers )
+               return INT_MIN;
+
+       return gaBank_Users[ID].Flags;
+}
+
 int Bank_AlterUserBalance(int ID, int Delta)
 {
        // Sanity
@@ -88,9 +91,11 @@ int Bank_SetUserBalance(int ID, int Value)
 int Bank_GetMinAllowedBalance(int ID)
 {
        if( ID < 0 || ID >= giBank_NumUsers )
-               return -1;
+               return 0;
+
+//     printf("gaBank_Users[%i].Flags = 0x%x\n", ID, gaBank_Users[ID].Flags);
 
-       switch( gaBank_Users[ID].Flags & FLAG_TYPEMASK )
+       switch( gaBank_Users[ID].Flags & USER_FLAG_TYPEMASK )
        {
        case USER_TYPE_NORMAL:  return     0;
        case USER_TYPE_COKE:    return  -2000;
@@ -118,6 +123,13 @@ int Bank_AddUser(const char *Username)
        gaBank_Users[giBank_NumUsers].Balance = 0;
        gaBank_Users[giBank_NumUsers].Flags = 0;
        
+       if( strcmp(Username, COKEBANK_DEBT_ACCT) == 0 ) {
+               gaBank_Users[giBank_NumUsers].Flags = USER_TYPE_GOD;    // No minium
+       }
+       else if( strcmp(Username, "root") == 0 ) {
+               gaBank_Users[giBank_NumUsers].Flags = USER_TYPE_GOD;    // No minium
+       }
+       
        // Commit to file
        fseek(gBank_File, giBank_NumUsers*sizeof(gaBank_Users[0]), SEEK_SET);
        fwrite(&gaBank_Users[giBank_NumUsers], sizeof(gaBank_Users[0]), 1, gBank_File);
@@ -140,10 +152,10 @@ char *Bank_GetUserName(int ID)
                return NULL;
        
        if( gaBank_Users[ID].UnixID == -1 )
-               return strdup(">sales");
+               return strdup(COKEBANK_SALES_ACCT);
 
        if( gaBank_Users[ID].UnixID == -2 )
-               return strdup(">liability");
+               return strdup(COKEBANK_DEBT_ACCT);
 
        pwd = getpwuid(gaBank_Users[ID].UnixID);
        if( !pwd )      return NULL;
@@ -155,10 +167,10 @@ static int GetUnixID(const char *Username)
 {
         int    uid;
 
-       if( strcmp(Username, ">sales") == 0 ) { // Pseudo account that sales are made into
+       if( strcmp(Username, COKEBANK_SALES_ACCT) == 0 ) {      // Pseudo account that sales are made into
                uid = -1;
        }
-       else if( strcmp(Username, ">liability") == 0 ) {        // Pseudo acount that money is added from
+       else if( strcmp(Username, COKEBANK_DEBT_ACCT) == 0 ) {  // Pseudo acount that money is added from
                uid = -2;
        }
        else {

UCC git Repository :: git.ucc.asn.au