Continue processing files if we are asked to touch, but not create, a file
[oweals/busybox.git] / coreutils / touch.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini touch implementation for busybox
4  *
5  * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
6  * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <fcntl.h>
27 #include <utime.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include "busybox.h"
32
33 extern int touch_main(int argc, char **argv)
34 {
35         int fd;
36         int create = TRUE;
37
38         /* Parse options */
39         while (--argc > 0 && **(++argv) == '-') {
40                 while (*(++(*argv))) {
41                         switch (**argv) {
42                         case 'c':
43                                 create = FALSE;
44                                 break;
45                         default:
46                                 show_usage();
47                         }
48                 }
49         }
50
51         if (argc < 1) {
52                 show_usage();
53         }
54
55         while (argc > 0) {
56                 fd = open(*argv, (create == FALSE) ? O_RDWR : O_RDWR | O_CREAT,
57                                 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
58                 if (fd < 0) {
59                         if (create == FALSE && errno == ENOENT) {
60                                 argc--;
61                                 argv++;
62                                 continue;
63                         } else {
64                                 perror_msg_and_die("%s", *argv);
65                         }
66                 }
67                 close(fd);
68                 if (utime(*argv, NULL)) {
69                         perror_msg_and_die("%s", *argv);
70                 }
71                 argc--;
72                 argv++;
73         }
74
75         return EXIT_SUCCESS;
76 }