From a45deb01daa2c2fb67361ea8ffce6b63ad020558 Mon Sep 17 00:00:00 2001 From: Christian Neukirchen Date: Fri, 31 Jul 2015 14:20:17 +0200 Subject: reformat --- nq.c | 238 +++++++++++++++++++++++++++++++++---------------------------------- 1 file changed, 119 insertions(+), 119 deletions(-) diff --git a/nq.c b/nq.c index ab2e1ee..7611395 100644 --- a/nq.c +++ b/nq.c @@ -1,9 +1,10 @@ /* -##% gcc -Wall -g -o $STEM $FILE + ##% gcc -Wall -g -o $STEM $FILE */ #include #include +#include #include #include @@ -19,13 +20,12 @@ static void quit(int sig) { - exit(0); + exit(0); } -#include /* -char * -timestamp() { + char * + timestamp() { struct timeval tv; gettimeofday(&tv, NULL); static char buf[10]; @@ -35,141 +35,141 @@ timestamp() { buf[9] = 0; int i; for (i = 8; i >= 0; i--) { - buf[i] = 'a' + (ms % 26); - ms /= 26; + buf[i] = 'a' + (ms % 26); + ms /= 26; } return buf; -} + } */ int main(int argc, char *argv[]) { - pid_t child; - int lockfd; - char lockfile[128]; - int dirfd; - - struct timeval tv; - gettimeofday(&tv, NULL); - int64_t ms = tv.tv_sec * 1000 + tv.tv_usec / 1000; - - char *path = getenv("NQDIR"); - if (!path) - path = "."; - - dirfd = open(path, O_RDONLY); - if (dirfd < 0) { - perror("dir open"); - exit(111); - } - - child = fork(); - if (child == -1) { - perror("fork"); - exit(111); - } - - if (child > 0) { - // background - signal(SIGINT, quit); - pause(); - exit(0); - } - - child = fork(); - if (child == -1) { - perror("fork"); - exit(111); - } - if (child > 0) { - int status; - sprintf(lockfile, ",%lx.%d", ms, child); - - puts(lockfile); - - // signal parent to exit - kill(getppid(), SIGINT); - - wait(&status); - int fd; - fd = openat(dirfd, lockfile, O_RDWR | O_APPEND); - fchmod(fd, 0600); - if (WIFEXITED(status)) - dprintf(fd, "\n[exited with status %d.]\n", WEXITSTATUS(status)); - else - dprintf(fd, "\n[killed by signal %d.]\n", WTERMSIG(status)); - - exit(0); - } - - sprintf(lockfile, ".,%lx.%d", ms, getpid()); - lockfd = openat(dirfd, lockfile, O_CREAT | O_EXCL | O_RDWR | O_APPEND, 0600); + pid_t child; + int lockfd; + char lockfile[128]; + int dirfd; + + struct timeval tv; + gettimeofday(&tv, NULL); + int64_t ms = tv.tv_sec * 1000 + tv.tv_usec / 1000; + + char *path = getenv("NQDIR"); + if (!path) + path = "."; + + dirfd = open(path, O_RDONLY); + if (dirfd < 0) { + perror("dir open"); + exit(111); + } + + child = fork(); + if (child == -1) { + perror("fork"); + exit(111); + } + + if (child > 0) { + // background + signal(SIGINT, quit); + pause(); + exit(0); + } + + child = fork(); + if (child == -1) { + perror("fork"); + exit(111); + } + if (child > 0) { + int status; + sprintf(lockfile, ",%lx.%d", ms, child); + + puts(lockfile); + + // signal parent to exit + kill(getppid(), SIGINT); + + wait(&status); + int fd; + fd = openat(dirfd, lockfile, O_RDWR | O_APPEND); + fchmod(fd, 0600); + if (WIFEXITED(status)) + dprintf(fd, "\n[exited with status %d.]\n", WEXITSTATUS(status)); + else + dprintf(fd, "\n[killed by signal %d.]\n", WTERMSIG(status)); + + exit(0); + } + + sprintf(lockfile, ".,%lx.%d", ms, getpid()); + lockfd = openat(dirfd, lockfile, O_CREAT | O_EXCL | O_RDWR | O_APPEND, 0600); - if (lockfd < 0) { - perror("open"); - exit(222); - } + if (lockfd < 0) { + perror("open"); + exit(222); + } - flock(lockfd, LOCK_EX); + flock(lockfd, LOCK_EX); - // drop leading . - renameat(dirfd, lockfile, dirfd, lockfile+1); - - write(lockfd, "exec", 4); - int i; - for (i = 0; i < argc; i++) { - int j, l = strlen(argv[i]); - write(lockfd, " '", 2); - for (j = 0; j < l; j++) { - if (argv[i][j] == '\'') - write(lockfd, "'\\''", 4); - else - write(lockfd, argv[i]+j, 1); - } - write(lockfd, "'", 1); - } - - DIR *dir = fdopendir(dirfd); - if (!dir) { - perror("fdopendir"); - exit(111); - } - - struct dirent *ent; + // drop leading . + renameat(dirfd, lockfile, dirfd, lockfile+1); + + write(lockfd, "exec", 4); + int i; + for (i = 0; i < argc; i++) { + int j, l = strlen(argv[i]); + write(lockfd, " '", 2); + for (j = 0; j < l; j++) { + if (argv[i][j] == '\'') + write(lockfd, "'\\''", 4); + else + write(lockfd, argv[i]+j, 1); + } + write(lockfd, "'", 1); + } + + DIR *dir = fdopendir(dirfd); + if (!dir) { + perror("fdopendir"); + exit(111); + } + + struct dirent *ent; again: - while ((ent = readdir(dir))) { - if (ent->d_name[0] == ',' && strcmp(ent->d_name, lockfile+1) < 0) { - int f = openat(dirfd, ent->d_name, O_RDWR); + while ((ent = readdir(dir))) { + if (ent->d_name[0] == ',' && strcmp(ent->d_name, lockfile+1) < 0) { + int f = openat(dirfd, ent->d_name, O_RDWR); - if (flock(f, LOCK_EX | LOCK_NB) == -1 && errno == EWOULDBLOCK) { - flock(f, LOCK_EX); // sit it out + if (flock(f, LOCK_EX | LOCK_NB) == -1 && errno == EWOULDBLOCK) { + flock(f, LOCK_EX); // sit it out - rewinddir(dir); - goto again; - } + rewinddir(dir); + goto again; + } - fchmod(f, 0600); - close(f); - } - } + fchmod(f, 0600); + close(f); + } + } - closedir(dir); + closedir(dir); - // ready to run + // ready to run - write(lockfd, "\n", 1); + write(lockfd, "\n", 1); - fchmod(lockfd, 0700); + fchmod(lockfd, 0700); - dup2(lockfd, 2); - dup2(lockfd, 1); - close(lockfd); - close(dirfd); + dup2(lockfd, 2); + dup2(lockfd, 1); + close(lockfd); + close(dirfd); - execvp(argv[1], argv+1); + execvp(argv[1], argv+1); - return 111; + return 111; } -- cgit 1.4.1