1878f85ac16f2ddd689e1a0643f5611768db89ab
[tprouty/samba.git] / source / 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         char **lines;
51         int i;
52
53         lines = file_lines_pload("/usr/bin/lpstat -v", NULL);
54         if (!lines) return;
55
56         for (i=0;lines[i];i++) {
57                 printer_t *ptmp;
58                 char *name, *tmp;
59                 char *buf = lines[i];
60
61                 /* eat "system/device for " */
62                 if (((tmp = strchr_m(buf, ' ')) == NULL) ||
63                     ((tmp = strchr_m(++tmp, ' ')) == NULL))
64                         continue;
65
66                 /*
67                  * In case we're only at the "for ".
68                  */
69
70                 if(!strncmp("for ",++tmp,4)) {
71                         tmp=strchr_m(tmp, ' ');
72                         tmp++;
73                 }
74
75                 /* Eat whitespace. */
76
77                 while(*tmp == ' ')
78                         ++tmp;
79
80                 /*
81                  * On HPUX there is an extra line that can be ignored.
82                  * d.thibadeau 2001/08/09
83                  */
84                 if(!strncmp("remote to",tmp,9))
85                         continue;
86
87                 name = tmp;
88
89                 /* truncate the ": ..." */
90                 if ((tmp = strchr_m(name, ':')) != NULL)
91                         *tmp = '\0';
92                 
93                 /* add it to the cache */
94                 if ((ptmp = malloc(sizeof (*ptmp))) != NULL) {
95                         ZERO_STRUCTP(ptmp);
96                         if((ptmp->name = strdup(name)) == NULL)
97                                 DEBUG(0,("populate_printers: malloc fail in strdup !\n"));
98                         ptmp->next = printers;
99                         printers = ptmp;
100                 } else {
101                         DEBUG(0,("populate_printers: malloc fail for ptmp\n"));
102                 }
103         }
104
105         file_lines_free(lines);
106 }
107
108
109 /*
110  * provide the equivalent of pcap_printer_fn() for SVID/XPG4 conforming
111  * systems.  It was unclear why pcap_printer_fn() was tossing names longer
112  * than 8 characters.  I suspect that its a protocol limit, but amazingly
113  * names longer than 8 characters appear to work with my test
114  * clients (Win95/NT).
115  */
116 void sysv_printer_fn(void (*fn)(char *, char *))
117 {
118         printer_t *tmp;
119
120         if (printers == NULL)
121                 populate_printers();
122         for (tmp = printers; tmp != NULL; tmp = tmp->next)
123                 (fn)(tmp->name, "");
124 }
125
126
127 /*
128  * provide the equivalent of pcap_printername_ok() for SVID/XPG4 conforming
129  * systems.
130  */
131 int sysv_printername_ok(char *name)
132 {
133         printer_t *tmp;
134
135         if (printers == NULL)
136                 populate_printers();
137         for (tmp = printers; tmp != NULL; tmp = tmp->next)
138                 if (strcmp(tmp->name, name) == 0)
139                         return (True);
140         return (False);
141 }
142
143 #else
144 /* this keeps fussy compilers happy */
145  void print_svid_dummy(void);
146  void print_svid_dummy(void) {}
147 #endif