xref: /illumos-gate/usr/src/cmd/lp/lib/lp/next.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.10	*/
27 /* LINTLIBRARY */
28 
29 #include "string.h"
30 #include "errno.h"
31 
32 #include "lp.h"
33 
34 #if	defined(__STDC__)
35 static int		is ( char *, char *, unsigned int );
36 #else
37 static int		is();
38 #endif
39 
40 /**
41  ** next_x() - GO TO NEXT ENTRY UNDER PARENT DIRECTORY
42  **/
43 
44 char *
45 #if	defined(__STDC__)
next_x(char * parent,long * lastdirp,unsigned int what)46 next_x (
47 	char *			parent,
48 	long *			lastdirp,
49 	unsigned int		what
50 )
51 #else
52 next_x (parent, lastdirp, what)
53 	char			*parent;
54 	long			*lastdirp;
55 	unsigned int		what;
56 #endif
57 {
58 	DIR			*dirp;
59 
60 	register char		*ret = 0;
61 
62 	struct dirent		*direntp;
63 
64 
65 	if (!(dirp = Opendir(parent)))
66 		return (0);
67 
68 	if (*lastdirp != -1)
69 		Seekdir (dirp, *lastdirp);
70 
71 	do
72 		direntp = Readdir(dirp);
73 	while (
74 		direntp
75 	     && (
76 			STREQU(direntp->d_name, ".")
77 		     || STREQU(direntp->d_name, "..")
78 		     || !is(parent, direntp->d_name, what)
79 		)
80 	);
81 
82 	if (direntp) {
83 		if (!(ret = Strdup(direntp->d_name)))
84 			errno = ENOMEM;
85 		*lastdirp = Telldir(dirp);
86 	} else {
87 		errno = ENOENT;
88 		*lastdirp = -1;
89 	}
90 
91 	Closedir (dirp);
92 
93 	return (ret);
94 }
95 
96 static int
97 #if	defined(__STDC__)
is(char * parent,char * name,unsigned int what)98 is (
99 	char *			parent,
100 	char *			name,
101 	unsigned int		what
102 )
103 #else
104 is (parent, name, what)
105 	char			*parent;
106 	char			*name;
107 	unsigned int		what;
108 #endif
109 {
110 	char			*path;
111 
112 	struct stat		statbuf;
113 
114 	if (!(path = makepath(parent, name, (char *)0)))
115 		return (0);
116 	if (Stat(path, &statbuf) == -1) {
117 		Free (path);
118 		return (0);
119 	}
120 	Free (path);
121 	return ((statbuf.st_mode & S_IFMT) == what);
122 }
123