Fix two memory leaks in an error path in idmap.c
[Samba/gebeck_regimport.git] / testsuite / printing / vlp.c
blob15459889e9fc50ffd55171b8e14eefbc158d4bc6
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"
24 #define PRINT_TDB "/tmp/vlp.tdb"
25 #define PRINT_FIRSTJOB "100"
27 static TDB_CONTEXT *tdb;
29 struct vlp_job {
30 fstring owner;
31 int jobid;
32 fstring jobname;
33 int size;
34 int status;
35 time_t submit_time;
36 int deleted;
39 /* Print usage */
41 static void usage(void)
43 printf("Usage: print-test lpq|lprm|print|queuepause|queueresume|"
44 "lppause|lpresume [args]\n");
47 /* Return an array of vlp jobs that is the printer queue */
49 static void get_job_list(char *printer, struct vlp_job **job_list,
50 int *num_jobs)
52 fstring keystr;
53 TDB_DATA data;
55 slprintf(keystr, sizeof(keystr) - 1, "LPQ/%s", printer);
56 data = tdb_fetch_bystring(tdb, keystr);
58 *job_list = (struct vlp_job *)data.dptr;
59 *num_jobs = data.dsize / sizeof(struct vlp_job);
62 /* Store an array of vl jobs for the queue */
64 static void set_job_list(char *printer, struct vlp_job *job_list,
65 int num_jobs)
67 fstring keystr;
68 TDB_DATA data;
70 slprintf(keystr, sizeof(keystr) - 1, "LPQ/%s", printer);
72 data.dptr = (unsigned char *)job_list;
73 data.dsize = num_jobs * sizeof(struct vlp_job);
74 tdb_store_bystring(tdb, keystr, data, TDB_REPLACE);
77 /* Return the next job number for a printer */
79 static int next_jobnum(char *printer)
81 fstring keystr;
82 int jobnum;
84 slprintf(keystr, sizeof(keystr) - 1, "JOBNUM/%s", printer);
86 tdb_lock_bystring(tdb, keystr);
88 jobnum = tdb_fetch_int32(tdb, keystr);
90 /* Create next job index if none exists */
92 if (jobnum == -1) {
93 jobnum = atoi(PRINT_FIRSTJOB);
96 jobnum++;
97 tdb_store_int32(tdb, keystr, jobnum);
99 tdb_unlock_bystring(tdb, keystr);
101 return jobnum;
104 static void set_printer_status(char *printer, int status)
106 fstring keystr;
107 int result;
109 slprintf(keystr, sizeof(keystr) - 1, "STATUS/%s", printer);
110 result = tdb_store_int32(tdb, keystr, status);
113 static int get_printer_status(char *printer)
115 fstring keystr;
116 TDB_DATA data;
118 slprintf(keystr, sizeof(keystr) - 1, "STATUS/%s", printer);
120 data.dptr = (unsigned char *)keystr;
121 data.dsize = strlen(keystr) + 1;
123 if (!tdb_exists(tdb, data)) {
124 set_printer_status(printer, LPSTAT_OK);
125 return LPSTAT_OK;
128 return tdb_fetch_int32(tdb, keystr);
131 /* Display printer queue */
133 static int lpq_command(int argc, char **argv)
135 char *printer;
136 struct vlp_job *job_list = NULL;
137 int i, num_jobs, job_count = 0;
139 if (argc != 2) {
140 printf("Usage: lpq <printername>\n");
141 return 1;
144 printer = argv[1];
146 /* Display printer status */
148 switch (get_printer_status(printer)) {
149 case LPSTAT_OK:
150 printf("enabled\n");
151 break;
152 case LPSTAT_STOPPED:
153 printf("disabled\n");
154 break;
155 case LPSTAT_ERROR:
156 default:
157 printf("error\n");
158 break;
161 /* Print queued documents */
163 get_job_list(printer, &job_list, &num_jobs);
165 for (i = 0; i < num_jobs; i++) {
166 if (job_list[i].deleted) continue;
167 printf("%d\t%d\t%d\t%ld\t%s\t%s\n", job_list[i].jobid,
168 job_list[i].size,
169 (i == 0 && job_list[i].status == LPQ_QUEUED) ?
170 LPQ_SPOOLING : job_list[i].status,
171 job_list[i].submit_time, job_list[i].owner,
172 job_list[i].jobname);
173 job_count++;
176 free(job_list);
178 return 0;
181 /* Remove a job */
183 static int lprm_command(int argc, char **argv)
185 char *printer;
186 int jobid, num_jobs, i;
187 struct vlp_job *job_list;
189 if (argc < 3) {
190 printf("Usage: lprm <printername> <jobid>\n");
191 return 1;
194 printer = argv[1];
195 jobid = atoi(argv[2]);
197 get_job_list(printer, &job_list, &num_jobs);
199 for (i = 0; i < num_jobs; i++) {
200 if (job_list[i].jobid == jobid) {
201 job_list[i].deleted = 1;
202 set_job_list(printer, job_list, num_jobs);
203 break;
207 return 0;
210 /* print command = print-test %p %s */
212 static int print_command(int argc, char **argv)
214 char *printer;
215 fstring keystr;
216 struct passwd *pw;
217 TDB_DATA value, queue;
218 struct vlp_job job;
219 int i;
221 if (argc < 3) {
222 printf("Usage: print <printername> <jobname>\n");
223 return 1;
226 printer = argv[1];
228 ZERO_STRUCT(job);
230 /* Create a job record */
232 for (i = 2; i < argc; i++) {
233 fstrcat(job.jobname, argv[i]);
234 if (i < argc - 1) {
235 fstrcat(job.jobname, " ");
239 if (!(pw = getpwuid(getuid()))) {
240 return 1;
243 fstrcpy(job.owner, pw->pw_name);
245 job.jobid = next_jobnum(printer);
246 job.size = 666;
247 job.submit_time = time(NULL);
249 /* Store job entry in queue */
251 slprintf(keystr, sizeof(keystr) - 1, "LPQ/%s", printer);
253 value = tdb_fetch_bystring(tdb, keystr);
255 if (value.dptr) {
257 /* Add job to end of queue */
259 queue.dptr = (unsigned char *)SMB_MALLOC(value.dsize +
260 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 */
380 if (argc == 1) {
381 usage();
382 return 1;
385 /* Initialise */
387 if (!(tdb = tdb_open(PRINT_TDB, 0, 0, O_RDWR | O_CREAT,
388 0666))) {
389 printf("%s: unable to open %s\n", argv[0], PRINT_TDB);
390 return 1;
393 /* Ensure we are modes 666 */
395 chmod(PRINT_TDB, 0666);
397 /* Do commands */
399 if (strcmp(argv[1], "lpq") == 0) {
400 return lpq_command(argc - 1, &argv[1]);
403 if (strcmp(argv[1], "lprm") == 0) {
404 return lprm_command(argc - 1, &argv[1]);
407 if (strcmp(argv[1], "print") == 0) {
408 return print_command(argc - 1, &argv[1]);
411 if (strcmp(argv[1], "queuepause") == 0) {
412 return queuepause_command(argc - 1, &argv[1]);
415 if (strcmp(argv[1], "queueresume") == 0) {
416 return queueresume_command(argc - 1, &argv[1]);
419 if (strcmp(argv[1], "lppause") == 0) {
420 return lppause_command(argc - 1, &argv[1]);
423 if (strcmp(argv[1], "lpresume") == 0) {
424 return lpresume_command(argc - 1, &argv[1]);
427 /* Unknown command */
429 printf("%s: invalid command %s\n", argv[0], argv[1]);
430 return 1;