diff options
author | Christian Neukirchen <chneukirchen@gmail.com> | 2015-08-02 18:23:50 +0200 |
---|---|---|
committer | Christian Neukirchen <chneukirchen@gmail.com> | 2015-08-02 18:23:50 +0200 |
commit | 2d7dc7eb781e06bb84be51225f2256b4cd2dd4fa (patch) | |
tree | b7ccf21328489f6cdf649b0c1598297460811c84 /nq.c | |
parent | 39f9d9462b4edcbdfde118055e73fe2a15bad458 (diff) | |
download | nq-2d7dc7eb781e06bb84be51225f2256b4cd2dd4fa.tar.gz nq-2d7dc7eb781e06bb84be51225f2256b4cd2dd4fa.tar.xz nq-2d7dc7eb781e06bb84be51225f2256b4cd2dd4fa.zip |
wait for arguments of -w
Diffstat (limited to 'nq.c')
-rw-r--r-- | nq.c | 72 |
1 files changed, 49 insertions, 23 deletions
diff --git a/nq.c b/nq.c index 6783b88..6ed6b16 100644 --- a/nq.c +++ b/nq.c @@ -1,5 +1,5 @@ /* - * nq [-w | CMD...] - run CMD... in background and in order, saving output + * 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 @@ -93,9 +93,9 @@ main(int argc, char *argv[]) } } - if (argc <= 1 || (wflag && argc != 2)) { + if (argc <= 1) { usage: - swrite(2, "usage: nq [-w | CMD...]\n"); + swrite(2, "usage: nq [-w ... | CMD...]\n"); exit(1); } @@ -186,44 +186,70 @@ usage: perror("flock"); exit(222); } - + /* drop leading '.' */ renameat(dirfd, lockfile, dirfd, lockfile+1); write_execline(lockfd, argc, argv); wait: - dir = fdopendir(dirfd); - if (!dir) { - perror("fdopendir"); - exit(111); - } + if (wflag && argc - optind > 0) { + /* wait for files passed as command line arguments. */ -again: - while ((ent = readdir(dir))) { - if (ent->d_name[0] == ',' && - strcmp(ent->d_name, lockfile+1) < 0) { + int i; + for (i = optind; i < argc; i++) { int fd; - fd = openat(dirfd, ent->d_name, O_RDWR); + if (strchr(argv[i], '/')) + fd = open(argv[i], O_RDWR); + else + fd = openat(dirfd, argv[i], O_RDWR); if (fd < 0) continue; - + if (flock(fd, LOCK_EX | LOCK_NB) == -1 && errno == EWOULDBLOCK) { flock(fd, LOCK_EX); /* sit it out. */ - - close(fd); - rewinddir(dir); - goto again; } - + fchmod(fd, 0600); close(fd); } - } + } else { +again: + dir = fdopendir(dirfd); + if (!dir) { + perror("fdopendir"); + exit(111); + } - closedir(dir); /* closes dirfd too. */ + while ((ent = readdir(dir))) { + /* wait for all ,* files. */ + + if (ent->d_name[0] == ',' && + strcmp(ent->d_name, lockfile+1) < 0) { + int fd; + + fd = openat(dirfd, ent->d_name, O_RDWR); + if (fd < 0) + continue; + + if (flock(fd, LOCK_EX | LOCK_NB) == -1 && + errno == EWOULDBLOCK) { + flock(fd, LOCK_EX); /* sit it out. */ + + close(fd); + rewinddir(dir); + goto again; + } + + fchmod(fd, 0600); + close(fd); + } + } + + closedir(dir); /* closes dirfd too. */ + } if (wflag) return 0; @@ -232,7 +258,7 @@ again: swrite(lockfd, "\n\n"); fchmod(lockfd, 0700); - + dup2(lockfd, 2); dup2(lockfd, 1); close(lockfd); |