s3-includes: only include system/passwd.h when needed.
[Samba.git] / source3 / printing / tests / vlp.c
blobab05a425001e3d3a8b4e0691e7fef7df20552468
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 "printing.h"
26 #ifdef malloc
27 #undef malloc
28 #endif
30 #define PRINT_FIRSTJOB "100"
32 static TDB_CONTEXT *tdb;
34 struct vlp_job {
35 fstring owner;
36 int jobid;
37 fstring jobname;
38 int size;
39 int status;
40 time_t submit_time;
41 int deleted;
44 /* Print usage */
46 static void usage(void)
48 printf("Usage: vlp tdbfile=/tmp/vlp.tdb lpq|lprm|print|queuepause|queueresume|"
49 "lppause|lpresume [args]\n");
52 /* Return an array of vlp jobs that is the printer queue */
54 static void get_job_list(char *printer, struct vlp_job **job_list,
55 int *num_jobs)
57 fstring keystr;
58 TDB_DATA data;
60 slprintf(keystr, sizeof(keystr) - 1, "LPQ/%s", printer);
61 data = tdb_fetch_bystring(tdb, keystr);
63 *job_list = (struct vlp_job *)data.dptr;
64 *num_jobs = data.dsize / sizeof(struct vlp_job);
67 /* Store an array of vl jobs for the queue */
69 static void set_job_list(char *printer, struct vlp_job *job_list,
70 int num_jobs)
72 fstring keystr;
73 TDB_DATA data;
75 slprintf(keystr, sizeof(keystr) - 1, "LPQ/%s", printer);
77 data.dptr = (unsigned char *)job_list;
78 data.dsize = num_jobs * sizeof(struct vlp_job);
79 tdb_store_bystring(tdb, keystr, data, TDB_REPLACE);
82 /* Return the next job number for a printer */
84 static int next_jobnum(char *printer)
86 fstring keystr;
87 int jobnum;
89 slprintf(keystr, sizeof(keystr) - 1, "JOBNUM/%s", printer);
91 tdb_lock_bystring(tdb, keystr);
93 jobnum = tdb_fetch_int32(tdb, keystr);
95 /* Create next job index if none exists */
97 if (jobnum == -1) {
98 jobnum = atoi(PRINT_FIRSTJOB);
101 jobnum++;
102 tdb_store_int32(tdb, keystr, jobnum);
104 tdb_unlock_bystring(tdb, keystr);
106 return jobnum;
109 static void set_printer_status(char *printer, int status)
111 fstring keystr;
112 int result;
114 slprintf(keystr, sizeof(keystr) - 1, "STATUS/%s", printer);
115 result = tdb_store_int32(tdb, keystr, status);
118 static int get_printer_status(char *printer)
120 fstring keystr;
121 TDB_DATA data;
123 slprintf(keystr, sizeof(keystr) - 1, "STATUS/%s", printer);
125 data.dptr = (unsigned char *)keystr;
126 data.dsize = strlen(keystr) + 1;
128 if (!tdb_exists(tdb, data)) {
129 set_printer_status(printer, LPSTAT_OK);
130 return LPSTAT_OK;
133 return tdb_fetch_int32(tdb, keystr);
136 /* Display printer queue */
138 static int lpq_command(int argc, char **argv)
140 char *printer;
141 struct vlp_job *job_list = NULL;
142 int i, num_jobs, job_count = 0;
144 if (argc != 2) {
145 printf("Usage: lpq <printername>\n");
146 return 1;
149 printer = argv[1];
151 /* Display printer status */
153 switch (get_printer_status(printer)) {
154 case LPSTAT_OK:
155 printf("enabled\n");
156 break;
157 case LPSTAT_STOPPED:
158 printf("disabled\n");
159 break;
160 case LPSTAT_ERROR:
161 default:
162 printf("error\n");
163 break;
166 /* Print queued documents */
168 get_job_list(printer, &job_list, &num_jobs);
170 for (i = 0; i < num_jobs; i++) {
171 if (job_list[i].deleted) continue;
172 printf("%d\t%d\t%d\t%ld\t%s\t%s\n", job_list[i].jobid,
173 job_list[i].size,
174 (i == 0 && job_list[i].status == LPQ_QUEUED) ?
175 LPQ_SPOOLING : job_list[i].status,
176 (long int)job_list[i].submit_time, job_list[i].owner,
177 job_list[i].jobname);
178 job_count++;
181 free(job_list);
183 return 0;
186 /* Remove a job */
188 static int lprm_command(int argc, char **argv)
190 char *printer;
191 int jobid, num_jobs, i;
192 struct vlp_job *job_list;
194 if (argc < 3) {
195 printf("Usage: lprm <printername> <jobid>\n");
196 return 1;
199 printer = argv[1];
200 jobid = atoi(argv[2]);
202 get_job_list(printer, &job_list, &num_jobs);
204 for (i = 0; i < num_jobs; i++) {
205 if (job_list[i].jobid == jobid) {
206 job_list[i].deleted = 1;
207 set_job_list(printer, job_list, num_jobs);
208 break;
212 return 0;
215 /* print command = print-test %p %s */
217 static int print_command(int argc, char **argv)
219 char *printer;
220 fstring keystr;
221 struct passwd *pw;
222 TDB_DATA value, queue;
223 struct vlp_job job;
225 if (argc < 3) {
226 printf("Usage: print <printername> <jobname>\n");
227 return 1;
230 printer = argv[1];
232 ZERO_STRUCT(job);
234 /* Create a job record */
236 slprintf(job.jobname, sizeof(job.jobname) - 1, "%s", argv[2]);
238 if (!(pw = getpwuid(getuid()))) {
239 return 1;
242 slprintf(job.owner, sizeof(job.owner) - 1, "%s", pw->pw_name);
244 job.jobid = next_jobnum(printer);
245 job.size = 666;
246 job.submit_time = time(NULL);
248 /* Store job entry in queue */
250 slprintf(keystr, sizeof(keystr) - 1, "LPQ/%s", printer);
252 value = tdb_fetch_bystring(tdb, keystr);
254 if (value.dptr) {
256 /* Add job to end of queue */
258 queue.dptr = (unsigned char *)malloc(value.dsize + sizeof(struct vlp_job));
259 if (!queue.dptr) return 1;
261 memcpy(queue.dptr, value.dptr, value.dsize);
262 memcpy(queue.dptr + value.dsize, &job, sizeof(struct vlp_job));
264 queue.dsize = value.dsize + sizeof(struct vlp_job);
266 tdb_store_bystring(tdb, keystr, queue, TDB_REPLACE);
268 free(queue.dptr);
270 } else {
272 /* Create new queue */
273 queue.dptr = (unsigned char *)&job;
274 queue.dsize = sizeof(struct vlp_job);
276 tdb_store_bystring(tdb, keystr, queue, TDB_REPLACE);
279 return 0;
282 /* Pause the queue */
284 static int queuepause_command(int argc, char **argv)
286 char *printer;
288 if (argc != 2) {
289 printf("Usage: queuepause <printername>\n");
290 return 1;
293 printer = argv[1];
294 set_printer_status(printer, LPSTAT_STOPPED);
296 return 0;
299 /* Resume the queue */
301 static int queueresume_command(int argc, char **argv)
303 char *printer;
305 if (argc != 2) {
306 printf("Usage: queueresume <printername>\n");
307 return 1;
310 printer = argv[1];
311 set_printer_status(printer, LPSTAT_OK);
313 return 0;
316 /* Pause a job */
318 static int lppause_command(int argc, char **argv)
320 struct vlp_job *job_list;
321 char *printer;
322 int jobid, num_jobs, i;
324 if (argc != 3) {
325 printf("Usage: lppause <printername> <jobid>\n");
326 return 1;
329 printer = argv[1];
330 jobid = atoi(argv[2]);
332 get_job_list(printer, &job_list, &num_jobs);
334 for (i = 0; i < num_jobs; i++) {
335 if (job_list[i].jobid == jobid) {
336 job_list[i].status = LPQ_PAUSED;
337 set_job_list(printer, job_list, num_jobs);
338 return 0;
342 return 1;
345 /* Resume a job */
347 static int lpresume_command(int argc, char **argv)
349 struct vlp_job *job_list;
350 char *printer;
351 int jobid, num_jobs, i;
353 if (argc != 3) {
354 printf("Usage: lpresume <printername> <jobid>\n");
355 return 1;
358 printer = argv[1];
359 jobid = atoi(argv[2]);
361 get_job_list(printer, &job_list, &num_jobs);
363 for (i = 0; i < num_jobs; i++) {
364 if (job_list[i].jobid == jobid) {
365 job_list[i].status = LPQ_QUEUED;
366 set_job_list(printer, job_list, num_jobs);
367 return 0;
371 return 1;
374 int main(int argc, char **argv)
376 /* Parameter check */
377 const char *printdb_path = NULL;
379 if (argc < 2) {
380 usage();
381 return 1;
384 if (strncmp(argv[1], "tdbfile", strlen("tdbfile")) != 0) {
385 usage();
386 return 1;
389 printdb_path = get_string_param(argv[1]);
390 if (!printdb_path) {
391 return 1;
394 if (!(tdb = tdb_open(printdb_path, 0, 0, O_RDWR | O_CREAT,
395 0666))) {
396 printf("%s: unable to open %s\n", argv[0], printdb_path);
397 return 1;
400 /* Ensure we are modes 666 */
402 chmod(printdb_path, 0666);
404 /* Do commands */
406 if (strcmp(argv[2], "lpq") == 0) {
407 return lpq_command(argc - 2, &argv[2]);
410 if (strcmp(argv[2], "lprm") == 0) {
411 return lprm_command(argc - 2, &argv[2]);
414 if (strcmp(argv[2], "print") == 0) {
415 return print_command(argc - 2, &argv[2]);
418 if (strcmp(argv[2], "queuepause") == 0) {
419 return queuepause_command(argc - 2, &argv[2]);
422 if (strcmp(argv[2], "queueresume") == 0) {
423 return queueresume_command(argc - 2, &argv[2]);
426 if (strcmp(argv[2], "lppause") == 0) {
427 return lppause_command(argc - 2, &argv[2]);
430 if (strcmp(argv[2], "lpresume") == 0) {
431 return lpresume_command(argc - 2, &argv[2]);
434 /* Unknown command */
436 printf("%s: invalid command %s\n", argv[0], argv[1]);
437 return 1;