#include #include #include #include #include #include #include #include #define DEFAULT_QUEUE_DIR "." int main(int argc, char **argv) { pid_t mypid; struct timeval tv; int64_t microtime; char *timestr = NULL, *queuedir = NULL, *tmpfile = NULL, *newfile = NULL; char buf[1024], *tmp; int fd, l, m; int perm = 0640; mypid = getpid(); gettimeofday(&tv, NULL); microtime = tv.tv_sec * 1000000 + tv.tv_usec; asprintf(×tr, "%li.%u", microtime, mypid); if (argc > 1) { queuedir = strdup(argv[1]); } else { queuedir = strdup(DEFAULT_QUEUE_DIR); } asprintf(&tmpfile, "%s/tmp/%s", queuedir, timestr); asprintf(&newfile, "%s/new/%s", queuedir, timestr); free(timestr); free(queuedir); if (getenv("NEWBATCH_EXEC") != NULL) { perm = 0750; } fd = open(tmpfile, O_WRONLY | O_CREAT | O_EXCL, perm); if (fd == -1) err(1, "open()"); while ((l = read(STDIN_FILENO, buf, sizeof(buf))) != 0) { if (l == -1) { if (errno == EINTR) continue; unlink(tmpfile); err(1, NULL); } tmp = buf; while (l > 0) { m = write(fd, tmp, l); if (m == -1) { if (errno == EINTR) continue; unlink(tmpfile); err(1, NULL); } l -= m; tmp += m; } } close(fd); if (rename(tmpfile, newfile) == -1) { unlink(tmpfile); err(1, NULL); } free(tmpfile); free(newfile); return 0; }