Commit | Line | Data |
---|---|---|
f6227721 | 1 | #ifndef lint |
e4ea4742 | 2 | static char sccsid[] = "@(#)touch.c 4.5 (Berkeley) %G%"; |
f6227721 | 3 | #endif |
0219f737 KM |
4 | |
5 | /* | |
6 | * attempt to set the modify date of a file to the current date. | |
7 | * if the file exists, read and write its first character. | |
8 | * if the file doesn't exist, create it, unless -c option prevents it. | |
9 | * if the file is read-only, -f forces chmod'ing and touch'ing. | |
10 | */ | |
11 | ||
7add65c5 | 12 | #include <stdio.h> |
0219f737 | 13 | #include <sys/types.h> |
c41eeace | 14 | #include <sys/file.h> |
0219f737 KM |
15 | #include <sys/stat.h> |
16 | ||
17 | int dontcreate; /* set if -c option */ | |
18 | int force; /* set if -f option */ | |
7add65c5 | 19 | |
0219f737 | 20 | char *whoami = "touch"; |
7add65c5 BJ |
21 | |
22 | main(argc,argv) | |
0219f737 KM |
23 | int argc; |
24 | char **argv; | |
7add65c5 | 25 | { |
0219f737 | 26 | char *argp; |
7add65c5 | 27 | |
0219f737 KM |
28 | dontcreate = 0; |
29 | force = 0; | |
30 | for (argv++; **argv == '-'; argv++) { | |
31 | for (argp = &(*argv)[1]; *argp; argp++) { | |
32 | switch (*argp) { | |
33 | case 'c': | |
34 | dontcreate = 1; | |
35 | break; | |
36 | case 'f': | |
37 | force = 1; | |
38 | break; | |
39 | default: | |
40 | fprintf(stderr, "%s: bad option -%c\n", | |
41 | whoami, *argp); | |
42 | exit(1); | |
43 | } | |
44 | } | |
45 | } | |
46 | for (/*void*/; *argv; argv++) { | |
47 | touch(*argv); | |
48 | } | |
7add65c5 BJ |
49 | } |
50 | ||
0219f737 KM |
51 | touch(filename) |
52 | char *filename; | |
7add65c5 | 53 | { |
0219f737 | 54 | struct stat statbuffer; |
7add65c5 | 55 | |
0219f737 KM |
56 | if (stat(filename,&statbuffer) == -1) { |
57 | if (!dontcreate) { | |
58 | readwrite(filename,0); | |
59 | } else { | |
60 | fprintf(stderr, "%s: %s: does not exist\n", | |
61 | whoami, filename); | |
62 | } | |
7add65c5 | 63 | return; |
0219f737 KM |
64 | } |
65 | if ((statbuffer.st_mode & S_IFMT) != S_IFREG) { | |
66 | fprintf(stderr, "%s: %s: can only touch regular files\n", | |
67 | whoami, filename); | |
68 | return; | |
69 | } | |
c41eeace | 70 | if (!access(filename,R_OK | W_OK)) { |
0219f737 KM |
71 | readwrite(filename,statbuffer.st_size); |
72 | return; | |
73 | } | |
74 | if (force) { | |
75 | if (chmod(filename,0666)) { | |
76 | fprintf(stderr, "%s: %s: couldn't chmod: ", | |
77 | whoami, filename); | |
78 | perror(""); | |
79 | return; | |
7add65c5 | 80 | } |
0219f737 KM |
81 | readwrite(filename,statbuffer.st_size); |
82 | if (chmod(filename,statbuffer.st_mode)) { | |
83 | fprintf(stderr, "%s: %s: couldn't chmod back: ", | |
84 | whoami, filename); | |
85 | perror(""); | |
86 | return; | |
87 | } | |
88 | } else { | |
89 | fprintf(stderr, "%s: %s: cannot touch\n", whoami, filename); | |
90 | } | |
91 | } | |
7add65c5 | 92 | |
0219f737 KM |
93 | readwrite(filename,size) |
94 | char *filename; | |
95 | int size; | |
96 | { | |
97 | int filedescriptor; | |
98 | char first; | |
7add65c5 | 99 | |
0219f737 KM |
100 | if (size) { |
101 | filedescriptor = open(filename,2); | |
102 | if (filedescriptor == -1) { | |
103 | error: | |
104 | fprintf(stderr, "%s: %s: ", whoami, filename); | |
105 | perror(""); | |
106 | return; | |
107 | } | |
108 | if (read(filedescriptor, &first, 1) != 1) { | |
109 | goto error; | |
110 | } | |
111 | if (lseek(filedescriptor,0l,0) == -1) { | |
112 | goto error; | |
113 | } | |
114 | if (write(filedescriptor, &first, 1) != 1) { | |
115 | goto error; | |
116 | } | |
117 | } else { | |
118 | filedescriptor = creat(filename,0666); | |
119 | if (filedescriptor == -1) { | |
120 | goto error; | |
121 | } | |
7add65c5 | 122 | } |
0219f737 KM |
123 | if (close(filedescriptor) == -1) { |
124 | goto error; | |
7add65c5 | 125 | } |
7add65c5 | 126 | } |