Advertisement
Guest User

Untitled

a guest
May 15th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "menu.c"
  5.  
  6.  
  7.  
  8.  
  9.  
  10. char *record;
  11.  
  12.  
  13.  
  14. typedef struct node {
  15.     char *username;
  16.     char *password;
  17.     char *usertype;
  18.     struct node * next;
  19. } llnode;
  20.  
  21. llnode* head;
  22.  
  23. llnode* makeLinkedList(){
  24.    
  25.     head = (llnode *)malloc( sizeof(llnode) );
  26.     head -> username = NULL;
  27.     head -> password = NULL;
  28.     head -> usertype = NULL;
  29.     head -> next = NULL;
  30.  
  31.   return head;
  32. }
  33.  
  34. void addNode(char *username, char *password, char *usertype){
  35.     llnode *addNode = (llnode *)malloc(sizeof(llnode));
  36.    
  37.     llnode * curNode  = head;
  38.    
  39.     while (next -> next != NULL){
  40.      curNode = curNode -> next;
  41.     }
  42.  
  43.     next -> next = addNode;
  44.  
  45.     addNode -> username = username;
  46.     addNode -> password = password;
  47.     addNode -> usertype = usertype;
  48.     addNode -> next = NULL;
  49.  
  50.     printf("%s , %s, %s", username, password, usertype);
  51.  
  52. }
  53.  
  54.  
  55. void loadToLinkedList(FILE *file){
  56.     char * record = (char *)malloc(100*(sizeof(char)));
  57.     char *delims[] = {", " , "\n"};
  58.     char *name;
  59.     char *pass;
  60.     char *type;
  61.     while (!feof(file)){
  62.             fgets(record, 99, file);
  63.  
  64.  //while the next line of the file isn't the end of file, assign that record's fields to the NODE struct  
  65.    
  66.        
  67.         name = strtok(record, &delims[0]);
  68.         pass = strtok(NULL, &delims[0]);
  69.         type = strtok(NULL, &delims[1]);
  70.  
  71.      
  72.         addNode(name, pass, type);
  73.    
  74.     }
  75.  
  76. fclose(file);
  77. }
  78.  
  79. //int main(void){
  80. //  FILE *file = fopen("password.csv", "rt");
  81. //  makeLinkedList();
  82. //  loadToLinkedList(file);
  83. //  return 0;
  84. //}
  85.  
  86. void backToTheFile(llnode* head){ //haha like Back to the Future
  87.     FILE *passwords = fopen("password.csv", "wt");
  88.     head = head -> next;
  89.    
  90.     while (head != NULL){
  91.         char *record = makeRecord(head -> username, head -> password, head -> usertype);
  92.         fputs(record, passwords);
  93.         head = head -> next;
  94.     }
  95. }
  96.  
  97. char *makeRecord(char user[20], char pass[20], char type[20]){
  98.  
  99.      strcat(user, ", ");
  100.      strcat(user, pass);
  101.      strcat(user, ", ");
  102.      strcat(user, type);
  103.  
  104.       char *record = user;
  105.  
  106.       return record;
  107.  
  108. }
  109.  
  110. int main(int argc, char *argv[]){
  111.     int i;
  112.     char* username = argv[2];
  113.     char* password = argv[3];
  114.     char* usertype = argv[4];
  115.     char *user2 = argv[5];
  116.     char *pass2 = argv[6];
  117.     char *type2 = argv[7];
  118.     char recordArray[50][50];
  119.  
  120.     makeLinkedList();  
  121.     loadIntoLinkedList(fopen("passwords.csv", "rt"));
  122.    
  123.  
  124.       if (!strcmp(argv[1], "-menu")){
  125.           menu(); //switch to menu mode
  126.       }
  127.       else{
  128.         for (i = 0; i < argc; i++){
  129.             if (argv[i] == "menu" && i != 1){
  130.             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");
  131.             return 0;
  132.             }
  133.         }
  134.       }
  135.  
  136.       if (strcmp(argv[1], "-add") == 0 || strcmp(argv[1], "-del") == 0 || strcmp(argv[1], "-edit") == 0 || strcmp(argv[1], "-verify") == 0){
  137.       char *flagFound;
  138.       for (i = 2; i < argc; i++){ //if there are any other flags present in addition to the one you want, return an error
  139.         if (sscanf(argv[i], "-", flagFound)){
  140.           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");
  141.           return 0;
  142.           }
  143.         }
  144.  
  145.  
  146.     else if (strcmp(argv[1], "-add") == 0){
  147.            add(username, password, usertype);
  148.         }
  149.    
  150.  
  151.     else    if (strcmp(argv[1], "-del") == 0){
  152.           delete(username);
  153.  
  154.         }
  155.  
  156.     else if (strcmp(argv[1], "-edit") == 0){
  157.           edit(username, user2, pass2, type2);
  158.  
  159.         }
  160.     else if (strcmp(argv[1], "-verify") == 0){
  161.           verify(username, password);  
  162.          
  163.           }
  164.  
  165.      
  166.   }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement