8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / lp / lib / access / loadaccess.c
blob9541528b22b3c23a971fa00f00555b884ae35845
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 1997 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.9 */
32 /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
34 #include "stdio.h"
35 #include "errno.h"
36 #include "string.h"
37 #include "stdlib.h"
39 #include "lp.h"
40 #include "access.h"
42 static char **_loadaccess ( char * );
44 /**
45 ** load_userform_access() - LOAD ALLOW/DENY LISTS FOR USER+FORM
46 **/
48 int
49 load_userform_access(char *form, char ***pallow, char ***pdeny)
51 return (loadaccess(Lp_A_Forms, form, "", pallow, pdeny));
54 /**
55 ** load_userprinter_access() - LOAD ALLOW/DENY LISTS FOR USER+PRINTER
56 **/
58 int
59 load_userprinter_access(char *printer, char ***pallow, char ***pdeny)
61 return (loadaccess(Lp_A_Printers, printer, UACCESSPREFIX, pallow,
62 pdeny));
65 /**
66 ** load_formprinter_access() - LOAD ALLOW/DENY LISTS FOR FORM+PRINTER
67 **/
69 int
70 load_formprinter_access(char *printer, char ***pallow, char ***pdeny)
72 return (loadaccess(Lp_A_Printers, printer, FACCESSPREFIX, pallow,
73 pdeny));
76 /**
77 ** load_paperprinter_access() - LOAD ALLOW/DENY LISTS FOR FORM+PRINTER
78 **/
80 int
81 load_paperprinter_access(char *printer, char ***pallow, char ***pdeny)
83 return (loadaccess(Lp_A_Printers, printer, PACCESSPREFIX, pallow,
84 pdeny));
87 /**
88 ** loadaccess() - LOAD ALLOW OR DENY LISTS
89 **/
91 int
92 loadaccess(char *dir, char *name, char *prefix, char ***pallow, char ***pdeny)
94 register char *allow_file = 0,
95 *deny_file = 0;
97 int ret;
99 if (
100 !(allow_file = getaccessfile(dir, name, prefix, ALLOWFILE))
101 || !(*pallow = _loadaccess(allow_file)) && errno != ENOENT
102 || !(deny_file = getaccessfile(dir, name, prefix, DENYFILE))
103 || !(*pdeny = _loadaccess(deny_file)) && errno != ENOENT
105 ret = -1;
106 else
107 ret = 0;
109 if (allow_file)
110 Free (allow_file);
111 if (deny_file)
112 Free (deny_file);
114 return (ret);
118 ** _loadaccess() - LOAD ALLOW OR DENY FILE
121 static char **
122 _loadaccess(char *file)
124 register size_t nalloc,
125 nlist;
127 register char **list;
129 int fd;
131 char buf[BUFSIZ];
134 if ((fd = open_locked(file, "r", 0)) < 0)
135 return (0);
138 * Preallocate space for the initial list. We'll always
139 * allocate one more than the list size, for the terminating null.
141 nalloc = ACC_MAX_GUESS;
142 list = (char **)Malloc((nalloc + 1) * sizeof(char *));
143 if (!list) {
144 close(fd);
145 errno = ENOMEM;
146 return (0);
149 errno = 0;
150 for (nlist = 0; fdgets(buf, BUFSIZ, fd); ) {
152 buf[strlen(buf) - 1] = 0;
155 * Allocate more space if needed.
157 if (nlist >= nalloc) {
158 nalloc += ACC_MAX_GUESS;
159 list = (char **)Realloc(
160 (char *)list,
161 (nalloc + 1) * sizeof(char *)
163 if (!list) {
164 close(fd);
165 return (0);
169 list[nlist] = Strdup(buf); /* if fail, minor problem */
170 list[++nlist] = 0;
173 if (errno != 0) {
174 int save_errno = errno;
176 close(fd);
177 freelist (list);
178 errno = save_errno;
179 return (0);
181 close(fd);
184 * If we have more space allocated than we need,
185 * return the extra.
187 if (nlist != nalloc) {
188 list = (char **)Realloc(
189 (char *)list,
190 (nlist + 1) * sizeof(char *)
192 if (!list) {
193 errno = ENOMEM;
194 return (0);
197 list[nlist] = 0;
199 return (list);