Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mini touch implementation for busybox |
| 3 | * |
| 4 | * Copyright (C) 1998 by Erik Andersen <andersee@debian.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | * |
| 20 | */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 21 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 22 | #include "internal.h" |
| 23 | #include <stdio.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <utime.h> |
| 28 | #include <errno.h> |
| 29 | |
| 30 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 31 | static const char touch_usage[] = "touch [-c] file [file ...]\n\n" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 32 | "\tUpdate the last-modified date on the given file[s].\n"; |
| 33 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 34 | |
| 35 | |
| 36 | extern int |
| 37 | touch_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 38 | { |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 39 | int fd; |
| 40 | int create=TRUE; |
| 41 | |
| 42 | if (argc < 2) { |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 43 | usage( touch_usage); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 44 | } |
| 45 | argc--; |
| 46 | argv++; |
| 47 | |
| 48 | /* Parse options */ |
| 49 | while (**argv == '-') { |
| 50 | while (*++(*argv)) switch (**argv) { |
| 51 | case 'c': |
| 52 | create = FALSE; |
| 53 | break; |
| 54 | default: |
| 55 | fprintf(stderr, "Unknown option: %c\n", **argv); |
| 56 | exit( FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 57 | } |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 58 | argc--; |
| 59 | argv++; |
| 60 | } |
| 61 | |
| 62 | fd = open (*argv, (create==FALSE)? O_RDWR : O_RDWR | O_CREAT, 0644); |
| 63 | if (fd < 0 ) { |
| 64 | if (create==FALSE && errno == ENOENT) |
| 65 | exit( TRUE); |
| 66 | else { |
| 67 | perror("touch"); |
| 68 | exit( FALSE); |
| 69 | } |
| 70 | } |
| 71 | close( fd); |
| 72 | if (utime (*argv, NULL)) { |
| 73 | perror("touch"); |
| 74 | exit( FALSE); |
| 75 | } |
| 76 | else |
| 77 | exit( TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 78 | } |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |