about summary refs log tree commit diff
diff options
context:
space:
mode:
authorChristian Neukirchen <chneukirchen@gmail.com>2015-08-09 19:52:32 +0200
committerChristian Neukirchen <chneukirchen@gmail.com>2015-08-09 19:52:32 +0200
commit3d5eb4abb3bb59ef157afcdeea65f7194668f8ef (patch)
treec573ee2daeb9781f4ddf3bebcc94b6d3b1e49f6e
parentfb6b948958465f3166da576f8161c48427dcdda7 (diff)
downloadnq-3d5eb4abb3bb59ef157afcdeea65f7194668f8ef.tar.gz
nq-3d5eb4abb3bb59ef157afcdeea65f7194668f8ef.tar.xz
nq-3d5eb4abb3bb59ef157afcdeea65f7194668f8ef.zip
fq: expand and sort $NDIR/,* ourselves
-rw-r--r--fq.c56
1 files changed, 45 insertions, 11 deletions
diff --git a/fq.c b/fq.c
index 27d4b33..f603d00 100644
--- a/fq.c
+++ b/fq.c
@@ -11,6 +11,7 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 
+#include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <stdlib.h>
@@ -30,7 +31,7 @@ char ibuf[8192];
 
 char buf[8192];
 
-int
+static int
 islocked(int fd)
 {
 	if (flock(fd, LOCK_EX|LOCK_NB) == -1) {
@@ -41,14 +42,23 @@ islocked(int fd)
 	}
 }
 
+static int
+alphabetic(const void* a, const void* b)
+{
+	return strcmp(*(char**)a, *(char **)b);
+}
+
 int
 main(int argc, char *argv[])
 {
-	int i, fd;
+	int i, fd, dirfd;
 	off_t off, loff;
 	ssize_t rd;
 	int didsth = 0, seen_nl = 0;
 	int opt = 0, aflag = 0, qflag = 0;
+	DIR *dir;
+	struct dirent *d;
+	char *path;
 
 #ifdef USE_INOTIFY
 	int ifd, wd;
@@ -69,14 +79,38 @@ main(int argc, char *argv[])
 		}
 	}
 
-	if (optind == argc) {
-		/* little better than glob(3)... */
-		execl("/bin/sh", "sh", "-c",
-		    qflag ?
-		    "exec fq -q ${NQDIR:+$NQDIR/},*" :
-		    "exec fq ${NQDIR:+$NQDIR/},*",
-		    (char *) 0);
+	path = getenv("NQDIR");
+	if (!path)
+		path = ".";
+	dirfd = open(path, O_RDONLY);
+	if (dirfd < 0)
 		exit(111);
+
+	if (optind == argc) {
+		int len = 0;
+		argc = 0;
+		argv = 0;
+		optind = 0;
+
+		dir = fdopendir(dirfd);
+		if (!dir)
+			exit(111);
+
+		while ((d = readdir(dir))) {
+			if (d->d_name[0] != ',')
+				continue;
+			if (argc >= len) {
+				len = 2*len + 1;
+				argv = realloc(argv, len * sizeof (char *));
+				if (!argv)
+					break;
+			}
+			if (!(argv[argc] = strdup(d->d_name)))
+				break;
+			argc++;
+		}
+
+		qsort(argv, argc, sizeof (char *), alphabetic);
 	}
 
 #ifdef USE_INOTIFY
@@ -89,13 +123,13 @@ main(int argc, char *argv[])
 		loff = 0;
 		seen_nl = 0;
 
-		fd = open(argv[i], O_RDONLY);
+		fd = openat(dirfd, argv[i], O_RDONLY);
 		if (fd < 0)
 			continue;
 
 		/* skip not running jobs, unless we did not output anything yet
 		 * and are at the last argument.  */
-		if (!aflag && !islocked(fd) && (didsth || i != args_cnt - 1))
+		if (!aflag && !islocked(fd) && (didsth || i != argc - 1))
 			continue;
 
 		write(1, "==> ", 4);