Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: Lib.c
- *
- * Created on 11 aprile 2011, 15.45
- */
- #define _GNU_SOURCE
- #define DEFAULT_QUOTA_SIZ 1048576
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <dirent.h>
- #include <errno.h>
- typedef struct msg{
- char destination[BUFSIZ];
- char subject[BUFSIZ];
- char text[BUFSIZ];
- }message;
- /* User Data.
- * It contains all data stored of an user.
- */
- typedef struct userData{
- char username[BUFSIZ];
- char password[BUFSIZ];
- long int nMessages;
- long int bytes_quota;
- message *msgs;
- }user;
- int addUser(char *username, user *users, int nAccounts){
- user *tmp;
- char path[BUFSIZ];
- FILE *fp;
- int i,last;
- printf("nAccounts(main): %d\n",nAccounts);
- tmp=(user *)realloc(users, (nAccounts+1)*sizeof(user));
- users=tmp;
- last=nAccounts;
- strcpy(users[last].username,username);
- printf(" ___ADD_USER___\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);
- }
- printf("Insert a password for the '%s' account: \n",username);
- scanf("%s",users[last].password);
- for(i=0;i<nAccounts+1;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);
- }
- users[last].bytes_quota=DEFAULT_QUOTA_SIZ; /* default quota: 1MB */
- strcpy(path,"dir/");
- /* Creating user mailbox file */
- sprintf(path,"%s",username);
- printf("path: %s\n", path); /* Stampa di controllo */
- fp=fopen(path, "w");
- if(fp==NULL){
- perror("user file creation");
- return EXIT_FAILURE;
- }
- fwrite(&users[last],sizeof(user),1,fp);
- fclose(fp);
- /* Updating 'usrs' file with the new user */
- fp=fopen("dir/usrs", "a");
- if(fp==NULL){
- perror("user file creation");
- return EXIT_FAILURE;
- }
- fprintf(fp,"%s\n",username);
- fclose(fp);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement