about summary refs log tree commit diff
path: root/nq.c
diff options
context:
space:
mode:
authorChristian Neukirchen <chneukirchen@gmail.com>2015-07-31 14:20:17 +0200
committerChristian Neukirchen <chneukirchen@gmail.com>2015-07-31 14:20:17 +0200
commita45deb01daa2c2fb67361ea8ffce6b63ad020558 (patch)
tree94e9e479244227a89bb6fe6c510cdf9280368c75 /nq.c
parente6b97161209a4a851e63f3cef49e2c156b609d33 (diff)
downloadnq-a45deb01daa2c2fb67361ea8ffce6b63ad020558.tar.gz
nq-a45deb01daa2c2fb67361ea8ffce6b63ad020558.tar.xz
nq-a45deb01daa2c2fb67361ea8ffce6b63ad020558.zip
reformat
Diffstat (limited to 'nq.c')
-rw-r--r--nq.c238
1 files 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 <sys/file.h>
 #include <sys/stat.h>
+#include <sys/time.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 
@@ -19,13 +20,12 @@
 static void
 quit(int sig)
 {
-  exit(0);
+	exit(0);
 }
 
-#include <sys/time.h>
 /*
-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;
 }