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