1 /* vi: set sw=4 ts=4: */
3 * bare bones version of lpr & lpq: BSD printing utilities
5 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
7 * Original idea and code:
8 * Walter Harms <WHarms@bfs.de>
10 * Licensed under GPLv2, see file LICENSE in this tarball for details.
12 * See RFC 1179 for protocol description.
17 * LPD returns binary 0 on success.
18 * Otherwise it returns error message.
20 static void get_response_or_say_and_die(const char *errmsg)
28 sz = safe_read(STDOUT_FILENO, buf, 1);
31 // try to make sure last char is '\n', but do not add
33 sz = full_read(STDOUT_FILENO, buf + 1, 126);
34 bb_error_msg("error while %s%s", errmsg,
35 (sz > 0 ? ". Server said:" : ""));
37 // sz = (bytes in buf) - 1
40 safe_write(STDERR_FILENO, buf, sz + 1);
46 int lpqr_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE;
47 int lpqr_main(int argc, char *argv[])
50 OPT_P = 1 << 0, // -P queue[@host[:port]]. If no -P is given use $PRINTER, then "lp@localhost:515"
51 OPT_U = 1 << 1, // -U username
53 LPR_V = 1 << 2, // -V: be verbose
54 LPR_h = 1 << 3, // -h: want banner printed
55 LPR_C = 1 << 4, // -C class: job "class" (? supposedly printed on banner)
56 LPR_J = 1 << 5, // -J title: the job title for the banner page
57 LPR_m = 1 << 6, // -m: send mail back to user
59 LPQ_SHORT_FMT = 1 << 2, // -s: short listing format
60 LPQ_DELETE = 1 << 3, // -d: delete job(s)
61 LPQ_FORCE = 1 << 4, // -f: force waiting job(s) to be printed
63 char tempfile[sizeof("/tmp/lprXXXXXX")];
64 const char *job_title;
65 const char *printer_class = ""; // printer class, max 32 char
66 const char *queue; // name of printer queue
67 const char *server = "localhost"; // server[:port] of printer queue
69 // N.B. IMHO getenv("USER") can be way easily spoofed!
70 const char *user = bb_getpwuid(NULL, -1, getuid());
76 // TODO: set opt_complementary: s,d,f are mutually exclusive
78 (/*lp*/'r' == applet_name[2]) ? "P:U:VhC:J:m" : "P:U:sdf"
80 , &printer_class, &job_title
84 // if queue is not specified -> use $PRINTER
86 queue = getenv("PRINTER");
87 // if queue is still not specified ->
89 // ... queue defaults to "lp"
90 // server defaults to "localhost"
92 // if queue is specified ->
94 // queue name is to the left of '@'
95 char *s = strchr(queue, '@');
97 // server name is to the right of '@'
104 fd = create_and_connect_stream_or_die(server, 515);
105 // play with descriptors to save space: fdprintf > printf
106 old_stdout = dup(STDOUT_FILENO);
107 xmove_fd(fd, STDOUT_FILENO);
110 // LPQ ------------------------
112 if (/*lp*/'q' == applet_name[2]) {
114 // force printing of every job still in queue
115 if (opts & LPQ_FORCE) {
119 } else if (opts & LPQ_DELETE) {
120 printf("\x5" "%s %s", queue, user);
122 printf(" %s", *argv++);
125 // dump current jobs status
126 // N.B. periodical polling should be achieved
127 // via "watch -n delay lpq"
128 // They say it's the UNIX-way :)
130 cmd = (opts & LPQ_SHORT_FMT) ? 3 : 4;
132 printf("%c" "%s\n", cmd, queue);
134 bb_copyfd_eof(STDOUT_FILENO, old_stdout);
141 // LPR ------------------------
144 bb_error_msg("connected to server");
146 job = getpid() % 1000;
147 // TODO: when do finally we invent char *xgethostname()?!!
148 hostname = xzalloc(MAXHOSTNAMELEN+1);
149 gethostname(hostname, MAXHOSTNAMELEN);
151 // no files given on command line? -> use stdin
153 *--argv = (char *)"-";
155 printf("\x2" "%s\n", queue);
156 get_response_or_say_and_die("setting queue");
162 char *remote_filename;
165 // if data file is stdin, we need to dump it first
166 if (LONE_DASH(*argv)) {
167 strcpy(tempfile, "/tmp/lprXXXXXX");
168 fd = mkstemp(tempfile);
170 bb_perror_msg_and_die("mkstemp");
171 bb_copyfd_eof(STDIN_FILENO, fd);
172 xlseek(fd, 0, SEEK_SET);
173 *argv = (char*)bb_msg_standard_input;
175 fd = xopen(*argv, O_RDONLY);
178 /* "The name ... should start with ASCII "cfA",
179 * followed by a three digit job number, followed
180 * by the host name which has constructed the file."
181 * We supply 'c' or 'd' as needed for control/data file. */
182 remote_filename = xasprintf("fA%03u%s", job, hostname);
184 // create control file
185 // TODO: all lines but 2 last are constants! How we can use this fact?
186 controlfile = xasprintf(
187 "H" "%.32s\n" "P" "%.32s\n" /* H HOST, P USER */
188 "C" "%.32s\n" /* C CLASS - printed on banner page (if L cmd is also given) */
189 "J" "%.99s\n" /* J JOBNAME */
190 /* "class name for banner page and job name
191 * for banner page commands must precede L command" */
192 "L" "%.32s\n" /* L USER - print banner page, with given user's name */
193 "M" "%.32s\n" /* M WHOM_TO_MAIL */
194 "l" "d%.31s\n" /* l DATA_FILE_NAME ("dfAxxx") */
196 , printer_class /* can be "" */
197 , ((opts & LPR_J) ? job_title : *argv)
198 , (opts & LPR_h) ? user : ""
199 , (opts & LPR_m) ? user : ""
202 // delete possible "\nX\n" patterns
204 while ((c = strchr(c, '\n')) != NULL) {
206 while (c[0] && c[1] == '\n')
207 memmove(c, c+2, strlen(c+1)); /* strlen(c+1) == strlen(c+2) + 1 */
212 bb_error_msg("sending control file");
213 /* "Once all of the contents have
214 * been delivered, an octet of zero bits is sent as
215 * an indication that the file being sent is complete.
216 * A second level of acknowledgement processing
217 * must occur at this point." */
218 printf("\x2" "%u c%s\n" "%s" "%c",
219 (unsigned)strlen(controlfile),
220 remote_filename, controlfile, '\0');
221 get_response_or_say_and_die("sending control file");
223 // send data file, with name "dfaXXX"
225 bb_error_msg("sending data file");
226 st.st_size = 0; /* paranoia: fstat may theoretically fail */
228 printf("\x3" "%"OFF_FMT"u d%s\n", st.st_size, remote_filename);
230 if (bb_copyfd_size(fd, STDOUT_FILENO, st.st_size) != st.st_size) {
231 // We're screwed. We sent less bytes than we advertised.
232 bb_error_msg_and_die("local file changed size?!");
235 get_response_or_say_and_die("sending data file");
237 // delete temporary file if we dumped stdin
238 if (*argv == (char*)bb_msg_standard_input)
243 free(remote_filename);
247 job = (job + 1) % 1000;