svn commit: trunk/busybox/util-linux

landley at busybox.net landley at busybox.net
Mon May 8 02:53:24 UTC 2006


Author: landley
Date: 2006-05-07 19:53:23 -0700 (Sun, 07 May 2006)
New Revision: 15030

Log:
Shrink the code about 50 bytes, allocate less run-time memory, and add a
comment that null terminating the string we sscanf() shouldn't be required
since the kernel adds \n to the end of it and sscanf will stop there.


Modified:
   trunk/busybox/util-linux/mdev.c


Changeset:
Modified: trunk/busybox/util-linux/mdev.c
===================================================================
--- trunk/busybox/util-linux/mdev.c	2006-05-08 02:22:24 UTC (rev 15029)
+++ trunk/busybox/util-linux/mdev.c	2006-05-08 02:53:23 UTC (rev 15030)
@@ -38,23 +38,25 @@
 	int mode = 0660;
 	uid_t uid = 0;
 	gid_t gid = 0;
+	char *temp = path + strlen(path);
 
-	RESERVE_CONFIG_BUFFER(temp, PATH_MAX);
+	/* Try to read major/minor string.  Note that the kernel puts \n after
+	 * the data, so we don't need to worry about null terminating the string
+	 * because sscanf() will stop at the first nondigit, which \n is.  We
+	 * also depend on path having writeable space after it. */
 
-	/* Try to read major/minor string */
-
-	snprintf(temp, PATH_MAX, "%s/dev", path);
-	fd = open(temp, O_RDONLY);
-	len = read(fd, temp, PATH_MAX-1);
+	strcat(path, "/dev");
+	fd = open(path, O_RDONLY);
+	len = read(fd, temp + 1, 64);
+	*temp++ = 0;
 	close(fd);
-	if (len < 1) goto end;
+	if (len < 1) return;
 
 	/* Determine device name, type, major and minor */
 
 	device_name = strrchr(path, '/') + 1;
-	type = strncmp(path+5, "block/", 6) ? S_IFCHR : S_IFBLK;
-	if (sscanf(temp, "%d:%d", &major, &minor) != 2)
-		goto end;
+	type = path[5]=='c' ? S_IFCHR : S_IFBLK;
+	if (sscanf(temp, "%d:%d", &major, &minor) != 2) return;
 
 	/* If we have a config file, look up permissions for this device */
 
@@ -167,18 +169,14 @@
 		}
 	}
 
-	sprintf(temp, "%s/%s", DEV_PATH, device_name);
 	umask(0);
-	if (mknod(temp, mode | type, makedev(major, minor)) && errno != EEXIST)
-		bb_perror_msg_and_die("mknod %s failed", temp);
+	if (mknod(device_name, mode | type, makedev(major, minor)) && errno != EEXIST)
+		bb_perror_msg_and_die("mknod %s failed", device_name);
 
 	if (major==root_major && minor==root_minor)
-		symlink(temp,DEV_PATH "/root");
+		symlink(device_name, "root");
 	
-	if (ENABLE_FEATURE_MDEV_CONF) chown(temp,uid,gid);
-
-end:
-	RELEASE_CONFIG_BUFFER(temp);
+	if (ENABLE_FEATURE_MDEV_CONF) chown(device_name, uid, gid);
 }
 
 /* Recursive search of /sys/block or /sys/class.  path must be a writeable
@@ -220,6 +218,8 @@
 	char *env_path;
 	RESERVE_CONFIG_BUFFER(temp,PATH_MAX);
 
+	bb_xchdir(DEV_PATH);
+
 	/* Scan */
 
 	if (argc == 2 && !strcmp(argv[1],"-s")) {
@@ -245,8 +245,7 @@
 			sprintf(temp, "/sys%s", env_path);
 			make_device(temp);
 		} else if (!strcmp(action, "remove")) {
-			sprintf(temp, "%s/%s", DEV_PATH, strrchr(env_path, '/') + 1);
-			unlink(temp);
+			unlink(strrchr(env_path, '/') + 1);
 		}
 	}
 




More information about the busybox-cvs mailing list