xref: /illumos-gate/usr/src/cmd/lp/lib/filters/putfilter.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.9	*/
27 /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
28 
29 #include "errno.h"
30 #include "string.h"
31 #include "stdlib.h"
32 
33 #include "lp.h"
34 #include "filters.h"
35 
36 /**
37  ** putfilter() - PUT FILTER INTO FILTER TABLE
38  **/
39 
40 int
41 #if	defined(__STDC__)
putfilter(char * name,FILTER * flbufp)42 putfilter (
43 	char *			name,
44 	FILTER *		flbufp
45 )
46 #else
47 putfilter (name, flbufp)
48 	char			*name;
49 	FILTER			*flbufp;
50 #endif
51 {
52 	_FILTER			_flbuf;
53 
54 	register _FILTER	*pf;
55 
56 
57 	if (!name || !*name) {
58 		errno = EINVAL;
59 		return (-1);
60 	}
61 
62 	if (STREQU(NAME_ALL, name)) {
63 		errno = EINVAL;
64 		return (-1);
65 	}
66 
67 	_flbuf.name = Strdup(name);
68 	_flbuf.command = (flbufp->command? Strdup(flbufp->command) : 0);
69 	_flbuf.type = flbufp->type;
70 	_flbuf.printer_types = sl_to_typel(flbufp->printer_types);
71 	_flbuf.printers = duplist(flbufp->printers);
72 	_flbuf.input_types = sl_to_typel(flbufp->input_types);
73 	_flbuf.output_types = sl_to_typel(flbufp->output_types);
74 	if (!flbufp->templates)
75 		_flbuf.templates = 0;
76 	else if (!(_flbuf.templates = sl_to_templatel(flbufp->templates))) {
77 		free_filter (&_flbuf);
78 		errno = EBADF;
79 		return (-1);
80 	}
81 
82 	if (!filters && get_and_load() == -1 && errno != ENOENT) {
83 		free_filter (&_flbuf);
84 		return (-1);
85 	}
86 
87 	if (filters) {
88 
89 		if ((pf = search_filter(name)))
90 			free_filter (pf);
91 		else {
92 			nfilters++;
93 			filters = (_FILTER *)Realloc(
94 				(char *)filters,
95 				(nfilters + 1) * sizeof(_FILTER)
96 			);
97 			if (!filters) {
98 				free_filter (&_flbuf);
99 				errno = ENOMEM;
100 				return (-1);
101 			}
102 			filters[nfilters].name = 0;
103 			pf = filters + nfilters - 1;
104 		}
105 
106 	} else {
107 
108 		nfilters = 1;
109 		pf = filters = (_FILTER *)Malloc(
110 			(nfilters + 1) * sizeof(_FILTER)
111 		);
112 		if (!filters) {
113 			free_filter (&_flbuf);
114 			errno = ENOMEM;
115 			return (-1);
116 		}
117 		filters[nfilters].name = 0;
118 
119 	}
120 
121 	*pf = _flbuf;
122 
123 	return (dumpfilters((char *)0));
124 }
125