Advertisement
Guest User

Untitled

a guest
May 15th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "menu.c"
  5.  
  6. char *record;
  7.  
  8. llnode * makeLinkedList(){
  9.    
  10.     head = (llnode *)malloc( sizeof(llnode) );
  11.     head -> username = NULL;
  12.     head -> password = NULL;
  13.     head -> usertype = NULL;
  14.     head -> next = NULL;
  15.  
  16.   return head;
  17. }
  18.  
  19. int loadToLinkedList(FILE *file){
  20.     char * record = (char *)malloc(100*(sizeof(char)));
  21.     char *name;
  22.     char *pass;
  23.     char *type;
  24.  
  25.    
  26.     while (!feof(file)){
  27.             fgets(record, 99, file);
  28.  
  29.  //while the next line of the file isn't the end of file, assign that record's fields to the NODE struct  
  30.    
  31.        
  32.         name = strtok(record, ",");
  33.         pass = strtok(NULL, ",");
  34.         type = strtok(NULL, "\n");
  35.  
  36.      
  37.         addNode(name, pass, type);
  38.    
  39.     }
  40.  
  41. fclose(file);
  42. }
  43.  
  44.  
  45. char * makeRecord(char * user, char * pass, char * type){
  46.  
  47.     char * buffer = (char *)malloc( 1000 );
  48.  
  49.     strcat(buffer, user);
  50.      strcat(user, ",");
  51.      strcat(user, pass);
  52.      strcat(user, ",");
  53.      strcat(user, type);
  54.  
  55.       return buffer;
  56. }
  57.  
  58. int backToTheFile(llnode* head){       //like Back to the Future
  59.     FILE *passwords = fopen("password.csv", "at");
  60.     llnode *temp = (llnode *)malloc(sizeof(llnode));
  61.     temp = head;
  62.     temp = temp -> next;
  63.    
  64.     while (temp != NULL){
  65.         char * record = makeRecord(temp -> username, temp -> password, temp -> usertype);
  66.         fputs(record, passwords);
  67.         temp = temp -> next;
  68.     }
  69.  
  70.     fclose(passwords);
  71. }
  72.  
  73.  
  74.  
  75. int main(int argc, char *argv[]){
  76.     int i;
  77.     char* username = argv[2];
  78.     char* password = argv[3];
  79.     char* usertype = argv[4];
  80.     char *user2 = argv[5];
  81.     char *pass2 = argv[6];
  82.     char *type2 = argv[7];
  83.    
  84.  
  85.     makeLinkedList();  
  86.     loadToLinkedList(fopen("passwords.csv", "rt"));
  87.  
  88.     if (fopen("password.csv", "rt") == NULL){
  89.         system("touch password.csv");
  90.         }
  91.    
  92.  
  93.       if (!strcmp(argv[1], "-menu")){
  94.           menu(); //switch to menu mode
  95.       }
  96.       else{
  97.         for (i = 0; i < argc; i++){
  98.             if (argv[i] == "menu" && i != 1){
  99.             printf("Error: the proper syntax for this command is $passweb -menu -add -del - edit -verify username password type user_2 pass_2 type_2.  Please check your syntax and try again. \n");
  100.             return 0;
  101.             }
  102.         }
  103.       }
  104.  
  105.       if (strcmp(argv[1], "-add") == 0 || strcmp(argv[1], "-del") == 0 || strcmp(argv[1], "-edit") == 0 || strcmp(argv[1], "-verify") == 0){
  106.       char * flagFound;
  107.       for (i = 2; i < argc; i++){ //if there are any other flags present in addition to the one you want, return an error
  108.         if (sscanf(argv[i], "-", flagFound)){
  109.           printf("Error: the proper syntax for this command is $passweb -menu -add -del - edit -verify username password type user_2 pass_2 type_2.  Please check your syntax and try again. \n");
  110.           return 0;
  111.           }
  112.        }
  113.       }
  114.     else if (strcmp(argv[1], "-add") == 0){
  115.            add(username, password, usertype);
  116.         }
  117.    
  118.  
  119.     else    if (strcmp(argv[1], "-del") == 0){
  120.           delete(username);
  121.  
  122.         }
  123.  
  124.     else if (strcmp(argv[1], "-edit") == 0){
  125.           edit(username, user2, pass2, type2);
  126.  
  127.         }
  128.     else if (strcmp(argv[1], "-verify") == 0){
  129.           verify(username, password);  
  130.          
  131.           }
  132.  
  133.      
  134.    
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement