Advertisement
Guest User

Untitled

a guest
May 15th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.07 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include<string.h>
  3. #include<stdio.h>
  4. //#include "cipher.c"
  5.  
  6.  
  7. typedef struct node {
  8.     char *username;
  9.     char *password;
  10.     char *usertype;
  11.     struct node * next;
  12. } llnode;
  13.  
  14. llnode* head;
  15.  
  16. int menu(void);
  17.  
  18.  
  19. int addNode(char *username, char *password, char *usertype){
  20.     llnode *addNode = (llnode *)malloc(sizeof(llnode));
  21.     llnode *temp = (llnode *)malloc(sizeof(llnode));
  22.    
  23.     temp  = head;
  24.    
  25.     while (temp != NULL){
  26.      temp = temp -> next;
  27.     }
  28.  
  29.     temp -> next = addNode;
  30.  
  31.     addNode -> username = username;
  32.     addNode -> password = password;
  33.     addNode -> usertype = usertype;
  34.     addNode -> next = NULL;
  35.  
  36.     printf("%s , %s, %s", username, password, usertype);
  37.  
  38. }
  39.  
  40.  
  41.  
  42.  
  43.  
  44. int add(char *user, char* pass, char* usertype){
  45.         printf("boooo2323");
  46.         fflush(stdout);
  47.         //FILE *file = fopen("password.csv", "at");
  48.         //decrypt(file);
  49.         printf("yoyoyo");
  50.         //loadToLinkedList(file);
  51.         llnode *temp = (llnode *)malloc(sizeof(llnode *));
  52.         temp = head;
  53.         temp = temp -> next;
  54.         printf("boooo3");
  55.        
  56.         while(temp != NULL){
  57.             if (strcmp(user, temp -> username)){
  58.                 temp = temp -> next;
  59.             }
  60.             else if (!strcmp(pass, temp -> password) && !strcmp(usertype, temp -> usertype)){
  61.                 printf("This record already exists!");
  62.             }
  63.    
  64.         }  
  65.            
  66.         addNode(user, pass, usertype);
  67.         //backToTheFile(file);
  68.         //encrypt(file);  
  69.         //fclose(file);
  70.    
  71.    return 0;
  72. }
  73.  
  74. int delete(char * username){
  75.             FILE *file = fopen("paswords.csv", "at");
  76.         llnode *temp = (llnode *)malloc(sizeof(llnode *));
  77.         llnode *cur = (llnode *)malloc(sizeof(llnode *));
  78.         llnode *pcur = (llnode *)malloc(sizeof(llnode *)); //keeps track of previous node
  79.         pcur = head;
  80.        
  81.         //decrypt(file);
  82.         loadToLinkedList(file);
  83.         cur = head -> next;
  84.  
  85.         while (cur != NULL){
  86.             if (strcmp(username, cur -> username)){
  87.                 cur = cur -> next;
  88.                 pcur = pcur -> next;
  89.             }
  90.             else {
  91.                 pcur -> next = cur -> next;
  92.                 temp = cur;
  93.                 cur -> next = NULL;
  94.                
  95.             }
  96.             if (strcmp(username, cur -> username) && cur -> next == NULL){
  97.                 printf("This record does not exist!");
  98.             }
  99.    
  100.         }  
  101.            
  102.         //addNode(username, pass, usertype);
  103.         backToTheFile(file);
  104.         //encrypt(file);  
  105.         fclose(file);
  106.    
  107.    return 0;
  108.      
  109.         }
  110.    
  111.  
  112. int edit(char *user1, char *user2, char *pass2, char* usertype2){
  113.     delete(user1);
  114.     add(user2, pass2, usertype2);
  115.      
  116. }
  117.  
  118.  
  119. int verify(char *username, char* password){
  120.         //FILE *file = fopen("passwords.csv", "wt");
  121.         //decrypt(file);
  122.         //loadToLinkedList(file);
  123.         llnode *temp = (llnode *)malloc(sizeof(llnode *));
  124.         temp = head;
  125.         temp = temp -> next;
  126.         int result;
  127.  
  128.         while(temp != NULL){
  129.             if (strcmp(username, head -> username)){
  130.                 if (temp -> next == NULL){
  131.                     printf("INVALID\n");
  132.                     result = EXIT_FAILURE;
  133.                     break;
  134.                 }
  135.                 else
  136.                     temp = temp -> next;
  137.             }
  138.             else if (!strcmp(password, temp -> password)) {
  139.                 printf("VALID\n");
  140.                 result = EXIT_SUCCESS;
  141.                 break;
  142.             }
  143.            
  144.         }      
  145.        
  146.         //backToTheFile(file);
  147.         //encrypt(file);  
  148.         //fclose(file);
  149.    
  150.         return result;
  151.      
  152.      
  153. }
  154.  
  155.  
  156.  
  157. int menu(void){
  158.     FILE *passwords = fopen("password.csv", "at");
  159.     int choice;
  160.     char *username = (char *)malloc(20*sizeof(char));
  161.     char *password = (char *)malloc(20*sizeof(char));
  162.     char *usertype = (char *)malloc(20*sizeof(char));
  163.     char *user2 = (char *)malloc(20*sizeof(char));
  164.     char *pass2 = (char *)malloc(20*sizeof(char));
  165.     char *type2 = (char *)malloc(20*sizeof(char));
  166.  
  167.    
  168.  
  169.  
  170.  
  171.  
  172. while(1){
  173. printf("  Menu \n ---------- \n choose one of the following options:\n 1. add a record\n2. delete a record\n3. edit a record\n4. login verification\n5. quit\n");
  174. scanf("%d", &choice);
  175.  
  176. switch(choice){
  177.  
  178. case 1:
  179.    
  180.     printf("enter a username: \n");
  181.     scanf("%s", username);
  182.     printf("enter a password: \n");
  183.     scanf("%s", password);
  184.     printf("enter the user's type: \n");
  185.     scanf("%s", usertype);
  186.  
  187.     add(username, password, usertype);
  188.     break;
  189.    
  190. case 2:
  191.      
  192.       printf("enter the username contained in the record you would like to delete: \n");
  193.       scanf("%s", username);
  194.  
  195.       delete(username);
  196.      
  197.      
  198.       break;
  199.  
  200. case 3:
  201.     printf("Enter the username contained in the record you would like to edit: \n");
  202.     scanf("%s", username);
  203.     printf("Enter the username of the new record: \n");
  204.     scanf("%s", user2);
  205.     printf("Enter the password of the new record: \n");
  206.     scanf("%s", pass2);
  207.     printf("Enter the type of the user: \n");
  208.     scanf("%s", type2);
  209.    
  210.     edit(username, user2, pass2, type2);
  211.  
  212.     break;
  213.  
  214.  
  215. case 4:
  216.    
  217.     printf("Enter your username: \n");
  218.     scanf("%s", username);
  219.     printf("Enter your password: \n");
  220.     scanf("%s", password);
  221.  
  222.     verify(username, password);
  223.    
  224.     break;
  225.    
  226.  
  227. case 5:
  228.     backToTheFile(passwords);
  229.     fclose(passwords);
  230.     exit(0);
  231.     break;
  232.  
  233. default:
  234.     break;
  235. }
  236. }
  237. //flose(password);
  238. return 0;
  239.  
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement