Research V7 development
authorKen Thompson <ken@research.uucp>
Wed, 10 Jan 1979 20:08:53 +0000 (15:08 -0500)
committerKen Thompson <ken@research.uucp>
Wed, 10 Jan 1979 20:08:53 +0000 (15:08 -0500)
Work on file usr/src/cmd/touch.c

Co-Authored-By: Dennis Ritchie <dmr@research.uucp>
Synthesized-from: v7

usr/src/cmd/touch.c [new file with mode: 0644]

diff --git a/usr/src/cmd/touch.c b/usr/src/cmd/touch.c
new file mode 100644 (file)
index 0000000..33bcc50
--- /dev/null
@@ -0,0 +1,70 @@
+#include <stdio.h>
+
+
+main(argc,argv)
+int argc;
+char *argv[];
+{
+int i;
+static int force = 1;
+
+for(i = 1 ; i < argc ; ++i)
+       if( strcmp(argv[i], "-c") )
+               touch(force, argv[i]);
+       else
+               force = 0;
+}
+
+
+
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+
+touch(force, name)
+int force;
+char *name;
+{
+struct stat stbuff;
+char junk[1];
+int fd;
+
+if( stat(name,&stbuff) < 0)
+       if(force)
+               goto create;
+       else
+               {
+               fprintf(stderr, "touch: file %s does not exist.\n", name);
+               return;
+               }
+
+if(stbuff.st_size == 0)
+       goto create;
+
+if( (fd = open(name, 2)) < 0)
+       goto bad;
+
+if( read(fd, junk, 1) < 1)
+       {
+       close(fd);
+       goto bad;
+       }
+lseek(fd, 0L, 0);
+if( write(fd, junk, 1) < 1 )
+       {
+       close(fd);
+       goto bad;
+       }
+close(fd);
+return;
+
+bad:
+       fprintf(stderr, "Cannot touch %s\n", name);
+       return;
+
+create:
+       if( (fd = creat(name, 0666)) < 0)
+               goto bad;
+       close(fd);
+}