chiark / gitweb /
path_id: add ID_PATH_TAG= to be used in udev tags
authorKay Sievers <kay.sievers@vrfy.org>
Thu, 7 Jul 2011 17:59:03 +0000 (19:59 +0200)
committerKay Sievers <kay.sievers@vrfy.org>
Thu, 7 Jul 2011 17:59:03 +0000 (19:59 +0200)
extras/path_id/path_id.c

index 9e77b8a..98f0e40 100644 (file)
@@ -508,7 +508,37 @@ int main(int argc, char **argv)
        }
 out:
        if (path != NULL) {
+               char tag[UTIL_NAME_SIZE];
+               size_t i;
+               const char *p;
+
+               /* compose valid udev tag name */
+               for (p = path, i = 0; *p; p++) {
+                       if ((*p >= '0' && *p <= '9') ||
+                           (*p >= 'A' && *p <= 'Z') ||
+                           (*p >= 'a' && *p <= 'z') ||
+                           *p == '-') {
+                               tag[i++] = *p;
+                               continue;
+                       }
+
+                       /* skip all leading '_' */
+                       if (i == 0)
+                               continue;
+
+                       /* avoid second '_' */
+                       if (tag[i-1] == '_')
+                               continue;
+
+                       tag[i++] = '_';
+               }
+               /* strip trailing '_' */
+               while (i > 0 && tag[i-1] == '_')
+                       i--;
+               tag[i] = '\0';
+
                printf("ID_PATH=%s\n", path);
+               printf("ID_PATH_TAG=%s\n", tag);
                free(path);
                rc = EXIT_SUCCESS;
        }