diff options
author | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-10-14 02:23:43 +0000 |
---|---|---|
committer | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-10-14 02:23:43 +0000 |
commit | 9e617927e057d79ceb2ac4869b18b1e84567dc68 (patch) | |
tree | 37b7584ae40b99edb5583fbc4392b62ffdadf278 /libbb | |
parent | 6147e0907e5b5e3a07a4c8976097a585faaa3895 (diff) | |
download | busybox-w32-9e617927e057d79ceb2ac4869b18b1e84567dc68.tar.gz busybox-w32-9e617927e057d79ceb2ac4869b18b1e84567dc68.tar.bz2 busybox-w32-9e617927e057d79ceb2ac4869b18b1e84567dc68.zip |
add open_read_close() and similar stuff
git-svn-id: svn://busybox.net/trunk/busybox@16377 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/Kbuild | 4 | ||||
-rw-r--r-- | libbb/full_read.c | 42 | ||||
-rw-r--r-- | libbb/procps.c | 10 | ||||
-rw-r--r-- | libbb/read.c | 133 | ||||
-rw-r--r-- | libbb/safe_read.c | 26 | ||||
-rw-r--r-- | libbb/xfuncs.c | 32 |
6 files changed, 144 insertions, 103 deletions
diff --git a/libbb/Kbuild b/libbb/Kbuild index 27ed68c2c..3b16f5c45 100644 --- a/libbb/Kbuild +++ b/libbb/Kbuild | |||
@@ -9,7 +9,7 @@ lib-y:= \ | |||
9 | compare_string_array.o concat_path_file.o copy_file.o copyfd.o \ | 9 | compare_string_array.o concat_path_file.o copy_file.o copyfd.o \ |
10 | crc32.o create_icmp_socket.o create_icmp6_socket.o \ | 10 | crc32.o create_icmp_socket.o create_icmp6_socket.o \ |
11 | device_open.o dump.o error_msg.o error_msg_and_die.o \ | 11 | device_open.o dump.o error_msg.o error_msg_and_die.o \ |
12 | find_pid_by_name.o find_root_device.o fgets_str.o full_read.o \ | 12 | find_pid_by_name.o find_root_device.o fgets_str.o \ |
13 | full_write.o get_last_path_component.o get_line_from_file.o \ | 13 | full_write.o get_last_path_component.o get_line_from_file.o \ |
14 | herror_msg.o herror_msg_and_die.o \ | 14 | herror_msg.o herror_msg_and_die.o \ |
15 | human_readable.o inet_common.o inode_hash.o isdirectory.o \ | 15 | human_readable.o inet_common.o inode_hash.o isdirectory.o \ |
@@ -19,7 +19,7 @@ lib-y:= \ | |||
19 | perror_msg_and_die.o get_console.o \ | 19 | perror_msg_and_die.o get_console.o \ |
20 | process_escape_sequence.o procps.o \ | 20 | process_escape_sequence.o procps.o \ |
21 | recursive_action.o remove_file.o \ | 21 | recursive_action.o remove_file.o \ |
22 | restricted_shell.o run_parts.o run_shell.o safe_read.o safe_write.o \ | 22 | restricted_shell.o run_parts.o run_shell.o read.o safe_write.o \ |
23 | safe_strncpy.o setup_environment.o sha1.o simplify_path.o \ | 23 | safe_strncpy.o setup_environment.o sha1.o simplify_path.o \ |
24 | trim.o u_signal_names.o vdprintf.o verror_msg.o \ | 24 | trim.o u_signal_names.o vdprintf.o verror_msg.o \ |
25 | vherror_msg.o vperror_msg.o wfopen.o xconnect.o xgetcwd.o \ | 25 | vherror_msg.o vperror_msg.o wfopen.o xconnect.o xgetcwd.o \ |
diff --git a/libbb/full_read.c b/libbb/full_read.c deleted file mode 100644 index 068d16698..000000000 --- a/libbb/full_read.c +++ /dev/null | |||
@@ -1,42 +0,0 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> | ||
6 | * | ||
7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | ||
8 | */ | ||
9 | |||
10 | #include <stdio.h> | ||
11 | #include <unistd.h> | ||
12 | #include "libbb.h" | ||
13 | |||
14 | /* | ||
15 | * Read all of the supplied buffer from a file. | ||
16 | * This does multiple reads as necessary. | ||
17 | * Returns the amount read, or -1 on an error. | ||
18 | * A short read is returned on an end of file. | ||
19 | */ | ||
20 | ssize_t full_read(int fd, void *buf, size_t len) | ||
21 | { | ||
22 | ssize_t cc; | ||
23 | ssize_t total; | ||
24 | |||
25 | total = 0; | ||
26 | |||
27 | while (len) { | ||
28 | cc = safe_read(fd, buf, len); | ||
29 | |||
30 | if (cc < 0) | ||
31 | return cc; /* read() returns -1 on failure. */ | ||
32 | |||
33 | if (cc == 0) | ||
34 | break; | ||
35 | |||
36 | buf = ((char *)buf) + cc; | ||
37 | total += cc; | ||
38 | len -= cc; | ||
39 | } | ||
40 | |||
41 | return total; | ||
42 | } | ||
diff --git a/libbb/procps.c b/libbb/procps.c index 2bcd2cced..eba90705c 100644 --- a/libbb/procps.c +++ b/libbb/procps.c | |||
@@ -22,15 +22,9 @@ | |||
22 | 22 | ||
23 | static int read_to_buf(const char *filename, void *buf) | 23 | static int read_to_buf(const char *filename, void *buf) |
24 | { | 24 | { |
25 | int fd; | ||
26 | ssize_t ret; | 25 | ssize_t ret; |
27 | 26 | ret = open_read_close(filename, buf, PROCPS_BUFSIZE-1); | |
28 | fd = open(filename, O_RDONLY); | 27 | ((char *)buf)[ret > 0 ? ret : 0] = '\0'; |
29 | if (fd < 0) | ||
30 | return -1; | ||
31 | ret = read(fd, buf, PROCPS_BUFSIZE-1); | ||
32 | ((char *)buf)[ret > 0 ? ret : 0] = 0; | ||
33 | close(fd); | ||
34 | return ret; | 28 | return ret; |
35 | } | 29 | } |
36 | 30 | ||
diff --git a/libbb/read.c b/libbb/read.c new file mode 100644 index 000000000..1c2945f45 --- /dev/null +++ b/libbb/read.c | |||
@@ -0,0 +1,133 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> | ||
6 | * | ||
7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | ||
8 | */ | ||
9 | |||
10 | #include "libbb.h" | ||
11 | |||
12 | ssize_t safe_read(int fd, void *buf, size_t count) | ||
13 | { | ||
14 | ssize_t n; | ||
15 | |||
16 | do { | ||
17 | n = read(fd, buf, count); | ||
18 | } while (n < 0 && errno == EINTR); | ||
19 | |||
20 | return n; | ||
21 | } | ||
22 | |||
23 | /* | ||
24 | * Read all of the supplied buffer from a file. | ||
25 | * This does multiple reads as necessary. | ||
26 | * Returns the amount read, or -1 on an error. | ||
27 | * A short read is returned on an end of file. | ||
28 | */ | ||
29 | ssize_t full_read(int fd, void *buf, size_t len) | ||
30 | { | ||
31 | ssize_t cc; | ||
32 | ssize_t total; | ||
33 | |||
34 | total = 0; | ||
35 | |||
36 | while (len) { | ||
37 | cc = safe_read(fd, buf, len); | ||
38 | |||
39 | if (cc < 0) | ||
40 | return cc; /* read() returns -1 on failure. */ | ||
41 | |||
42 | if (cc == 0) | ||
43 | break; | ||
44 | |||
45 | buf = ((char *)buf) + cc; | ||
46 | total += cc; | ||
47 | len -= cc; | ||
48 | } | ||
49 | |||
50 | return total; | ||
51 | } | ||
52 | |||
53 | // Die with an error message if we can't read the entire buffer. | ||
54 | void xread(int fd, void *buf, size_t count) | ||
55 | { | ||
56 | if (count) { | ||
57 | ssize_t size = full_read(fd, buf, count); | ||
58 | if (size != count) | ||
59 | bb_error_msg_and_die("short read"); | ||
60 | } | ||
61 | } | ||
62 | |||
63 | // Die with an error message if we can't read one character. | ||
64 | unsigned char xread_char(int fd) | ||
65 | { | ||
66 | char tmp; | ||
67 | |||
68 | xread(fd, &tmp, 1); | ||
69 | |||
70 | return tmp; | ||
71 | } | ||
72 | |||
73 | // Read one line a-la fgets. Works only on seekable streams | ||
74 | char *reads(int fd, char *buffer, size_t size) | ||
75 | { | ||
76 | char *p; | ||
77 | |||
78 | if (size < 2) | ||
79 | return NULL; | ||
80 | size = full_read(fd, buffer, size-1); | ||
81 | if ((ssize_t)size <= 0) | ||
82 | return NULL; | ||
83 | |||
84 | buffer[size] = '\0'; | ||
85 | p = strchr(buffer, '\n'); | ||
86 | if (p) { | ||
87 | off_t offset; | ||
88 | *p++ = '\0'; | ||
89 | offset = (p-buffer) - size; | ||
90 | // set fd position the right after the \n | ||
91 | if (offset && lseek(fd, offset, SEEK_CUR) == (off_t)-1) | ||
92 | return NULL; | ||
93 | } | ||
94 | return buffer; | ||
95 | } | ||
96 | |||
97 | ssize_t read_close(int fd, void *buf, size_t size) | ||
98 | { | ||
99 | int e; | ||
100 | size = full_read(fd, buf, size); | ||
101 | e = errno; | ||
102 | close(fd); | ||
103 | errno = e; | ||
104 | return size; | ||
105 | } | ||
106 | |||
107 | ssize_t open_read_close(const char *filename, void *buf, size_t size) | ||
108 | { | ||
109 | int fd = open(filename, O_RDONLY); | ||
110 | if (fd < 0) | ||
111 | return fd; | ||
112 | return read_close(fd, buf, size); | ||
113 | } | ||
114 | |||
115 | void *xmalloc_open_read_close(const char *filename, size_t *sizep) | ||
116 | { | ||
117 | char *buf; | ||
118 | size_t size = sizep ? *sizep : INT_MAX; | ||
119 | int fd = xopen(filename, O_RDONLY); | ||
120 | off_t len = xlseek(fd, 0, SEEK_END); | ||
121 | xlseek(fd, 0, SEEK_SET); | ||
122 | |||
123 | if (len > size) | ||
124 | bb_error_msg_and_die("file '%s' is too big", filename); | ||
125 | size = len; | ||
126 | buf = xmalloc(size+1); | ||
127 | size = read_close(fd, buf, size); | ||
128 | if ((ssize_t)size < 0) | ||
129 | bb_perror_msg_and_die("'%s'", filename); | ||
130 | buf[size] = '\0'; | ||
131 | if (sizep) *sizep = size; | ||
132 | return buf; | ||
133 | } | ||
diff --git a/libbb/safe_read.c b/libbb/safe_read.c deleted file mode 100644 index a59934a4d..000000000 --- a/libbb/safe_read.c +++ /dev/null | |||
@@ -1,26 +0,0 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> | ||
6 | * | ||
7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | ||
8 | */ | ||
9 | |||
10 | #include <stdio.h> | ||
11 | #include <errno.h> | ||
12 | #include <unistd.h> | ||
13 | #include "libbb.h" | ||
14 | |||
15 | |||
16 | |||
17 | ssize_t safe_read(int fd, void *buf, size_t count) | ||
18 | { | ||
19 | ssize_t n; | ||
20 | |||
21 | do { | ||
22 | n = read(fd, buf, count); | ||
23 | } while (n < 0 && errno == EINTR); | ||
24 | |||
25 | return n; | ||
26 | } | ||
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index efc919491..0a5abb878 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c | |||
@@ -95,7 +95,7 @@ int xopen(const char *pathname, int flags) | |||
95 | if (ENABLE_DEBUG && (flags & O_CREAT)) | 95 | if (ENABLE_DEBUG && (flags & O_CREAT)) |
96 | bb_error_msg_and_die("xopen() with O_CREAT"); | 96 | bb_error_msg_and_die("xopen() with O_CREAT"); |
97 | 97 | ||
98 | return xopen3(pathname, flags, 0777); | 98 | return xopen3(pathname, flags, 0666); |
99 | } | 99 | } |
100 | 100 | ||
101 | // Die if we can't open a new file and return an fd. | 101 | // Die if we can't open a new file and return an fd. |
@@ -110,16 +110,6 @@ int xopen3(const char *pathname, int flags, int mode) | |||
110 | return ret; | 110 | return ret; |
111 | } | 111 | } |
112 | 112 | ||
113 | // Die with an error message if we can't read the entire buffer. | ||
114 | void xread(int fd, void *buf, size_t count) | ||
115 | { | ||
116 | if (count) { | ||
117 | ssize_t size = full_read(fd, buf, count); | ||
118 | if (size != count) | ||
119 | bb_error_msg_and_die("short read"); | ||
120 | } | ||
121 | } | ||
122 | |||
123 | // Die with an error message if we can't write the entire buffer. | 113 | // Die with an error message if we can't write the entire buffer. |
124 | void xwrite(int fd, void *buf, size_t count) | 114 | void xwrite(int fd, void *buf, size_t count) |
125 | { | 115 | { |
@@ -131,20 +121,12 @@ void xwrite(int fd, void *buf, size_t count) | |||
131 | } | 121 | } |
132 | 122 | ||
133 | // Die with an error message if we can't lseek to the right spot. | 123 | // Die with an error message if we can't lseek to the right spot. |
134 | void xlseek(int fd, off_t offset, int whence) | 124 | off_t xlseek(int fd, off_t offset, int whence) |
135 | { | 125 | { |
136 | if (offset != lseek(fd, offset, whence)) | 126 | off_t off = lseek(fd, offset, whence); |
137 | bb_error_msg_and_die("lseek"); | 127 | if (off == (off_t)-1) |
138 | } | 128 | bb_perror_msg_and_die("lseek"); |
139 | 129 | return off; | |
140 | // Die with an error message if we can't read one character. | ||
141 | unsigned char xread_char(int fd) | ||
142 | { | ||
143 | char tmp; | ||
144 | |||
145 | xread(fd, &tmp, 1); | ||
146 | |||
147 | return tmp; | ||
148 | } | 130 | } |
149 | 131 | ||
150 | // Die with supplied error message if this FILE * has ferror set. | 132 | // Die with supplied error message if this FILE * has ferror set. |
@@ -309,7 +291,7 @@ off_t fdlength(int fd) | |||
309 | 291 | ||
310 | // If we can read from the current location, it's bigger. | 292 | // If we can read from the current location, it's bigger. |
311 | 293 | ||
312 | if (lseek(fd, pos, 0)>=0 && safe_read(fd, &temp, 1)==1) { | 294 | if (lseek(fd, pos, SEEK_SET)>=0 && safe_read(fd, &temp, 1)==1) { |
313 | if (bottom == top) bottom = top = (top+1) * 2; | 295 | if (bottom == top) bottom = top = (top+1) * 2; |
314 | else bottom = pos; | 296 | else bottom = pos; |
315 | 297 | ||