adduser.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. This program adds an user to account.txt
  3. Don't usr it When login-sever is working.
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. char *account_txt = "../save/account.txt";
  9. //-----------------------------------------------------
  10. // Function to suppress control characters in a string.
  11. //-----------------------------------------------------
  12. int remove_control_chars(unsigned char *str) {
  13. int i;
  14. int change = 0;
  15. for(i = 0; str[i]; i++) {
  16. if (str[i] < 32) {
  17. str[i] = '_';
  18. change = 1;
  19. }
  20. }
  21. return change;
  22. }
  23. int main(int argc, char *argv[]) {
  24. char username[24];
  25. char password[24];
  26. char sex[2];
  27. int next_id, id;
  28. char line[1024];
  29. // Check to see if account.txt exists.
  30. printf("Checking if '%s' file exists...\n", account_txt);
  31. FILE *FPaccin = fopen(account_txt, "r");
  32. if (FPaccin == NULL) {
  33. printf("'%s' file not found!\n", account_txt);
  34. printf("Run the setup wizard please.\n");
  35. exit(0);
  36. }
  37. next_id = 2000000;
  38. while(fgets(line, sizeof(line)-1, FPaccin)) {
  39. if (line[0] == '/' && line[1] == '/') { continue; }
  40. if (sscanf(line, "%d\t%%newid%%\n", &id) == 1) {
  41. if (next_id < id) {
  42. next_id = id;
  43. }
  44. } else {
  45. sscanf(line,"%i%[^ ]", &id);
  46. if (next_id <= id) {
  47. next_id = id +1;
  48. }
  49. }
  50. }
  51. close(FPaccin);
  52. printf("File exists.\n");
  53. printf("Don't create an account if the login-server is online!!!\n");
  54. printf("If the login-server is online, press ctrl+C now to stop this software.\n");
  55. printf("\n");
  56. strcpy(username, "");
  57. while (strlen(username) < 4 || strlen(username) > 23) {
  58. printf("Enter an username (4-23 characters): ");
  59. scanf("%s", &username);
  60. username[23] = 0;
  61. remove_control_chars(username);
  62. }
  63. strcpy(password, "");
  64. while (strlen(password) < 4 || strlen(password) > 23) {
  65. printf("Enter a password (4-23 characters): ");
  66. scanf("%s", &password);
  67. password[23] = 0;
  68. remove_control_chars(password);
  69. }
  70. strcpy(sex, "");
  71. while (strcmp(sex, "F") != 0 && strcmp(sex, "M") != 0) {
  72. printf("Enter a gender (M for male, F for female): ");
  73. scanf("%s", &sex);
  74. }
  75. FILE *FPaccout = fopen(account_txt, "r+");
  76. fseek(FPaccout, 0, SEEK_END);
  77. fprintf(FPaccout, "%i %s %s - %s -\r\n", next_id, username, password, sex);
  78. close(FPaccout);
  79. printf("Account added.\n");
  80. }