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