static char *sccsid
= "@(#)touch.c 4.2 (Berkeley) 82/06/09";
* 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 */
for (argv
++; **argv
== '-'; argv
++) {
for (argp
= &(*argv
)[1]; *argp
; argp
++) {
fprintf(stderr
, "%s: bad option -%c\n",
for (/*void*/; *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
,4|2)) {
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) {