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