commit b495d15669bc4727054c935d1b938355a5100394
parent 1953099f5f153e6e6bb19db54bd334bc25e6f655
Author: Christoph Lohmann <20h@r-36.net>
Date: Sun, 12 Sep 2021 21:22:05 +0200
Fix argument handling in nltrigger.
* Make arguments more flexible.
Diffstat:
1 file changed, 9 insertions(+), 8 deletions(-)
diff --git a/nltrigger.c b/nltrigger.c
@@ -15,8 +15,6 @@
#include "arg.h"
char *argv0;
-static char *action = "add";
-static useconds_t delay = 1000;
static void venprintf(int, const char *, va_list);
void
@@ -83,7 +81,8 @@ agetcwd(void)
}
void
-recurse(const char *path, void (*fn)(const char *))
+recurse(char *path, char *action, useconds_t delay,
+ void (*fn)(char *, char *, useconds_t))
{
char *cwd;
struct dirent *d;
@@ -102,7 +101,7 @@ recurse(const char *path, void (*fn)(const char *))
while((d = readdir(dp))) {
if(strcmp(d->d_name, ".") && strcmp(d->d_name, ".."))
- fn(d->d_name);
+ fn(d->d_name, action, delay);
}
closedir(dp);
@@ -113,11 +112,11 @@ recurse(const char *path, void (*fn)(const char *))
}
void
-trigger(const char *path)
+trigger(char *path, char *action, useconds_t delay)
{
int fd;
- recurse(path, trigger);
+ recurse(path, action, delay, trigger);
if (strstr(path, "uevent")) {
if ((fd = open("uevent", O_WRONLY | O_CLOEXEC)) != -1) {
dprintf(fd, "%s\n", action);
@@ -135,7 +134,8 @@ void usage(void)
int
main(int argc, char *argv[])
{
- char *path = NULL;
+ char *path = NULL, *action = "add";
+ static useconds_t delay = 1000;
ARGBEGIN {
default:
@@ -146,6 +146,7 @@ main(int argc, char *argv[])
usage();
path = argv[0];
+ argc--; argv++;
if (argc > 0) {
action = argv[0];
argc--; argv++;
@@ -153,7 +154,7 @@ main(int argc, char *argv[])
if (argc > 0)
delay = atol(argv[0]);
- trigger(path);
+ trigger(path, action, delay);
return 0;
}