Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Created on 11 aprile 2011, 15.34
- */
- #include <lib.c>
- int main(){
- int i=0, j, flag=0, nAccounts=0;
- char *buf=malloc(BUFSIZ-1), *path=malloc(BUFSIZ);
- FILE *fp, *fp2;
- DIR *dp;
- user *users, *tmp;
- struct dirent *ep;
- /* Checking if is the first time that the app is running */
- dp=opendir("dir");
- /* if there is not the 'dir' directory, i will create it */
- if(errno==ENOENT){
- mkdir("dir", 0777);
- dp=NULL;
- }
- /* if 'dir' exists, i look for 'admin' file */
- if (dp!=NULL){
- ep=readdir(dp);
- while (ep){
- if(!strcmp(ep->d_name, "admin")){
- closedir(dp);
- flag=1;
- }
- ep=readdir (dp);
- }
- }
- if(flag){
- /* 'admin' file exists, so isn't the first time */
- /* i'm going to load data */
- printf("admin trovato\n");
- fp=fopen("dir/usrs","r");
- if(fp==NULL){
- perror("admin file creation");
- return EXIT_FAILURE;
- }
- printf("Conto il n. di accounts: ");
- while((fgets(buf,BUFSIZ,fp)) != NULL) {
- nAccounts++;
- }
- printf("%d\n",nAccounts);
- users=(user *)malloc(nAccounts*sizeof(user));
- rewind(fp);
- printf("importo i dati\n");
- for(i=0;i<nAccounts;i++){
- sprintf(path, "dir/%s", fgets(buf,BUFSIZ,fp));
- fp2=fopen(path, "r");
- if(fp2==NULL){
- perror("admin file read");
- return EXIT_FAILURE;
- }
- printf("importing data for '%s' . . .\n", fgets(buf,BUFSIZ,fp));
- fread(&users[i],sizeof(user),1,fp2);
- }
- }
- /* 'admin' file not exists then, is the first time */
- /* and it must be created */
- else{
- printf("admin non trovato\n");
- printf("Creazione account...\n");
- nAccounts=1;
- users=(user *)malloc(nAccounts*sizeof(user));
- i=0;
- users[i].username="admin";
- printf("Insert a password for the 'admin' account: ");
- /*users[i].password=malloc(BUFSIZ);*/
- scanf("%s",users[i].password);
- users[i].bytes_quota=DEFAULT_QUOTA_SIZ; /* default quota: 1MB */
- fp=fopen("dir/admin", "w");
- if(fp==NULL){
- perror("admin file creation");
- return EXIT_FAILURE;
- }
- fwrite(&users[i],sizeof(user),1,fp);
- fclose(fp);
- /* 'usrs' file contains a list of all system users */
- /* i'm going to update it */
- fp=fopen("dir/usrs", "w");
- if(fp==NULL){
- perror("admin file creation");
- return EXIT_FAILURE;
- }
- fprintf(fp,"admin");
- fclose(fp);
- }
- printf("__PRIMA_STAMPA_MAIN__\n");
- for(i=0;i<nAccounts;i++){
- printf("username: %s\npassword: %s\nbytes_quota: %ld\nnMessages: %ld\n", users[i].username, users[i].password, users[i].bytes_quota, users[i].nMessages);
- for(j=0;j<users[i].nMessages;j++){
- printf("\tdestination: %s\n\tsubject: %s\n\ttext: %s",users[i].msgs[j].destination,users[i].msgs[j].subject,users[i].msgs[j].text);
- }
- }
- printf("nAccounts(main): %d\n",nAccounts);
- tmp=(user *)realloc(users, (nAccounts+1)*sizeof(user));
- users=tmp;
- nAccounts++;
- if(!addUser("nuovo_utente", users,nAccounts)){
- perror("adduser");
- return EXIT_FAILURE;
- }
- else{
- printf("__SECONDA_STAMPA_MAIN__\n");
- for(i=0;i<nAccounts;i++){
- printf("username: %s\npassword: %s\nbytes_quota: %ld\nnMessages: %ld\n", users[i].username, users[i].password, users[i].bytes_quota, users[i].nMessages);
- for(j=0;j<users[i].nMessages;j++){
- printf("\tdestination: %s\n\tsubject: %s\n\ttext: %s",users[i].msgs[j].destination,users[i].msgs[j].subject,users[i].msgs[j].text);
- }
- }
- }
- printf("exit_success;\n");
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement