- small size tweak
[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 <stdio.h>
21 #include <sys/types.h>
22 #include <fcntl.h>
23 #include <utime.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include "busybox.h"
28
29 int touch_main(int argc, char **argv)
30 {
31         int fd;
32         int status = EXIT_SUCCESS;
33         bool flags = (getopt32(argc, argv, "c") & 1);
34
35         argv += optind;
36
37         if (!*argv) {
38                 bb_show_usage();
39         }
40
41         do {
42                 if (utime(*argv, NULL)) {
43                         if (errno == ENOENT) {  /* no such file*/
44                                 if (flags) {    /* Creation is disabled, so ignore. */
45                                         continue;
46                                 }
47                                 /* Try to create the file. */
48                                 fd = open(*argv, O_RDWR | O_CREAT,
49                                                   S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
50                                                   );
51                                 if ((fd >= 0) && !close(fd)) {
52                                         continue;
53                                 }
54                         }
55                         status = EXIT_FAILURE;
56                         bb_perror_msg("%s", *argv);
57                 }
58         } while (*++argv);
59
60         return status;
61 }