Introduction
Introduction Statistics Contact Development Disclaimer Help
Factor out path parsing - smdev - suckless mdev
git clone git://git.suckless.org/smdev
Log
Files
Refs
README
LICENSE
---
commit 4edeed89d479ba3a582913a577fbe315d53464fc
parent 47d91161ece9e1c8e2dbc976c4903871858437e1
Author: sin <[email protected]>
Date: Fri, 23 Aug 2013 10:33:16 +0100
Factor out path parsing
Diffstat:
M smdev.c | 91 +++++++++++++++++------------…
1 file changed, 49 insertions(+), 42 deletions(-)
---
diff --git a/smdev.c b/smdev.c
@@ -40,6 +40,8 @@ static struct pregentry {
static int dohotplug(void);
static int matchrule(int ruleidx, char *devname);
static void runrule(struct Event *ev, struct Rule *Rule);
+static void parsepath(struct Rule *Rule, char *devpath, size_t sz,
+ char *devname);
static int createdev(struct Event *ev);
static void populatedev(const char *path);
@@ -168,6 +170,49 @@ runrule(struct Event *ev, struct Rule *Rule)
}
}
+static void
+parsepath(struct Rule *Rule, char *devpath, size_t sz,
+ char *devname)
+{
+ char buf[BUFSIZ], *p;
+ char *dirc, *basec;
+
+ if (Rule->path[0] != '=' && Rule->path[0] != '>')
+ eprintf("Invalid path '%s'\n", Rule->path);
+
+ p = strchr(&Rule->path[1], '/');
+ if (p) {
+ if (Rule->path[strlen(Rule->path) - 1] == '/') {
+ snprintf(buf, sizeof(buf), "/dev/%s",
+ &Rule->path[1]);
+ snprintf(devpath, sz, "/dev/%s%s",
+ &Rule->path[1], devname);
+ } else {
+ dirc = strdup(&Rule->path[1]);
+ if (!dirc)
+ eprintf("strdup:");
+ snprintf(buf, sizeof(buf), "/dev/%s", dirname(dirc));
+ free(dirc);
+
+ basec = strdup(&Rule->path[1]);
+ if (!basec)
+ eprintf("strdup:");
+ strlcpy(devname, basename(basec), sizeof(devname));
+ free(basec);
+
+ snprintf(devpath, sz, "%s/%s",
+ buf, devname);
+ }
+ umask(022);
+ if (mkpath(buf, 0755) < 0)
+ eprintf("mkdir %s:", buf);
+ umask(0);
+ } else {
+ strlcpy(devname, &Rule->path[1], sizeof(devname));
+ snprintf(devpath, sz, "/dev/%s", devname);
+ }
+}
+
static int
createdev(struct Event *ev)
{
@@ -176,10 +221,9 @@ createdev(struct Event *ev)
struct group *gr;
char devpath[PATH_MAX];
char devname[PATH_MAX];
- char *dirc, *basec;
char buf[BUFSIZ];
int type;
- int i, j;
+ int i;
snprintf(buf, sizeof(buf), "%d:%d", ev->maj, ev->min);
type = devtype(buf);
@@ -194,46 +238,9 @@ createdev(struct Event *ev)
if (matchrule(i, devname) < 0)
continue;
- if (Rule->path) {
- if (Rule->path[0] != '=' && Rule->path[0] != '>')
- eprintf("Invalid path '%s'\n", Rule->path);
-
- for (j = 1; Rule->path[j]; j++) {
- if (Rule->path[j] != '/')
- continue;
- if (Rule->path[strlen(Rule->path) - 1] == '/')…
- snprintf(buf, sizeof(buf), "/dev/%s",
- &Rule->path[1]);
- snprintf(devpath, sizeof(devpath), "/d…
- &Rule->path[1], devname);
- } else {
- dirc = strdup(&Rule->path[1]);
- if (!dirc)
- eprintf("strdup:");
- snprintf(buf, sizeof(buf), "/dev/%s", …
- free(dirc);
-
- basec = strdup(&Rule->path[1]);
- if (!basec)
- eprintf("strdup:");
- strlcpy(devname, basename(basec), size…
- free(basec);
-
- snprintf(devpath, sizeof(devpath), "%s…
- buf, devname);
- }
- umask(022);
- if (mkpath(buf, 0755) < 0)
- eprintf("mkdir %s:", buf);
- umask(0);
- break;
- }
-
- if (!Rule->path[j]) {
- strlcpy(devname, &Rule->path[1], sizeof(devnam…
- snprintf(devpath, sizeof(devpath), "/dev/%s", …
- }
- }
+ if (Rule->path)
+ parsepath(Rule, devpath, sizeof(devpath),
+ devname);
/* Create the actual dev nodes */
if (mknod(devpath, Rules[i].mode | type,
You are viewing proxied material from suckless.org. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.