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