f43685fa08cf2e82fc72d73a734aa141bc610154
* Copyright (c) 1988 Regents of the University of California.
* %sccs.include.redist.c%
"@(#) Copyright (c) 1988 Regents of the University of California.\n\
static char sccsid
[] = "@(#)touch.c 5.1 (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.
static int dontcreate
; /* set if -c option */
static int force
; /* set if -f option */
void err
__P((const char *, ...));
int readwrite
__P((char *, off_t
));
__dead
void usage
__P((void));
dontcreate
= force
= retval
= 0;
while ((ch
= getopt(argc
, argv
, "cf")) != EOF
)
if (stat(filename
, &sb
) == -1) {
return (readwrite(filename
, (off_t
)0));
err("%s: %s", filename
, strerror(ENOENT
));
if ((sb
.st_mode
& S_IFMT
) != S_IFREG
) {
err("%s: %s", filename
, strerror(EFTYPE
));
if (!access(filename
, R_OK
| W_OK
))
return (readwrite(filename
, sb
.st_size
));
if (chmod(filename
, DEFFILEMODE
)) {
err("%s: add permissions: %s",
filename
, strerror(errno
));
retval
= readwrite(filename
, sb
.st_size
);
if (chmod(filename
, sb
.st_mode
)) {
err("%s: restore permissions: %s",
filename
, strerror(errno
));
err("%s: cannot touch\n", filename
);
readwrite(filename
, size
)
fd
= open(filename
, O_RDWR
, 0);
if (read(fd
, &first
, 1) != 1)
if (lseek(fd
, (off_t
)0, SEEK_SET
) == -1)
if (write(fd
, &first
, 1) != 1)
fd
= creat(filename
, DEFFILEMODE
);
error
: err("%s: %s", filename
, strerror(errno
));
fprintf(stderr
, "usage: touch [-cf] file ...\n");
err(const char *fmt
, ...)
(void)fprintf(stderr
, "touch: ");
(void)vfprintf(stderr
, fmt
, ap
);
(void)fprintf(stderr
, "\n");