comparison toys/touch.c @ 218:bc87305c391f

Make touch work reliably when file doesn't exist and clean up headers a bit.
author Rob Landley <rob@landley.net>
date Fri, 28 Dec 2007 03:29:33 -0600
parents 3227c5316260
children d4176f3f3835
comparison
equal deleted inserted replaced
217:ca48a878255d 218:bc87305c391f
5 * Copyright (C) 2007 Charlie Shepherd <masterdriverz@gentoo.org> 5 * Copyright (C) 2007 Charlie Shepherd <masterdriverz@gentoo.org>
6 * 6 *
7 * See http://www.opengroup.org/onlinepubs/009695399/utilities/touch.html 7 * See http://www.opengroup.org/onlinepubs/009695399/utilities/touch.html
8 */ 8 */
9 9
10 #define _XOPEN_SOURCE 600
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <utime.h>
15 #include <time.h>
16 #include "toys.h" 10 #include "toys.h"
17 11
18 #define OPT_MTIME 0x01 12 #define OPT_MTIME 0x01
19 #define OPT_NOCREATE 0x02 13 #define OPT_NOCREATE 0x02
20 #define OPT_ATIME 0x04 14 #define OPT_ATIME 0x04
21 #define OPT_REFERENCE 0x08 15 #define OPT_REFERENCE 0x08
22 #define OPT_TIME 0x10 16 #define OPT_TIME 0x10
23 #define OPT_LENGTH 0x20 17 #define OPT_LENGTH 0x20
24 18
25 void touch_main(void) 19 void touch_main(void)
26 { 20 {
27 char *arg; 21 char *arg;
28 int i, set_a, set_m; 22 int i, set_a, set_m;
63 struct stat sb; 57 struct stat sb;
64 58
65 buf.modtime = curr_m; 59 buf.modtime = curr_m;
66 buf.actime = curr_a; 60 buf.actime = curr_a;
67 61
68 if (stat(arg, &sb) == -1) { 62 if (stat(arg, &sb)) {
69 if (!(toys.optflags & OPT_NOCREATE) && errno == ENOENT) { 63 if (!(toys.optflags & OPT_NOCREATE)) {
70 if (creat(arg, 0644)) 64 int temp = umask(0);
71 goto error; 65 xcreate(arg, O_CREAT, 0644);
66 if (CFG_TOYBOX_FREE) umask(temp);
72 if (stat(arg, &sb)) 67 if (stat(arg, &sb))
73 goto error; 68 goto error;
74 } 69 }
75 } 70 }
76 71