123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- #include "libbb.h"
- static void get_response_or_say_and_die(int fd, const char *errmsg)
- {
- ssize_t sz;
- char buf[128];
- buf[0] = ' ';
- sz = safe_read(fd, buf, 1);
- if ('\0' != buf[0]) {
-
-
-
- sz = full_read(fd, buf + 1, 126);
- bb_error_msg("error while %s%s", errmsg,
- (sz > 0 ? ". Server said:" : ""));
- if (sz > 0) {
-
- if (buf[sz] != '\n')
- buf[++sz] = '\n';
- safe_write(STDERR_FILENO, buf, sz + 1);
- }
- xfunc_die();
- }
- }
- int lpqr_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE;
- int lpqr_main(int argc UNUSED_PARAM, char *argv[])
- {
- enum {
- OPT_P = 1 << 0,
- OPT_U = 1 << 1,
- LPR_V = 1 << 2,
- LPR_h = 1 << 3,
- LPR_C = 1 << 4,
- LPR_J = 1 << 5,
- LPR_m = 1 << 6,
- LPQ_SHORT_FMT = 1 << 2,
- LPQ_DELETE = 1 << 3,
- LPQ_FORCE = 1 << 4,
- };
- char tempfile[sizeof("/tmp/lprXXXXXX")];
- const char *job_title;
- const char *printer_class = "";
- const char *queue;
- const char *server = "localhost";
- char *hostname;
-
- const char *user = xuid2uname(getuid());
- unsigned job;
- unsigned opts;
- int fd;
- queue = getenv("PRINTER");
- if (!queue)
- queue = "lp";
-
-
- opts = getopt32(argv,
- ('r' == applet_name[2]) ? "P:U:VhC:J:m" : "P:U:sdf"
- , &queue, &user
- , &printer_class, &job_title
- );
- argv += optind;
- {
-
- char *s = strchr(queue, '@');
- if (s) {
-
- *s = '\0';
- server = s + 1;
- }
- }
-
- fd = create_and_connect_stream_or_die(server, 515);
-
-
-
- if ('q' == applet_name[2]) {
- char cmd;
-
- if (opts & LPQ_FORCE) {
- cmd = 1;
- goto command;
-
- } else if (opts & LPQ_DELETE) {
- fdprintf(fd, "\x5" "%s %s", queue, user);
- while (*argv) {
- fdprintf(fd, " %s", *argv++);
- }
- bb_putchar('\n');
-
-
-
-
- } else {
- cmd = (opts & LPQ_SHORT_FMT) ? 3 : 4;
- command:
- fdprintf(fd, "%c" "%s\n", cmd, queue);
- bb_copyfd_eof(fd, STDOUT_FILENO);
- }
- return EXIT_SUCCESS;
- }
-
-
-
- if (opts & LPR_V)
- bb_error_msg("connected to server");
- job = getpid() % 1000;
- hostname = safe_gethostname();
-
- if (!*argv)
- *--argv = (char *)"-";
- fdprintf(fd, "\x2" "%s\n", queue);
- get_response_or_say_and_die(fd, "setting queue");
-
- do {
- unsigned cflen;
- int dfd;
- struct stat st;
- char *c;
- char *remote_filename;
- char *controlfile;
-
- if (LONE_DASH(*argv)) {
- strcpy(tempfile, "/tmp/lprXXXXXX");
- dfd = xmkstemp(tempfile);
- bb_copyfd_eof(STDIN_FILENO, dfd);
- xlseek(dfd, 0, SEEK_SET);
- *argv = (char*)bb_msg_standard_input;
- } else {
- dfd = xopen(*argv, O_RDONLY);
- }
- st.st_size = 0;
- fstat(dfd, &st);
-
- if (st.st_size == 0) {
- bb_error_msg("nothing to print");
- continue;
- }
-
- remote_filename = xasprintf("fA%03u%s", job, hostname);
-
-
- controlfile = xasprintf(
- "H" "%.32s\n" "P" "%.32s\n"
- "C" "%.32s\n"
- "J" "%.99s\n"
-
- "L" "%.32s\n"
- "M" "%.32s\n"
- "l" "d%.31s\n"
- , hostname, user
- , printer_class
- , ((opts & LPR_J) ? job_title : *argv)
- , (opts & LPR_h) ? user : ""
- , (opts & LPR_m) ? user : ""
- , remote_filename
- );
-
- c = controlfile;
- while ((c = strchr(c, '\n')) != NULL) {
- if (c[1] && c[2] == '\n') {
- overlapping_strcpy(c, c+2);
- } else {
- c++;
- }
- }
-
- if (opts & LPR_V)
- bb_error_msg("sending control file");
-
- cflen = (unsigned)strlen(controlfile);
- fdprintf(fd, "\x2" "%u c%s\n", cflen, remote_filename);
- get_response_or_say_and_die(fd, "sending control file");
-
- full_write(fd, controlfile, cflen + 1);
- get_response_or_say_and_die(fd, "sending control file");
-
- if (opts & LPR_V)
- bb_error_msg("sending data file");
- 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) {
-
- bb_error_msg_and_die("local file changed size?!");
- }
- write(fd, "", 1);
- get_response_or_say_and_die(fd, "sending data file");
-
- if (*argv == (char*)bb_msg_standard_input)
- unlink(tempfile);
-
- close(fd);
- free(remote_filename);
- free(controlfile);
-
- if (opts & LPR_V)
- bb_error_msg("job accepted");
-
- job = (job + 1) % 1000;
- } while (*++argv);
- return EXIT_SUCCESS;
- }
|