changeset 1773:4d688f02feaa draft

Add readfileat() to lib
author Rob Landley <rob@landley.net>
date Fri, 03 Apr 2015 11:49:31 -0500
parents 4172d341590a
children 014a5e821603
files lib/lib.c lib/lib.h
diffstat 2 files changed, 8 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/lib/lib.c	Fri Apr 03 11:46:44 2015 -0500
+++ b/lib/lib.c	Fri Apr 03 11:49:31 2015 -0500
@@ -362,14 +362,12 @@
 
 // Read contents of file as a single nul-terminated string.
 // malloc new one if buf=len=0
-char *readfile(char *name, char *ibuf, off_t len)
+char *readfileat(int dirfd, char *name, char *ibuf, off_t len)
 {
   int fd;
   char *buf;
 
-  fd = open(name, O_RDONLY);
-  if (fd == -1) return 0;
-
+  if (-1 == (fd = openat(dirfd, name, O_RDONLY))) return 0;
   if (len<1) {
     len = fdlength(fd);
     // proc files don't report a length, so try 1 page minimum.
@@ -388,6 +386,11 @@
   return buf;
 }
 
+char *readfile(char *name, char *ibuf, off_t len)
+{
+  return readfileat(AT_FDCWD, name, ibuf, len);
+}
+
 // Sleep for this many thousandths of a second
 void msleep(long miliseconds)
 {
--- a/lib/lib.h	Fri Apr 03 11:46:44 2015 -0500
+++ b/lib/lib.h	Fri Apr 03 11:49:31 2015 -0500
@@ -144,6 +144,7 @@
 off_t lskip(int fd, off_t offset);
 int mkpathat(int atfd, char *dir, mode_t lastmode, int flags);
 struct string_list **splitpath(char *path, struct string_list **list);
+char *readfileat(int dirfd, char *name, char *buf, off_t len);
 char *readfile(char *name, char *buf, off_t len);
 void msleep(long miliseconds);
 int64_t peek_le(void *ptr, unsigned size);