Initial public busybox maemo commit, 3:1.10.2.legal-1osso12
[busybox4maemo] / 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 int touch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
25 int touch_main(int argc ATTRIBUTE_UNUSED, char **argv)
26 {
27         int fd;
28         int status = EXIT_SUCCESS;
29         int flags = getopt32(argv, "cf");
30
31         flags &= 1; /* ignoring -f (BSD compat thingy) */
32         argv += optind;
33
34         if (!*argv) {
35                 bb_show_usage();
36         }
37
38         do {
39                 if (utime(*argv, NULL)) {
40                         if (errno == ENOENT) {  /* no such file */
41                                 if (flags) {    /* Creation is disabled, so ignore. */
42                                         continue;
43                                 }
44                                 /* Try to create the file. */
45                                 fd = open(*argv, O_RDWR | O_CREAT,
46                                                   S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
47                                                   );
48                                 if ((fd >= 0) && !close(fd)) {
49                                         continue;
50                                 }
51                         }
52                         status = EXIT_FAILURE;
53                         bb_simple_perror_msg(*argv);
54                 }
55         } while (*++argv);
56
57         return status;
58 }