home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / c / gdir.c < prev    next >
C/C++ Source or Header  |  1994-03-04  |  9KB  |  261 lines

  1. ----------------------------- GDIR.DOC (cut here) -----------------------------
  2. Gdir is an enhancement over DOS's dir command.  It includes file attributes
  3. in its display, allows you to perform directories of hidden and system files,
  4. and also allows you to recursively check all of a directory's subdirectories
  5. as well.
  6.  
  7. Usage:      GDIR [/I] [/A] [/D] [/R] [d:][dirspec][filespec]
  8. Options:    /I - Include invisible (hidden and system) files in listing.
  9.          Default is to not list invisible files.
  10.         /A - Include files with leading dot in listing.  Default is to
  11.          not list files with a leading dot.
  12.         /D - Only list directories.  Default is to list directories and
  13.          files.
  14.         /R - Recursively GDIR on all subdirectories
  15. Parameters: d        - drive to search; defaults to current drive
  16.             dirspec  - directory to search; defaults to current directory
  17.             filespec - file(s) to search for; defaults to "*.*"
  18.             Note: filespec may be wildcarded.  If file extension is left
  19.         blank, it will default to none, not "*"
  20. Returns:    0 = normal return     1 = no files found     2 = syntax error
  21.  
  22. Notes:
  23. - Any switches must be given before the path.
  24. - Unlike dir, "gdir \foo" where \foo is a directory will only show the
  25.   directory entry for \foo (which will show the /D, or directory, attribute).
  26.   It will not list the files contained in \foo.  Use "gdir \foo\" for to list
  27.   the files in \foo.
  28.  
  29. Examples:
  30.  
  31. GDIR
  32. Normal directory listing of current directory.
  33. GDIR /I \*.COM
  34. Lists all .COM files in the root directory, including hidden and system files.
  35. GDIR /D /R \
  36. Lists all directories on current drive.
  37. GDIR /D /R /A \
  38. Same as above, but includes "." and ".." directory entries as well.
  39.  
  40. Attribute flags:
  41. Gdir displays the following attribute flags for a file:
  42. /R - read-only        /S - system        /M - modified
  43. /H - hidden        /D - directory
  44. ------------------------- end of GDIR.DOC (cut here) --------------------------
  45.  
  46. ------------------------------ GDIR.C (cut here) ------------------------------
  47. /*                      ---------------------------------         
  48.                         |GLOBAL DIRECTORY SEARCH PROGRAM|
  49.                         |    by R. Lenoil - 8/16/84     |
  50.                         ---------------------------------
  51.  
  52. Placed in the public domain, June 1986.
  53. Author's electronic mail address:
  54. USENET: lenoil@mit-eddie.uucp            ARPA: lenoil@eddie.mit.edu   */
  55.  
  56. /******************************************************************************
  57.  Effect:     Shows directory information for files in several directories.
  58.  Usage:      GDIR [/I] [/A] [/D] [/R] [d:][dirspec][filespec]
  59.  Parameters: d        - drive to search; defaults to current drive
  60.              dirspec  - directory to search; defaults to current directory
  61.              filespec - file(s) to search for; defaults to "*.*"
  62.              Note: filespec may be wildcarded.  If file extension is left
  63.            blank, it will default to none, not "*"
  64.  Options:    /I - Include invisible (hidden and system) files in listing.
  65.           Default is to not list invisible files.
  66.          /A - Include files with leading dot in listing.  Default is to
  67.           not list files with a leading dot.
  68.          /D - Only list directories.  Default is to list directories and
  69.           files.
  70.          /R - Recursively GDIR on all subdirectories
  71.  Returns:    0 = normal return        1 = no files found    2 = syntax error
  72. ******************************************************************************/
  73. /* Updated 7/12/85 to not use TREE.COM for directory search */
  74.  
  75. #include <stdio.h>
  76. #include <dos.h>
  77. #include <ctype.h>
  78. /* attribute bits */
  79. #define READ_ONLY 1
  80. #define HIDDEN 2
  81. #define SYSTEM 4
  82. #define SUBDIRS 16
  83. #define MODIFIED 32
  84. /* strings */
  85. #define PATH_SEPERATOR "\\"
  86. #define FNAME_DEFAULT "*.*"
  87. /* system calls */
  88. #define SET_DTA        0x1a
  89. #define GET_DTA        0x2f
  90. #define FIND_FIRST    0x4e
  91. #define FIND_NEXT    0x4f
  92.  
  93.       /*++++++++++++++++++++  External Definitions  ++++++++++++++++++++*/
  94. static    char    fname_default[] = FNAME_DEFAULT,
  95.         path_seperator[] = PATH_SEPERATOR,
  96.         switchar;    /* holds system switch character */
  97. char    *strrchr();    /* finds last occurrence of a character in a string */
  98. struct dta    /* disk transfer area (for find) */
  99. {    char    rsvd[21],    /* reserved for DOS */
  100.         attrib;        /* file atribute */
  101.     struct            /* Rep for file modification date */
  102.     {    unsigned twosec    : 5;
  103.         unsigned min    : 6;
  104.         unsigned hour    : 5;
  105.         unsigned day    : 5;
  106.         unsigned month    : 4;
  107.         unsigned year    : 7;
  108.     }    time;
  109.     long    size;        /* file size */
  110.     char    name[13];    /* file name */
  111. };
  112.     /*++++++++++++++++++++  end External Definitions  ++++++++++++++++++++*/
  113.  
  114.      /*++++++++++++++++++++ procedure MAIN ++++++++++++++++++++*/
  115. main(nargs,args)
  116.  
  117. int   nargs;  /* # of words on cmd line */
  118. char *args[]; /* array of ptrs to words */
  119. {    char    search_string[100],    /* search string passed to DOS */
  120.         fname[13],        /* filename to search for */
  121.         get_switchar();        /* returns switchar */
  122.     register i;
  123.     int    attribs = 0,        /* attributes to search for */
  124.         subdirs = 0,        /* global search or not */
  125.         all    = 0,        /* print all files or not */
  126.         files;            /* number of files matched */
  127.  
  128.     /* get switch character */
  129.     switchar = get_switchar();
  130.  
  131.     /* uppercase args */
  132.     for (i=0; ++i < nargs;) strupr(args[i]);
  133.  
  134.     /* parse switches */
  135.     while (++args,--nargs,**args == switchar && nargs)
  136.     {    if (args[0][2]) Syntax();
  137.         switch (toupper(args[0][1]))
  138.         {    case 'I':    attribs |= HIDDEN + SYSTEM; break;
  139.             case 'A':    all = 1; break;
  140.             case 'R':    subdirs = 1; break;
  141.             case 'D':    attribs |= SUBDIRS; break;
  142.             default:    Syntax();
  143.         }
  144.     }
  145.  
  146.     if (nargs > 1) Syntax();
  147.     if (rootpath( (nargs)? *args : fname_default , search_string))
  148.         files = 0;
  149.     else
  150.     {    char *name_start = strrchr(search_string,'\\')+1;
  151.         if (*name_start)
  152.         {    strncpy(fname,name_start,12);
  153.             fname[12] = *name_start = 0;
  154.         }
  155.         else    strcpy(fname,fname_default);
  156.         files = gdir(search_string,fname,attribs,subdirs,all);
  157.     }
  158.     printf("\n Found %d file%c\n",files,files==1? ' ' : 's');
  159.     return (files)? 0 : 1;
  160. }
  161.    /*++++++++++++++++++++ end procedure MAIN ++++++++++++++++++++*/
  162.  
  163.     /*++++++++++++++++++++ procedure SYNTAX ++++++++++++++++++++*/
  164. Syntax()
  165. {    printf("Usage: GDIR [/I] [/A] [/R] [/D] [d:][path\\][filespec]");
  166.     exit(2);
  167. }
  168.   /*++++++++++++++++++++ end procedure SYNTAX ++++++++++++++++++++*/
  169.  
  170.          /*++++++++++++++++++++  procedure GDIR  ++++++++++++++++++++*/
  171. gdir(path,fname,attribs,subdirs,all)
  172.  
  173. char    *path,*fname;        /* search path */
  174.                 /* Note: path must be as large as PATHMAX */
  175.                 /* Warning: path is modified */
  176. int    attribs,        /* attribs to search for */
  177.     subdirs,        /* TRUE = do recursive search */
  178.     all;            /* TRUE = show files starting with "." */
  179. {    struct dta findbuf;
  180.     char    *name_start;        /* start of filename in path */
  181.     unsigned    pdta;        /* address of dta on entry */
  182.     union    REGS    regs;
  183.     int    files_found = 0;
  184.  
  185.     /* Save dta address, and set to ours */
  186.     regs.h.ah = GET_DTA;
  187.     intdos(®s,®s);
  188.     pdta = regs.x.bx;
  189.     regs.h.ah = SET_DTA;
  190.     regs.x.dx = (unsigned) &findbuf;
  191.     intdos(®s,®s);
  192.  
  193.     if ((name_start = strrchr(path,'\\') + 1) == (char *)1)
  194.         name_start = path;
  195.  
  196.     regs.h.ah = FIND_FIRST;
  197.     regs.x.cx = attribs | SUBDIRS;
  198.     regs.x.dx = (unsigned) path;
  199.  
  200.     /* find all matches in specified directory */
  201.     strcpy(name_start,fname);
  202.     for (;intdos(®s,®s),!regs.x.cflag;regs.h.ah=FIND_NEXT)
  203.         if (attribs & SUBDIRS && !(findbuf.attrib & SUBDIRS)) continue;
  204.         else if (*findbuf.name != '.' || all)
  205.         {    strcpy(name_start,findbuf.name);
  206.             display(path,&findbuf);
  207.             ++files_found;
  208.         }
  209.  
  210.     if (subdirs)    /* recurse on subdirs */
  211.     {    regs.h.ah = FIND_FIRST;
  212.         regs.x.cx |= SUBDIRS;
  213.         strcpy(name_start,fname_default);
  214.  
  215.         for (;intdos(®s,®s),!regs.x.cflag;regs.h.ah=FIND_NEXT)
  216.         {    if (findbuf.attrib & SUBDIRS && *findbuf.name != '.')
  217.             {    strcpy(name_start,findbuf.name);
  218.                 strcat(name_start,path_seperator);
  219.                 files_found +=
  220.                     gdir(path,fname,attribs,subdirs,all);
  221.             }
  222.         }
  223.     }
  224.     /* Restore dta */
  225.     regs.h.ah = SET_DTA;
  226.     regs.x.dx = pdta;
  227.     intdos(®s,®s);
  228.  
  229.     return files_found;
  230. }
  231.        /*++++++++++++++++++++  end procedure GDIR  ++++++++++++++++++++*/
  232.  
  233.         /*++++++++++++++++++++  procedure DISPLAY  ++++++++++++++++++++*/
  234. display(path,f)
  235.  
  236. char    *path;
  237. struct    dta    *f;
  238. #define T f->time
  239. {    printf("%s %lu %.2u-%.2u-%.2u %.2u:%.2u:%.2u",
  240.         path,f->size,T.month,T.day,80+T.year,T.hour,T.min,T.twosec*2);
  241.     if (f->attrib & READ_ONLY) printf(" /R");
  242.     if (f->attrib & HIDDEN) printf(" /H");
  243.     if (f->attrib & SYSTEM) printf(" /S");
  244.     if (f->attrib & SUBDIRS) printf(" /D");
  245.     if (f->attrib & MODIFIED) printf(" /M");
  246.     printf("\n");
  247. }
  248. #undef T
  249.       /*++++++++++++++++++++  end procedure DISPLAY  ++++++++++++++++++++*/
  250.  
  251.      /*++++++++++++++++++++  procedure GET_SWITCHAR  ++++++++++++++++++++*/
  252. char get_switchar()
  253.  
  254. {    union REGS regs;
  255.  
  256.     regs.x.ax = 0x3700; intdos(®s,®s);
  257.     return regs.h.dl;
  258. }
  259.    /*++++++++++++++++++++  end procedure GET_SWITCHAR  ++++++++++++++++++++*/
  260. -------------------------- end of GDIR.C (cut here) ---------------------------
  261.