xref: /titanic_51/usr/src/cmd/lp/lib/class/getclass.c (revision fd845fc0cb4fbc8e85f974e2e4eaacca1cc26e81)
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 1997 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"	/* SVr4.0 1.9	*/
32 /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
33 
34 #include "stdio.h"
35 #include "string.h"
36 #include "errno.h"
37 #include "sys/types.h"
38 
39 #include "lp.h"
40 #include "class.h"
41 
42 /**
43  ** getclass() - READ CLASS FROM TO DISK
44  **/
45 
46 CLASS *
47 getclass(char *name)
48 {
49 	static long		lastdir		= -1;
50 
51 	static CLASS		clsbuf;
52 
53 	char			*file,
54 				buf[BUFSIZ];
55 
56 	int fd;
57 
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 	if (!(clsbuf.name = Strdup(name))) {
88 		close(fd);
89 		errno = ENOMEM;
90 		return (0);
91 	}
92 
93 	clsbuf.members = 0;
94 	errno = 0;
95 	while (fdgets(buf, BUFSIZ, fd)) {
96 		buf[strlen(buf) - 1] = 0;
97 		addlist (&clsbuf.members, buf);
98 	}
99 	if (errno != 0) {
100 		int			save_errno = errno;
101 
102 		freelist (clsbuf.members);
103 		Free (clsbuf.name);
104 		close(fd);
105 		errno = save_errno;
106 		return (0);
107 	}
108 	close(fd);
109 
110 	if (!clsbuf.members) {
111 		Free (clsbuf.name);
112 		errno = EBADF;
113 		return (0);
114 	}
115 
116 	return (&clsbuf);
117 }
118