libbb: use improved xmalloc_read() from modprobe-small

who: fix compile breakage on some systems
modprobe-small: improve Config help text wording
This commit is contained in:
Denis Vlasenko 2008-07-09 09:50:33 +00:00
parent dbef1173b0
commit f62ab2d774
5 changed files with 63 additions and 126 deletions

View File

@ -56,16 +56,20 @@ int who_main(int argc UNUSED_PARAM, char **argv)
printf("USER TTY IDLE TIME HOST\n"); printf("USER TTY IDLE TIME HOST\n");
while ((ut = getutent()) != NULL) { while ((ut = getutent()) != NULL) {
if (ut->ut_user[0] && (opt || ut->ut_type == USER_PROCESS)) { if (ut->ut_user[0] && (opt || ut->ut_type == USER_PROCESS)) {
time_t tmp;
/* ut->ut_line is device name of tty - "/dev/" */ /* ut->ut_line is device name of tty - "/dev/" */
name = concat_path_file("/dev", ut->ut_line); name = concat_path_file("/dev", ut->ut_line);
str6[0] = '?'; str6[0] = '?';
str6[1] = '\0'; str6[1] = '\0';
if (stat(name, &st) == 0) if (stat(name, &st) == 0)
idle_string(str6, st.st_atime); idle_string(str6, st.st_atime);
/* manpages say ut_tv.tv_sec *is* time_t,
* but some systems have it wrong */
tmp = ut->ut_tv.tv_sec;
/* 15 chars for time: Nov 10 19:33:20 */ /* 15 chars for time: Nov 10 19:33:20 */
printf("%-10s %-8s %-9s %-15.15s %s\n", printf("%-10s %-8s %-9s %-15.15s %s\n",
ut->ut_user, ut->ut_line, str6, ut->ut_user, ut->ut_line, str6,
ctime(&(ut->ut_tv.tv_sec)) + 4, ut->ut_host); ctime(&tmp) + 4, ut->ut_host);
if (ENABLE_FEATURE_CLEAN_UP) if (ENABLE_FEATURE_CLEAN_UP)
free(name); free(name);
} }

View File

@ -589,6 +589,7 @@ extern char *reads(int fd, char *buf, size_t count) FAST_FUNC;
extern char *xmalloc_reads(int fd, char *pfx, size_t *maxsz_p) FAST_FUNC; extern char *xmalloc_reads(int fd, char *pfx, size_t *maxsz_p) FAST_FUNC;
extern ssize_t read_close(int fd, void *buf, size_t maxsz) FAST_FUNC; extern ssize_t read_close(int fd, void *buf, size_t maxsz) FAST_FUNC;
extern ssize_t open_read_close(const char *filename, void *buf, size_t maxsz) FAST_FUNC; extern ssize_t open_read_close(const char *filename, void *buf, size_t maxsz) FAST_FUNC;
extern void *xmalloc_read(int fd, size_t *sizep) FAST_FUNC;
/* Returns NULL if file can't be opened */ /* Returns NULL if file can't be opened */
extern void *xmalloc_open_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC; extern void *xmalloc_open_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC;
/* Never returns NULL */ /* Never returns NULL */

View File

@ -106,7 +106,7 @@ ssize_t FAST_FUNC full_read(int fd, void *buf, size_t len)
return total; return total;
} }
// Die with an error message if we can't read the entire buffer. /* Die with an error message if we can't read the entire buffer. */
void FAST_FUNC xread(int fd, void *buf, size_t count) void FAST_FUNC xread(int fd, void *buf, size_t count)
{ {
if (count) { if (count) {
@ -116,7 +116,7 @@ void FAST_FUNC xread(int fd, void *buf, size_t count)
} }
} }
// Die with an error message if we can't read one character. /* Die with an error message if we can't read one character. */
unsigned char FAST_FUNC xread_char(int fd) unsigned char FAST_FUNC xread_char(int fd)
{ {
char tmp; char tmp;
@ -124,7 +124,7 @@ unsigned char FAST_FUNC xread_char(int fd)
return tmp; return tmp;
} }
// Read one line a-la fgets. Works only on seekable streams /* Read one line a-la fgets. Works only on seekable streams */
char* FAST_FUNC reads(int fd, char *buffer, size_t size) char* FAST_FUNC reads(int fd, char *buffer, size_t size)
{ {
char *p; char *p;
@ -140,9 +140,9 @@ char* FAST_FUNC reads(int fd, char *buffer, size_t size)
if (p) { if (p) {
off_t offset; off_t offset;
*p++ = '\0'; *p++ = '\0';
// avoid incorrect (unsigned) widening /* avoid incorrect (unsigned) widening */
offset = (off_t)(p - buffer) - (off_t)size; offset = (off_t)(p - buffer) - (off_t)size;
// set fd position right after '\n' /* set fd position right after '\n' */
if (offset && lseek(fd, offset, SEEK_CUR) == (off_t)-1) if (offset && lseek(fd, offset, SEEK_CUR) == (off_t)-1)
return NULL; return NULL;
} }
@ -203,39 +203,52 @@ ssize_t FAST_FUNC open_read_close(const char *filename, void *buf, size_t size)
return read_close(fd, buf, size); return read_close(fd, buf, size);
} }
// Read (potentially big) files in one go. File size is estimated // Read (potentially big) files in one go. File size is estimated
// by stat. // by stat. Extra '\0' byte is appended.
void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep) void* FAST_FUNC xmalloc_read(int fd, size_t *sizep)
{ {
char *buf; char *buf;
size_t size; size_t size, rd_size, total;
int fd; off_t to_read;
off_t len;
struct stat st; struct stat st;
fd = open(filename, O_RDONLY); to_read = sizep ? *sizep : MAXINT(ssize_t); /* max to read */
if (fd < 0)
return NULL;
st.st_size = 0; /* in case fstat fail, define to 0 */ /* Estimate file size */
st.st_size = 0; /* in case fstat fails, assume 0 */
fstat(fd, &st); fstat(fd, &st);
/* /proc/N/stat files report len 0 here */ /* /proc/N/stat files report st_size 0 */
/* In order to make such files readable, we add small const */ /* In order to make such files readable, we add small const */
len = st.st_size | 0x3ff; /* read only 1k on unseekable files */ size = (st.st_size | 0x3ff) + 1;
size = sizep ? *sizep : INT_MAX;
if (len < size) total = 0;
size = len; buf = NULL;
buf = xmalloc(size + 1); while (1) {
size = read_close(fd, buf, size); if (to_read < size)
if ((ssize_t)size < 0) { size = to_read;
free(buf); buf = xrealloc(buf, total + size + 1);
return NULL; rd_size = full_read(fd, buf + total, size);
if ((ssize_t)rd_size < 0) { /* error */
free(buf);
return NULL;
}
total += rd_size;
if (rd_size < size) /* EOF */
break;
to_read -= rd_size;
if (to_read <= 0)
break;
/* grow by 1/8, but in [1k..64k] bounds */
size = ((total / 8) | 0x3ff) + 1;
if (size > 64*1024)
size = 64*1024;
} }
xrealloc(buf, size + 1); xrealloc(buf, total + 1);
buf[size] = '\0'; buf[total] = '\0';
if (sizep) if (sizep)
*sizep = size; *sizep = total;
return buf; return buf;
} }
@ -284,6 +297,22 @@ void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep)
} }
#endif #endif
// Read (potentially big) files in one go. File size is estimated
// by stat.
void* FAST_FUNC xmalloc_open_read_close(const char *filename, size_t *sizep)
{
char *buf;
int fd;
fd = open(filename, O_RDONLY);
if (fd < 0)
return NULL;
buf = xmalloc_read(fd, sizep);
close(fd);
return buf;
}
void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *sizep) void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *sizep)
{ {
void *buf = xmalloc_open_read_close(filename, sizep); void *buf = xmalloc_open_read_close(filename, sizep);
@ -291,52 +320,3 @@ void* FAST_FUNC xmalloc_xopen_read_close(const char *filename, size_t *sizep)
bb_perror_msg_and_die("can't read '%s'", filename); bb_perror_msg_and_die("can't read '%s'", filename);
return buf; return buf;
} }
/* libbb candidate */
#if 0
static void *xmalloc_read(int fd, size_t *sizep)
{
char *buf;
size_t size, rd_size, total;
off_t to_read;
struct stat st;
to_read = sizep ? *sizep : INT_MAX; /* max to read */
/* Estimate file size */
st.st_size = 0; /* in case fstat fails, assume 0 */
fstat(fd, &st);
/* /proc/N/stat files report st_size 0 */
/* In order to make such files readable, we add small const */
size = (st.st_size | 0x3ff) + 1;
total = 0;
buf = NULL;
while (1) {
if (to_read < size)
size = to_read;
buf = xrealloc(buf, total + size + 1);
rd_size = full_read(fd, buf + total, size);
if ((ssize_t)rd_size < 0) { /* error */
free(buf);
return NULL;
}
total += rd_size;
if (rd_size < size) /* EOF */
break;
to_read -= rd_size;
if (to_read <= 0)
break;
/* grow by 1/8, but in [1k..64k] bounds */
size = ((total / 8) | 0x3ff) + 1;
if (size > 64*1024)
size = 64*1024;
}
xrealloc(buf, total + 1);
buf[total] = '\0';
if (sizep)
*sizep = total;
return buf;
}
#endif

View File

@ -31,7 +31,7 @@ config MODPROBE_SMALL
than "non-small" modutils. than "non-small" modutils.
config FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE config FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE
bool "module options on cmdline" bool "Accept module options on modprobe command line"
default n default n
depends on MODPROBE_SMALL depends on MODPROBE_SMALL
help help

View File

@ -14,54 +14,6 @@
#include <sys/utsname.h> /* uname() */ #include <sys/utsname.h> /* uname() */
#include <fnmatch.h> #include <fnmatch.h>
/* libbb candidate */
static void *xmalloc_read(int fd, size_t *sizep)
{
char *buf;
size_t size, rd_size, total;
off_t to_read;
struct stat st;
to_read = sizep ? *sizep : INT_MAX; /* max to read */
/* Estimate file size */
st.st_size = 0; /* in case fstat fails, assume 0 */
fstat(fd, &st);
/* /proc/N/stat files report st_size 0 */
/* In order to make such files readable, we add small const */
size = (st.st_size | 0x3ff) + 1;
total = 0;
buf = NULL;
while (1) {
if (to_read < size)
size = to_read;
buf = xrealloc(buf, total + size + 1);
rd_size = full_read(fd, buf + total, size);
if ((ssize_t)rd_size < 0) { /* error */
free(buf);
return NULL;
}
total += rd_size;
if (rd_size < size) /* EOF */
break;
to_read -= rd_size;
if (to_read <= 0)
break;
/* grow by 1/8, but in [1k..64k] bounds */
size = ((total / 8) | 0x3ff) + 1;
if (size > 64*1024)
size = 64*1024;
}
xrealloc(buf, total + 1);
buf[total] = '\0';
if (sizep)
*sizep = total;
return buf;
}
#define dbg1_error_msg(...) ((void)0) #define dbg1_error_msg(...) ((void)0)
#define dbg2_error_msg(...) ((void)0) #define dbg2_error_msg(...) ((void)0)
//#define dbg1_error_msg(...) bb_error_msg(__VA_ARGS__) //#define dbg1_error_msg(...) bb_error_msg(__VA_ARGS__)