Advertisement
Guest User

Untitled

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