merge system forced touch with user touch (peter)
authorKirk McKusick <mckusick@ucbvax.Berkeley.EDU>
Thu, 10 Jun 1982 07:57:17 +0000 (23:57 -0800)
committerKirk McKusick <mckusick@ucbvax.Berkeley.EDU>
Thu, 10 Jun 1982 07:57:17 +0000 (23:57 -0800)
SCCS-vsn: usr.bin/touch/touch.c 4.2

usr/src/usr.bin/touch/touch.c

index 5a56a6e..26d5bfd 100644 (file)
-static char *sccsid = "@(#)touch.c     4.1 (Berkeley) %G%";
+#ifndef LINT
+static char *sccsid = "@(#)touch.c     4.2 (Berkeley) 82/06/09";
+#endif not LINT
+
+/*
+ *     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.
+ */
+
 #include <stdio.h>
 #include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+int    dontcreate;     /* set if -c option */
+int    force;          /* set if -f option */
 
 
+char *whoami = "touch";
 
 main(argc,argv)
 
 main(argc,argv)
-int argc;
-char *argv[];
+       int     argc;
+       char    **argv;
 {
 {
-int i;
-static int force = 1;
+       char    *argp;
 
 
-for(i = 1 ; i < argc ; ++i)
-       if( strcmp(argv[i], "-c") )
-               touch(force, argv[i]);
-       else
-               force = 0;
+       dontcreate = 0;
+       force = 0;
+       for (argv++; **argv == '-'; argv++) {
+               for (argp = &(*argv)[1]; *argp; argp++) {
+                       switch (*argp) {
+                       case 'c':
+                               dontcreate = 1;
+                               break;
+                       case 'f':
+                               force = 1;
+                               break;
+                       default:
+                               fprintf(stderr, "%s: bad option -%c\n",
+                                       whoami, *argp);
+                               exit(1);
+                       }
+               }
+       }
+       for (/*void*/; *argv; argv++) {
+               touch(*argv);
+       }
 }
 
 }
 
-
-
-
-#include <sys/types.h>
-#include <sys/stat.h>
-
-
-touch(force, name)
-int force;
-char *name;
+touch(filename)
+       char    *filename;
 {
 {
-struct stat stbuff;
-char junk[1];
-int fd;
+       struct stat     statbuffer;
 
 
-if( stat(name,&stbuff) < 0)
-       if(force)
-               goto create;
-       else
-               {
-               fprintf(stderr, "touch: file %s does not exist.\n", name);
+       if (stat(filename,&statbuffer) == -1) {
+               if (!dontcreate) {
+                       readwrite(filename,0);
+               } else {
+                       fprintf(stderr, "%s: %s: does not exist\n",
+                               whoami, filename);
+               }
                return;
                return;
+       }
+       if ((statbuffer.st_mode & S_IFMT) != S_IFREG) {
+               fprintf(stderr, "%s: %s: can only touch regular files\n",
+                       whoami, filename);
+               return;
+       }
+       if (!access(filename,4|2)) {
+               readwrite(filename,statbuffer.st_size);
+               return;
+       }
+       if (force) {
+               if (chmod(filename,0666)) {
+                       fprintf(stderr, "%s: %s: couldn't chmod: ",
+                               whoami, filename);
+                       perror("");
+                       return;
                }
                }
+               readwrite(filename,statbuffer.st_size);
+               if (chmod(filename,statbuffer.st_mode)) {
+                       fprintf(stderr, "%s: %s: couldn't chmod back: ",
+                               whoami, filename);
+                       perror("");
+                       return;
+               }
+       } else {
+               fprintf(stderr, "%s: %s: cannot touch\n", whoami, filename);
+       }
+}
 
 
-if(stbuff.st_size == 0)
-       goto create;
-
-if( (fd = open(name, 2)) < 0)
-       goto bad;
+readwrite(filename,size)
+       char    *filename;
+       int     size;
+{
+       int     filedescriptor;
+       char    first;
 
 
-if( read(fd, junk, 1) < 1)
-       {
-       close(fd);
-       goto bad;
+       if (size) {
+               filedescriptor = open(filename,2);
+               if (filedescriptor == -1) {
+error:
+                       fprintf(stderr, "%s: %s: ", whoami, filename);
+                       perror("");
+                       return;
+               }
+               if (read(filedescriptor, &first, 1) != 1) {
+                       goto error;
+               }
+               if (lseek(filedescriptor,0l,0) == -1) {
+                       goto error;
+               }
+               if (write(filedescriptor, &first, 1) != 1) {
+                       goto error;
+               }
+       } else {
+               filedescriptor = creat(filename,0666);
+               if (filedescriptor == -1) {
+                       goto error;
+               }
        }
        }
-lseek(fd, 0L, 0);
-if( write(fd, junk, 1) < 1 )
-       {
-       close(fd);
-       goto bad;
+       if (close(filedescriptor) == -1) {
+               goto error;
        }
        }
-close(fd);
-return;
-
-bad:
-       fprintf(stderr, "Cannot touch %s\n", name);
-       return;
-
-create:
-       if( (fd = creat(name, 0666)) < 0)
-               goto bad;
-       close(fd);
 }
 }