5c5eebeb5028c193c59661af4fe5cddb2a6aec15
[kai/samba.git] / source3 / printing / print_svid.c
1 /*
2  * Copyright (C) 1997-1998 by Norm Jacobs, Colorado Springs, Colorado, USA
3  * Copyright (C) 1997-1998 by Sun Microsystem, Inc.
4  * All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * 
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 /*
22  * This module implements support for gathering and comparing available
23  * printer information on a SVID or XPG4 compliant system.  It does this
24  * through the use of the SVID/XPG4 command "lpstat(1)".
25  *
26  * The expectations is that execution of the command "lpstat -v" will
27  * generate responses in the form of:
28  *
29  *      device for serial: /dev/term/b
30  *      system for fax: server
31  *      system for color: server (as printer chroma)
32  */
33
34
35 #include "includes.h"
36 #include "smb.h"
37
38 #ifdef SYSV
39
40 extern int DEBUGLEVEL;
41
42 typedef struct printer {
43         char *name;
44         struct printer *next;
45 } printer_t;
46 static printer_t *printers = NULL;
47
48 static void populate_printers(void)
49 {
50         FILE *fp;
51
52         if ((fp = popen("/usr/bin/lpstat -v", "r")) != NULL) {
53                 char buf[BUFSIZ];
54
55                 while (fgets(buf, sizeof (buf), fp) != NULL) {
56                         printer_t *ptmp;
57                         char *name, *tmp;
58
59                         /* eat "system/device for " */
60                         if (((tmp = strchr(buf, ' ')) == NULL) ||
61                             ((tmp = strchr(++tmp, ' ')) == NULL))
62                                 continue;
63                         name = ++tmp;
64
65                         /* truncate the ": ..." */
66                         if ((tmp = strchr(name, ':')) != NULL)
67                                 *tmp = '\0';
68
69                         /* add it to the cache */
70                         if ((ptmp = malloc(sizeof (*ptmp))) != NULL) {
71                                 ZERO_STRUCTP(ptmp);
72                                 ptmp->name = strdup(name);
73                                 ptmp->next = printers;
74                                 printers = ptmp;
75                         }
76                 }
77                 pclose(fp);
78         } else {
79                 DEBUG(0,( "Unable to run lpstat!\n"));
80         }
81 }
82
83
84 /*
85  * provide the equivalent of pcap_printer_fn() for SVID/XPG4 conforming
86  * systems.  It was unclear why pcap_printer_fn() was tossing names longer
87  * than 8 characters.  I suspect that its a protocol limit, but amazingly
88  * names longer than 8 characters appear to work with my test
89  * clients (Win95/NT).
90  */
91 void sysv_printer_fn(void (*fn)(char *, char *))
92 {
93         printer_t *tmp;
94
95         if (printers == NULL)
96                 populate_printers();
97         for (tmp = printers; tmp != NULL; tmp = tmp->next)
98                 (fn)(tmp->name, "");
99 }
100
101
102 /*
103  * provide the equivalent of pcap_printername_ok() for SVID/XPG4 conforming
104  * systems.
105  */
106 int sysv_printername_ok(char *name)
107 {
108         printer_t *tmp;
109
110         if (printers == NULL)
111                 populate_printers();
112         for (tmp = printers; tmp != NULL; tmp = tmp->next)
113                 if (strcmp(tmp->name, name) == 0)
114                         return (True);
115         return (False);
116 }
117
118 #else
119 /* this keeps fussy compilers happy */
120  void print_svid_dummy(void);
121  void print_svid_dummy(void) {}
122 #endif