|
|
1.1 root 1: /*
2: * Touch -- update the last modified
3: * time of a file, as for make.
4: */
5:
6: #include <stdio.h>
7:
8: #define DEFMODE 0666
9:
10: int cflag;
11:
12: char nocreate[] = "touch: cannot create %s\n";
13:
14: main(argc, argv)
15: char *argv[];
16: {
17: register int i;
18: register int estat = 0;
19:
20: if (argc>1 && *argv[1]=='-') {
21: if (argv[1][1]=='c' && argv[1][2]=='\0')
22: cflag = 1;
23: else
24: usage();
25: argv++;
26: argc--;
27: }
28: if (argc < 2)
29: usage();
30: for (i=1; i<argc; i++)
31: estat |= touch(argv[i]);
32: exit(estat);
33: }
34:
35: /*
36: * Touch each file in the file-list
37: * (The `cflag' indicates that
38: * non-existent files should not be
39: * created.)
40: */
41: touch(f)
42: char *f;
43: {
44: char c;
45: register int fd;
46: register int ret = 0;
47:
48: if ((fd = open(f, 2)) < 0) {
49: if (!cflag && (fd = creat(f, DEFMODE))<0)
50: fprintf(stderr, nocreate, f);
51: if (fd >= 0)
52: close(fd);
53: else
54: ret++;
55: return (ret);
56: }
57: if (read(fd, &c, sizeof c) == sizeof c) {
58: lseek(fd, 0L, 0);
59: if (write(fd, &c, sizeof c) != sizeof c) {
60: fprintf(stderr, "touch: write error on %s\n", f);
61: ret++;
62: }
63: close(fd);
64: } else { /* Zero-byte file */
65: close(fd);
66: if (cflag)
67: ret++;
68: else if ((fd = creat(f, DEFMODE)) < 0)
69: fprintf(stderr, nocreate, f);
70: else
71: close(fd);
72: }
73: return (ret);
74: }
75:
76: usage()
77: {
78: fprintf(stderr, "Usage: touch [-c] file ...\n");
79: exit(1);
80: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.