getopt_ulflags -> getopt32.
[platform/upstream/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 flags;
33         int status = EXIT_SUCCESS;
34
35         flags = getopt32(argc, argv, "c");
36
37         argv += optind;
38
39         if (!*argv) {
40                 bb_show_usage();
41         }
42
43         do {
44                 if (utime(*argv, NULL)) {
45                         if (errno == ENOENT) {  /* no such file*/
46                                 if (flags & 1) {        /* Creation is disabled, so ignore. */
47                                         continue;
48                                 }
49                                 /* Try to create the file. */
50                                 fd = open(*argv, O_RDWR | O_CREAT,
51                                                   S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
52                                                   );
53                                 if ((fd >= 0) && !close(fd)) {
54                                         continue;
55                                 }
56                         }
57                         status = EXIT_FAILURE;
58                         bb_perror_msg("%s", *argv);
59                 }
60         } while (*++argv);
61
62         return status;
63 }