示例#1
0
int
main( int argc, char *argv[])
{
   int size, index, flagALRT = 0;    //Variables to check integers
   struct fileInfo *table;           //Table struct to store everything
   const char *flag = {"alrt"};      //Available flags
   char c;                           //Checks for the certain flag

   //While loop that checks though for all possible flags
   while ((c = getopt(argc, argv, flag)) != -1)
   {
      //Switch statement that checks for the type of flag.
      switch(c)
      {
         case 'a':
            flagALRT += AFLAG;
            break;
         case 'l':
            flagALRT += LFLAG;
            break;
         case 'r':
            flagALRT += RFLAG;
            break;
         case 't':
            flagALRT += TFLAG;
            break;
         case '?':                   //Checks if there is an unknown flag
            (void)fprintf(stderr, "Usage: %s [-alrt] [file ...]\n", argv[0]);
            exit(EXIT_FAILURE);
      }
   }

   //Checks if it's the current directory input
   if( argc == optind )
   {
      size = buildFileInfoTable( "." , &table ); //Builds table in current
      displayFileInfo(table, size, flagALRT);    //Displays all the file info
      free(table);                               //Frees up the table
   }

   //Checks of specific file or files
   for(index = optind; index < argc; index++)
   {
      size = buildFileInfoTable(argv[index], &table); //Checks multiple files
      displayFileInfo(table, size, flagALRT);         //Display info about file
      free(table);                                    //Frees up the table
   }

   return ( 0 );                    /* Returns 0 if sucessfully executed */
}
示例#2
0
int main( int argc, char *argv[] ){
    unsigned int displayMode;
    int entries;
    struct fileInfo ** table_ptr =\
                (struct fileInfo**) malloc(sizeof(struct fileInfo**));
    
    /* Check if allocation is successful*/
    if (table_ptr == NULL) {
        perror(STR_ALLOC_ERR);
        exit(1);
    }
    
    *table_ptr = (struct fileInfo*) malloc(sizeof(struct fileInfo*));
    
    /* Check if allocation is successful*/
    if (*table_ptr == NULL) {
        perror(STR_ALLOC_ERR);
        exit(1);
    }
    
    // Set display mode
    displayMode = setDisplayMode(argc, argv);
    
    /* Check for error flag of display mode*/
    if (displayMode & ERR_FLAG) {
        (void) fprintf(stderr, STR_USAGE, argv[0]);
        exit(1);
    }
    
    
    if (argv[optind] == NULL) { // No files specified in command line
        
        // Build the file info table and the number of entries
        entries = buildFileInfoTable(".", table_ptr);
        // Display the file info
        displayFileInfo(*table_ptr, entries, displayMode);
        free(*table_ptr);
    } else {    // files specified in command line
        while (argv[optind] != NULL) {  // Keeps reading multiple files
            entries = buildFileInfoTable(argv[optind++], table_ptr);
            displayFileInfo(*table_ptr, entries, displayMode);
            free(*table_ptr);
        }
    }
    free(table_ptr);
    return 0;
}