aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2007-01-18 21:54:08 -0500
committerRob Landley <rob@landley.net>2007-01-18 21:54:08 -0500
commit901637760b4206e968e73dd5ff7430c107c27b57 (patch)
tree61923e296b7728cb32261bd64eb1ad2261f5ad75
parent813840c9d199bf9e26d23291cf23e22a68e8ad82 (diff)
downloadtoybox-901637760b4206e968e73dd5ff7430c107c27b57.tar.gz
Add fdprintf(). Remove reread() and rewrite() which handle -EINTR, which
shouldn't be a problem if we register signal handlers with sigaction(SA_RESTART) Straighten out count and len (I generally consistently use "count" for the current progress and "len" for the total, but this time I got them backwards for some reason and don't want to confuse myself in future.)
-rw-r--r--lib/functions.c57
-rw-r--r--lib/lib.h12
-rw-r--r--lib/portability.h6
-rw-r--r--toys.h3
-rw-r--r--toys/catv.c2
5 files changed, 35 insertions, 45 deletions
diff --git a/lib/functions.c b/lib/functions.c
index 98a344a2..ce3edccc 100644
--- a/lib/functions.c
+++ b/lib/functions.c
@@ -174,30 +174,12 @@ FILE *xfopen(char *path, char *mode)
return f;
}
-// Read from file handle, retrying if interrupted.
-ssize_t reread(int fd, void *buf, size_t count)
-{
- for (;;) {
- ssize_t len = read(fd, buf, count);
- if (len >= 0 || errno != EINTR) return len;
- }
-}
-
-// Write to file handle, retrying if interrupted.
-ssize_t rewrite(int fd, void *buf, size_t count)
-{
- for (;;) {
- ssize_t len = write(fd, buf, count);
- if (len >= 0 || errno != EINTR) return len;
- }
-}
-
// Keep reading until full or EOF
-ssize_t readall(int fd, void *buf, size_t count)
+ssize_t readall(int fd, void *buf, size_t len)
{
- size_t len = 0;
- while (len<count) {
- int i = reread(fd, buf, count);
+ size_t count = 0;
+ while (count<len) {
+ int i = read(fd, buf+count, len-count);
if (!i) return len;
if (i<0) return i;
count += i;
@@ -207,13 +189,12 @@ ssize_t readall(int fd, void *buf, size_t count)
}
// Keep writing until done or EOF
-ssize_t writeall(int fd, void *buf, size_t count)
+ssize_t writeall(int fd, void *buf, size_t len)
{
- size_t len = 0;
- while (len<count) {
- int i = rewrite(fd, buf, count);
- if (!i) return len;
- if (i<0) return i;
+ size_t count = 0;
+ while (count<len) {
+ int i = write(fd, buf+count, len-count);
+ if (i<1) return i;
count += i;
}
@@ -221,22 +202,26 @@ ssize_t writeall(int fd, void *buf, size_t count)
}
// Die if there's an error other than EOF.
-size_t xread(int fd, void *buf, size_t count)
+size_t xread(int fd, void *buf, size_t len)
{
- count = reread(fd, buf, count);
- if (count < 0) perror_exit("xread");
+ len = read(fd, buf, len);
+ if (len < 0) perror_exit("xread");
- return count;
+ return len;
}
-void xreadall(int fd, void *buf, size_t count)
+void xreadall(int fd, void *buf, size_t len)
{
- if (count != readall(fd, buf, count)) perror_exit("xreadall");
+ if (len != readall(fd, buf, len)) perror_exit("xreadall");
}
-void xwrite(int fd, void *buf, size_t count)
+// There's no xwriteall(), just xwrite(). When we read, there may or may not
+// be more data waiting. When we write, there is data and it had better go
+// somewhere.
+
+void xwrite(int fd, void *buf, size_t len)
{
- if (count != writeall(fd, buf, count)) perror_exit("xwrite");
+ if (len != writeall(fd, buf, len)) perror_exit("xwrite");
}
char *xgetcwd(void)
diff --git a/lib/lib.h b/lib/lib.h
index 24d6f82a..3a7d6507 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -43,13 +43,11 @@ void xaccess(char *path, int flags);
int xcreate(char *path, int flags, int mode);
int xopen(char *path, int flags);
FILE *xfopen(char *path, char *mode);
-ssize_t reread(int fd, void *buf, size_t count);
-ssize_t rewrite(int fd, void *buf, size_t count);
-ssize_t readall(int fd, void *buf, size_t count);
-ssize_t writeall(int fd, void *buf, size_t count);
-size_t xread(int fd, void *buf, size_t count);
-void xreadall(int fd, void *buf, size_t count);
-void xwrite(int fd, void *buf, size_t count);
+ssize_t readall(int fd, void *buf, size_t len);
+ssize_t writeall(int fd, void *buf, size_t len);
+size_t xread(int fd, void *buf, size_t len);
+void xreadall(int fd, void *buf, size_t len);
+void xwrite(int fd, void *buf, size_t len);
char *xgetcwd(void);
char *xabspath(char *path);
struct string_list *find_in_path(char *path, char *filename);
diff --git a/lib/portability.h b/lib/portability.h
index dca0ddea..5aa4a0bf 100644
--- a/lib/portability.h
+++ b/lib/portability.h
@@ -1,3 +1,9 @@
+
+// Humor glibc to get dprintf, then #define it to something more portable.
+#define _GNU_SOURCE
+#include <stdio.h>
+#define fdprintf(...) dprintf(__VA_ARGS__)
+
#include <endian.h>
#if __BYTE_ORDER == __BIG_ENDIAN
diff --git a/toys.h b/toys.h
index 27647c93..403388ed 100644
--- a/toys.h
+++ b/toys.h
@@ -6,6 +6,8 @@
* Licensed under GPL version 2, see file LICENSE in this tarball for details.
*/
+#include "lib/portability.h"
+
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
@@ -26,7 +28,6 @@
#include <unistd.h>
#include "lib/lib.h"
-#include "lib/portability.h"
#include "gen_config.h"
#include "toys/toylist.h"
diff --git a/toys/catv.c b/toys/catv.c
index e37f307b..9097c065 100644
--- a/toys/catv.c
+++ b/toys/catv.c
@@ -27,7 +27,7 @@ int catv_main(void)
else for(;;) {
int i, res;
- res = reread(fd, toybuf, sizeof(toybuf));
+ res = read(fd, toybuf, sizeof(toybuf));
if (res < 0) retval = EXIT_FAILURE;
if (res < 1) break;
for (i=0; i<res; i++) {