[BusyBox] [PATCH] id minor bugfix and code clean up for my_getpwuid and my_getgrgid

Tito farmatito at tiscali.it
Mon Sep 6 23:43:04 UTC 2004


On Saturday 04 September 2004 OO:46, Tito wrote:
> Hi, to all
> This patch fixes a corner case in which id applet doesn't produce the same output
> as GNU id:
> 
> user: pippo, uid 502, gid 504,  default group pippo:504 in /etc/group deleted
> 
> ./busybox id -g pippo
> id: unknown gid 504
> GNU:
> id -g pippo
> 504
> 
> The patch also cleans up code formatting and removes
> some not needed includes.
> 
Hi Erik,
Hi to all,
this patch substitutes integrally the previous.
It fixes the previous bug and another corner case
# chown 777.777 busybox
# chmod 6755 busybox
# ./busybox id -gn
 not showing
id: unknown gid 777

I've to point out that there is also a pending patch
by vodz last_patch136_2 which achieves the same result
with a space saving of ~ 8 bytes for id.
Vodz patch is a little more intrusive as it rewrites more code
but is cool stuff.

So Erik, its up to you to decide what to do.........
Thanks in advance .
Ciao,
Tito

P.S: special thanks to vodz for his patience to dispute with me ;-)
-------------- next part --------------
--- libbb/my_getpwuid_orig.c	2004-09-02 22:21:40.000000000 +0000
+++ libbb/my_getpwuid.c	2004-09-03 23:19:56.306987136 +0000
@@ -38,7 +38,6 @@
 #include <assert.h>
 #include "libbb.h"
 #include "pwd_.h"
-#include "grp_.h"
 
 
 
@@ -58,8 +57,7 @@
 		}
 		return NULL;
 	} else {
-		if(bufsize > 0 )
-		{
+		if(bufsize > 0 ) {
 			assert(name != NULL);
 			return safe_strncpy(name, myuser->pw_name, bufsize);
 		}
--- libbb/my_getgrgid_orig.c	2004-09-02 22:21:40.000000000 +0000
+++ libbb/my_getgrgid.c	2004-09-03 23:19:43.351956600 +0000
@@ -37,7 +37,6 @@
 #include <string.h>
 #include <assert.h>
 #include "libbb.h"
-#include "pwd_.h"
 #include "grp_.h"
 
 
@@ -57,8 +56,7 @@
 		} 
 		return NULL;
 	} else {
-		if(bufsize > 0)
-		{
+		if(bufsize > 0) {
 			assert(group != NULL);
 			return safe_strncpy(group, mygroup->gr_name, bufsize);
 		}
--- coreutils/id_cvs.c	2004-09-06 22:34:45.000000000 +0000
+++ coreutils/id.c	2004-09-06 22:58:06.753904576 +0000
@@ -44,11 +44,11 @@
 #define JUST_USER         4
 #define JUST_GROUP        8
 
-void printf_full(unsigned int id, char *arg, char prefix)
+void printf_full(unsigned int id, char *arg, char *prefix)
 {	
-	printf("%cid=%u",prefix, id);
+	printf("%sid=%u",prefix, id);
 	if(arg)
-		printf("(%s) ", arg);
+		printf("(%s)", arg);
 }
 
 extern int id_main(int argc, char **argv)
@@ -90,7 +90,7 @@
 		/* in this case PRINT_REAL is the same */ 
 	}
 	
-	user=my_getpwuid(NULL, uid, (flags & JUST_USER) ? -1 : 0);
+	user=my_getpwuid(NULL, uid, (flags & NAME_NOT_NUMBER) ? -1 : 0);
 
 	if(flags & JUST_USER)
 	{
@@ -99,12 +99,12 @@
 		goto PRINT; 
 	}
 	
-	group=my_getgrgid(NULL, gid, (flags & JUST_GROUP) ? -1 : 0);
+	group=my_getgrgid(NULL, gid, (flags & NAME_NOT_NUMBER) ? -1 : 0);
 
 	if(flags & JUST_GROUP) 
 	{
 PRINT:		
-		if(flags & NAME_NOT_NUMBER)
+		if((flags & NAME_NOT_NUMBER) && group)
 			puts(group);
 		else
 			printf ("%u\n", gid);
@@ -112,8 +112,8 @@
 	}
 	
 	/* Print full info like GNU id */
-	printf_full(uid, user, 'u');
-	printf_full(gid, group, 'g');
+	printf_full(uid, user, "u");
+	printf_full(gid, group, " g");
 #ifdef CONFIG_SELINUX
 	if(is_flask_enabled_flag)
 	{
@@ -123,10 +123,11 @@
 		context[0] = '\0';
 		if(security_sid_to_context(mysid, context, &len))
 			strcpy(context, "unknown");
-		printf("context=%s", context);
+		printf(" context=%s\n", context);
 	}
+#else
+	putchar('\n');
 #endif
-	puts("");
 	bb_fflush_stdout_and_exit((user && group) ? EXIT_SUCCESS : EXIT_FAILURE);
 }
 
--- coreutils/id_cvs.c	2004-09-06 22:34:45.000000000 +0000
+++ coreutils/id.c	2004-09-06 23:26:37.655807952 +0000
@@ -44,11 +44,11 @@
 #define JUST_USER         4
 #define JUST_GROUP        8
 
-void printf_full(unsigned int id, char *arg, char prefix)
+void printf_full(unsigned int id, char *arg, char *prefix)
 {	
-	printf("%cid=%u",prefix, id);
+	printf("%sid=%u",prefix, id);
 	if(arg)
-		printf("(%s) ", arg);
+		printf("(%s)", arg);
 }
 
 extern int id_main(int argc, char **argv)
@@ -80,9 +80,7 @@
 		gid = getgid();
 	}
 	
-	if(argv[optind])
-	{
-
+	if(argv[optind]) {
 		p=getpwnam(argv[optind]);
 		/* this is needed because it exits on failure */
 		uid = my_getpwnam(argv[optind]);
@@ -90,21 +88,19 @@
 		/* in this case PRINT_REAL is the same */ 
 	}
 	
-	user=my_getpwuid(NULL, uid, (flags & JUST_USER) ? -1 : 0);
+	user=my_getpwuid(NULL, uid, (flags & NAME_NOT_NUMBER) ? -1 : 0);
 
-	if(flags & JUST_USER)
-	{
+	if(flags & JUST_USER) {
 		gid=uid;
 		group=user;
 		goto PRINT; 
 	}
 	
-	group=my_getgrgid(NULL, gid, (flags & JUST_GROUP) ? -1 : 0);
+	group=my_getgrgid(NULL, gid, (flags & NAME_NOT_NUMBER) ? -1 : 0);
 
-	if(flags & JUST_GROUP) 
-	{
+	if(flags & JUST_GROUP) {
 PRINT:		
-		if(flags & NAME_NOT_NUMBER)
+		if((flags & NAME_NOT_NUMBER) && group)
 			puts(group);
 		else
 			printf ("%u\n", gid);
@@ -112,21 +108,21 @@
 	}
 	
 	/* Print full info like GNU id */
-	printf_full(uid, user, 'u');
-	printf_full(gid, group, 'g');
+	printf_full(uid, user, "u");
+	printf_full(gid, group, " g");
 #ifdef CONFIG_SELINUX
-	if(is_flask_enabled_flag)
-	{
+	if(is_flask_enabled_flag) {
 		security_id_t mysid = getsecsid();
 		char context[80];
 		int len = sizeof(context);
 		context[0] = '\0';
 		if(security_sid_to_context(mysid, context, &len))
 			strcpy(context, "unknown");
-		printf("context=%s", context);
+		printf(" context=%s\n", context);
 	}
+#else
+	putchar('\n');
 #endif
-	puts("");
 	bb_fflush_stdout_and_exit((user && group) ? EXIT_SUCCESS : EXIT_FAILURE);
 }
 


More information about the busybox mailing list