svn commit: trunk/busybox: coreutils include libbb modutils

vda at busybox.net vda at busybox.net
Wed Jul 9 09:50:34 UTC 2008


Author: vda
Date: 2008-07-09 02:50:33 -0700 (Wed, 09 Jul 2008)
New Revision: 22699

Log:
libbb: use improved xmalloc_read() from modprobe-small
who: fix compile breakage on some systems
modprobe-small: improve Config help text wording



Modified:
   trunk/busybox/coreutils/who.c
   trunk/busybox/include/libbb.h
   trunk/busybox/libbb/read.c
   trunk/busybox/modutils/Config.in
   trunk/busybox/modutils/modprobe-small.c


Changeset:
Modified: trunk/busybox/coreutils/who.c
===================================================================
--- trunk/busybox/coreutils/who.c	2008-07-09 07:14:57 UTC (rev 22698)
+++ trunk/busybox/coreutils/who.c	2008-07-09 09:50:33 UTC (rev 22699)
@@ -56,16 +56,20 @@
 	printf("USER       TTY      IDLE      TIME            HOST\n");
 	while ((ut = getutent()) != NULL) {
 		if (ut->ut_user[0] && (opt || ut->ut_type == USER_PROCESS)) {
+			time_t tmp;
 			/* ut->ut_line is device name of tty - "/dev/" */
 			name = concat_path_file("/dev", ut->ut_line);
 			str6[0] = '?';
 			str6[1] = '\0';
 			if (stat(name, &st) == 0)
 				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 */
 			printf("%-10s %-8s %-9s %-15.15s %s\n",
 					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)
 				free(name);
 		}

Modified: trunk/busybox/include/libbb.h
===================================================================
--- trunk/busybox/include/libbb.h	2008-07-09 07:14:57 UTC (rev 22698)
+++ trunk/busybox/include/libbb.h	2008-07-09 09:50:33 UTC (rev 22699)
@@ -589,6 +589,7 @@
 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 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 */
 extern void *xmalloc_open_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC;
 /* Never returns NULL */

Modified: trunk/busybox/libbb/read.c
===================================================================
--- trunk/busybox/libbb/read.c	2008-07-09 07:14:57 UTC (rev 22698)
+++ trunk/busybox/libbb/read.c	2008-07-09 09:50:33 UTC (rev 22699)
@@ -106,7 +106,7 @@
 	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)
 {
 	if (count) {
@@ -116,7 +116,7 @@
 	}
 }
 
-// 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)
 {
 	char tmp;
@@ -124,7 +124,7 @@
 	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 *p;
@@ -140,9 +140,9 @@
 	if (p) {
 		off_t offset;
 		*p++ = '\0';
-		// avoid incorrect (unsigned) widening
+		/* avoid incorrect (unsigned) widening */
 		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)
 			return NULL;
 	}
@@ -203,39 +203,52 @@
 	return read_close(fd, buf, size);
 }
 
+
 // 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)
+// by stat. Extra '\0' byte is appended.
+void* FAST_FUNC xmalloc_read(int fd, size_t *sizep)
 {
 	char *buf;
-	size_t size;
-	int fd;
-	off_t len;
+	size_t size, rd_size, total;
+	off_t to_read;
 	struct stat st;
 
-	fd = open(filename, O_RDONLY);
-	if (fd < 0)
-		return NULL;
+	to_read = sizep ? *sizep : MAXINT(ssize_t); /* max to read */
 
-	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);
-	/* /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 */
-	len = st.st_size | 0x3ff; /* read only 1k on unseekable files */
-	size = sizep ? *sizep : INT_MAX;
-	if (len < size)
-		size = len;
-	buf = xmalloc(size + 1);
-	size = read_close(fd, buf, size);
-	if ((ssize_t)size < 0) {
-		free(buf);
-		return NULL;
+	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, size + 1);
-	buf[size] = '\0';
+	xrealloc(buf, total + 1);
+	buf[total] = '\0';
 
 	if (sizep)
-		*sizep = size;
+		*sizep = total;
 	return buf;
 }
 
@@ -284,6 +297,22 @@
 }
 #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 *buf = xmalloc_open_read_close(filename, sizep);
@@ -291,52 +320,3 @@
 		bb_perror_msg_and_die("can't read '%s'", filename);
 	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

Modified: trunk/busybox/modutils/Config.in
===================================================================
--- trunk/busybox/modutils/Config.in	2008-07-09 07:14:57 UTC (rev 22698)
+++ trunk/busybox/modutils/Config.in	2008-07-09 09:50:33 UTC (rev 22699)
@@ -31,7 +31,7 @@
 	  than "non-small" modutils.
 
 config FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE
-	bool "module options on cmdline"
+	bool "Accept module options on modprobe command line"
 	default n
 	depends on MODPROBE_SMALL
 	help

Modified: trunk/busybox/modutils/modprobe-small.c
===================================================================
--- trunk/busybox/modutils/modprobe-small.c	2008-07-09 07:14:57 UTC (rev 22698)
+++ trunk/busybox/modutils/modprobe-small.c	2008-07-09 09:50:33 UTC (rev 22699)
@@ -14,54 +14,6 @@
 #include <sys/utsname.h> /* uname() */
 #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 dbg2_error_msg(...) ((void)0)
 //#define dbg1_error_msg(...) bb_error_msg(__VA_ARGS__)




More information about the busybox-cvs mailing list