home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / FILELIST.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  102 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. ** FILELIST.C - Creates a link list of files and returns number of
  5. **              files found.
  6. **
  7. ** public domain by Phi Nguyen
  8. */
  9.  
  10. #include "filelist.h"
  11.  
  12. /*
  13. ** Free_FileList - This function simply frees each linked list item.
  14. */
  15.  
  16. void Free_FileList(FILELIST *p)
  17. {
  18.       FILELIST *next = p->next;
  19.  
  20.       while (NULL != next)
  21.       {
  22.             p->next = next->next;
  23.             free(next);
  24.             next = p->next;
  25.       }
  26. }
  27.  
  28. /*
  29. ** Get_FileList - This function creates a linked list of input source
  30. **                files.  Wildcard specifications are accommodated.
  31. */
  32.  
  33. int Get_FileList(FILELIST *list, char **argv, int argc, int attr)
  34. {
  35.       int          i = 0,
  36.                    nCount = 0;
  37.       DOSFileData  file;
  38.       FILELIST    *base = list,
  39.                   *node;
  40.  
  41.       for ( ; i < argc; i++)
  42.       {
  43.             if (!FIND_FIRST(argv[i], attr, &file)) do
  44.             {
  45.                   if (NULL == (node =(FILELIST *)malloc(LIST_SIZE)))
  46.                   {
  47.                         Free_FileList(list);
  48.                         list = NULL;
  49.                         return 0;
  50.                   }
  51.                   base->file = file;
  52.                   base->next = node;
  53.                   node->next = NULL;
  54.                   base = node;
  55.                   nCount++;
  56.             } while (!FIND_NEXT(&file));
  57.       }
  58.       return nCount;
  59. }
  60.  
  61. #ifdef TEST
  62.  
  63. #include <stdio.h>
  64. #define _A_FILE (_A_NORMAL|_A_RDONLY|_A_HIDDEN|_A_SYSTEM|_A_ARCH)
  65.  
  66. main(int argc, char *argv[])
  67. {
  68.       FILELIST  list,
  69.                *plist = &list;
  70.       int       i,
  71.                 nFiles;
  72.  
  73.       if (argc < 2)
  74.       {
  75.             puts("Usage: FILELIST filespec ....");
  76.             return EXIT_FAILURE;
  77.       }
  78.  
  79.       nFiles = Get_FileList(plist, argv + 1, argc - 1, _A_FILE);
  80.       if (nFiles >= 0)
  81.       {
  82.             for (i = 0; i < nFiles; i++)
  83.             {
  84.                   printf("%-12s %8li  %2u-%02u-%02u  %2u:%02u\n",
  85.                         ff_name(&plist->file), ff_size(&plist->file),
  86.                         ff_mo(&plist->file), ff_day(&plist->file),
  87.                         ff_yr(&plist->file) + 80, ff_hr(&plist->file),
  88.                         ff_min(&plist->file));
  89.                   plist = plist->next;
  90.             }
  91.             Free_FileList(&list);
  92.       }
  93.       else
  94.       {
  95.             puts("Get_FileList() failed");
  96.             return EXIT_FAILURE;
  97.       }
  98.       return EXIT_SUCCESS;
  99. }
  100.  
  101. #endif /* TEST */
  102.