예제 #1
0
파일: fsinfo.c 프로젝트: 0mp/freebsd
/*
 * MAIN
 */
int
main(int argc, char *argv[])
{
  /*
   * Process arguments
   */
  fsi_get_args(argc, argv);

  /*
   * If no hostname given then use the local name
   */
  if (!*hostname && gethostname(hostname, sizeof(hostname)) < 0) {
    perror("gethostname");
    exit(1);
  }
  hostname[sizeof(hostname) - 1] = '\0';

  /*
   * Get the username
   */
  username = find_username();

  /*
   * New hosts and automounts
   */
  list_of_hosts = new_que();
  list_of_automounts = new_que();

  /*
   * New dictionaries
   */
  dict_of_volnames = new_dict();
  dict_of_hosts = new_dict();

  /*
   * Parse input
   */
  show_area_being_processed("read config", 11);
  if (fsi_parse())
    errors = 1;
  errors += file_io_errors + parse_errors;

  if (errors == 0) {
    /*
     * Do semantic analysis of input
     */
    analyze_hosts(list_of_hosts);
    analyze_automounts(list_of_automounts);
  }

  /*
   * Give up if errors
   */
  if (errors == 0) {
    /*
     * Output data files
     */

    write_atab(list_of_automounts);
    write_bootparams(list_of_hosts);
    write_dumpset(list_of_hosts);
    write_exportfs(list_of_hosts);
    write_fstab(list_of_hosts);
  }
  col_cleanup(1);

  exit(errors);
  return errors; /* should never reach here */
}
// USER FUNCTIONS
// Manages the user interaction
void manage_user(int fd, int registration) {
    User user = None;
    char buffer[MAXBUF+1];
    if (registration == 1) {
        _info("Serving the registration form.");
        User new_user = (struct UserType*)malloc(sizeof(struct UserType));
        new_user->fd = fd;
        new_user->notifications = None;
        new_user->noti_count = 0;
        new_user->private_messages = None;
        
        do {
            _send(fd, " > Username: "******":q") == 0) {
                _error("Registration task was interrupted by user..");
                return;
            }
        } while (find_username(Users, buffer) is_not None);
        new_user->username = (char*)malloc(sizeof(MAXBUF));
        strcpy(new_user->username, buffer);
        
        _send(fd, " > Nome: ");
        _recv(fd, buffer, 1);
        if (strcmp(buffer, ":q") == 0) {
            _error("Registration task was interrupted by user..");
            return;
        }
        new_user->name = (char*)malloc(sizeof(MAXBUF));
        strcpy(new_user->name, buffer);
        
        _send(fd, " > Cognome: ");
        _recv(fd, buffer, 1);
        if (strcmp(buffer, ":q") == 0) {
            _error("Registration task was interrupted by user..");
            return;
        }
        new_user->surname = (char*)malloc(sizeof(MAXBUF));
        strcpy(new_user->surname, buffer);
        
        _send(fd, " > Posizione X (int): ");
        _recv(fd, buffer, 1);
        if (strcmp(buffer, ":q") == 0) {
            _error("Registration task was interrupted by user..");
            return;
        }
        new_user->x = atoi(buffer) % 256;
        
        _send(fd, " > Posizione Y (int): ");
        _recv(fd, buffer, 1);
        if (strcmp(buffer, ":q") == 0) {
            _error("Registration task was interrupted by user..");
            return;
        }
        new_user->y = atoi(buffer) % 256;
        
        _send(fd, " > Raggio d'Azione (int): ");
        _recv(fd, buffer, 1);
        if (strcmp(buffer, ":q") == 0) {
            _error("Registration task was interrupted by user..");
            return;
        }
        new_user->rad = atoi(buffer) % 256;
        user = new_user;
        pthread_mutex_lock(&users_mutex);
        if (Users is None) {
            Users = (UserList)malloc(sizeof(struct UserListType));
            Users->info = user;
            Users->next = NULL;
        } else {
            UserList N = (UserList)malloc(sizeof(struct UserListType));
            N->info = user;
            N->next = Users;
            Users = N;
        }
        pthread_mutex_unlock(&users_mutex);
        pthread_mutex_lock(&map_mutex);
        map_user(user);
        pthread_mutex_unlock(&map_mutex);
        _infoUser("New user created.", user->username);
    }
    assert(user is_not None);
    _infoUser("User logged in.", user->username);
    int choice;
    do {
        memset(buffer, 0, MAXBUF);
        char notif[MAXBUF];
        if (user->noti_count > 0) {
            sprintf(notif, "\n## MESSAGE SYSTEM MENU ## [Notifiche: %d]",
                    user->noti_count);
        } else {
            sprintf(notif, "\n## MESSAGE SYSTEM MENU ##");
        }
        strcat(notif, menu);
        _send(user->fd, notif);
        _recv(user->fd, buffer, 1);
        while (atoi(buffer) <= 0 or atoi(buffer) > 8) {
            _send(user->fd, " > ");
            _recv(user->fd, buffer, 1);
        }
        choice = atoi(buffer);
        switch (choice) {
            case 1: {
                show_users_available(user);
            }
                break;
            case 2: {
                send_public_message(user);
            }
                break;
            case 3: {
                send_private_message(user);
            }
                break;
            case 4: {
                read_public_messages(user);
                break;
            }
                break;
            case 5: {
                read_private_messages(user);
                break;
            }
            case 6: {
                move_user(user);
            }
                break;
            case 7: {
                read_notifications(user);
            }
                break;
            case 8: {
                return;
            }
            default:
                break;
        }
    } while (choice > 0 && choice < 8);
}