*/
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <limits.h>
+#include <pwd.h>
#include "common.h"
enum {
USER_TYPE_GOD = 0x03
};
+// === PROTOTYPES ===
+static int GetUnixID(const char *Name);
+
// === GLOBALS ===
tUser *gaBank_Users;
int giBank_NumUsers;
}
}
-int Bank_GetUserUnixID(int ID)
-{
- if( ID < 0 || ID >= giBank_NumUsers )
- return -1;
-
- return gaBank_Users[ID].UnixID;
-}
-
/**
* \brief Create a new user in our database
*/
-int Bank_AddUser(int UnixID)
+int Bank_AddUser(const char *Username)
{
void *tmp;
+ int uid = GetUnixID(Username);
// Can has moar space plz?
tmp = realloc(gaBank_Users, (giBank_NumUsers+1)*sizeof(gaBank_Users[0]));
gaBank_Users = tmp;
// Crete new user
- gaBank_Users[giBank_NumUsers].UnixID = UnixID;
+ gaBank_Users[giBank_NumUsers].UnixID = uid;
gaBank_Users[giBank_NumUsers].Balance = 0;
gaBank_Users[giBank_NumUsers].Flags = 0;
return 0;
}
+
+// ---
+// Unix user dependent code
+// TODO: Modify to keep its own list of usernames
+// ---
+char *Bank_GetUserName(int ID)
+{
+ struct passwd *pwd;
+
+ if( ID < 0 || ID >= giBank_NumUsers )
+ return NULL;
+
+ if( gaBank_Users[ID].UnixID == -1 )
+ return strdup(">sales");
+
+ if( gaBank_Users[ID].UnixID == -2 )
+ return strdup(">liability");
+
+ pwd = getpwuid(gaBank_Users[ID].UnixID);
+ if( !pwd ) return NULL;
+
+ return strdup(pwd->pw_name);
+}
+
+static int GetUnixID(const char *Username)
+{
+ int uid;
+
+ if( strcmp(Username, ">sales") == 0 ) { // Pseudo account that sales are made into
+ uid = -1;
+ }
+ else if( strcmp(Username, ">liability") == 0 ) { // Pseudo acount that money is added from
+ uid = -2;
+ }
+ else {
+ struct passwd *pwd;
+ // Get user ID
+ pwd = getpwnam(Username);
+ if( !pwd ) return -1;
+ uid = pwd->pw_uid;
+ }
+ return uid;
+}
int Flags;
} tUser;
+// === IMPORTS ===
+extern int Bank_GetMinAllowedBalance(int ID);
+extern int Bank_GetUserBalance(int ID);
+extern int Bank_AlterUserBalance(int ID, int Delta);
+extern char *Bank_GetUserName(int ID);
+extern int Bank_GetUserByName(const char *Username);
+extern int Bank_AddUser(const char *Username);
+extern FILE *gBank_File;
+extern tUser *gaBank_Users;
+extern int giBank_NumUsers;
+
#endif
*/
#include <stdlib.h>
#include <stdio.h>
-#include <pwd.h>
#include <string.h>
#include <openssl/sha.h>
#include "common.h"
-// === IMPORTS ===
-extern int Bank_GetMinAllowedBalance(int ID);
-extern int Bank_GetUserBalance(int ID);
-extern int Bank_AlterUserBalance(int ID, int Delta);
-extern int Bank_GetUserByUnixID(int UnixID);
-extern int Bank_GetUserUnixID(int ID);
-extern int Bank_AddUser(int UnixID);
-extern FILE *gBank_File;
-extern tUser *gaBank_Users;
-extern int giBank_NumUsers;
-
// === PROTOTYPES ===
void Init_Cokebank(const char *Argument);
int Transfer(int SourceUser, int DestUser, int Ammount, const char *Reason);
*/
char *GetUserName(int User)
{
- struct passwd *pwd;
- int unixid = Bank_GetUserUnixID(User);
-
- if( unixid == -1 )
- return strdup(">sales");
-
- if( unixid == -2 )
- return strdup(">liability");
-
- pwd = getpwuid(unixid);
- if( !pwd ) return NULL;
-
- return strdup(pwd->pw_name);
+ return Bank_GetUserName(User);
}
/**
*/
int GetUserID(const char *Username)
{
- int ret, uid;
-
- if( strcmp(Username, ">sales") == 0 ) { // Pseudo account that sales are made into
- uid = -1;
- }
- else if( strcmp(Username, ">liability") == 0 ) { // Pseudo acount that money is added from
- uid = -2;
- }
- else {
- struct passwd *pwd;
- // Get user ID
- pwd = getpwnam(Username);
- if( !pwd ) return -1;
- uid = pwd->pw_uid;
- }
+ int ret;
// Get internal ID (or create new user)
- ret = Bank_GetUserByUnixID(uid);
+ ret = Bank_GetUserByName(Username);
if( ret == -1 ) {
- ret = Bank_AddUser(uid);
+ ret = Bank_AddUser(Username);
}
return ret;