static char sccsid
[] = "@(#)touch.c 4.5 (Berkeley) %G%";
* attempt to set the modify date of a file to the current date.
* if the file exists, read and write its first character.
* if the file doesn't exist, create it, unless -c option prevents it.
* if the file is read-only, -f forces chmod'ing and touch'ing.
int dontcreate
; /* set if -c option */
int force
; /* set if -f option */
while ((ch
= getopt(argc
, argv
, "cf")) != EOF
)
fprintf(stderr
, "usage: %s [-cf] file ...\n", whoami
);
for (argv
+= optind
; *argv
; ++argv
)
if (stat(filename
,&statbuffer
) == -1) {
fprintf(stderr
, "%s: %s: does not exist\n",
if ((statbuffer
.st_mode
& S_IFMT
) != S_IFREG
) {
fprintf(stderr
, "%s: %s: can only touch regular files\n",
if (!access(filename
,R_OK
| W_OK
)) {
readwrite(filename
,statbuffer
.st_size
);
if (chmod(filename
,0666)) {
fprintf(stderr
, "%s: %s: couldn't chmod: ",
readwrite(filename
,statbuffer
.st_size
);
if (chmod(filename
,statbuffer
.st_mode
)) {
fprintf(stderr
, "%s: %s: couldn't chmod back: ",
fprintf(stderr
, "%s: %s: cannot touch\n", whoami
, filename
);
filedescriptor
= open(filename
,2);
if (filedescriptor
== -1) {
fprintf(stderr
, "%s: %s: ", whoami
, filename
);
if (read(filedescriptor
, &first
, 1) != 1) {
if (lseek(filedescriptor
,0l,0) == -1) {
if (write(filedescriptor
, &first
, 1) != 1) {
filedescriptor
= creat(filename
,0666);
if (filedescriptor
== -1) {
if (close(filedescriptor
) == -1) {