xref: /illumos-gate/usr/src/cmd/lp/cmd/lpadmin/pick_opts.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 /*
23  * Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	All Rights Reserved   */
29 
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <stdio.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <limits.h>
37 #include <sys/types.h>
38 #include <stdlib.h>
39 #include <libintl.h>
40 
41 #include "lp.h"
42 #include "class.h"
43 #include "printers.h"
44 #include "msgs.h"
45 
46 char **
47 pick_opts(char * infile_opts, char ** new_opts)
48 {
49 	char * flasts = NULL;
50 	char * old_opt;
51 	char ** final_opts = NULL;
52 	int key_len;
53 	int keyfound = 0;
54 	char ** head;
55 
56 	if (infile_opts == NULL || new_opts == NULL) {
57 	(void) printf("lpadmin error: Cannot process -o options");
58 		return (NULL);
59 	}
60 
61 	head = new_opts;
62 	for (; *new_opts != NULL; new_opts++) {
63 		if (strlen(*new_opts) > (strcspn(*new_opts, "=") + 1)) {
64 			if ((addlist(&final_opts, *new_opts)) != 0) {
65 				fprintf(stderr,
66 				    gettext("lpadmin: System Error %d\n"),
67 				    errno);
68 
69 				return (NULL);
70 			}
71 		}
72 	}
73 	/*
74 	 * For each currently set option, ie, those already in the file,
75 	 * compare to new list from lpadmin (new_opts).
76 	 */
77 	for (old_opt = strtok_r(infile_opts, LP_SEP, &flasts);
78 		old_opt != NULL; old_opt = strtok_r(NULL, LP_SEP, &flasts)) {
79 
80 		keyfound = 0;
81 
82 		for (new_opts = head; *new_opts != NULL; new_opts++) {
83 
84 			key_len = strcspn(*new_opts, "=");
85 			/*
86 			 * if the keys match, and the the key from the
87 			 * lpadmin -o has a value, take the new value from
88 			 * lpadmin
89 			 */
90 			if ((strncmp(old_opt, *new_opts, key_len + 1)) == 0) {
91 				keyfound++;
92 			}
93 		}
94 		if (keyfound == 0) {
95 			if ((addlist(&final_opts, old_opt)) != 0) {
96 				fprintf(stderr,
97 				    gettext("lpadmin: System Error %d\n"),
98 				    errno);
99 
100 				return (NULL);
101 			}
102 		}
103 
104 	}
105 
106 	return (final_opts);
107 }
108