home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / FBE11.ZIP / FE.C < prev    next >
C/C++ Source or Header  |  1990-09-06  |  12KB  |  446 lines

  1. //---------------------------------------------------------------------------
  2. //
  3. //    Copyright (C) 1990, Dave Peckham
  4. //
  5. //
  6. //         Name:    FE.EXE
  7. //
  8. //      Purpose:    Show files in current directory by file extension
  9. //
  10. //   Parameters:    -h  show help screen
  11. //
  12. //  Description:    FE lists files in the current directory by their file 
  13. //                                extension.  Extensions and files within each extension 
  14. //                                are sorted alphabetically.
  15. //
  16. //                                Please refer to FE.DOC or FE.WP for more information.
  17. //
  18. //     Filename:    FE.C    - Microsoft C 5.1, Small Model
  19. //
  20. //----------------------------- Current Revision ----------------------------
  21. //
  22. //    Revision:   1.1
  23. //
  24. //        Date:   27 Aug 1990
  25. //
  26. //      Author:   Dave Peckham
  27. //
  28. //----------------------------- Revision History ----------------------------
  29. //
  30. //    Rev 1.0   27 Aug 1990   dhp
  31. // Initial revision.  Please distribute freely.
  32. //  
  33. //---------------------------------------------------------------------------
  34.  
  35.  
  36. #define INCL_DOSPROCESS
  37. #define INCL_DOSSEMAPHORES
  38. #define INCL_DOSQUEUES
  39. #define INCL_DOSFILEMGR
  40. #define INCL_DOSMEMMGR
  41. #define INCL_DOSMISC
  42. #define INCL_KBD
  43. #define INCL_VIO
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <os2.h>
  47. #include <fcntl.h>
  48. #include <ctype.h>
  49. #include <string.h>
  50. #include "getopt.h"
  51.  
  52.  
  53. /* For now, limits are constants */
  54. #define MAXPATHLEN 256            /* max length of path names */
  55. #define MAXNAMELEN 14                /* max length of file name */
  56. #define MAXEXTLEN 3                    /* max length of file extension */
  57. #define MAXEXTS 100                    /* max file extensions in a directory */
  58. #define MAXFILES 500                /* max files per extension */
  59.  
  60. /* Macro and string buffer for VioWrtTTY call */
  61. #define Write(s)    VioWrtTTY(s, strlen (s), 0)
  62. char write_buf [80] ;
  63.  
  64. /* Globals */
  65. static char *aExt [MAXEXTS] ;        /* file extensions */
  66. static USHORT usExt = 0 ;
  67. static char *aFile [MAXFILES] ;    /* files */
  68. static USHORT usFile = 0 ;
  69. static USHORT usFileTotal = 0 ;
  70. static USHORT usSubDirs = 0 ;
  71. static USHORT usColumns = 6 ;
  72. static USHORT usTotals = 0 ;
  73.  
  74.  
  75.  
  76. /* Function prototypes */
  77. int compare (char **arg1, char **arg2) ;
  78. USHORT GetExtensions (void) ;
  79. USHORT GetFiles (char *szExt) ;
  80. void ShowHelp (void) ;
  81. void CheckForKey (void) ;
  82.  
  83.  
  84.  
  85. //---------------------------------------------------------------------------
  86. //
  87. //     Function:    ShowHelp ()
  88. //
  89. //       Syntax:    void ShowHelp (void)
  90. //
  91. //   Parameters:    None
  92. //
  93. //      Returns:    Nothing
  94. //    
  95. //  Description:    Displays information about this program
  96. //
  97. //---------------------------------------------------------------------------
  98.  
  99. void ShowHelp (void)
  100. {
  101.     Write ("    FE [-H] [-T] [-C#]\r\n\n") ;
  102.     Write ("Switches\r\n") ;
  103.     Write ("    -H   Show this help information\r\n") ;
  104.     Write ("    -T   Show file totals\r\n") ;
  105.     Write ("    -C#  Display files in # columns (1..255)\r\n\n") ;
  106.     Write ("Author\r\n") ;
  107.     Write ("    Dave Peckham\r\n") ;
  108.     Write ("    6809 Prince Georges Avenue\r\n") ;
  109.     Write ("    Takoma Park, MD 20912-4862\r\n") ;
  110. }
  111.  
  112.  
  113.  
  114. //---------------------------------------------------------------------------
  115. //
  116. //     Function:    compare ()
  117. //
  118. //       Syntax:    int compare (char **arg1, char **arg2)
  119. //
  120. //   Parameters:    char **arg1        first string
  121. //                                char **arg2        second string
  122. //
  123. //      Returns:    Zero if identical
  124. //                                Negative if first string is less than second
  125. //                                Positive if first string is greater than second
  126. //    
  127. //  Description:    Comparison function used by qsort().  Compares
  128. //                                two strings.
  129. //
  130. //---------------------------------------------------------------------------
  131.  
  132. int compare (char **arg1, char **arg2)
  133. {
  134.     return (strncmp (*arg1, *arg2, max (strlen (*arg1), strlen (*arg2)))) ;
  135. }
  136.  
  137.  
  138.  
  139. //---------------------------------------------------------------------------
  140. //
  141. //     Function:    CheckForKey ()
  142. //
  143. //       Syntax:    void CheckForKey (void)
  144. //
  145. //   Parameters:    None
  146. //
  147. //      Returns:    None
  148. //    
  149. //  Description:    Checks for user input.  If user pressed <Esc>, return
  150. //                                exit immediately.  Any other key pauses display.
  151. //
  152. //---------------------------------------------------------------------------
  153.  
  154. void CheckForKey (void)
  155. {
  156.     KBDKEYINFO kbci ;
  157.  
  158.     /* Was a key pressed? */
  159.     KbdCharIn (&kbci,         
  160.                         IO_NOWAIT, 
  161.                         0);                 
  162.     if (kbci.fbStatus == FINAL_CHAR_IN) {
  163.         if (kbci.chChar != 27) {
  164.             Write ("Paused: Press any key to continue...") ;
  165.             KbdFlushBuffer (0) ;
  166.             KbdCharIn (&kbci,    
  167.                             IO_WAIT,
  168.                             0);            
  169.             Write ("\r                                    \r") ;
  170.             }
  171.         else
  172.             DosExit (EXIT_PROCESS, 1) ;
  173.         }
  174. }
  175.  
  176.  
  177.  
  178. //---------------------------------------------------------------------------
  179. //
  180. //     Function:    GetExtensions ()
  181. //
  182. //       Syntax:    USHORT GetExtensions (void)
  183. //
  184. //   Parameters:    None
  185. //
  186. //      Returns:    1 if successful, 0 otherwise
  187. //    
  188. //  Description:    Finds all unique file extensions in the current
  189. //                                directory.
  190. //
  191. //---------------------------------------------------------------------------
  192.  
  193. USHORT GetExtensions (void)
  194. {
  195.     HDIR hdir = HDIR_CREATE ;
  196.     USHORT usSearchCount = 1 ;
  197.     FILEFINDBUF findbuf ;
  198.     USHORT found ;
  199.     char *buffer ;
  200.     USHORT result, i = 0 ;
  201.  
  202.     if (DosFindFirst ("*.*",            /* filename to search for      */
  203.             &hdir,                                        /* address of directory handle */
  204.             FILE_NORMAL,                            /* type of files to search for */
  205.             &findbuf,                                    /* address of buffer                     */
  206.             sizeof (findbuf),                    /* size of buffer                            */
  207.             &usSearchCount,                        /* number of matching entries    */
  208.             0L))                                            /* reserved                                        */
  209.         Write ("No files in this directory\r\n") ;
  210.     else {
  211.         /* Create the unsorted list */
  212.         usExt = 0 ;
  213.         do
  214.             {
  215.             buffer = strchr (findbuf.achName, '.') ;
  216.             buffer++ ;
  217.  
  218.             /* Do we have this extension already? */
  219.             found = 0 ;
  220.             for (i = 0 ; i < usExt ; i++) {
  221.                 if (strncmp (buffer, aExt [i],
  222.                     max (strlen (buffer), strlen (aExt [i]))) == 0)
  223.                     found = 1 ;
  224.                 }
  225.             if (!found) {
  226.                 aExt [usExt] = (char *) calloc (1, MAXEXTLEN + 1) ;
  227.                 if (aExt [usExt] == NULL) {
  228.                     Write ("Not enough memory for file extensions\r\n") ;
  229.                     DosExit (EXIT_PROCESS, 1) ;
  230.                     }
  231.                 else
  232.                     strcpy (aExt [usExt++], buffer) ;
  233.                 }
  234.             }
  235.         while (DosFindNext (hdir,      /* handle of directory     */
  236.             &findbuf,                    /* address of buffer       */
  237.             sizeof (findbuf),            /* length of buffer        */
  238.             &usSearchCount)              /* number of files to find */
  239.             == 0) ;                      /* while no error          */
  240.  
  241.         /* Sort the list of file extensions */
  242.         qsort ((void *) aExt, (size_t) usExt, (size_t) sizeof (char *), compare) ;
  243.  
  244.         result = 1 ;
  245.         }
  246.  
  247.     return (result) ;
  248. }
  249.  
  250.  
  251.  
  252. //---------------------------------------------------------------------------
  253. //
  254. //     Function:    GetFiles ()
  255. //
  256. //   Parameters:    char *szExt                File extension to be processed
  257. //                                USHORT usColumns    Number of file columns in display
  258. //                                USHORT usTotals        If 1, show count of files
  259. //
  260. //      Returns:    1 if successful, 0 otherwise
  261. //    
  262. //  Description:    Finds all files in the current directory with 
  263. //                                extension szExt.
  264. //
  265. //---------------------------------------------------------------------------
  266.  
  267. USHORT GetFiles (char *szExt)
  268. {
  269.     HDIR hdir = HDIR_CREATE ;
  270.     USHORT usSearchCount = 1 ;
  271.     FILEFINDBUF findbuf ;
  272.     char buffer [MAXEXTLEN + 3] ;
  273.     USHORT result, i = 0 ;
  274.  
  275.     /* fe.c globals */
  276.     extern USHORT usColumns ;
  277.     extern USHORT usTotals ;
  278.     extern USHORT usSubDirs ;
  279.     extern USHORT usFile ;
  280.     extern USHORT usFileTotal ;
  281.  
  282.     usFile = 0 ;
  283.  
  284.     strcpy (buffer, "*.") ;
  285.     strcat (buffer, szExt) ;
  286.  
  287.     if (DosFindFirst (buffer,            /* filename to search for      */
  288.             &hdir,                                        /* address of directory handle */
  289.             FILE_NORMAL,                            /* type of files to search for */
  290.             &findbuf,                                    /* address of buffer                     */
  291.             sizeof (findbuf),                    /* size of buffer                            */
  292.             &usSearchCount,                        /* number of matching entries    */
  293.             0L)) {                                        /* reserved                                        */
  294.         sprintf (write_buf, "No .%-3s files found\n", szExt) ;
  295.         Write (write_buf) ;
  296.         }
  297.     else {
  298.         /* Create the unsorted list */
  299.         usFile = 0 ;
  300.         do
  301.             {
  302.             aFile [usFile] = (char *) calloc (1, MAXNAMELEN + 1) ;
  303.             if (aFile [usFile] == NULL) {
  304.                 Write ("Not enough memory for file names\r\n") ;
  305.                 DosExit (EXIT_PROCESS, 1) ;
  306.                 }
  307.             else
  308.                 strncpy (aFile [usFile++], findbuf.achName, strcspn (findbuf.achName, ".")) ;
  309.             }
  310.         while (DosFindNext (hdir,      /* handle of directory     */
  311.             &findbuf,                    /* address of buffer       */
  312.             sizeof (findbuf),            /* length of buffer        */
  313.             &usSearchCount)              /* number of files to find */
  314.             == 0) ;                      /* while no error          */
  315.  
  316.         /* Sort the list of files */
  317.         qsort ((void *) aFile, (size_t) usFile, (size_t) sizeof (char *), compare) ;
  318.  
  319.         for (i = 0; i < usFile; i++) {
  320.             if ((i > 0) && !(i % usColumns)) {
  321.                 /* indent first column of file names all but first row */
  322.                 Write ("\n\r") ;
  323.                 CheckForKey () ;
  324.                 sprintf (write_buf, "                   %-8s  ", strlwr (aFile [i])) ;
  325.                 Write (write_buf) ;
  326.                 }
  327.             else {
  328.                 /* any other column on this line */
  329.                 sprintf (write_buf, "%-8s  ", strlwr (aFile [i])) ;
  330.                 Write (write_buf) ;
  331.                 }
  332.             free (aFile [i]) ;
  333.             }
  334.  
  335.         if (usTotals) {
  336.             sprintf (write_buf, "\r\n                   %d ", usFile) ;
  337.             Write (write_buf) ;
  338.             if (usFile == 1)
  339.                 Write ("file\r\n") ;
  340.             else
  341.                 Write ("files\r\n") ;
  342.             }
  343.         else
  344.             Write ("\r\n") ;
  345.  
  346.         usFileTotal += usFile ;
  347.         result = 1 ;
  348.         }
  349.  
  350.     return (result) ;
  351. }
  352.  
  353.  
  354.  
  355. void main (int argc, char **argv)
  356. {
  357.     /* Machine mode */
  358. //    BYTE bMode;
  359.  
  360.     /* Drive and path */
  361.     char pszPath [MAXPATHLEN] ;
  362.     USHORT cbPath = MAXPATHLEN ;
  363.     USHORT usDisk ;
  364.     ULONG ulDrives ;
  365.     USHORT i ;
  366.     char c ;
  367.  
  368.     /* fe.c globals */
  369.     extern USHORT usColumns ;
  370.     extern USHORT usTotals ;
  371.     extern USHORT usSubDirs ;
  372.     extern USHORT usFileTotal ;
  373.  
  374.     /* defined in getopt.c */
  375.     extern char *optarg ;
  376.     extern int opterr ;
  377.  
  378.     opterr = 0 ;
  379.  
  380.  
  381.     Write ("FE-Files by Extension 1.1, Copyright (C) 1990, Dave Peckham\r\n\n") ;
  382.  
  383.      /* parse command line options */
  384.     while ((c = (char) getopt (argc, argv, "c:hst")) != EOF) {
  385.         switch (c) {
  386.             case 'c':
  387.                 usColumns = min (max (atoi (optarg), 1), 255) ;
  388.                 break ;
  389.             case 's':
  390.                 usSubDirs = 1 ;
  391.                 break ;
  392.             case 't':
  393.                 usTotals = 1 ;
  394.                 break ;
  395.             case 'h':
  396.             default :
  397.                 ShowHelp () ;
  398.                 DosExit (EXIT_PROCESS, 1) ;
  399.                 break ;
  400.             }
  401.         }
  402.  
  403.     /* Find out if we're in protected or real mode */
  404. //    DosGetMachineMode (&bMode);
  405.  
  406.     /* Get the current drive and path */
  407.     DosQCurDisk (&usDisk, &ulDrives);        /* get current drive */
  408.  
  409.     DosQCurDir (usDisk,            /* drive number                */
  410.                             pszPath,        /* buffer for directory path   */
  411.                             &cbPath) ;    /* length of directory buffer  */
  412.  
  413.     sprintf (write_buf, "  %c:\\%s\r\n\n", usDisk+'A'-1, pszPath) ;
  414.     Write (write_buf) ;
  415.  
  416.     if (GetExtensions ()) {
  417.         /* Display the sorted list */
  418.         for (i = 0 ; i < usExt ; i++) {
  419.              sprintf (write_buf, "    .%-3s files:    ", aExt [i]) ;
  420.              Write (write_buf) ;
  421.             GetFiles (aExt [i]) ;
  422.             free (aExt [i]) ;
  423.             CheckForKey () ;
  424.             }
  425.  
  426.         if (usTotals) {
  427.             sprintf (write_buf, "\r\n    %3d file extension", usExt) ;
  428.             Write (write_buf) ;
  429.             if (usExt == 1)
  430.                 Write (" found") ;
  431.             else
  432.                 Write ("s found") ;
  433.             sprintf (write_buf, "\r\n    %3d file", usFileTotal) ;
  434.             Write (write_buf) ;
  435.             if (usFileTotal == 1)
  436.                 Write (" found") ;
  437.             else
  438.                 Write ("s found") ;
  439.             Write ("\r\n") ;
  440.             }
  441.         }
  442.  
  443.  
  444.     DosExit (EXIT_PROCESS, 0) ;
  445. }
  446.