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

  1. /*
  2. **  STUB.C - Utility to truncate files
  3. **
  4. **      STUB is used with MAKE utilities which lack the ability to timestamp
  5. **      library object modules to reduce disk space requirements. After
  6. **      compiling and building your library or executable, run "STUB *.OBJ"
  7. **      to truncate all object files to zero length. The files' time and
  8. **      date stamps will remain unchanged. By doing this, your make utility
  9. **      will still know which modules are out of date even though the files
  10. **      themselves have all been truncated to zero length. STUB also supports
  11. **      the standard response file format so you can use your linker response
  12. **      files to direct the files to be truncated.
  13. **
  14. **  public domain by Bob Stout
  15. **
  16. **  Notes: To expand command line arguments with wildcards,
  17. **         TC/TC++/BC++  - Link in WILDARGS.OBJ.
  18. **         MSC/QC        - Link in SETARGV.OBJ.
  19. **         ZTC/C++       - Link in _MAINx.OBJ, where 'x' is the memory model.
  20. **         Watcom C/C++  - Compile & link with WILDARGV.C
  21. **
  22. **         Allows file list(s) using standard "@file_list_name" convention.
  23. */
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <dos.h>
  28. #include <io.h>
  29. #include <fcntl.h>
  30.  
  31. #define LAST_CHAR(s) (s)[strlen(s)-1]
  32.  
  33. int fd;
  34.  
  35. void truncate(char *);
  36.  
  37. int main(int argc, char **argv)
  38. {
  39.       int i;
  40.  
  41.       if (2 > argc)
  42.       {
  43.             puts("Usage: STUB filespec [...filespec]");
  44.             puts("where: filespec = fully-specified file name, or");
  45.             puts("       filespec = wildcard-specified file name, or");
  46.             puts("       filespec = response file name, e.g. \"@FILE.LST\"");
  47.             return 1;
  48.       }
  49.  
  50.       for (i = 1; i < argc; ++i)    /* Scan for simple file specs       */
  51.       {
  52.             if ('@' == *argv[i])
  53.                   continue;
  54.             else  truncate(argv[i]);
  55.       }
  56.       for (i = 1; i < argc; ++i)    /* Scan for response file specs     */
  57.       {
  58.             if ('@' == *argv[i])
  59.             {
  60.                   FILE *fp;
  61.                   char buf[256], *ptr = &argv[i][1];
  62.  
  63.                   if (NULL == (fp = fopen(ptr, "r")))
  64.                   {
  65.                         printf("\aSTUB: Error opening %s\n", ptr);
  66.                         return -1;
  67.                   }
  68.                   while (NULL != fgets(buf, 255, fp))
  69.                   {
  70.                         LAST_CHAR(buf) = '\0';  /* Strip '\n'           */
  71.                         truncate(buf);
  72.                   }
  73.                   fclose(fp);
  74.             }
  75.       }
  76.       return 0;
  77. }
  78.  
  79. /*
  80. **  The actual truncation function
  81. */
  82.  
  83. #ifdef __ZTC__
  84.  #define GETFTIME dos_getftime
  85.  #define SETFTIME dos_setftime
  86. #else
  87.  #define GETFTIME _dos_getftime
  88.  #define SETFTIME _dos_setftime
  89. #endif
  90.  
  91. void truncate(char *fname)
  92. {
  93. #ifdef __TURBOC__
  94.             struct ftime Ftime;
  95. #else
  96.             unsigned short date, time;
  97. #endif
  98.  
  99.             fd = open(fname, O_WRONLY);
  100.  
  101. #ifdef __TURBOC__                         /* Save the time/date         */
  102.             getftime(fd, &Ftime);
  103. #else
  104.             GETFTIME(fd, &date, &time);
  105. #endif
  106.  
  107.             chsize(fd, 0L);               /* Truncate the file          */
  108.  
  109. #ifdef __TURBOC__                         /* Restore the time/date      */
  110.             setftime(fd, &Ftime);
  111. #else
  112.             SETFTIME(fd, date, time);
  113. #endif
  114.             close(fd);
  115. }
  116.