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