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