From b6125c9eac61eca81eae42fb63a26ce16a8dc8db Mon Sep 17 00:00:00 2001 From: Marius Halden Date: Fri, 19 Aug 2016 16:00:54 +0200 Subject: Initial commit --- newbatch.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 newbatch.c (limited to 'newbatch.c') diff --git a/newbatch.c b/newbatch.c new file mode 100644 index 0000000..6b27234 --- /dev/null +++ b/newbatch.c @@ -0,0 +1,70 @@ +#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; + + 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); + + fd = open(tmpfile, O_WRONLY | O_CREAT | O_EXCL, 0660); + if (fd == -1) + err(1, "open()"); + + while ((l = read(STDIN_FILENO, buf, sizeof(buf))) != 0) { + if (l == -1) { + unlink(tmpfile); + err(1, NULL); + } + + tmp = buf; + while (l > 0) { + m = write(fd, tmp, l); + if (m == -1) { + 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; +} -- cgit v1.2.3