xref: /illumos-gate/usr/src/cmd/lp/lib/class/getclass.c (revision e3ae4b35c024af1196582063ecee3ab79367227d)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 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 /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
31 
32 #include "stdio.h"
33 #include "string.h"
34 #include "errno.h"
35 #include "sys/types.h"
36 #include <syslog.h>
37 
38 #include "lp.h"
39 #include "class.h"
40 
41 /**
42  ** getclass() - READ CLASS FROM TO DISK
43  **/
44 
45 CLASS *
46 getclass(char *name)
47 {
48 	static long		lastdir		= -1;
49 
50 	CLASS		*clsp;
51 
52 	char			*file,
53 				buf[BUFSIZ];
54 
55 	int fd;
56 
57 	syslog(LOG_DEBUG, "getclass(%s)", name ? name : "");
58 
59 	if (!name || !*name) {
60 		errno = EINVAL;
61 		return (0);
62 	}
63 
64 	/*
65 	 * Getting ``all''? If so, jump into the directory
66 	 * wherever we left off.
67 	 */
68 	if (STREQU(NAME_ALL, name)) {
69 		if (!(name = next_file(Lp_A_Classes, &lastdir)))
70 			return (0);
71 	} else
72 		lastdir = -1;
73 
74 	/*
75 	 * Get the class list.
76 	 */
77 
78 	if (!(file = getclassfile(name)))
79 		return (0);
80 
81 	if ((fd = open_locked(file, "r", 0)) < 0) {
82 		Free (file);
83 		return (0);
84 	}
85 	Free (file);
86 
87 	clsp = (CLASS *)calloc(sizeof (*clsp), 1);
88 
89 	if (!(clsp->name = Strdup(name))) {
90 		Free (clsp);
91 		close(fd);
92 		errno = ENOMEM;
93 		return (0);
94 	}
95 
96 	clsp->members = 0;
97 	errno = 0;
98 	while (fdgets(buf, BUFSIZ, fd)) {
99 		buf[strlen(buf) - 1] = 0;
100 		addlist (&clsp->members, buf);
101 	}
102 	if (errno != 0) {
103 		int			save_errno = errno;
104 
105 		freelist (clsp->members);
106 		Free (clsp->name);
107 		Free (clsp);
108 		close(fd);
109 		errno = save_errno;
110 		return (0);
111 	}
112 	close(fd);
113 
114 	if (!clsp->members) {
115 		Free (clsp->name);
116 		Free (clsp);
117 		errno = EBADF;
118 		return (0);
119 	}
120 
121 	return (clsp);
122 }
123