summary refs log tree commit diff
diff options
context:
space:
mode:
authorChristian Neukirchen <chneukirchen@gmail.com>2015-07-31 17:31:17 +0200
committerChristian Neukirchen <chneukirchen@gmail.com>2015-07-31 17:31:17 +0200
commit76cc08db1e154a355c3996a993939015e5605b02 (patch)
tree5f31ac22e37af4996a0e437a90fc06e588d4f8cc
parent5e72d9e76f90bd1aeef6f08507a3590df1cbacca (diff)
downloadnq-76cc08db1e154a355c3996a993939015e5605b02.tar.gz
nq-76cc08db1e154a355c3996a993939015e5605b02.tar.xz
nq-76cc08db1e154a355c3996a993939015e5605b02.zip
add -w
-rw-r--r--nq.c33
1 files changed, 28 insertions, 5 deletions
diff --git a/nq.c b/nq.c
index 83909c8..13a1e5d 100644
--- a/nq.c
+++ b/nq.c
@@ -1,9 +1,10 @@
 /*
- * nq CMD... - run CMD... in background and in order, saving output to ,* files
+ * nq [-w] CMD... - run CMD... in background and in order, saving output
+ * -w  wait for all jobs queued so far to finish
  *
  * - requires POSIX.1-2008
  * - enforcing order works like this:
- *   - every job has a flock(2)ed file ala ",TIMESTAMP.PID"
+ *   - every job has a flock(2)ed output file ala ",TIMESTAMP.PID"
  *   - every job starts only after all earlier flock(2)ed files finished
  *   - the lock is released when job terminates
  *   - no sub-second file system time stamps are required, jobs are started
@@ -69,19 +70,32 @@ write_execline(int fd, int argc, char *argv[])
 int
 main(int argc, char *argv[])
 {
-	int dirfd, lockfd;
+	int dirfd = 0, lockfd = 0, opt = 0, wflag = 0;
 	int pipefd[2];
 	char lockfile[32];
 	pid_t child;
 	struct timeval started;
 	struct dirent *ent;
+	DIR *dir;
 
 	/* timestamp is milliseconds since epoch.  */
 	gettimeofday(&started, NULL);
 	int64_t ms = started.tv_sec*1000 + started.tv_usec/1000;
 
+	while ((opt = getopt(argc, argv, "+hw")) != -1) {
+		switch (opt) {
+		case 'w':
+			wflag = 1;
+			break;
+		case 'h':
+		default:
+			goto usage;
+		}
+	}
+
 	if (argc <= 1) {
-		swrite(2, "usage: nq CMD...\n");
+usage:
+		swrite(2, "usage: nq [-w] CMD...\n");
 		exit(1);
 	}
 
@@ -95,6 +109,11 @@ main(int argc, char *argv[])
 		exit(111);
 	}
 
+	if (wflag) {
+		sprintf(lockfile, ".,%011lx.%d", ms, getpid());
+		goto wait;
+	}
+
 	pipe(pipefd);
 
 	/* first fork, parent exits to run in background.  */
@@ -173,7 +192,8 @@ main(int argc, char *argv[])
 
 	write_execline(lockfd, argc, argv);
 
-	DIR *dir = fdopendir(dirfd);
+wait:
+	dir = fdopendir(dirfd);
 	if (!dir) {
 		perror("fdopendir");
 		exit(111);
@@ -205,6 +225,9 @@ again:
 
 	closedir(dir);		/* closes dirfd too.  */
 
+	if (wflag)
+		return 0;
+
 	/* ready to run.  */
 
 	swrite(lockfd, "\n\n");