home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / STUB.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  4KB  |  123 lines

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