home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / RCUTILS.ZIP / DDIR.C < prev    next >
C/C++ Source or Header  |  1992-09-28  |  10KB  |  229 lines

  1.  
  2. /********************************************************/
  3. /*                                                      */
  4. /*  Function: ddir                                      */
  5. /*                                                      */
  6. /*   Purpose: Prints current directory contents, sorted */
  7. /*              in double width.                        */
  8. /*                                                      */
  9. /*   To build: CL /F 2000 ddir.c                        */
  10. /*                                                      */
  11. /*   To use:  ddir template                             */
  12. /*                                                      */
  13. /*      File: ddir.c                                    */
  14. /*                                                      */
  15. /********************************************************/
  16.  
  17.         /* Include system calls/library routines/macros */
  18. #define INCL_DOSFILEMGR
  19. #define INCL_DOSERRORS
  20. #define INCL_DOSDATETIME
  21. #include <os2.h>
  22. #include <process.h>
  23. #include <search.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include "errdiag.h"
  28.  
  29.         /* Define constants */
  30. #define true -1
  31. #define false 0
  32. #define ALLFILETYPES  (FILE_READONLY|FILE_HIDDEN|FILE_SYSTEM|FILE_DIRECTORY)
  33. #define errex(sp) {printf(sp); exit(0);}
  34. #define MAXFILES 200
  35.  
  36. typedef struct _MYDIRSTRCT {            // Setup for directory output
  37.   char sys_desig;                       // 'S' if a system file, else '_'
  38.   char hid_desig;                       // 'H' if hidden, else '_'
  39.   char rdo_desig;                       // 'R' if read-only, else '_'
  40.   char dir_desig;                       // '/' if a directory, else '_'
  41.   char fname[8];                        // 8 char filename
  42.   char decpt;                           // A '.' if there is an extention
  43.   char ext[3];                          // 3 char extension
  44.   char size[8];                         // 8 digit # bytes in file
  45.   char sp1;                             // space for formatting
  46.   char date[6];                         // 6 char YYMMDD date
  47.   char sp2;                             // space for formatting
  48.   char time[4];                         // 4 char military time
  49.   char written_later;                   // 'L' if file written to more than one
  50.                                         //      day after it's creation date
  51.   char recent_write;                    // 'R' if file written to in last week
  52.   char write;                           // 'W' if either of above true
  53.   char recent_access;                   // 'A' if accessed in last week
  54.   char term;                            // NULL terminator
  55. } MYDIRSTRCT;
  56.  
  57. int fnamecmp(MYDIRSTRCT *buf1, MYDIRSTRCT *buf2);
  58.  
  59.         /* Define general variables */
  60. ULONG ii;
  61. ULONG total_bytes=0;                    // Total bytes in all files
  62. USHORT api_err;                         // Generic error returns
  63. PSZ FileSpec;                           // Pointer to a file spec
  64. HDIR hdir=HDIR_CREATE;                  // Create a new search handle
  65. USHORT usSearchCount = 25*MAXFILES;     // Setup to have error if too many files
  66. FILEFINDBUF filefindbuf[MAXFILES];      // Receives file info
  67. FSALLOCATE fsallocate;                  // Receives disk allocation info
  68. char *allfiles="*.*";                   // Default search spec (all files)
  69. char *nulldummy="";                     // Dummy NULL string
  70. char BuildTemp[60];                     // An array to build a template into
  71.  
  72. MYDIRSTRCT build_array, *dest_ptr;
  73. FILEFINDBUF *src_ptr;
  74. DATETIME datetime;
  75.  
  76.         /* Define file access structures */
  77.  
  78. main(argc, argv)
  79. int argc;
  80. char *argv[];
  81.   {
  82.   if (argc > 2) errex("\n\nProper usage: 'ddir [template]'\n\n");
  83.   if (argc > 1)
  84.   {
  85.     char lastchar;
  86.  
  87.     FileSpec=argv[1];
  88.     if (*FileSpec == '.')               // Starts with '.'
  89.     {
  90.       if ( (*(FileSpec+1) != '.') && (*(FileSpec+1) != '\0') )
  91.       {                                 // Is not one of the 'psuedo dirs'
  92.         strcpy(&BuildTemp[1], argv[1]); // Prepend with '*'
  93.         BuildTemp[0] = '*';
  94.       }
  95.       else strcpy(&BuildTemp[0], argv[1]);
  96.     }
  97.     else strcpy(&BuildTemp[0], argv[1]);// One way or another, string in BuildTemp
  98.     FileSpec = BuildTemp;
  99.     if ( (lastchar=BuildTemp[strlen(BuildTemp)-1]) == '.' )
  100.       strcat(BuildTemp, "\\*.*");
  101.     else if ( (lastchar == '\\') || (lastchar == ':') )
  102.       strcat(BuildTemp, "*.*");
  103.   }
  104.   else FileSpec=allfiles;
  105.  
  106.   api_err = DosFindFirst(FileSpec, &hdir, ALLFILETYPES, filefindbuf,
  107.                                     sizeof(filefindbuf), &usSearchCount, 0L);
  108.   if ( (api_err == ERROR_FILE_NOT_FOUND) || (api_err == ERROR_NO_MORE_FILES) )
  109.   {
  110.     printf("Aint got no %s\n", FileSpec);
  111.     exit(0);
  112.   }
  113.   ERRCHK(api_err);                      // Check for errors & print
  114.  
  115.   api_err=DosGetDateTime(&datetime);
  116.   ERRCHK(api_err);                      // Check for errors & print
  117.   datetime.year -= 80;                  // Get to same base as DosFindFirst
  118.   datetime.year %= 100;                 // Take mod 100 of year
  119.                                         // Get disk usage of current disk
  120.   api_err=DosQFSInfo(0, FSIL_ALLOC, (PBYTE)&fsallocate, sizeof(fsallocate));
  121.   ERRCHK(api_err);                      // Check for errors & print
  122.  
  123.   ii = sizeof(filefindbuf) - sizeof(build_array);
  124.   dest_ptr = (MYDIRSTRCT *)             // Init the end pointer
  125.      ( (char *)filefindbuf + ii );
  126.   src_ptr = (FILEFINDBUF *)filefindbuf; // Init the start pointer
  127.  
  128.   build_array.term = '\0';              // Only needs done once
  129.   for (ii=0; ii<usSearchCount; ii++)
  130.   {
  131.     char *fnameptr, *extptr;
  132.     FDATE *dispdate;
  133.     FTIME *disptime;
  134.     long recent_write, later_write, recent_access;
  135.  
  136.     fnameptr = strtok(src_ptr->achName, "."); // NULL term fname, remove '.'
  137.     extptr = strtok(NULL, ".");         // Get extension string
  138.     fnameptr = src_ptr->achName;        // Guarantee filename for '.' & '..'
  139.     if (extptr == NULL)
  140.       extptr=nulldummy;                 // Guarantee NULL ext available
  141.  
  142.     if (src_ptr->fdateCreation.year == 0)
  143.     {
  144.       dispdate = &src_ptr->fdateLastWrite;  // No create date, use last write
  145.       disptime = &src_ptr->ftimeLastWrite;
  146.     }
  147.     else
  148.     {
  149.       dispdate = &src_ptr->fdateCreation;   // Create date exists, use it
  150.       disptime = &src_ptr->ftimeCreation;
  151.     }
  152.  
  153.     sprintf( (char *)&build_array, "%c%c%c%c%-8.8s %-3.3s%8lu ",
  154.               ((src_ptr->attrFile & FILE_SYSTEM)    ? 'S' : '_'),
  155.               ((src_ptr->attrFile & FILE_HIDDEN)    ? 'H' : '_'),
  156.               ((src_ptr->attrFile & FILE_READONLY)  ? 'R' : '_'),
  157.               ((src_ptr->attrFile & FILE_DIRECTORY) ? '/' : '_'),
  158.               fnameptr, extptr, src_ptr->cbFile);
  159.  
  160.     sprintf( (char *)build_array.date, "%02d%02d%02d %02d%02d",
  161.               (dispdate->year+80)%100, dispdate->month, dispdate->day,
  162.               disptime->hours, disptime->minutes);
  163.  
  164.     total_bytes += src_ptr->cbFile;     // Add # bytes in this file to total
  165.  
  166.     if (extptr == nulldummy)            // Did we need to dummy out extension?
  167.       build_array.decpt = ' ';          // No extension, no dot
  168.     else build_array.decpt = '.';       // Put a dot before extension
  169.     build_array.write = ' ';            // See if file written in last week
  170.     recent_write = 365*(datetime.year - src_ptr->fdateLastWrite.year) +
  171.                     30*(datetime.month - src_ptr->fdateLastWrite.month) +
  172.                        (datetime.day - src_ptr->fdateLastWrite.day);
  173.     if (recent_write <= 7)
  174.     {
  175.       build_array.write='W';
  176.       build_array.recent_write='R';
  177.     }
  178.     else build_array.recent_write=' ';
  179.  
  180.     if (src_ptr->fdateCreation.year != 0)
  181.     {                                   // See if file written after creation
  182.       later_write = 365*(src_ptr->fdateLastWrite.year - src_ptr->fdateCreation.year) +
  183.                      30*(src_ptr->fdateLastWrite.month - src_ptr->fdateCreation.month) +
  184.                         (src_ptr->fdateLastWrite.day - src_ptr->fdateCreation.day);
  185.     }
  186.     if ( (src_ptr->fdateCreation.year != 0) && (later_write > 1) )
  187.     {
  188.       build_array.write='W';
  189.       build_array.written_later='L';
  190.     }
  191.     else build_array.written_later=' ';
  192.  
  193.     if (src_ptr->fdateLastAccess.year != 0)
  194.     {                                   // See if file accessed in last week
  195.       recent_access = 365*(datetime.year - src_ptr->fdateLastAccess.year) +
  196.                        30*(datetime.month - src_ptr->fdateLastAccess.month) +
  197.                           (datetime.day - src_ptr->fdateLastAccess.day);
  198.     }
  199.     if ( (src_ptr->fdateLastAccess.year != 0) && (recent_access <= 7) )
  200.           build_array.recent_access='A';
  201.     else  build_array.recent_access=' ';
  202.  
  203.     *dest_ptr-- = build_array;
  204.  
  205.         // Point to next file in buffer
  206.     src_ptr = (FILEFINDBUF *)&src_ptr->achName[src_ptr->cchName+1];
  207.   }
  208.   dest_ptr++;                           // Point to last item converted
  209.                                         // Sort based on flags and fname/ext
  210.   qsort((void *)dest_ptr, usSearchCount, sizeof(MYDIRSTRCT), fnamecmp );
  211.  
  212.   for (ii=0; ii<usSearchCount; ii++)
  213.   {
  214.     fputs((char *)dest_ptr++, stdout);
  215. //    if (ii&1) fputc('\n', stdout);
  216.   }
  217.   if (ii&1) fputc('\n', stdout);
  218.  
  219.   printf("Total of %d files using %lu bytes. %lu bytes free (on current disk)\n",
  220.     usSearchCount, total_bytes,
  221.             fsallocate.cSectorUnit*fsallocate.cUnitAvail*fsallocate.cbSector);
  222. }
  223.  
  224. int fnamecmp(MYDIRSTRCT *buf1, MYDIRSTRCT *buf2)
  225. {
  226.   return( strncmp((void *)buf1, (void *)buf2, 15) );
  227. }
  228.  
  229.