OSDN Git Service

Add openro() and WARN_ONLY flag so xopen() and friends can warn without exiting.
authorRob Landley <rob@landley.net>
Sun, 4 Sep 2016 22:26:34 +0000 (17:26 -0500)
committerRob Landley <rob@landley.net>
Sun, 4 Sep 2016 22:26:34 +0000 (17:26 -0500)
lib/lib.h
lib/xwrap.c

index 5cd991c..3c5adb4 100644 (file)
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -102,6 +102,11 @@ struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node));
 
 void show_help(FILE *out);
 
+// Tell xopen and friends to print warnings but return -1 as necessary
+// The largest O_BLAH flag so far is arch/alpha's O_PATH at 0x800000 so
+// plenty of headroom.
+#define WARN_ONLY (1<<31)
+
 // xwrap.c
 void xstrncpy(char *dest, char *src, size_t size);
 void xstrncat(char *dest, char *src, size_t size);
@@ -132,6 +137,7 @@ int xcreate(char *path, int flags, int mode);
 int xopen(char *path, int flags);
 int xcreate_stdio(char *path, int flags, int mode);
 int xopen_stdio(char *path, int flags);
+int openro(char *path, int flags);
 int xopenro(char *path);
 void xpipe(int *pp);
 void xclose(int fd);
index 48e0296..89488ac 100644 (file)
@@ -318,11 +318,13 @@ void xunlink(char *path)
 }
 
 // Die unless we can open/create a file, returning file descriptor.
+// The meaning of O_CLOEXEC is reversed (it defaults on, pass it to disable)
+// and WARN_ONLY tells us not to exit.
 int xcreate_stdio(char *path, int flags, int mode)
 {
-  int fd = open(path, flags^O_CLOEXEC, mode);
+  int fd = open(path, (flags^O_CLOEXEC)&~WARN_ONLY, mode);
 
-  if (fd == -1) perror_exit_raw(path);
+  if (fd == -1) ((mode&WARN_ONLY) ? perror_msg_raw : perror_exit_raw)(path);
   return fd;
 }
 
@@ -378,12 +380,18 @@ int xopen(char *path, int flags)
   return notstdio(xopen_stdio(path, flags));
 }
 
-// Open read only, treating "-" as a synonym for stdin.
-int xopenro(char *path)
+// Open read only, treating "-" as a synonym for stdin, defaulting to warn only
+int openro(char *path, int flags)
 {
   if (!strcmp(path, "-")) return 0;
 
-  return xopen(path, O_RDONLY);
+  return xopen(path, flags^WARN_ONLY);
+}
+
+// Open read only, treating "-" as a synonym for stdin.
+int xopenro(char *path)
+{
+  return openro(path, O_RDONLY|WARN_ONLY);
 }
 
 FILE *xfdopen(int fd, char *mode)
@@ -674,6 +682,8 @@ char *xreadfile(char *name, char *buf, off_t len)
   return buf;
 }
 
+// The data argument to ioctl() is actually long, but it's usually used as
+// a pointer. If you need to feed in a number, do (void *)(long) typecast.
 int xioctl(int fd, int request, void *data)
 {
   int rc;