1 static char *sccsid = "@(#)touch.c 4.1 (Berkeley) 10/01/80"; 2 #include <stdio.h> 3 4 5 main(argc,argv) 6 int argc; 7 char *argv[]; 8 { 9 int i; 10 static int force = 1; 11 12 for(i = 1 ; i < argc ; ++i) 13 if( strcmp(argv[i], "-c") ) 14 touch(force, argv[i]); 15 else 16 force = 0; 17 } 18 19 20 21 22 #include <sys/types.h> 23 #include <sys/stat.h> 24 25 26 touch(force, name) 27 int force; 28 char *name; 29 { 30 struct stat stbuff; 31 char junk[1]; 32 int fd; 33 34 if( stat(name,&stbuff) < 0) 35 if(force) 36 goto create; 37 else 38 { 39 fprintf(stderr, "touch: file %s does not exist.\n", name); 40 return; 41 } 42 43 if(stbuff.st_size == 0) 44 goto create; 45 46 if( (fd = open(name, 2)) < 0) 47 goto bad; 48 49 if( read(fd, junk, 1) < 1) 50 { 51 close(fd); 52 goto bad; 53 } 54 lseek(fd, 0L, 0); 55 if( write(fd, junk, 1) < 1 ) 56 { 57 close(fd); 58 goto bad; 59 } 60 close(fd); 61 return; 62 63 bad: 64 fprintf(stderr, "Cannot touch %s\n", name); 65 return; 66 67 create: 68 if( (fd = creat(name, 0666)) < 0) 69 goto bad; 70 close(fd); 71 } 72