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

  1.  
  2. /********************************************************/
  3. /*                                                      */
  4. /*  Function: new                                       */
  5. /*                                                      */
  6. /*   Purpose: Prints files created in the last nn days. */
  7. /*              nn defaults to 1 day.                   */
  8. /*                                                      */
  9. /*   To build: CL /F 2000 new.c                         */
  10. /*                                                      */
  11. /*   To use:  new /nn template                          */
  12. /*                                                      */
  13. /*      File: new.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 <time.h>
  28. #include "errdiag.h"
  29.  
  30.         /* Define constants */
  31. #define true -1
  32. #define false 0
  33. #define ALLFILETYPES  (FILE_READONLY|FILE_HIDDEN|FILE_SYSTEM|FILE_DIRECTORY)
  34. #define errex(sp) {printf(sp); exit(0);}
  35. #define MAXFILES 200
  36.  
  37. typedef struct _MYDIRSTRCT {            // Setup for directory output
  38.   char sys_desig;                       // 'S' if a system file, else '_'
  39.   char hid_desig;                       // 'H' if hidden, else '_'
  40.   char rdo_desig;                       // 'R' if read-only, else '_'
  41.   char dir_desig;                       // '/' if a directory, else '_'
  42.   char sp1;                             // space for formatting
  43.   char fname[8];                        // 8 char filename
  44.   char decpt;                           // A '.' if there is an extention
  45.   char ext[3];                          // 3 char extension
  46.   char size[8];                         // 8 digit # bytes in file
  47.   char sp2;                             // space for formatting
  48.   char date[6];                         // 6 char YYMMDD date
  49.   char sp3;                             // space for formatting
  50.   char time[4];                         // 4 char military time
  51.   char dummy[3];                        // 3 available spaces if desired
  52.   char term;                            // NULL terminator
  53. } MYDIRSTRCT;
  54.  
  55. int fnamecmp(MYDIRSTRCT *buf1, MYDIRSTRCT *buf2);
  56.  
  57.         /* Define general variables */
  58. ULONG ii;
  59. ULONG total_bytes=0;                    // Total bytes in all files
  60. USHORT api_err;                         // Generic error returns
  61. PSZ FileSpec;                           // Pointer to a file spec
  62. HDIR hdir=HDIR_CREATE;                  // Create a new search handle
  63. USHORT usSearchCount = 25*MAXFILES;     // Setup to have error if too many files
  64. FILEFINDBUF filefindbuf[MAXFILES];      // Receives file info
  65. FSALLOCATE fsallocate;                  // Receives disk allocation info
  66. char *allfiles="*.*";                   // Default search spec (all files)
  67. char *nulldummy="";                     // Dummy NULL string
  68. char BuildTemp[60];                     // An array to build a template into
  69.  
  70. MYDIRSTRCT build_array, *dest_ptr;      // Structs for building/moving dir info
  71. FILEFINDBUF *src_ptr;                   // Ptr to record being interpreted
  72. time_t time_abs;
  73. struct tm time_local;
  74. union FTIME_CMP {                       // Time we are looking since (6AM)
  75.   FTIME ft;                             // To access in bit mode
  76.   USHORT us;                            // To access as an unsigned short
  77. } target_time;
  78. union FDATE_CMP {                       // Date we are looking since (current)
  79.   FDATE fd;                             // To access in bit mode
  80.   USHORT us;                            // To access as an unsigned short
  81. } target_date;
  82. long daysback = 0;                      // # days back to look
  83. int numkept = 0;                        // # entries not discarded
  84.  
  85. main(argc, argv)
  86. int argc;
  87. char *argv[];
  88. {
  89.   if (argc > 3) errex("\n\nProper usage: 'new [/nn] [template]'\n\n");
  90.   if (argc > 1)
  91.   {
  92.     if ( (*argv[1] == '-') || (*argv[1] == '/') )   // If there is a switch
  93.     {
  94.       daysback = atol( argv[1]+1 ) - 1; // Interpret as # days to look back
  95.       argv++;                           // Remove the 1st argument
  96.       argc--;
  97.     }
  98.   }
  99.   if (argc > 1)
  100.   {
  101.     char lastchar;
  102.  
  103.     FileSpec=argv[1];
  104.     if (*FileSpec == '.')               // Starts with '.'
  105.     {
  106.       if ( (*(FileSpec+1) != '.') && (*(FileSpec+1) != '\0') )
  107.       {                                 // Is not one of the 'psuedo dirs'
  108.         strcpy(&BuildTemp[1], argv[1]); // Prepend with '*'
  109.         BuildTemp[0] = '*';
  110.       }
  111.       else strcpy(&BuildTemp[0], argv[1]);
  112.     }
  113.     else strcpy(&BuildTemp[0], argv[1]);// One way or another, string in BuildTemp
  114.     FileSpec = BuildTemp;
  115.     if ( (lastchar=BuildTemp[strlen(BuildTemp)-1]) == '.' )
  116.       strcat(BuildTemp, "\\*.*");
  117.     else if ( (lastchar == '\\') || (lastchar == ':') )
  118.       strcat(BuildTemp, "*.*");
  119.   }
  120.   else FileSpec=allfiles;
  121.  
  122.   api_err = DosFindFirst(FileSpec, &hdir, ALLFILETYPES, filefindbuf,
  123.                                     sizeof(filefindbuf), &usSearchCount, 0L);
  124.   if ( (api_err == ERROR_FILE_NOT_FOUND) || (api_err == ERROR_NO_MORE_FILES) )
  125.   {
  126.     printf("Aint got no %s\n", FileSpec);
  127.     exit(0);
  128.   }
  129.   ERRCHK(api_err);                      // Check for errors & print
  130.  
  131.   api_err = time(&time_abs);
  132.   time_abs -= 60L*60L*24L*daysback;     // Count backwards n days
  133.   time_local = *localtime(&time_abs);
  134.   if (time_local.tm_hour < 6) {         // If now is between midnight and 6AM
  135.     time_abs -= 60L*60L*24L;            // Read from 6AM yesterday
  136.     time_local = *localtime(&time_abs);
  137.   }
  138.   target_date.fd.year  = time_local.tm_year; // Build the 1st date & time we should
  139.   target_date.fd.month = time_local.tm_mon+1;// keep file info for
  140.   target_date.fd.day   = time_local.tm_mday;
  141.   target_date.fd.year -= 80;            // Get to same base as DosFindFirst
  142.   target_date.fd.year %= 100;           // Take mod 100 of year
  143.  
  144.   target_time.ft.hours   = 6;           // Always read from 6 AM
  145.   target_time.ft.minutes =
  146.   target_time.ft.twosecs = 0;
  147.  
  148.                                         // Get disk usage of current disk
  149.   api_err=DosQFSInfo(0, FSIL_ALLOC, (PBYTE)&fsallocate, sizeof(fsallocate));
  150.   ERRCHK(api_err);                      // Check for errors & print
  151.  
  152.   printf("Directory of files written since %d/%d/%d at 6 AM.\n",
  153.             target_date.fd.month, target_date.fd.day, target_date.fd.year+1980);
  154.   ii = sizeof(filefindbuf) - sizeof(build_array);
  155.   dest_ptr = (MYDIRSTRCT *)             // Init the end pointer
  156.      ( (char *)filefindbuf + ii );
  157.   src_ptr = (FILEFINDBUF *)filefindbuf; // Init the start pointer
  158.  
  159.   build_array.term = '\0';              // Only needs done once (NULL term struct)
  160.   build_array.dummy[0] =
  161.   build_array.dummy[1] =
  162.   build_array.dummy[2] = ' ';           // Blank out the dummies
  163.   for (ii=0; ii<usSearchCount; ii++,    // Point to next file in source buffer
  164.               src_ptr = (FILEFINDBUF *)&src_ptr->achName[src_ptr->cchName+1])
  165.   {
  166.     char *fnameptr, *extptr;
  167.     union FDATE_CMP *dispdate;
  168.     union FTIME_CMP *disptime;
  169.  
  170.     fnameptr = strtok(src_ptr->achName, "."); // NULL term fname, remove '.'
  171.     extptr = strtok(NULL, ".");         // Get extension string
  172.     fnameptr = src_ptr->achName;        // Guarantee filename for '.' & '..'
  173.     if (extptr == NULL)
  174.       extptr=nulldummy;                 // Guarantee NULL ext available
  175.  
  176.     dispdate = (union FDATE_CMP *)&src_ptr->fdateLastWrite;
  177.     disptime = (union FTIME_CMP *)&src_ptr->ftimeLastWrite;
  178.  
  179.     if (dispdate->us < target_date.us)
  180.       continue;                         // File is too old to be of interest
  181.  
  182.     if ( (dispdate->us == target_date.us) &&
  183.           (disptime->us < target_time.us) )
  184.       continue;                         // File is too old to be of interest
  185.  
  186.     numkept++;                          // It's a keeper
  187.  
  188.     sprintf( (char *)&build_array, "%c%c%c%c %-8.8s %-3.3s%8lu ",
  189.               ((src_ptr->attrFile & FILE_SYSTEM)    ? 'S' : '_'),
  190.               ((src_ptr->attrFile & FILE_HIDDEN)    ? 'H' : '_'),
  191.               ((src_ptr->attrFile & FILE_READONLY)  ? 'R' : '_'),
  192.               ((src_ptr->attrFile & FILE_DIRECTORY) ? '/' : '_'),
  193.               fnameptr, extptr, src_ptr->cbFile);
  194.  
  195.     sprintf( (char *)build_array.date, "%02d%02d%02d %02d%02d",
  196.               (dispdate->fd.year+80)%100, dispdate->fd.month, dispdate->fd.day,
  197.               disptime->ft.hours, disptime->ft.minutes);
  198.  
  199.     build_array.dummy[0] = ' ';         // Get rid of NULL from sprintf above
  200.  
  201.     total_bytes += src_ptr->cbFile;     // Add # bytes in this file to total
  202.  
  203.     if (extptr == nulldummy)            // Did we need to dummy out extension?
  204.       build_array.decpt = ' ';          // No extension, no dot
  205.     else build_array.decpt = '.';       // Put a dot before extension
  206.  
  207.     *dest_ptr-- = build_array;          // Copy structure to dest & next dest
  208.   }
  209.   dest_ptr++;                           // Point to last item converted
  210.  
  211.   if (numkept==0)                       // No recent files
  212.   {
  213.     printf("No files found. %lu bytes free (on current disk)\n",
  214.             fsallocate.cSectorUnit*fsallocate.cUnitAvail*fsallocate.cbSector);
  215.     exit(0);
  216.   }
  217.                                         // Sort based on flags and fname/ext
  218.   qsort((void *)dest_ptr, numkept, sizeof(MYDIRSTRCT), fnamecmp );
  219.  
  220.   for (ii=0; ii<numkept; ii++)
  221.   {
  222.     fputs((char *)dest_ptr++, stdout);
  223. //    if (ii&1) fputc('\n', stdout);
  224.   }
  225.   if (ii&1) fputc('\n', stdout);
  226.  
  227.   printf("Total of %d files using %lu bytes. %lu bytes free (on current disk)\n",
  228.     numkept, total_bytes,
  229.             fsallocate.cSectorUnit*fsallocate.cUnitAvail*fsallocate.cbSector);
  230. }
  231.  
  232. int fnamecmp(MYDIRSTRCT *buf1, MYDIRSTRCT *buf2)
  233. {
  234.   long datecmp;
  235.  
  236.   datecmp = strncmp((void *)buf2->date, (void *)buf1->date, 11);
  237.   if (datecmp != 0) return(datecmp);
  238.   return( strncmp((void *)buf1, (void *)buf2, 15) );
  239. }
  240.  
  241.