summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/stub.c
blob: dd8f9a5cd1823fb56bf821079fe89c7e3c5bc9de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
**  STUB.C - Utility to truncate files
**
**      STUB is used with MAKE utilities which lack the ability to timestamp
**      library object modules to reduce disk space requirements. After
**      compiling and building your library or executable, run "STUB *.OBJ"
**      to truncate all object files to zero length. The files' time and
**      date stamps will remain unchanged. By doing this, your make utility
**      will still know which modules are out of date even though the files
**      themselves have all been truncated to zero length. STUB also supports
**      the standard response file format so you can use your linker response
**      files to direct the files to be truncated.
**
**  public domain by Bob Stout
**
**  Notes: To expand command line arguments with wildcards,
**         TC/TC++/BC++  - Link in WILDARGS.OBJ.
**         MSC/QC        - Link in SETARGV.OBJ.
**         ZTC/C++       - Link in _MAINx.OBJ, where 'x' is the memory model.
**         Watcom C/C++  - Compile & link with WILDARGV.C
**
**         Allows file list(s) using standard "@file_list_name" convention.
*/

#include <stdio.h>
#include <string.h>
#include <dos.h>
#include <io.h>
#include <fcntl.h>

#define LAST_CHAR(s) (s)[strlen(s)-1]

int fd;

void truncate(char *);

int main(int argc, char **argv)
{
      int i;

      if (2 > argc)
      {
            puts("Usage: STUB filespec [...filespec]");
            puts("where: filespec = fully-specified file name, or");
            puts("       filespec = wildcard-specified file name, or");
            puts("       filespec = response file name, e.g. \"@FILE.LST\"");
            return 1;
      }

      for (i = 1; i < argc; ++i)    /* Scan for simple file specs       */
      {
            if ('@' == *argv[i])
                  continue;
            else  truncate(argv[i]);
      }
      for (i = 1; i < argc; ++i)    /* Scan for response file specs     */
      {
            if ('@' == *argv[i])
            {
                  FILE *fp;
                  char buf[256], *ptr = &argv[i][1];

                  if (NULL == (fp = fopen(ptr, "r")))
                  {
                        printf("\aSTUB: Error opening %s\n", ptr);
                        return -1;
                  }
                  while (NULL != fgets(buf, 255, fp))
                  {
                        LAST_CHAR(buf) = '\0';  /* Strip '\n'           */
                        truncate(buf);
                  }
                  fclose(fp);
            }
      }
      return 0;
}

/*
**  The actual truncation function
*/

#ifdef __ZTC__
 #define GETFTIME dos_getftime
 #define SETFTIME dos_setftime
#else
 #define GETFTIME _dos_getftime
 #define SETFTIME _dos_setftime
#endif

void truncate(char *fname)
{
#ifdef __TURBOC__
            struct ftime Ftime;
#else
            unsigned short date, time;
#endif

            fd = open(fname, O_WRONLY);

#ifdef __TURBOC__                         /* Save the time/date         */
            getftime(fd, &Ftime);
#else
            GETFTIME(fd, &date, &time);
#endif

            chsize(fd, 0L);               /* Truncate the file          */

#ifdef __TURBOC__                         /* Restore the time/date      */
            setftime(fd, &Ftime);
#else
            SETFTIME(fd, date, time);
#endif
            close(fd);
}