lpd: now with "svn add"...
diff --git a/printutils/lpd.c b/printutils/lpd.c
new file mode 100644
index 0000000..ed3d9d1
--- /dev/null
+++ b/printutils/lpd.c
@@ -0,0 +1,79 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * micro lpd - a small non-queueing lpd
+ *
+ * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
+ *
+ * Licensed under GPLv2, see file LICENSE in this tarball for details.
+ */
+#include "libbb.h"
+
+int lpd_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE;
+int lpd_main(int argc, char *argv[])
+{
+ char *s;
+
+ // goto spool directory
+ // spool directory contains either links to real printer devices or just simple files
+ // these links or files are called "queues"
+ if (!argv[1])
+ bb_show_usage();
+
+ xchdir(argv[1]);
+
+ xdup2(1, 2);
+
+ // read command
+ s = xmalloc_reads(STDIN_FILENO, NULL);
+
+ // N.B. we keep things simple
+ // only "receive job" command is meaningful here...
+ if (2 == *s) {
+ char *queue;
+
+ // parse command: "\x2QUEUE_NAME\n"
+ queue = s + 1;
+ *strchrnul(s, '\n') = '\0';
+
+ while (1) {
+ // signal OK
+ write(STDOUT_FILENO, "", 1);
+ // get subcommand
+ s = xmalloc_reads(STDIN_FILENO, NULL);
+ if (!s)
+ return EXIT_SUCCESS; // EOF (probably)
+ // valid s must be of form: SUBCMD | LEN | SP | FNAME
+ // N.B. we bail out on any error
+ // control or data file follows
+ if (2 == s[0] || 3 == s[0]) {
+ int fd;
+ size_t len;
+ // 2: control file (ignoring), 3: data file
+ fd = -1;
+ if (3 == s[0])
+ fd = xopen(queue, O_RDWR | O_APPEND);
+ // get data length
+ *strchrnul(s, ' ') = '\0';
+ len = xatou(s + 1);
+ // dump exactly len bytes to file, or die
+ bb_copyfd_exact_size(STDIN_FILENO, fd, len);
+ close(fd); // NB: can do close(-1). Who cares?
+ free(s);
+ // got no ACK? -> bail out
+ if (safe_read(STDIN_FILENO, s, 1) <= 0 || s[0]) {
+ // don't talk to peer - it obviously
+ // don't follow the protocol
+ return EXIT_FAILURE;
+ }
+ } else {
+ // any other subcommand aborts receiving job
+ // N.B. abort subcommand itself doesn't contain
+ // fname so it failed earlier...
+ break;
+ }
+ }
+ }
+
+ printf("Command %02x not supported\n", (unsigned char)*s);
+ return EXIT_FAILURE;
+}