xref: /onnv-gate/usr/src/cmd/lp/lib/class/getclass.c (revision 3125:084bca4d4623)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3125Sjacobs  * Common Development and Distribution License (the "License").
6*3125Sjacobs  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*3125Sjacobs 
220Sstevel@tonic-gate /*
23*3125Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate 
31*3125Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include "stdio.h"
350Sstevel@tonic-gate #include "string.h"
360Sstevel@tonic-gate #include "errno.h"
370Sstevel@tonic-gate #include "sys/types.h"
38*3125Sjacobs #include <syslog.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include "lp.h"
410Sstevel@tonic-gate #include "class.h"
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /**
440Sstevel@tonic-gate  ** getclass() - READ CLASS FROM TO DISK
450Sstevel@tonic-gate  **/
460Sstevel@tonic-gate 
470Sstevel@tonic-gate CLASS *
getclass(char * name)480Sstevel@tonic-gate getclass(char *name)
490Sstevel@tonic-gate {
500Sstevel@tonic-gate 	static long		lastdir		= -1;
510Sstevel@tonic-gate 
52*3125Sjacobs 	CLASS		*clsp;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	char			*file,
550Sstevel@tonic-gate 				buf[BUFSIZ];
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	int fd;
580Sstevel@tonic-gate 
59*3125Sjacobs 	syslog(LOG_DEBUG, "getclass(%s)", name ? name : "");
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	if (!name || !*name) {
620Sstevel@tonic-gate 		errno = EINVAL;
630Sstevel@tonic-gate 		return (0);
640Sstevel@tonic-gate 	}
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	/*
670Sstevel@tonic-gate 	 * Getting ``all''? If so, jump into the directory
680Sstevel@tonic-gate 	 * wherever we left off.
690Sstevel@tonic-gate 	 */
700Sstevel@tonic-gate 	if (STREQU(NAME_ALL, name)) {
710Sstevel@tonic-gate 		if (!(name = next_file(Lp_A_Classes, &lastdir)))
720Sstevel@tonic-gate 			return (0);
730Sstevel@tonic-gate 	} else
740Sstevel@tonic-gate 		lastdir = -1;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	/*
770Sstevel@tonic-gate 	 * Get the class list.
780Sstevel@tonic-gate 	 */
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	if (!(file = getclassfile(name)))
810Sstevel@tonic-gate 		return (0);
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	if ((fd = open_locked(file, "r", 0)) < 0) {
840Sstevel@tonic-gate 		Free (file);
850Sstevel@tonic-gate 		return (0);
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate 	Free (file);
880Sstevel@tonic-gate 
89*3125Sjacobs 	clsp = (CLASS *)calloc(sizeof (*clsp), 1);
90*3125Sjacobs 
91*3125Sjacobs 	if (!(clsp->name = Strdup(name))) {
92*3125Sjacobs 		Free (clsp);
930Sstevel@tonic-gate 		close(fd);
940Sstevel@tonic-gate 		errno = ENOMEM;
950Sstevel@tonic-gate 		return (0);
960Sstevel@tonic-gate 	}
970Sstevel@tonic-gate 
98*3125Sjacobs 	clsp->members = 0;
990Sstevel@tonic-gate 	errno = 0;
1000Sstevel@tonic-gate 	while (fdgets(buf, BUFSIZ, fd)) {
1010Sstevel@tonic-gate 		buf[strlen(buf) - 1] = 0;
102*3125Sjacobs 		addlist (&clsp->members, buf);
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate 	if (errno != 0) {
1050Sstevel@tonic-gate 		int			save_errno = errno;
1060Sstevel@tonic-gate 
107*3125Sjacobs 		freelist (clsp->members);
108*3125Sjacobs 		Free (clsp->name);
109*3125Sjacobs 		Free (clsp);
1100Sstevel@tonic-gate 		close(fd);
1110Sstevel@tonic-gate 		errno = save_errno;
1120Sstevel@tonic-gate 		return (0);
1130Sstevel@tonic-gate 	}
1140Sstevel@tonic-gate 	close(fd);
1150Sstevel@tonic-gate 
116*3125Sjacobs 	if (!clsp->members) {
117*3125Sjacobs 		Free (clsp->name);
118*3125Sjacobs 		Free (clsp);
1190Sstevel@tonic-gate 		errno = EBADF;
1200Sstevel@tonic-gate 		return (0);
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 
123*3125Sjacobs 	return (clsp);
1240Sstevel@tonic-gate }
125