hush: add leak detector helper; fix/add tests for it
[oweals/busybox.git] / printutils / lpr.c
index 913da6f0f296eef0998574dc9399d7fea5e9c6fd..f21cffddfa9cb5346b1289248de1aab7a8ea5b1e 100644 (file)
  * LPD returns binary 0 on success.
  * Otherwise it returns error message.
  */
-static void get_response_or_say_and_die(const char *errmsg)
+static void get_response_or_say_and_die(int fd, const char *errmsg)
 {
        ssize_t sz;
        char buf[128];
 
-       fflush(stdout);
-
        buf[0] = ' ';
-       sz = safe_read(STDOUT_FILENO, buf, 1);
+       sz = safe_read(fd, buf, 1);
        if ('\0' != buf[0]) {
                // request has failed
                // try to make sure last char is '\n', but do not add
                // superfluous one
-               sz = full_read(STDOUT_FILENO, buf + 1, 126);
+               sz = full_read(fd, buf + 1, 126);
                bb_error_msg("error while %s%s", errmsg,
                                (sz > 0 ? ". Server said:" : ""));
                if (sz > 0) {
@@ -44,14 +42,14 @@ static void get_response_or_say_and_die(const char *errmsg)
 }
 
 int lpqr_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE;
-int lpqr_main(int argc, char *argv[])
+int lpqr_main(int argc UNUSED_PARAM, char *argv[])
 {
        enum {
                OPT_P           = 1 << 0, // -P queue[@host[:port]]. If no -P is given use $PRINTER, then "lp@localhost:515"
                OPT_U           = 1 << 1, // -U username
 
                LPR_V           = 1 << 2, // -V: be verbose
-               LPR_h           = 1 << 3, // -h: want banner printed    
+               LPR_h           = 1 << 3, // -h: want banner printed
                LPR_C           = 1 << 4, // -C class: job "class" (? supposedly printed on banner)
                LPR_J           = 1 << 5, // -J title: the job title for the banner page
                LPR_m           = 1 << 6, // -m: send mail back to user
@@ -67,10 +65,10 @@ int lpqr_main(int argc, char *argv[])
        const char *server = "localhost"; // server[:port] of printer queue
        char *hostname;
        // N.B. IMHO getenv("USER") can be way easily spoofed!
-       const char *user = bb_getpwuid(NULL, -1, getuid());
+       const char *user = xuid2uname(getuid());
        unsigned job;
        unsigned opts;
-       int old_stdout, fd;
+       int fd;
 
        // parse options
        // TODO: set opt_complementary: s,d,f are mutually exclusive
@@ -102,9 +100,6 @@ int lpqr_main(int argc, char *argv[])
 
        // do connect
        fd = create_and_connect_stream_or_die(server, 515);
-       // play with descriptors to save space: fdprintf > printf
-       old_stdout = dup(STDOUT_FILENO);
-       xmove_fd(fd, STDOUT_FILENO);
 
        //
        // LPQ ------------------------
@@ -117,9 +112,9 @@ int lpqr_main(int argc, char *argv[])
                        goto command;
                // delete job(s)
                } else if (opts & LPQ_DELETE) {
-                       printf("\x5" "%s %s", queue, user);
+                       fdprintf(fd, "\x5" "%s %s", queue, user);
                        while (*argv) {
-                               printf(" %s", *argv++);
+                               fdprintf(fd, " %s", *argv++);
                        }
                        bb_putchar('\n');
                // dump current jobs status
@@ -129,9 +124,8 @@ int lpqr_main(int argc, char *argv[])
                } else {
                        cmd = (opts & LPQ_SHORT_FMT) ? 3 : 4;
  command:
-                       printf("%c" "%s\n", cmd, queue);
-                       fflush(stdout);
-                       bb_copyfd_eof(STDOUT_FILENO, old_stdout);
+                       fdprintf(fd, "%c" "%s\n", cmd, queue);
+                       bb_copyfd_eof(fd, STDOUT_FILENO);
                }
 
                return EXIT_SUCCESS;
@@ -144,19 +138,19 @@ int lpqr_main(int argc, char *argv[])
                bb_error_msg("connected to server");
 
        job = getpid() % 1000;
-       // TODO: when do finally we invent char *xgethostname()?!!
-       hostname = xzalloc(MAXHOSTNAMELEN+1);
-       gethostname(hostname, MAXHOSTNAMELEN);
+       hostname = safe_gethostname();
 
        // no files given on command line? -> use stdin
        if (!*argv)
                *--argv = (char *)"-";
 
-       printf("\x2" "%s\n", queue);
-       get_response_or_say_and_die("setting queue");
+       fdprintf(fd, "\x2" "%s\n", queue);
+       get_response_or_say_and_die(fd, "setting queue");
 
        // process files
        do {
+               unsigned cflen;
+               int dfd;
                struct stat st;
                char *c;
                char *remote_filename;
@@ -165,14 +159,14 @@ int lpqr_main(int argc, char *argv[])
                // if data file is stdin, we need to dump it first
                if (LONE_DASH(*argv)) {
                        strcpy(tempfile, "/tmp/lprXXXXXX");
-                       fd = mkstemp(tempfile);
-                       if (fd < 0)
+                       dfd = mkstemp(tempfile);
+                       if (dfd < 0)
                                bb_perror_msg_and_die("mkstemp");
-                       bb_copyfd_eof(STDIN_FILENO, fd);
-                       xlseek(fd, 0, SEEK_SET);
+                       bb_copyfd_eof(STDIN_FILENO, dfd);
+                       xlseek(dfd, 0, SEEK_SET);
                        *argv = (char*)bb_msg_standard_input;
                } else {
-                       fd = xopen(*argv, O_RDONLY);
+                       dfd = xopen(*argv, O_RDONLY);
                }
 
                /* "The name ... should start with ASCII "cfA",
@@ -201,38 +195,45 @@ int lpqr_main(int argc, char *argv[])
                );
                // delete possible "\nX\n" patterns
                c = controlfile;
+               cflen = (unsigned)strlen(controlfile);
                while ((c = strchr(c, '\n')) != NULL) {
-                       c++;
-                       while (c[0] && c[1] == '\n')
-                               memmove(c, c+2, strlen(c+1)); /* strlen(c+1) == strlen(c+2) + 1 */
+                       if (c[1] && c[2] == '\n') {
+                               /* can't use strcpy, results are undefined */
+                               memmove(c, c+2, cflen - (c-controlfile) - 1);
+                               cflen -= 2;
+                       } else {
+                               c++;
+                       }
                }
 
                // send control file
                if (opts & LPR_V)
                        bb_error_msg("sending control file");
+               /* "Acknowledgement processing must occur as usual
+                * after the command is sent." */
+               fdprintf(fd, "\x2" "%u c%s\n", cflen, remote_filename);
+               get_response_or_say_and_die(fd, "sending control file");
                /* "Once all of the contents have
                 * been delivered, an octet of zero bits is sent as
                 * an indication that the file being sent is complete.
                 * A second level of acknowledgement processing
                 * must occur at this point." */
-               printf("\x2" "%u c%s\n" "%s" "%c",
-                               (unsigned)strlen(controlfile),
-                               remote_filename, controlfile, '\0');
-               get_response_or_say_and_die("sending control file");
+               full_write(fd, controlfile, cflen + 1); /* writes NUL byte too */
+               get_response_or_say_and_die(fd, "sending control file");
 
                // send data file, with name "dfaXXX"
                if (opts & LPR_V)
                        bb_error_msg("sending data file");
                st.st_size = 0; /* paranoia: fstat may theoretically fail */
-               fstat(fd, &st);
-               printf("\x3" "%"OFF_FMT"u d%s\n", st.st_size, remote_filename);
-               fflush(stdout);
-               if (bb_copyfd_size(fd, STDOUT_FILENO, st.st_size) != st.st_size) {
+               fstat(dfd, &st);
+               fdprintf(fd, "\x3" "%"OFF_FMT"u d%s\n", st.st_size, remote_filename);
+               get_response_or_say_and_die(fd, "sending data file");
+               if (bb_copyfd_size(dfd, fd, st.st_size) != st.st_size) {
                        // We're screwed. We sent less bytes than we advertised.
                        bb_error_msg_and_die("local file changed size?!");
                }
-               bb_putchar('\0');
-               get_response_or_say_and_die("sending data file");
+               write(fd, "", 1); // send ACK
+               get_response_or_say_and_die(fd, "sending data file");
 
                // delete temporary file if we dumped stdin
                if (*argv == (char*)bb_msg_standard_input)
@@ -243,6 +244,10 @@ int lpqr_main(int argc, char *argv[])
                free(remote_filename);
                free(controlfile);
 
+               // say job accepted
+               if (opts & LPR_V)
+                       bb_error_msg("job accepted");
+
                // next, please!
                job = (job + 1) % 1000;
        } while (*++argv);