+ * \brief Name compare function for qsort
+ */
+static int Bank_int_CompareBalance(const void *Ent1, const void *Ent2)
+{
+ const tUser *user1 = *(const tUser**)Ent1;
+ const tUser *user2 = *(const tUser**)Ent2;
+
+ return user1->Balance - user2->Balance;
+}
+
+#if 1
+static int Bank_int_ReadDatabase(void)
+{
+ int i;
+ if( gaBank_Users ) return 1;
+
+ // Get size
+ fseek(gBank_File, 0, SEEK_END);
+ giBank_NumUsers = ftell(gBank_File) / sizeof(tFileUser);
+ fseek(gBank_File, 0, SEEK_SET);
+
+ // Allocate structures
+ gaBank_Users = malloc( giBank_NumUsers * sizeof(tUser) );
+ gaBank_UsersByName = malloc( giBank_NumUsers * sizeof(tUser*) );
+ gaBank_UsersByBalance = malloc( giBank_NumUsers * sizeof(tUser*) );
+ // Read data
+ for( i = 0; i < giBank_NumUsers; i ++ )
+ {
+ tFileUser fu;
+ fread(&fu, sizeof(tFileUser), 1, gBank_File);
+ gaBank_Users[i].Name = NULL;
+ gaBank_Users[i].UnixID = fu.UnixID;
+ gaBank_Users[i].Balance = fu.Balance;
+ gaBank_Users[i].Flags = fu.Flags;
+ gaBank_Users[i].Name = Bank_GetAcctName(i);
+ gaBank_UsersByName[i] = &gaBank_Users[i]; // Add to name index
+ gaBank_UsersByBalance[i] = &gaBank_Users[i]; // Add to balance index
+ }
+
+ // Sort indexes
+ qsort(gaBank_UsersByName, giBank_NumUsers, sizeof(tUser*), Bank_int_CompareNames);
+ qsort(gaBank_UsersByBalance, giBank_NumUsers, sizeof(tUser*), Bank_int_CompareBalance);
+
+ return 0;
+}
+#else
+static int Bank_int_ReadDatabase(void)
+{
+ char buf[BUFSIZ];
+ // Alternate data format
+ // > Plain Text
+ // <username>,<uid>,<pin>,<lastused>,<balance>,<flags>,<altlogins...>
+ fseek(gBank_File, 0, SEEK_SET);
+
+ while(1)
+ {
+ fgets(buf, BUFSIZ-1, gBank_File);
+ }
+}
+#endif
+
+static int Bank_int_WriteEntry(int ID)
+{
+ tFileUser fu;
+ if( ID < 0 || ID >= giBank_NumUsers ) {
+ return -1;
+ }
+
+ // Commit to file
+ fseek(gBank_File, ID*sizeof(fu), SEEK_SET);
+
+ fu.UnixID = gaBank_Users[ID].UnixID;
+ fu.Balance = gaBank_Users[ID].Balance;
+ fu.Flags = gaBank_Users[ID].Flags;
+
+ fwrite(&fu, sizeof(fu), 1, gBank_File);
+
+ return 0;
+}
+
+/*
+ * Transfers money from one user to another