Guest User

Untitled

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