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 queue = getenv("PRINTER");
93 if (!queue)
94 queue = "lp";
95
96
97
98 opts = getopt32(argv,
99 ('r' == applet_name[2]) ? "P:U:VhC:J:m" : "P:U:sdf"
100 , &queue, &user
101 , &printer_class, &job_title
102 );
103 argv += optind;
104
105 {
106
107 char *s = strchr(queue, '@');
108 if (s) {
109
110 *s = '\0';
111 server = s + 1;
112 }
113 }
114
115
116 fd = create_and_connect_stream_or_die(server, 515);
117
118
119
120
121 if ('q' == applet_name[2]) {
122 char cmd;
123
124 if (opts & LPQ_FORCE) {
125 cmd = 1;
126 goto command;
127
128 } else if (opts & LPQ_DELETE) {
129 fdprintf(fd, "\x5" "%s %s", queue, user);
130 while (*argv) {
131 fdprintf(fd, " %s", *argv++);
132 }
133 bb_putchar('\n');
134
135
136
137
138 } else {
139 cmd = (opts & LPQ_SHORT_FMT) ? 3 : 4;
140 command:
141 fdprintf(fd, "%c" "%s\n", cmd, queue);
142 bb_copyfd_eof(fd, STDOUT_FILENO);
143 }
144
145 return EXIT_SUCCESS;
146 }
147
148
149
150
151 if (opts & LPR_V)
152 bb_error_msg("connected to server");
153
154 job = getpid() % 1000;
155 hostname = safe_gethostname();
156
157
158 if (!*argv)
159 *--argv = (char *)"-";
160
161 fdprintf(fd, "\x2" "%s\n", queue);
162 get_response_or_say_and_die(fd, "setting queue");
163
164
165 do {
166 unsigned cflen;
167 int dfd;
168 struct stat st;
169 char *c;
170 char *remote_filename;
171 char *controlfile;
172
173
174 if (LONE_DASH(*argv)) {
175 strcpy(tempfile, "/tmp/lprXXXXXX");
176 dfd = xmkstemp(tempfile);
177 bb_copyfd_eof(STDIN_FILENO, dfd);
178 xlseek(dfd, 0, SEEK_SET);
179 *argv = (char*)bb_msg_standard_input;
180 } else {
181 dfd = xopen(*argv, O_RDONLY);
182 }
183
184 st.st_size = 0;
185 fstat(dfd, &st);
186
187
188
189
190 if (st.st_size == 0) {
191 bb_error_msg("nothing to print");
192 continue;
193 }
194
195
196
197
198
199 remote_filename = xasprintf("fA%03u%s", job, hostname);
200
201
202
203 controlfile = xasprintf(
204 "H" "%.32s\n" "P" "%.32s\n"
205 "C" "%.32s\n"
206 "J" "%.99s\n"
207
208
209 "L" "%.32s\n"
210 "M" "%.32s\n"
211 "l" "d%.31s\n"
212 , hostname, user
213 , printer_class
214 , ((opts & LPR_J) ? job_title : *argv)
215 , (opts & LPR_h) ? user : ""
216 , (opts & LPR_m) ? user : ""
217 , remote_filename
218 );
219
220 c = controlfile;
221 while ((c = strchr(c, '\n')) != NULL) {
222 if (c[1] && c[2] == '\n') {
223 overlapping_strcpy(c, c+2);
224 } else {
225 c++;
226 }
227 }
228
229
230 if (opts & LPR_V)
231 bb_error_msg("sending control file");
232
233
234 cflen = (unsigned)strlen(controlfile);
235 fdprintf(fd, "\x2" "%u c%s\n", cflen, remote_filename);
236 get_response_or_say_and_die(fd, "sending control file");
237
238
239
240
241
242 full_write(fd, controlfile, cflen + 1);
243 get_response_or_say_and_die(fd, "sending control file");
244
245
246 if (opts & LPR_V)
247 bb_error_msg("sending data file");
248 fdprintf(fd, "\x3" "%"OFF_FMT"u d%s\n", st.st_size, remote_filename);
249 get_response_or_say_and_die(fd, "sending data file");
250 if (bb_copyfd_size(dfd, fd, st.st_size) != st.st_size) {
251
252 bb_error_msg_and_die("local file changed size?!");
253 }
254 write(fd, "", 1);
255 get_response_or_say_and_die(fd, "sending data file");
256
257
258 if (*argv == (char*)bb_msg_standard_input)
259 unlink(tempfile);
260
261
262 close(fd);
263 free(remote_filename);
264 free(controlfile);
265
266
267 if (opts & LPR_V)
268 bb_error_msg("job accepted");
269
270
271 job = (job + 1) % 1000;
272 } while (*++argv);
273
274 return EXIT_SUCCESS;
275}
276