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