xref: /titanic_51/usr/src/cmd/lp/lib/class/getclass.c (revision 0a44ef6d9afbfe052a7e975f55ea0d2954b62a82)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0a44ef6dSjacobs  * Common Development and Distribution License (the "License").
6*0a44ef6dSjacobs  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*0a44ef6dSjacobs 
227c478bd9Sstevel@tonic-gate /*
23*0a44ef6dSjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
31*0a44ef6dSjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include "stdio.h"
357c478bd9Sstevel@tonic-gate #include "string.h"
367c478bd9Sstevel@tonic-gate #include "errno.h"
377c478bd9Sstevel@tonic-gate #include "sys/types.h"
38*0a44ef6dSjacobs #include <syslog.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include "lp.h"
417c478bd9Sstevel@tonic-gate #include "class.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /**
447c478bd9Sstevel@tonic-gate  ** getclass() - READ CLASS FROM TO DISK
457c478bd9Sstevel@tonic-gate  **/
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate CLASS *
487c478bd9Sstevel@tonic-gate getclass(char *name)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	static long		lastdir		= -1;
517c478bd9Sstevel@tonic-gate 
52*0a44ef6dSjacobs 	CLASS		*clsp;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	char			*file,
557c478bd9Sstevel@tonic-gate 				buf[BUFSIZ];
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	int fd;
587c478bd9Sstevel@tonic-gate 
59*0a44ef6dSjacobs 	syslog(LOG_DEBUG, "getclass(%s)", name ? name : "");
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	if (!name || !*name) {
627c478bd9Sstevel@tonic-gate 		errno = EINVAL;
637c478bd9Sstevel@tonic-gate 		return (0);
647c478bd9Sstevel@tonic-gate 	}
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	/*
677c478bd9Sstevel@tonic-gate 	 * Getting ``all''? If so, jump into the directory
687c478bd9Sstevel@tonic-gate 	 * wherever we left off.
697c478bd9Sstevel@tonic-gate 	 */
707c478bd9Sstevel@tonic-gate 	if (STREQU(NAME_ALL, name)) {
717c478bd9Sstevel@tonic-gate 		if (!(name = next_file(Lp_A_Classes, &lastdir)))
727c478bd9Sstevel@tonic-gate 			return (0);
737c478bd9Sstevel@tonic-gate 	} else
747c478bd9Sstevel@tonic-gate 		lastdir = -1;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	/*
777c478bd9Sstevel@tonic-gate 	 * Get the class list.
787c478bd9Sstevel@tonic-gate 	 */
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	if (!(file = getclassfile(name)))
817c478bd9Sstevel@tonic-gate 		return (0);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	if ((fd = open_locked(file, "r", 0)) < 0) {
847c478bd9Sstevel@tonic-gate 		Free (file);
857c478bd9Sstevel@tonic-gate 		return (0);
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 	Free (file);
887c478bd9Sstevel@tonic-gate 
89*0a44ef6dSjacobs 	clsp = (CLASS *)calloc(sizeof (*clsp), 1);
90*0a44ef6dSjacobs 
91*0a44ef6dSjacobs 	if (!(clsp->name = Strdup(name))) {
92*0a44ef6dSjacobs 		Free (clsp);
937c478bd9Sstevel@tonic-gate 		close(fd);
947c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
957c478bd9Sstevel@tonic-gate 		return (0);
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
98*0a44ef6dSjacobs 	clsp->members = 0;
997c478bd9Sstevel@tonic-gate 	errno = 0;
1007c478bd9Sstevel@tonic-gate 	while (fdgets(buf, BUFSIZ, fd)) {
1017c478bd9Sstevel@tonic-gate 		buf[strlen(buf) - 1] = 0;
102*0a44ef6dSjacobs 		addlist (&clsp->members, buf);
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 	if (errno != 0) {
1057c478bd9Sstevel@tonic-gate 		int			save_errno = errno;
1067c478bd9Sstevel@tonic-gate 
107*0a44ef6dSjacobs 		freelist (clsp->members);
108*0a44ef6dSjacobs 		Free (clsp->name);
109*0a44ef6dSjacobs 		Free (clsp);
1107c478bd9Sstevel@tonic-gate 		close(fd);
1117c478bd9Sstevel@tonic-gate 		errno = save_errno;
1127c478bd9Sstevel@tonic-gate 		return (0);
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 	close(fd);
1157c478bd9Sstevel@tonic-gate 
116*0a44ef6dSjacobs 	if (!clsp->members) {
117*0a44ef6dSjacobs 		Free (clsp->name);
118*0a44ef6dSjacobs 		Free (clsp);
1197c478bd9Sstevel@tonic-gate 		errno = EBADF;
1207c478bd9Sstevel@tonic-gate 		return (0);
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 
123*0a44ef6dSjacobs 	return (clsp);
1247c478bd9Sstevel@tonic-gate }
125