Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.98 KB | None | 0 0
  1. /*
  2.  * Created on 11 aprile 2011, 15.34
  3.  */
  4.  
  5. #include <lib.c>
  6.  
  7. int main(){
  8.     int i=0, j, flag=0, nAccounts=0;
  9.     char *buf=malloc(BUFSIZ-1), *path=malloc(BUFSIZ);
  10.     FILE *fp, *fp2;
  11.     DIR *dp;
  12.     user *users, *tmp;
  13.     struct dirent *ep;
  14.  
  15.     /* Checking if is the first time that the app is running */
  16.  
  17.     dp=opendir("dir");
  18.     /* if there is not the 'dir' directory, i will create it */
  19.     if(errno==ENOENT){
  20.         mkdir("dir", 0777);
  21.         dp=NULL;
  22.     }
  23.     /* if 'dir' exists, i look for 'admin' file */
  24.     if (dp!=NULL){
  25.         ep=readdir(dp);
  26.         while (ep){
  27.             if(!strcmp(ep->d_name, "admin")){
  28.                 closedir(dp);
  29.                 flag=1;
  30.             }
  31.             ep=readdir (dp);
  32.         }
  33.     }
  34.     if(flag){
  35.         /* 'admin' file exists, so isn't the first time */
  36.         /* i'm going to load data */
  37.         printf("admin trovato\n");
  38.         fp=fopen("dir/usrs","r");
  39.         if(fp==NULL){
  40.             perror("admin file creation");
  41.             return EXIT_FAILURE;
  42.         }
  43.         printf("Conto il n. di accounts: ");
  44.         while((fgets(buf,BUFSIZ,fp)) != NULL) {
  45.             nAccounts++;
  46.         }
  47.         printf("%d\n",nAccounts);
  48.         users=(user *)malloc(nAccounts*sizeof(user));
  49.         rewind(fp);
  50.  
  51.         printf("importo i dati\n");
  52.         for(i=0;i<nAccounts;i++){
  53.             sprintf(path, "dir/%s", fgets(buf,BUFSIZ,fp));
  54.             fp2=fopen(path, "r");
  55.             if(fp2==NULL){
  56.                 perror("admin file read");
  57.                 return EXIT_FAILURE;
  58.             }
  59.             printf("importing data for '%s' . . .\n", fgets(buf,BUFSIZ,fp));
  60.             fread(&users[i],sizeof(user),1,fp2);
  61.         }
  62.     }
  63.  
  64.     /* 'admin' file not exists then, is the first time */
  65.     /* and it must be created */
  66.     else{
  67.         printf("admin non trovato\n");
  68.         printf("Creazione account...\n");
  69.         nAccounts=1;
  70.         users=(user *)malloc(nAccounts*sizeof(user));
  71.         i=0;
  72.         users[i].username="admin";
  73.         printf("Insert a password for the 'admin' account: ");
  74.         /*users[i].password=malloc(BUFSIZ);*/
  75.         scanf("%s",users[i].password);
  76.         users[i].bytes_quota=DEFAULT_QUOTA_SIZ; /* default quota: 1MB */
  77.         fp=fopen("dir/admin", "w");
  78.         if(fp==NULL){
  79.             perror("admin file creation");
  80.             return EXIT_FAILURE;
  81.         }
  82.         fwrite(&users[i],sizeof(user),1,fp);
  83.         fclose(fp);
  84.  
  85.         /* 'usrs' file contains a list of all system users */
  86.         /* i'm going to update it */
  87.         fp=fopen("dir/usrs", "w");
  88.         if(fp==NULL){
  89.             perror("admin file creation");
  90.             return EXIT_FAILURE;
  91.         }
  92.         fprintf(fp,"admin");
  93.         fclose(fp);
  94.     }
  95.     printf("__PRIMA_STAMPA_MAIN__\n");
  96.     for(i=0;i<nAccounts;i++){
  97.         printf("username: %s\npassword: %s\nbytes_quota: %ld\nnMessages: %ld\n", users[i].username, users[i].password, users[i].bytes_quota, users[i].nMessages);
  98.         for(j=0;j<users[i].nMessages;j++){
  99.             printf("\tdestination: %s\n\tsubject: %s\n\ttext: %s",users[i].msgs[j].destination,users[i].msgs[j].subject,users[i].msgs[j].text);
  100.         }
  101.     }
  102.     printf("nAccounts(main): %d\n",nAccounts);
  103.     tmp=(user *)realloc(users, (nAccounts+1)*sizeof(user));
  104.     users=tmp;
  105.     nAccounts++;
  106.     if(!addUser("nuovo_utente", users,nAccounts)){
  107.         perror("adduser");
  108.         return EXIT_FAILURE;
  109.     }
  110.     else{
  111.         printf("__SECONDA_STAMPA_MAIN__\n");
  112.         for(i=0;i<nAccounts;i++){
  113.         printf("username: %s\npassword: %s\nbytes_quota: %ld\nnMessages: %ld\n", users[i].username, users[i].password, users[i].bytes_quota, users[i].nMessages);
  114.             for(j=0;j<users[i].nMessages;j++){
  115.                 printf("\tdestination: %s\n\tsubject: %s\n\ttext: %s",users[i].msgs[j].destination,users[i].msgs[j].subject,users[i].msgs[j].text);
  116.             }
  117.         }
  118.  
  119.     }
  120.  
  121.     printf("exit_success;\n");
  122.     return EXIT_SUCCESS;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement