summaryrefslogtreecommitdiff
path: root/usr/src/cmd/print/lpstat/bsd-functions.c
blob: 8b5b42fe463c41baa71b05bd5020c29e4dbe9d28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1998 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <syslog.h>
#ifndef SUNOS_4
#include <libintl.h>
#endif
extern char *getenv(const char *);

#include <print/ns.h>
#include <print/network.h>
#include <print/misc.h>
#include <print/list.h>
#include <print/job.h>

#include "bsd-functions.h"


static char *order[] = {
	"", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th" };

static char *
show_rank(int i)
{
	static char rank[12];
	if ((i%100)/10 == 1)
		(void) sprintf(rank, "%dth", i);
	else
		(void) sprintf(rank, "%d%s", i, order[i%10]);
	return (rank);
}


static int
vadd_file(jobfile_t *file, va_list ap)
{
	char *mesg = va_arg(ap, char *);

	if (file != NULL) {
		(void) strlcat(mesg, file->jf_name, BUFSIZ);
		(void) strlcat(mesg, " ", BUFSIZ);
		return (file->jf_size);
	}
	return (0);
}

/*ARGSUSED*/
static int
vprint_file(jobfile_t *file, va_list ap)
{
	if (file != NULL)
		(void) printf("\t%-33.33s%ld bytes\n", file->jf_name,
			file->jf_size);
	return (0);
}

static int
vprint_job(job_t *job, va_list ap)
{
	int	jobSize = 0,
		*rank,
		format,
		curr,
		printIt = 0,
		ac;
	char	fileList[BUFSIZ],
		*printer,
		**av;

	printer = va_arg(ap, char *);
	format = va_arg(ap, int);
	rank = va_arg(ap, int *);
	ac = va_arg(ap, int);
	av = va_arg(ap, char **);

	if (strcmp(job->job_printer, printer) != 0)
		return (0);

	if (ac > 0) {
		for (curr = 0; curr < ac; curr++)
			if ((av[curr][0] >= '0') && (av[curr][0] <= '9') &&
				(job->job_id == atoi(av[curr])) ||
				(strcmp(job->job_user, av[curr]) == 0)) {
					printIt++;
					break;
			}
	} else
		printIt++;

	if (printIt != 0) {
		if (format == SHOW_QUEUE_SHORT_REQUEST) {
			(void) memset(fileList, 0, sizeof (fileList));
			jobSize = list_iterate((void **)job->job_df_list,
				(VFUNC_T)vadd_file, fileList);
			(void) printf(
				"%-7.7s%-8.8s	  %3.3d  %-38.38s%d bytes\n",
				show_rank((*rank)++), job->job_user,
				job->job_id, fileList, jobSize);
		} else {
			(void) printf("%s: %-7.7s \t\t\t\t [job %.3d%s]\n",
				job->job_user, show_rank((*rank)++),
				job->job_id, job->job_host);
			(void) list_iterate((void **)job->job_df_list,
					(VFUNC_T)vprint_file);
			(void) printf("\n");
		}
	}
	return (0);
}

static int
vjob_count(job_t *job, va_list ap)
{
	int	curr,
		ac;
	char	*printer,
		**av;

	printer = va_arg(ap, char *);
	ac = va_arg(ap, int);
	av = va_arg(ap, char **);

	if (strcmp(job->job_printer, printer) != 0)
		return (0);

	if (ac == 0)
		return (1);

	for (curr = 0; curr < ac; curr++)
		if ((av[curr][0] >= '0') && (av[curr][0] <= '9') &&
			(job->job_id == atoi(av[curr])) ||
			(strcmp(job->job_user, av[curr]) == 0))
				return (1);

	return (0);
}


void
clear_screen()	 /* for now use tput rather than link in UCB stuff */
{
	(void) system("/bin/tput clear");
}


int
bsd_queue(ns_bsd_addr_t *binding, int format, int ac, char *av[])
{
	char	*server = NULL,
		*printer = NULL;
	job_t	**list = NULL;
	int	nd = -1,
		idle = 0,
		rc;

	server = binding->server;
	printer = binding->printer;

	if ((nd = net_open(server, 15)) >= 0) {
		char buf[BUFSIZ];

		(void) memset(buf, 0, sizeof (buf));
		while (ac--) { /* potential SEGV if av's are more than BUFSIZ */
			(void) strlcat(buf, av[ac], sizeof (buf));
			if (strlcat(buf, " ", sizeof (buf)) >= sizeof (buf)) {
				syslog(LOG_ERR, "bsd_queue: buffer overflow");
			}
		}

		rc = net_printf(nd, "%c%s %s\n", format, printer, buf);
		if (rc < 0)
			syslog(LOG_ERR, "net_printf() failed: %m");
#ifdef SUNOS_4
		do {
		(void) memset(buf, 0, sizeof (buf));
		if ((ac = net_read(nd, buf, sizeof (buf))) > 0)
			(void) printf("%s", buf);
		} while (ac > 0);
#else
		while (memset(buf, 0, sizeof (buf)) &&
			(net_read(nd, buf, sizeof (buf)) > 0)) {
			(void) printf("%s", buf);
			if (strstr(buf, "no entries") != 0)
				idle = 1;
		}
#endif

		(void) net_close(nd);
	}

	if (nd < 0) {
		if (server != NULL)
			(void) fprintf(stderr, gettext(
				"could not talk to print service at %s\n"),
				server);
		else
			(void) fprintf(stderr, gettext(
				"could not locate server for printer: %s\n"),
				printer);
	}

	if (((list = job_list_append(NULL, printer,
						server, SPOOL_DIR)) != NULL) &&
	    (list_iterate((void **)list, (VFUNC_T)vjob_count, printer,
			ac, av) != 0)) {
		if ((nd < 0) && (format == SHOW_QUEUE_SHORT_REQUEST))
			(void) printf(gettext(
			"Rank\tOwner	Job\tFiles\t\t\t\t\tTotal Size\n"));
	}

	if (format == SHOW_QUEUE_LONG_REQUEST)
		(void) printf("\n");

	nd = 1;
	(void) list_iterate((void **)list, (VFUNC_T)vprint_job, printer, format,
			&nd, ac, av);

	if ((idle == 1) && (list == NULL))
		return (1);
	else
		return (0);
}