linux-hotplug.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] cleanup mult field string handling
@ 2004-03-03  5:13 Kay Sievers
  2004-03-03 18:36 ` Greg KH
  0 siblings, 1 reply; 2+ messages in thread
From: Kay Sievers @ 2004-03-03  5:13 UTC (permalink / raw)
  To: linux-hotplug

[-- Attachment #1: Type: text/plain, Size: 747 bytes --]

Here I try to cleanup our various multifield iteration over the strings.
Inspired by our nice list.h we now have a macro to iterate over the string
and process the parts of it:
It makes the code more readable and we don't change the string while we
process it like the former strsep() does.

Example:

  foreach_strpart(dev->symlink, " ", pos, len) {
  	if (strncmp(&dev->symlink[pos], find_name, len) != 0)
  		continue;

  	...
  }

For the callout part selector %c{2} we separate now not only by space but
also newline and return characters, cause some programs may give multiline
values back. A possible RESULT match must contain wildcards for these
characters.

Also a bug in the recent udevinfo symlink query feature is fixed.


thanks,
Kay

[-- Attachment #2: 01-cleanup-multi-fields.patch --]
[-- Type: text/plain, Size: 7611 bytes --]

===== namedev.c 1.121 vs edited =====
--- 1.121/namedev.c	Tue Mar  2 01:02:05 2004
+++ edited/namedev.c	Wed Mar  3 05:00:21 2004
@@ -214,14 +214,12 @@
 			 struct sysfs_device *sysfs_device)
 {
 	char temp[NAME_SIZE];
-	char temp1[NAME_SIZE];
 	char *tail;
 	char *pos;
-	char *pos2;
-	char *pos3;
 	char *attr;
 	int len;
 	int i;
+	int spos, slen;
 	char c;
 	struct sysfs_attribute *tmpattr;
 
@@ -278,20 +276,17 @@
 			if (attr != NULL)
 				i = atoi(attr);
 			if (i > 0) {
-				strfieldcpy(temp1, udev->program_result);
-				pos2 = temp1;
-				while (i) {
+				foreach_strpart(udev->program_result, " \n\r", spos, slen) {
 					i--;
-					pos3 = strsep(&pos2, " ");
-					if (pos3 == NULL) {
-						dbg("requested part of result string not found");
+					if (i == 0)
 						break;
-					}
 				}
-				if (pos3) {
-					strnfieldcat(string, pos3, maxsize);
-					dbg("substitute part of result string '%s'", pos3);
+				if (i > 0) {
+					dbg("requested part of result string not found");
+					break;
 				}
+				strnfieldcat(string, udev->program_result + spos, slen+1);
+				dbg("substitute part of result string '%s'", pos);
 			} else {
 				strnfieldcat(string, udev->program_result, maxsize);
 				dbg("substitute result string '%s'", udev->program_result);
===== udev-add.c 1.57 vs edited =====
--- 1.57/udev-add.c	Tue Mar  2 04:05:03 2004
+++ edited/udev-add.c	Wed Mar  3 04:38:41 2004
@@ -186,16 +186,16 @@
 
 static int create_node(struct udevice *dev, int fake)
 {
-	char filename[255];
-	char linktarget[255];
-	char partitionname[255];
-	char *linkname;
-	char *symlinks;
+	char filename[NAME_SIZE];
+	char linkname[NAME_SIZE];
+	char linktarget[NAME_SIZE];
+	char partitionname[NAME_SIZE];
 	int retval = 0;
 	uid_t uid = 0;
 	gid_t gid = 0;
 	int i;
 	int tail;
+	int pos, len;
 
 	strfieldcpy(filename, udev_root);
 	strfieldcat(filename, dev->name);
@@ -279,47 +279,41 @@
 		selinux_add_node(filename);
 
 	/* create symlink if requested */
-	if (dev->symlink[0] != '\0') {
-		symlinks = dev->symlink;
-		while (1) {
-			linkname = strsep(&symlinks, " ");
-			if (linkname == NULL || linkname[0] == '\0')
-				break;
-
-			strfieldcpy(filename, udev_root);
-			strfieldcat(filename, linkname);
-			dbg("symlink '%s' to node '%s' requested", filename, dev->name);
-			if (!fake)
-				if (strrchr(linkname, '/'))
-					create_path(filename);
-
-			/* optimize relative link */
-			linktarget[0] = '\0';
-			i = 0;
-			tail = 0;
-			while ((dev->name[i] == linkname[i]) && dev->name[i]) {
-				if (dev->name[i] == '/')
-					tail = i+1;
-				i++;
-			}
-			while (linkname[i] != '\0') {
-				if (linkname[i] == '/')
-					strfieldcat(linktarget, "../");
-				i++;
-			}
+	foreach_strpart(dev->symlink, " ", pos, len) {
+		strnfieldcpy(linkname, dev->symlink + pos, len+1);
+		strfieldcpy(filename, udev_root);
+		strfieldcat(filename, linkname);
+		dbg("symlink '%s' to node '%s' requested", filename, dev->name);
+		if (!fake)
+			if (strrchr(linkname, '/'))
+				create_path(filename);
+
+		/* optimize relative link */
+		linktarget[0] = '\0';
+		i = 0;
+		tail = 0;
+		while ((dev->name[i] == linkname[i]) && dev->name[i]) {
+			if (dev->name[i] == '/')
+				tail = i+1;
+			i++;
+		}
+		while (linkname[i] != '\0') {
+			if (linkname[i] == '/')
+				strfieldcat(linktarget, "../");
+			i++;
+		}
 
-			strfieldcat(linktarget, &dev->name[tail]);
+		strfieldcat(linktarget, &dev->name[tail]);
 
-			if (!fake)
-				unlink_entry(filename);
+		if (!fake)
+			unlink_entry(filename);
 
-			dbg("symlink(%s, %s)", linktarget, filename);
-			if (!fake) {
-				retval = symlink(linktarget, filename);
-				if (retval != 0)
-					dbg("symlink(%s, %s) failed with error '%s'",
-					    linktarget, filename, strerror(errno));
-			}
+		dbg("symlink(%s, %s)", linktarget, filename);
+		if (!fake) {
+			retval = symlink(linktarget, filename);
+			if (retval != 0)
+				dbg("symlink(%s, %s) failed with error '%s'",
+				    linktarget, filename, strerror(errno));
 		}
 	}
 
===== udev-remove.c 1.22 vs edited =====
--- 1.22/udev-remove.c	Sat Feb 28 16:09:24 2004
+++ edited/udev-remove.c	Wed Mar  3 04:43:18 2004
@@ -67,12 +67,12 @@
 
 static int delete_node(struct udevice *dev)
 {
-	char filename[255];
-	char partitionname[255];
-	char *symlinks;
-	char *linkname;
+	char filename[NAME_SIZE];
+	char linkname[NAME_SIZE];
+	char partitionname[NAME_SIZE];
 	int retval;
 	int i;
+	int pos, len;
 
 	strfieldcpy(filename, udev_root);
 	strfieldcat(filename, dev->name);
@@ -101,28 +101,22 @@
 	if (strchr(dev->name, '/'))
 		delete_path(filename);
 
-	if (dev->symlink[0] != '\0') {
-		symlinks = dev->symlink;
-		while (1) {
-			linkname = strsep(&symlinks, " ");
-			if (linkname == NULL)
-				break;
-
-			strfieldcpy(filename, udev_root);
-			strfieldcat(filename, linkname);
-
-			dbg("unlinking symlink '%s'", filename);
-			retval = unlink(filename);
-			if (errno == ENOENT)
-				retval = 0;
-			if (retval) {
-				dbg("unlink(%s) failed with error '%s'",
-					filename, strerror(errno));
-				return retval;
-			}
-			if (strchr(dev->symlink, '/')) {
-				delete_path(filename);
-			}
+	foreach_strpart(dev->symlink, " ", pos, len) {
+		strnfieldcpy(linkname, dev->symlink + pos, len+1);
+		strfieldcpy(filename, udev_root);
+		strfieldcat(filename, linkname);
+
+		dbg("unlinking symlink '%s'", filename);
+		retval = unlink(filename);
+		if (errno == ENOENT)
+			retval = 0;
+		if (retval) {
+			dbg("unlink(%s) failed with error '%s'",
+				filename, strerror(errno));
+			return retval;
+		}
+		if (strchr(dev->symlink, '/')) {
+			delete_path(filename);
 		}
 	}
 
===== udev.h 1.48 vs edited =====
--- 1.48/udev.h	Sat Feb 28 03:41:28 2004
+++ edited/udev.h	Wed Mar  3 04:39:01 2004
@@ -97,6 +97,12 @@
 	snprintf((to) + strlen(to), maxsize - strlen(to)-1, "%u", i); \
 } while (0)
 
+#define foreach_strpart(str, separator, pos, len) \
+	for(pos = 0, len = strcspn(str, separator); \
+	    (pos) < strlen(str); \
+	    pos = pos + (len) + 1, len = strcspn((str) + pos, separator)) \
+		if (len > 0)
+
 static inline char *get_action(void)
 {
 	char *action;
===== udevdb.c 1.24 vs edited =====
--- 1.24/udevdb.c	Mon Mar  1 16:46:28 2004
+++ edited/udevdb.c	Wed Mar  3 05:33:03 2004
@@ -179,7 +179,8 @@
 
 static int find_device_by_name(char *path, struct udevice *dev)
 {
-	int l, i, j;
+	int pos, len;
+
 	if (strncmp(dev->name, find_name, sizeof(dev->name)) == 0) {
 		memcpy(find_dev, dev, sizeof(struct udevice));
 		strnfieldcpy(find_path, path, NAME_SIZE);
@@ -188,20 +189,18 @@
 		return 1;
 	}
 	/* look for matching symlink*/
-	l = strlen(dev->symlink);
-	if (!l)
-		return 0;
-	i = j = 0;
-	do {
-		j = strcspn(&dev->symlink[i], " ");
-		if (j && strncmp(&dev->symlink[i], find_name, j) == 0) {
-			memcpy(find_dev, dev, sizeof(struct udevice));
-			strnfieldcpy(find_path, path, NAME_SIZE);
-			find_found = 1;
-			return 1;
-		}
-		i = i + j + 1;
-	} while (i < l);
+	foreach_strpart(dev->symlink, " ", pos, len) {
+		if (strncmp(&dev->symlink[pos], find_name, len) != 0)
+			continue;
+
+		if (len != strlen(find_name))
+			continue;
+
+		memcpy(find_dev, dev, sizeof(struct udevice));
+		strnfieldcpy(find_path, path, NAME_SIZE);
+		find_found = 1;
+		return 1;
+	}
 	return 0;
 }
 
===== udevinfo.8 1.7 vs edited =====
--- 1.7/udevinfo.8	Mon Mar  1 16:47:08 2004
+++ edited/udevinfo.8	Wed Mar  3 05:37:59 2004
@@ -35,6 +35,8 @@
 .TP
 .BI \-n " name"
 Specify the name of the node or the symlink for the device to query.
+Partition names generated with the NAME{all_partitons} option can not be
+queried, the main device must be used instead.
 .TP
 .B \-a
 Print all

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] cleanup mult field string handling
  2004-03-03  5:13 [PATCH] cleanup mult field string handling Kay Sievers
@ 2004-03-03 18:36 ` Greg KH
  0 siblings, 0 replies; 2+ messages in thread
From: Greg KH @ 2004-03-03 18:36 UTC (permalink / raw)
  To: linux-hotplug

On Wed, Mar 03, 2004 at 06:13:38AM +0100, Kay Sievers wrote:
> Here I try to cleanup our various multifield iteration over the strings.
> Inspired by our nice list.h we now have a macro to iterate over the string
> and process the parts of it:
> It makes the code more readable and we don't change the string while we
> process it like the former strsep() does.
> 
> Example:
> 
>   foreach_strpart(dev->symlink, " ", pos, len) {
>   	if (strncmp(&dev->symlink[pos], find_name, len) != 0)
>   		continue;
> 
>   	...
>   }

Ah, much saner, very nice.

Applied, thanks.

greg k-h


-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now.
Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id\x1356&alloc_id438&op=click
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-03-03 18:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-03-03  5:13 [PATCH] cleanup mult field string handling Kay Sievers
2004-03-03 18:36 ` Greg KH

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).