librpc: Shorten dcerpc_binding_handle_call a bit
[Samba/gebeck_regimport.git] / source3 / printing / tests / vlp.c
blob648fca106d0bfac981e7fea5c70ddebc79346352
1 /*
2 Unix SMB/Netbios implementation.
4 Virtual lp system for printer testing
6 Copyright (C) Tim Potter 2000
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "system/passwd.h"
24 #include "system/filesys.h"
25 #include "printing.h"
26 #include "util_tdb.h"
28 #ifdef malloc
29 #undef malloc
30 #endif
32 #define PRINT_FIRSTJOB "100"
34 static TDB_CONTEXT *tdb;
36 struct vlp_job {
37 fstring owner;
38 int jobid;
39 fstring jobname;
40 int size;
41 int status;
42 time_t submit_time;
43 int deleted;
46 /* Print usage */
48 static void usage(void)
50 printf("Usage: vlp tdbfile=/tmp/vlp.tdb lpq|lprm|print|queuepause|queueresume|"
51 "lppause|lpresume [args]\n");
54 /* Return an array of vlp jobs that is the printer queue */
56 static void get_job_list(char *printer, struct vlp_job **job_list,
57 int *num_jobs)
59 fstring keystr;
60 TDB_DATA data;
62 slprintf(keystr, sizeof(keystr) - 1, "LPQ/%s", printer);
63 data = tdb_fetch_bystring(tdb, keystr);
65 *job_list = (struct vlp_job *)data.dptr;
66 *num_jobs = data.dsize / sizeof(struct vlp_job);
69 /* Store an array of vl jobs for the queue */
71 static void set_job_list(char *printer, struct vlp_job *job_list,
72 int num_jobs)
74 fstring keystr;
75 TDB_DATA data;
77 slprintf(keystr, sizeof(keystr) - 1, "LPQ/%s", printer);
79 data.dptr = (unsigned char *)job_list;
80 data.dsize = num_jobs * sizeof(struct vlp_job);
81 tdb_store_bystring(tdb, keystr, data, TDB_REPLACE);
84 /* Return the next job number for a printer */
86 static int next_jobnum(char *printer)
88 fstring keystr;
89 int jobnum;
91 slprintf(keystr, sizeof(keystr) - 1, "JOBNUM/%s", printer);
93 tdb_lock_bystring(tdb, keystr);
95 jobnum = tdb_fetch_int32(tdb, keystr);
97 /* Create next job index if none exists */
99 if (jobnum == -1) {
100 jobnum = atoi(PRINT_FIRSTJOB);
103 jobnum++;
104 tdb_store_int32(tdb, keystr, jobnum);
106 tdb_unlock_bystring(tdb, keystr);
108 return jobnum;
111 static void set_printer_status(char *printer, int status)
113 fstring keystr;
115 slprintf(keystr, sizeof(keystr) - 1, "STATUS/%s", printer);
116 tdb_store_int32(tdb, keystr, status);
119 static int get_printer_status(char *printer)
121 fstring keystr;
122 TDB_DATA data;
124 slprintf(keystr, sizeof(keystr) - 1, "STATUS/%s", printer);
126 data.dptr = (unsigned char *)keystr;
127 data.dsize = strlen(keystr) + 1;
129 if (!tdb_exists(tdb, data)) {
130 set_printer_status(printer, LPSTAT_OK);
131 return LPSTAT_OK;
134 return tdb_fetch_int32(tdb, keystr);
137 /* Display printer queue */
139 static int lpq_command(int argc, char **argv)
141 char *printer;
142 struct vlp_job *job_list = NULL;
143 int i, num_jobs, job_count = 0;
145 if (argc != 2) {
146 printf("Usage: lpq <printername>\n");
147 return 1;
150 printer = argv[1];
152 /* Display printer status */
154 switch (get_printer_status(printer)) {
155 case LPSTAT_OK:
156 printf("enabled\n");
157 break;
158 case LPSTAT_STOPPED:
159 printf("disabled\n");
160 break;
161 case LPSTAT_ERROR:
162 default:
163 printf("error\n");
164 break;
167 /* Print queued documents */
169 get_job_list(printer, &job_list, &num_jobs);
171 for (i = 0; i < num_jobs; i++) {
172 if (job_list[i].deleted) continue;
173 printf("%d\t%d\t%d\t%ld\t%s\t%s\n", job_list[i].jobid,
174 job_list[i].size,
175 (i == 0 && job_list[i].status == LPQ_QUEUED) ?
176 LPQ_SPOOLING : job_list[i].status,
177 (long int)job_list[i].submit_time, job_list[i].owner,
178 job_list[i].jobname);
179 job_count++;
182 free(job_list);
184 return 0;
187 /* Remove a job */
189 static int lprm_command(int argc, char **argv)
191 char *printer;
192 int jobid, num_jobs, i;
193 struct vlp_job *job_list;
195 if (argc < 3) {
196 printf("Usage: lprm <printername> <jobid>\n");
197 return 1;
200 printer = argv[1];
201 jobid = atoi(argv[2]);
203 get_job_list(printer, &job_list, &num_jobs);
205 for (i = 0; i < num_jobs; i++) {
206 if (job_list[i].jobid == jobid) {
207 job_list[i].deleted = 1;
208 set_job_list(printer, job_list, num_jobs);
209 break;
213 return 0;
216 /* print command = print-test %p %s */
218 static int print_command(int argc, char **argv)
220 char *printer;
221 fstring keystr;
222 struct passwd *pw;
223 TDB_DATA value, queue;
224 struct vlp_job job;
226 if (argc < 3) {
227 printf("Usage: print <printername> <jobname>\n");
228 return 1;
231 printer = argv[1];
233 ZERO_STRUCT(job);
235 /* Create a job record */
237 slprintf(job.jobname, sizeof(job.jobname) - 1, "%s", argv[2]);
239 if (!(pw = getpwuid(geteuid()))) {
240 printf("getpwuid failed\n");
241 return 1;
244 slprintf(job.owner, sizeof(job.owner) - 1, "%s", pw->pw_name);
246 job.jobid = next_jobnum(printer);
247 job.size = 666;
248 job.submit_time = time(NULL);
250 /* Store job entry in queue */
252 slprintf(keystr, sizeof(keystr) - 1, "LPQ/%s", printer);
254 value = tdb_fetch_bystring(tdb, keystr);
256 if (value.dptr) {
258 /* Add job to end of queue */
260 queue.dptr = (unsigned char *)malloc(value.dsize + sizeof(struct vlp_job));
261 if (!queue.dptr) return 1;
263 memcpy(queue.dptr, value.dptr, value.dsize);
264 memcpy(queue.dptr + value.dsize, &job, sizeof(struct vlp_job));
266 queue.dsize = value.dsize + sizeof(struct vlp_job);
268 tdb_store_bystring(tdb, keystr, queue, TDB_REPLACE);
270 free(queue.dptr);
272 } else {
274 /* Create new queue */
275 queue.dptr = (unsigned char *)&job;
276 queue.dsize = sizeof(struct vlp_job);
278 tdb_store_bystring(tdb, keystr, queue, TDB_REPLACE);
281 return 0;
284 /* Pause the queue */
286 static int queuepause_command(int argc, char **argv)
288 char *printer;
290 if (argc != 2) {
291 printf("Usage: queuepause <printername>\n");
292 return 1;
295 printer = argv[1];
296 set_printer_status(printer, LPSTAT_STOPPED);
298 return 0;
301 /* Resume the queue */
303 static int queueresume_command(int argc, char **argv)
305 char *printer;
307 if (argc != 2) {
308 printf("Usage: queueresume <printername>\n");
309 return 1;
312 printer = argv[1];
313 set_printer_status(printer, LPSTAT_OK);
315 return 0;
318 /* Pause a job */
320 static int lppause_command(int argc, char **argv)
322 struct vlp_job *job_list;
323 char *printer;
324 int jobid, num_jobs, i;
326 if (argc != 3) {
327 printf("Usage: lppause <printername> <jobid>\n");
328 return 1;
331 printer = argv[1];
332 jobid = atoi(argv[2]);
334 get_job_list(printer, &job_list, &num_jobs);
336 for (i = 0; i < num_jobs; i++) {
337 if (job_list[i].jobid == jobid) {
338 job_list[i].status = LPQ_PAUSED;
339 set_job_list(printer, job_list, num_jobs);
340 return 0;
344 return 1;
347 /* Resume a job */
349 static int lpresume_command(int argc, char **argv)
351 struct vlp_job *job_list;
352 char *printer;
353 int jobid, num_jobs, i;
355 if (argc != 3) {
356 printf("Usage: lpresume <printername> <jobid>\n");
357 return 1;
360 printer = argv[1];
361 jobid = atoi(argv[2]);
363 get_job_list(printer, &job_list, &num_jobs);
365 for (i = 0; i < num_jobs; i++) {
366 if (job_list[i].jobid == jobid) {
367 job_list[i].status = LPQ_QUEUED;
368 set_job_list(printer, job_list, num_jobs);
369 return 0;
373 return 1;
376 int main(int argc, char **argv)
378 /* Parameter check */
379 const char *printdb_path = NULL;
381 if (argc < 2) {
382 usage();
383 return 1;
386 if (strncmp(argv[1], "tdbfile", strlen("tdbfile")) != 0) {
387 usage();
388 return 1;
391 printdb_path = get_string_param(argv[1]);
392 if (!printdb_path) {
393 return 1;
396 /* FIXME: We should *never* open a tdb without logging! */
397 if (!(tdb = tdb_open_compat(printdb_path, 0, 0, O_RDWR | O_CREAT,
398 0666, NULL, NULL))) {
399 printf("%s: unable to open %s\n", argv[0], printdb_path);
400 return 1;
403 /* Ensure we are modes 666 */
405 chmod(printdb_path, 0666);
407 /* Do commands */
409 if (strcmp(argv[2], "lpq") == 0) {
410 return lpq_command(argc - 2, &argv[2]);
413 if (strcmp(argv[2], "lprm") == 0) {
414 return lprm_command(argc - 2, &argv[2]);
417 if (strcmp(argv[2], "print") == 0) {
418 return print_command(argc - 2, &argv[2]);
421 if (strcmp(argv[2], "queuepause") == 0) {
422 return queuepause_command(argc - 2, &argv[2]);
425 if (strcmp(argv[2], "queueresume") == 0) {
426 return queueresume_command(argc - 2, &argv[2]);
429 if (strcmp(argv[2], "lppause") == 0) {
430 return lppause_command(argc - 2, &argv[2]);
433 if (strcmp(argv[2], "lpresume") == 0) {
434 return lpresume_command(argc - 2, &argv[2]);
437 /* Unknown command */
439 printf("%s: invalid command %s\n", argv[0], argv[1]);
440 return 1;