home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNIP9404.ZIP / LSARY.C < prev    next >
C/C++ Source or Header  |  1994-04-03  |  3KB  |  113 lines

  1. /*
  2. ** LSARY - A simple directory lister using a filename array
  3. ** A public domain C demo program by Bob Stout
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10. #include <dos.h>
  11.  
  12. /* For portability, make everything look like MSC 6 */
  13.  
  14. #if defined(__TURBOC__)
  15.  #include <dir.h>
  16.  #define _dos_findfirst(f,a,b) findfirst(f,b,a)
  17.  #define _dos_findnext(b) findnext(b)
  18.  #define find_t ffblk
  19.  #ifndef _A_SUBDIR
  20.   #define _A_SUBDIR FA_DIREC
  21.  #endif
  22.  #define attrib ff_attrib
  23.  #define name ff_name
  24.  #define size ff_fsize
  25.  #define wr_time ff_ftime
  26.  #define wr_date ff_fdate
  27.  #define _dos_getdiskfree getdfree
  28.  #define diskfree_t dfree
  29.  #define avail_clusters df_avail
  30.  #define sectors_per_cluster df_sclus
  31.  #define bytes_per_sector df_bsec
  32. #else                   /* assume MSC/QC                                */
  33.  #include <errno.h>
  34. #endif
  35.  
  36. #ifdef TRUE
  37.  #undef TRUE
  38. #endif
  39. #ifdef FALSE
  40.  #undef FALSE
  41. #endif
  42. #ifdef ERROR
  43.  #undef ERROR
  44. #endif
  45.  
  46. enum LOGICAL {ERROR = -1, SUCCESS, FALSE = 0, TRUE};
  47.  
  48. #ifndef CAST
  49.  #define CAST(new_type,old_object) (*((new_type *)&(old_object)))
  50. #endif
  51.  
  52. #define LAST_CHAR(s) (((char *)s)[strlen(s) - 1])
  53.  
  54. struct DirEntry {
  55.       char fname[FILENAME_MAX];
  56.       struct DirEntry *next;
  57. } DirRoot = {"", NULL};
  58.  
  59. /*
  60. **  Read a directory into an array
  61. */
  62.  
  63. int ReaDirArray(char *path)
  64. {
  65.       struct find_t ff;
  66.       char pattern[67];
  67.       struct DirEntry *base = &DirRoot;
  68.  
  69.       strcpy(pattern, path);
  70.       if ('/' != LAST_CHAR(pattern) && '\\' != LAST_CHAR(pattern))
  71.             strcat(pattern, "\\");
  72.       strcat(pattern, "*.*");
  73.       if (SUCCESS == _dos_findfirst(pattern, 0xff, &ff)) do
  74.       {
  75.             struct DirEntry *node;
  76.  
  77.             if (NULL == (node = malloc(sizeof(struct DirEntry))))
  78.                   return ERROR;
  79.             base->next = node;
  80.             strcpy(base->fname, ff.name);
  81.             node->next = NULL;
  82.             *node->fname = '\0';
  83.             base = node;
  84.             
  85.       } while (SUCCESS == _dos_findnext(&ff));
  86.       return SUCCESS;
  87. }
  88.  
  89. /*
  90. **  Simple directory lister
  91. */
  92.  
  93. void main(int argc, char *argv[])
  94. {
  95.       char *path;
  96.  
  97.       if (2 > argc)
  98.             path = ".";
  99.       else  path = argv[1];
  100.       if (ERROR == ReaDirArray(path))
  101.             printf("*** Could not read %s\n", path);
  102.       else
  103.       {
  104.             struct DirEntry *node = &DirRoot;
  105.             printf("Directory of %s\n\n", path);
  106.             while (node)
  107.             {
  108.                   puts(node->fname);
  109.                   node = node->next;
  110.             }
  111.       }
  112. }
  113.