Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.18 KB | None | 0 0
  1. /*
  2.  * File:    Lib.c
  3.  *
  4.  * Created on 11 aprile 2011, 15.45
  5.  */
  6.  
  7. #define _GNU_SOURCE
  8. #define DEFAULT_QUOTA_SIZ 1048576
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <fcntl.h>
  16. #include <unistd.h>
  17. #include <dirent.h>
  18. #include <errno.h>
  19.  
  20.  typedef struct msg{
  21.     char destination[BUFSIZ];
  22.     char subject[BUFSIZ];
  23.     char text[BUFSIZ];
  24. }message;
  25.  
  26. /* User Data.
  27.  * It contains all data stored of an user.
  28.  */
  29. typedef struct userData{
  30.     char username[BUFSIZ];
  31.     char password[BUFSIZ];
  32.     long int nMessages;
  33.     long int bytes_quota;
  34.     message *msgs;
  35. }user;
  36.  
  37. int addUser(char *username, user *users, int nAccounts){
  38.     user *tmp;
  39.     char path[BUFSIZ];
  40.     FILE *fp;
  41.     int i,last;
  42.  
  43.     printf("nAccounts(main): %d\n",nAccounts);
  44.     tmp=(user *)realloc(users, (nAccounts+1)*sizeof(user));
  45.     users=tmp;
  46.     last=nAccounts;
  47.  
  48.     strcpy(users[last].username,username);
  49.     printf(" ___ADD_USER___\n");
  50.     for(i=0;i<nAccounts;i++){
  51.         printf("username: %s\npassword: %s\nbytes_quota: %ld\nnMessages: %ld\n", users[i].username, users[i].password, users[i].bytes_quota, users[i].nMessages);
  52.     }
  53.    
  54.     printf("Insert a password for the '%s' account: \n",username);
  55.     scanf("%s",users[last].password);
  56.     for(i=0;i<nAccounts+1;i++){
  57.         printf("username: %s\npassword: %s\nbytes_quota: %ld\nnMessages: %ld\n", users[i].username, users[i].password, users[i].bytes_quota, users[i].nMessages);
  58.     }
  59.     users[last].bytes_quota=DEFAULT_QUOTA_SIZ; /* default quota: 1MB */
  60.     strcpy(path,"dir/");
  61.     /* Creating user mailbox file */
  62.     sprintf(path,"%s",username);
  63.     printf("path: %s\n", path); /* Stampa di controllo */
  64.     fp=fopen(path, "w");
  65.         if(fp==NULL){
  66.             perror("user file creation");
  67.             return EXIT_FAILURE;
  68.         }
  69.     fwrite(&users[last],sizeof(user),1,fp);
  70.     fclose(fp);
  71.  
  72.     /* Updating 'usrs' file with the new user */
  73.     fp=fopen("dir/usrs", "a");
  74.     if(fp==NULL){
  75.         perror("user file creation");
  76.         return EXIT_FAILURE;
  77.     }
  78.     fprintf(fp,"%s\n",username);
  79.     fclose(fp);
  80.     return EXIT_SUCCESS;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement