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