changeset 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 ca48a878255d
children cfa11e043e2b
files toys.h toys/touch.c
diffstat 2 files changed, 15 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/toys.h	Thu Dec 27 21:36:44 2007 -0600
+++ b/toys.h	Fri Dec 28 03:29:33 2007 -0600
@@ -31,8 +31,11 @@
 #include <sys/statvfs.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <unistd.h>
+#include <utime.h>
+
+#define _XOPEN_SOURCE 600
 #include <time.h>
-#include <unistd.h>
 
 #include "lib/lib.h"
 #include "toys/e2fs.h"
--- a/toys/touch.c	Thu Dec 27 21:36:44 2007 -0600
+++ b/toys/touch.c	Fri Dec 28 03:29:33 2007 -0600
@@ -7,20 +7,14 @@
  * See http://www.opengroup.org/onlinepubs/009695399/utilities/touch.html
  */
 
-#define _XOPEN_SOURCE 600
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <utime.h>
-#include <time.h>
 #include "toys.h"
 
-#define OPT_MTIME		0x01
-#define OPT_NOCREATE	0x02
-#define OPT_ATIME		0x04
-#define OPT_REFERENCE	0x08
-#define OPT_TIME		0x10
-#define OPT_LENGTH		0x20
+#define OPT_MTIME       0x01
+#define OPT_NOCREATE    0x02
+#define OPT_ATIME       0x04
+#define OPT_REFERENCE   0x08
+#define OPT_TIME        0x10
+#define OPT_LENGTH      0x20
 
 void touch_main(void)
 {
@@ -65,10 +59,11 @@
 		buf.modtime = curr_m;
 		buf.actime = curr_a;
 
-		if (stat(arg, &sb) == -1) {
-			if (!(toys.optflags & OPT_NOCREATE) && errno == ENOENT) {
-				if (creat(arg, 0644))
-					goto error;
+		if (stat(arg, &sb)) {
+			if (!(toys.optflags & OPT_NOCREATE)) {
+				int temp = umask(0);
+				xcreate(arg, O_CREAT, 0644);
+				if (CFG_TOYBOX_FREE) umask(temp);
 				if (stat(arg, &sb))
 					goto error;
 			}