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);
133 bb_copyfd_eof(STDOUT_FILENO, old_stdout);
140 // LPR ------------------------
143 bb_error_msg("connected to server");
145 job = getpid() % 1000;
146 // TODO: when do finally we invent char *xgethostname()?!!
147 hostname = xzalloc(MAXHOSTNAMELEN+1);
148 gethostname(hostname, MAXHOSTNAMELEN);
150 // no files given on command line? -> use stdin
152 *--argv = (char *)"-";
154 printf("\x2" "%s\n", queue);
155 get_response_or_say_and_die("setting queue");
161 char *remote_filename;
164 // if data file is stdin, we need to dump it first
165 if (LONE_DASH(*argv)) {
166 strcpy(tempfile, "/tmp/lprXXXXXX");
167 fd = mkstemp(tempfile);
169 bb_perror_msg_and_die("mkstemp");
170 bb_copyfd_eof(STDIN_FILENO, fd);
171 xlseek(fd, 0, SEEK_SET);
172 *argv = (char*)bb_msg_standard_input;
174 fd = xopen(*argv, O_RDONLY);
177 /* "The name ... should start with ASCII "cfA",
178 * followed by a three digit job number, followed
179 * by the host name which has constructed the file."
180 * We supply 'c' or 'd' as needed for control/data file. */
181 remote_filename = xasprintf("fA%03u%s", job, hostname);
183 // create control file
184 // TODO: all lines but 2 last are constants! How we can use this fact?
185 controlfile = xasprintf(
186 "H" "%.32s\n" "P" "%.32s\n" /* H HOST, P USER */
187 "C" "%.32s\n" /* C CLASS - printed on banner page (if L cmd is also given) */
188 "J" "%.99s\n" /* J JOBNAME */
189 /* "class name for banner page and job name
190 * for banner page commands must precede L command" */
191 "L" "%.32s\n" /* L USER - print banner page, with given user's name */
192 "M" "%.32s\n" /* M WHOM_TO_MAIL */
193 "l" "d%.31s\n" /* l DATA_FILE_NAME ("dfAxxx") */
195 , printer_class /* can be "" */
196 , ((opts & LPR_J) ? job_title : *argv)
197 , (opts & LPR_h) ? user : ""
198 , (opts & LPR_m) ? user : ""
201 // delete possible "\nX\n" patterns
203 while ((c = strchr(c, '\n')) != NULL) {
205 while (c[0] && c[1] == '\n')
206 memmove(c, c+2, strlen(c+1)); /* strlen(c+1) == strlen(c+2) + 1 */
211 bb_error_msg("sending control file");
212 /* "Once all of the contents have
213 * been delivered, an octet of zero bits is sent as
214 * an indication that the file being sent is complete.
215 * A second level of acknowledgement processing
216 * must occur at this point." */
217 printf("\x2" "%u c%s\n" "%s" "%c",
218 (unsigned)strlen(controlfile),
219 remote_filename, controlfile, '\0');
220 get_response_or_say_and_die("sending control file");
222 // send data file, with name "dfaXXX"
224 bb_error_msg("sending data file");
225 st.st_size = 0; /* paranoia: fstat may theoretically fail */
227 printf("\x3" "%"OFF_FMT"u d%s\n", st.st_size, remote_filename);
228 if (bb_copyfd_size(fd, STDOUT_FILENO, st.st_size) != st.st_size) {
229 // We're screwed. We sent less bytes than we advertised.
230 bb_error_msg_and_die("local file changed size?!");
233 get_response_or_say_and_die("sending data file");
235 // delete temporary file if we dumped stdin
236 if (*argv == (char*)bb_msg_standard_input)
241 free(remote_filename);
245 job = (job + 1) % 1000;