aboutsummaryrefslogtreecommitdiff
path: root/coreutils/touch.c
diff options
context:
space:
mode:
authormjn3 <mjn3@69ca8d6d-28ef-0310-b511-8ec308f3f277>2003-03-19 09:13:01 +0000
committermjn3 <mjn3@69ca8d6d-28ef-0310-b511-8ec308f3f277>2003-03-19 09:13:01 +0000
commite901c15d890dbbdce4c086963cb1513653fc46b5 (patch)
treea318d0f03aa076c74b576ea45dc543a5669e8e91 /coreutils/touch.c
parent40758c00616c3b2c85d83eb4afdeb04b1f65c9f1 (diff)
downloadbusybox-w32-e901c15d890dbbdce4c086963cb1513653fc46b5.tar.gz
busybox-w32-e901c15d890dbbdce4c086963cb1513653fc46b5.tar.bz2
busybox-w32-e901c15d890dbbdce4c086963cb1513653fc46b5.zip
Major coreutils update.
git-svn-id: svn://busybox.net/trunk/busybox@6751 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'coreutils/touch.c')
-rw-r--r--coreutils/touch.c67
1 files changed, 34 insertions, 33 deletions
diff --git a/coreutils/touch.c b/coreutils/touch.c
index f1c6dc484..c66f26e0d 100644
--- a/coreutils/touch.c
+++ b/coreutils/touch.c
@@ -21,6 +21,16 @@
21 * 21 *
22 */ 22 */
23 23
24/* BB_AUDIT SUSv3 _NOT_ compliant -- options -a, -m, -r, -t not supported. */
25/* http://www.opengroup.org/onlinepubs/007904975/utilities/touch.html */
26
27/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
28 *
29 * Previous version called open() and then utime(). While this will be
30 * be necessary to implement -r and -t, it currently only makes things bigger.
31 * Also, exiting on a failure was a bug. All args should be processed.
32 */
33
24#include <stdio.h> 34#include <stdio.h>
25#include <sys/types.h> 35#include <sys/types.h>
26#include <fcntl.h> 36#include <fcntl.h>
@@ -33,44 +43,35 @@
33extern int touch_main(int argc, char **argv) 43extern int touch_main(int argc, char **argv)
34{ 44{
35 int fd; 45 int fd;
36 int create = TRUE; 46 int flags;
47 int status = EXIT_SUCCESS;
37 48
38 /* Parse options */ 49 flags = bb_getopt_ulflags(argc, argv, "c");
39 while (--argc > 0 && **(++argv) == '-') { 50
40 while (*(++(*argv))) { 51 argv += optind;
41 switch (**argv) {
42 case 'c':
43 create = FALSE;
44 break;
45 default:
46 show_usage();
47 }
48 }
49 }
50 52
51 if (argc < 1) { 53 if (!*argv) {
52 show_usage(); 54 bb_show_usage();
53 } 55 }
54 56
55 while (argc > 0) { 57 do {
56 fd = open(*argv, create ? O_RDWR | O_CREAT : O_RDWR,
57 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
58 if (fd < 0) {
59 if (! create && 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)) { 58 if (utime(*argv, NULL)) {
69 perror_msg_and_die("%s", *argv); 59 if (errno == ENOENT) { /* no such file*/
60 if (flags & 1) { /* Creation is disabled, so ignore. */
61 continue;
62 }
63 /* Try to create the file. */
64 fd = open(*argv, O_RDWR | O_CREAT,
65 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
66 );
67 if ((fd >= 0) && !close(fd)) {
68 continue;
69 }
70 }
71 status = EXIT_FAILURE;
72 bb_perror_msg("%s", *argv);
70 } 73 }
71 argc--; 74 } while (*++argv);
72 argv++;
73 }
74 75
75 return EXIT_SUCCESS; 76 return status;
76} 77}