summaryrefslogtreecommitdiff
path: root/source3/printing/print_svid.c
blob: 85eaf8f95dd5a38fe5cd8d53e5c2c19e7e5ee02e (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
/*
 * Copyright (C) 1997-1998 by Norm Jacobs, Colorado Springs, Colorado, USA
 * Copyright (C) 1997-1998 by Sun Microsystem, Inc.
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * This module implements support for gathering and comparing available
 * printer information on a SVID or XPG4 compliant system.  It does this
 * through the use of the SVID/XPG4 command "lpstat(1)".
 *
 * The expectations is that execution of the command "lpstat -v" will
 * generate responses in the form of:
 *
 *	device for serial: /dev/term/b
 *	system for fax: server
 *	system for color: server (as printer chroma)
 */


#include "includes.h"
#include "smb.h"

#ifdef SYSV

extern int DEBUGLEVEL;

typedef struct printer {
	char *name;
	struct printer *next;
} printer_t;
static printer_t *printers = NULL;

static void populate_printers(void)
{
	FILE *fp;

	if ((fp = popen("/usr/bin/lpstat -v", "r")) != NULL) {
		char buf[BUFSIZ];

		while (fgets(buf, sizeof (buf), fp) != NULL) {
			printer_t *ptmp;
			char *name, *tmp;

			/* eat "system/device for " */
			if (((tmp = strchr(buf, ' ')) == NULL) ||
			    ((tmp = strchr(++tmp, ' ')) == NULL))
				continue;

			/*
			 * In case we're only at the "for ".
			 */

            if(!strncmp("for ",++tmp,4))
            {
				tmp=strchr(tmp, ' ');
				tmp++;
			}
			name = tmp;

			/* truncate the ": ..." */
			if ((tmp = strchr(name, ':')) != NULL)
				*tmp = '\0';

			/* add it to the cache */
			if ((ptmp = malloc(sizeof (*ptmp))) != NULL) {
				ZERO_STRUCTP(ptmp);
				if((ptmp->name = strdup(name)) == NULL)
					DEBUG(0,("populate_printers: malloc fail in strdup !\n"));
				ptmp->next = printers;
				printers = ptmp;
			} else {
				DEBUG(0,("populate_printers: malloc fail for ptmp\n"));
			}
		}
		pclose(fp);
	} else {
		DEBUG(0,( "Unable to run lpstat!\n"));
	}
}


/*
 * provide the equivalent of pcap_printer_fn() for SVID/XPG4 conforming
 * systems.  It was unclear why pcap_printer_fn() was tossing names longer
 * than 8 characters.  I suspect that its a protocol limit, but amazingly
 * names longer than 8 characters appear to work with my test
 * clients (Win95/NT).
 */
void sysv_printer_fn(void (*fn)(char *, char *))
{
	printer_t *tmp;

	if (printers == NULL)
		populate_printers();
	for (tmp = printers; tmp != NULL; tmp = tmp->next)
		(fn)(tmp->name, "");
}


/*
 * provide the equivalent of pcap_printername_ok() for SVID/XPG4 conforming
 * systems.
 */
int sysv_printername_ok(char *name)
{
	printer_t *tmp;

	if (printers == NULL)
		populate_printers();
	for (tmp = printers; tmp != NULL; tmp = tmp->next)
		if (strcmp(tmp->name, name) == 0)
			return (True);
	return (False);
}

#else
/* this keeps fussy compilers happy */
 void print_svid_dummy(void);
 void print_svid_dummy(void) {}
#endif