1*355b4669Sjacobs /*
2*355b4669Sjacobs * CDDL HEADER START
3*355b4669Sjacobs *
4*355b4669Sjacobs * The contents of this file are subject to the terms of the
5*355b4669Sjacobs * Common Development and Distribution License (the "License").
6*355b4669Sjacobs * You may not use this file except in compliance with the License.
7*355b4669Sjacobs *
8*355b4669Sjacobs * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*355b4669Sjacobs * or http://www.opensolaris.org/os/licensing.
10*355b4669Sjacobs * See the License for the specific language governing permissions
11*355b4669Sjacobs * and limitations under the License.
12*355b4669Sjacobs *
13*355b4669Sjacobs * When distributing Covered Code, include this CDDL HEADER in each
14*355b4669Sjacobs * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*355b4669Sjacobs * If applicable, add the following below this CDDL HEADER, with the
16*355b4669Sjacobs * fields enclosed by brackets "[]" replaced with your own identifying
17*355b4669Sjacobs * information: Portions Copyright [yyyy] [name of copyright owner]
18*355b4669Sjacobs *
19*355b4669Sjacobs * CDDL HEADER END
20*355b4669Sjacobs */
21*355b4669Sjacobs
22*355b4669Sjacobs /*
23*355b4669Sjacobs * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24*355b4669Sjacobs * Use is subject to license terms.
25*355b4669Sjacobs *
26*355b4669Sjacobs */
27*355b4669Sjacobs
28*355b4669Sjacobs /* $Id: lpq.c 146 2006-03-24 00:26:54Z njacobs $ */
29*355b4669Sjacobs
30*355b4669Sjacobs #include <stdio.h>
31*355b4669Sjacobs #include <stdlib.h>
32*355b4669Sjacobs #include <unistd.h>
33*355b4669Sjacobs #include <string.h>
34*355b4669Sjacobs #include <locale.h>
35*355b4669Sjacobs #include <libintl.h>
36*355b4669Sjacobs #include <papi.h>
37*355b4669Sjacobs #include "common.h"
38*355b4669Sjacobs
39*355b4669Sjacobs static void
usage(char * program)40*355b4669Sjacobs usage(char *program)
41*355b4669Sjacobs {
42*355b4669Sjacobs char *name;
43*355b4669Sjacobs
44*355b4669Sjacobs if ((name = strrchr(program, '/')) == NULL)
45*355b4669Sjacobs name = program;
46*355b4669Sjacobs else
47*355b4669Sjacobs name++;
48*355b4669Sjacobs
49*355b4669Sjacobs fprintf(stdout, gettext("Usage: %s [-P printer] (user|id ...)\n"),
50*355b4669Sjacobs name);
51*355b4669Sjacobs exit(1);
52*355b4669Sjacobs }
53*355b4669Sjacobs
54*355b4669Sjacobs static void
clear_screen()55*355b4669Sjacobs clear_screen()
56*355b4669Sjacobs {
57*355b4669Sjacobs static char buf[32];
58*355b4669Sjacobs
59*355b4669Sjacobs /* quick and dirty for now, this should be fixed real soon */
60*355b4669Sjacobs if (buf[0] == '\0') {
61*355b4669Sjacobs FILE *fp = popen("/bin/tput clear", "r");
62*355b4669Sjacobs if (fp != NULL) {
63*355b4669Sjacobs fgets(buf, sizeof (buf), fp);
64*355b4669Sjacobs fclose(fp);
65*355b4669Sjacobs }
66*355b4669Sjacobs }
67*355b4669Sjacobs printf("%s", buf);
68*355b4669Sjacobs }
69*355b4669Sjacobs
70*355b4669Sjacobs int
main(int ac,char * av[])71*355b4669Sjacobs main(int ac, char *av[])
72*355b4669Sjacobs {
73*355b4669Sjacobs char *printer = NULL;
74*355b4669Sjacobs papi_status_t status;
75*355b4669Sjacobs papi_service_t svc = NULL;
76*355b4669Sjacobs papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
77*355b4669Sjacobs int format = 3; /* lpq short format */
78*355b4669Sjacobs int interval = 0;
79*355b4669Sjacobs int num_jobs;
80*355b4669Sjacobs int c;
81*355b4669Sjacobs
82*355b4669Sjacobs (void) setlocale(LC_ALL, "");
83*355b4669Sjacobs (void) textdomain("SUNW_OST_OSCMD");
84*355b4669Sjacobs
85*355b4669Sjacobs while ((c = getopt(ac, av, "EP:l")) != EOF)
86*355b4669Sjacobs switch (c) {
87*355b4669Sjacobs case 'E':
88*355b4669Sjacobs encryption = PAPI_ENCRYPT_REQUIRED;
89*355b4669Sjacobs break;
90*355b4669Sjacobs case 'P':
91*355b4669Sjacobs printer = optarg;
92*355b4669Sjacobs break;
93*355b4669Sjacobs case 'l':
94*355b4669Sjacobs format = 4; /* lpq long format */
95*355b4669Sjacobs break;
96*355b4669Sjacobs default:
97*355b4669Sjacobs usage(av[0]);
98*355b4669Sjacobs }
99*355b4669Sjacobs
100*355b4669Sjacobs if ((optind < ac) && (av[optind][0] == '+'))
101*355b4669Sjacobs interval = atoi(av[optind++]);
102*355b4669Sjacobs
103*355b4669Sjacobs if ((printer == NULL) &&
104*355b4669Sjacobs ((printer = getenv("PRINTER")) == NULL) &&
105*355b4669Sjacobs ((printer = getenv("LPDEST")) == NULL))
106*355b4669Sjacobs printer = DEFAULT_DEST;
107*355b4669Sjacobs
108*355b4669Sjacobs status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback,
109*355b4669Sjacobs encryption, NULL);
110*355b4669Sjacobs if (status != PAPI_OK) {
111*355b4669Sjacobs fprintf(stderr, gettext(
112*355b4669Sjacobs "Failed to contact service for %s: %s\n"), printer,
113*355b4669Sjacobs verbose_papi_message(svc, status));
114*355b4669Sjacobs papiServiceDestroy(svc);
115*355b4669Sjacobs exit(1);
116*355b4669Sjacobs }
117*355b4669Sjacobs
118*355b4669Sjacobs do {
119*355b4669Sjacobs if (interval != 0)
120*355b4669Sjacobs clear_screen();
121*355b4669Sjacobs
122*355b4669Sjacobs num_jobs = berkeley_queue_report(svc, stdout, printer, format,
123*355b4669Sjacobs ac - optind, &av[optind]);
124*355b4669Sjacobs
125*355b4669Sjacobs if ((interval != 0) && (num_jobs > 0))
126*355b4669Sjacobs sleep(interval);
127*355b4669Sjacobs } while ((interval > 0) && (num_jobs > 0));
128*355b4669Sjacobs
129*355b4669Sjacobs papiServiceDestroy(svc);
130*355b4669Sjacobs
131*355b4669Sjacobs return (0);
132*355b4669Sjacobs }
133