1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50#include "libbb.h"
51
52
53
54
55
56static void get_response_or_say_and_die(int fd, const char *errmsg)
57{
58 ssize_t sz;
59 char buf[128];
60
61 buf[0] = ' ';
62 sz = safe_read(fd, buf, 1);
63 if ('\0' != buf[0]) {
64
65
66
67 sz = full_read(fd, buf + 1, 126);
68 bb_error_msg("error while %s%s", errmsg,
69 (sz > 0 ? ". Server said:" : ""));
70 if (sz > 0) {
71
72 if (buf[sz] != '\n')
73 buf[++sz] = '\n';
74 safe_write(STDERR_FILENO, buf, sz + 1);
75 }
76 xfunc_die();
77 }
78}
79
80int lpqr_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE;
81int lpqr_main(int argc UNUSED_PARAM, char *argv[])
82{
83 enum {
84 OPT_P = 1 << 0,
85 OPT_U = 1 << 1,
86
87 LPR_V = 1 << 2,
88 LPR_h = 1 << 3,
89 LPR_C = 1 << 4,
90 LPR_J = 1 << 5,
91 LPR_m = 1 << 6,
92
93 LPQ_SHORT_FMT = 1 << 2,
94 LPQ_DELETE = 1 << 3,
95 LPQ_FORCE = 1 << 4,
96 };
97 char tempfile[sizeof("/tmp/lprXXXXXX")];
98 const char *job_title;
99 const char *printer_class = "";
100 const char *queue;
101 const char *server = "localhost";
102 char *hostname;
103
104 const char *user = xuid2uname(getuid());
105 unsigned job;
106 unsigned opts;
107 int fd;
108
109 queue = getenv("PRINTER");
110 if (!queue)
111 queue = "lp";
112
113
114
115 opts = getopt32(argv,
116 ('r' == applet_name[2]) ? "P:U:VhC:J:m" : "P:U:sdf"
117 , &queue, &user
118 , &printer_class, &job_title
119 );
120 argv += optind;
121
122 {
123
124 char *s = strchr(queue, '@');
125 if (s) {
126
127 *s = '\0';
128 server = s + 1;
129 }
130 }
131
132
133 fd = create_and_connect_stream_or_die(server, 515);
134
135
136
137
138 if ('q' == applet_name[2]) {
139 char cmd;
140
141 if (opts & LPQ_FORCE) {
142 cmd = 1;
143 goto command;
144
145 } else if (opts & LPQ_DELETE) {
146 fdprintf(fd, "\x5" "%s %s", queue, user);
147 while (*argv) {
148 fdprintf(fd, " %s", *argv++);
149 }
150 bb_putchar('\n');
151
152
153
154
155 } else {
156 cmd = (opts & LPQ_SHORT_FMT) ? 3 : 4;
157 command:
158 fdprintf(fd, "%c" "%s\n", cmd, queue);
159 bb_copyfd_eof(fd, STDOUT_FILENO);
160 }
161
162 return EXIT_SUCCESS;
163 }
164
165
166
167
168 if (opts & LPR_V)
169 bb_error_msg("connected to server");
170
171 job = getpid() % 1000;
172 hostname = safe_gethostname();
173
174
175 if (!*argv)
176 *--argv = (char *)"-";
177
178 fdprintf(fd, "\x2" "%s\n", queue);
179 get_response_or_say_and_die(fd, "setting queue");
180
181
182 do {
183 unsigned cflen;
184 int dfd;
185 struct stat st;
186 char *c;
187 char *remote_filename;
188 char *controlfile;
189
190
191 if (LONE_DASH(*argv)) {
192 strcpy(tempfile, "/tmp/lprXXXXXX");
193 dfd = xmkstemp(tempfile);
194 bb_copyfd_eof(STDIN_FILENO, dfd);
195 xlseek(dfd, 0, SEEK_SET);
196 *argv = (char*)bb_msg_standard_input;
197 } else {
198 dfd = xopen(*argv, O_RDONLY);
199 }
200
201 st.st_size = 0;
202 fstat(dfd, &st);
203
204
205
206
207 if (st.st_size == 0) {
208 bb_error_msg("nothing to print");
209 continue;
210 }
211
212
213
214
215
216 remote_filename = xasprintf("fA%03u%s", job, hostname);
217
218
219
220 controlfile = xasprintf(
221 "H" "%.32s\n" "P" "%.32s\n"
222 "C" "%.32s\n"
223 "J" "%.99s\n"
224
225
226 "L" "%.32s\n"
227 "M" "%.32s\n"
228 "l" "d%.31s\n"
229 , hostname, user
230 , printer_class
231 , ((opts & LPR_J) ? job_title : *argv)
232 , (opts & LPR_h) ? user : ""
233 , (opts & LPR_m) ? user : ""
234 , remote_filename
235 );
236
237 c = controlfile;
238 while ((c = strchr(c, '\n')) != NULL) {
239 if (c[1] && c[2] == '\n') {
240 overlapping_strcpy(c, c+2);
241 } else {
242 c++;
243 }
244 }
245
246
247 if (opts & LPR_V)
248 bb_error_msg("sending control file");
249
250
251 cflen = (unsigned)strlen(controlfile);
252 fdprintf(fd, "\x2" "%u c%s\n", cflen, remote_filename);
253 get_response_or_say_and_die(fd, "sending control file");
254
255
256
257
258
259 full_write(fd, controlfile, cflen + 1);
260 get_response_or_say_and_die(fd, "sending control file");
261
262
263 if (opts & LPR_V)
264 bb_error_msg("sending data file");
265 fdprintf(fd, "\x3" "%"OFF_FMT"u d%s\n", st.st_size, remote_filename);
266 get_response_or_say_and_die(fd, "sending data file");
267 if (bb_copyfd_size(dfd, fd, st.st_size) != st.st_size) {
268
269 bb_error_msg_and_die("local file changed size?!");
270 }
271 write(fd, "", 1);
272 get_response_or_say_and_die(fd, "sending data file");
273
274
275 if (*argv == (char*)bb_msg_standard_input)
276 unlink(tempfile);
277
278
279 close(fd);
280 free(remote_filename);
281 free(controlfile);
282
283
284 if (opts & LPR_V)
285 bb_error_msg("job accepted");
286
287
288 job = (job + 1) % 1000;
289 } while (*++argv);
290
291 return EXIT_SUCCESS;
292}
293