use utimes() rather than obsolescent utime()
[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-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 /* BB_AUDIT SUSv3 _NOT_ compliant -- options -a, -m, -r, -t not supported. */
11 /* http://www.opengroup.org/onlinepubs/007904975/utilities/touch.html */
12
13 /* Mar 16, 2003      Manuel Novoa III   (mjn3@codepoet.org)
14  *
15  * Previous version called open() and then utime().  While this will be
16  * be necessary to implement -r and -t, it currently only makes things bigger.
17  * Also, exiting on a failure was a bug.  All args should be processed.
18  */
19
20 #include "libbb.h"
21
22 /* This is a NOFORK applet. Be very careful! */
23
24 /* coreutils implements:
25  * -a   change only the access time
26  * -c, --no-create
27  *      do not create any files
28  * -d, --date=STRING
29  *      parse STRING and use it instead of current time
30  * -f   (ignored, BSD compat)
31  * -m   change only the modification time
32  * -r, --reference=FILE
33  *      use this file's times instead of current time
34  * -t STAMP
35  *      use [[CC]YY]MMDDhhmm[.ss] instead of current time
36  * --time=WORD
37  *      change the specified time: WORD is access, atime, or use
38  */
39
40 int touch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
41 int touch_main(int argc UNUSED_PARAM, char **argv)
42 {
43 #if ENABLE_DESKTOP
44 # if ENABLE_LONG_OPTS
45         static const char touch_longopts[] ALIGN1 =
46                 /* name, has_arg, val */
47                 "no-create\0"         No_argument       "c"
48                 "reference\0"         Required_argument "r"
49                 "date\0"              Required_argument "d"
50         ;
51 # endif
52         struct timeval timebuf = {.tv_usec = 0};
53         char *reference_file = NULL;
54         char *date_str = NULL;
55 #else
56 # define reference_file NULL
57 # define date_str       NULL
58 # define timebuf        (*(struct timeval*)NULL)
59 #endif
60         int fd;
61         int status = EXIT_SUCCESS;
62         int opts;
63
64 #if ENABLE_DESKTOP && ENABLE_LONG_OPTS
65         applet_long_options = touch_longopts;
66 #endif
67         /* -d and -t both set time. In coreutils,
68          * accepted data format differs a bit between -d and -t.
69          * We accept the same formats for both */
70         opts = getopt32(argv, "c" IF_DESKTOP("r:d:t:")
71                                 /*ignored:*/ "fma"
72                                 IF_DESKTOP(, &reference_file)
73                                 IF_DESKTOP(, &date_str)
74                                 IF_DESKTOP(, &date_str)
75         );
76
77         opts &= 1; /* only -c bit is left */
78         argv += optind;
79         if (!*argv) {
80                 bb_show_usage();
81         }
82
83         if (reference_file) {
84                 struct stat stbuf;
85                 xstat(reference_file, &stbuf);
86                 timebuf.tv_sec = stbuf.st_mtime;
87         }
88
89         if (date_str) {
90                 struct tm tm_time;
91                 time_t t;
92
93                 //time(&t);
94                 //localtime_r(&t, &tm_time);
95                 memset(&tm_time, 0, sizeof(tm_time));
96                 parse_datestr(date_str, &tm_time);
97
98                 /* Correct any day of week and day of year etc. fields */
99                 tm_time.tm_isdst = -1;  /* Be sure to recheck dst */
100                 t = validate_tm_time(date_str, &tm_time);
101
102                 timebuf.tv_sec = t;
103         }
104
105         do {
106                 if (utimes(*argv, reference_file ? &timebuf : NULL)) {
107                         if (errno == ENOENT) { /* no such file */
108                                 if (opts) { /* creation is disabled, so ignore */
109                                         continue;
110                                 }
111                                 /* Try to create the file. */
112                                 fd = open(*argv, O_RDWR | O_CREAT,
113                                                   S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
114                                                   );
115                                 if ((fd >= 0) && !close(fd)) {
116                                         if (reference_file)
117                                                 utimes(*argv, &timebuf);
118                                         continue;
119                                 }
120                         }
121                         status = EXIT_FAILURE;
122                         bb_simple_perror_msg(*argv);
123                 }
124         } while (*++argv);
125
126         return status;
127 }