2 * $LP: LPlib/source/LPdir_unix.c,v 1.11 2004/09/23 22:07:22 _cvs_levitte Exp
6 * Copyright (c) 2004, Richard Levitte <richard@levitte.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/types.h>
43 * The POSIXly macro for the maximum number of characters in a file path is
44 * NAME_MAX. However, some operating systems use PATH_MAX instead.
45 * Therefore, it seems natural to first check for PATH_MAX and use that, and
46 * if it doesn't exist, use NAME_MAX.
49 # define LP_ENTRY_SIZE PATH_MAX
50 #elif defined(NAME_MAX)
51 # define LP_ENTRY_SIZE NAME_MAX
55 * Of course, there's the possibility that neither PATH_MAX nor NAME_MAX
56 * exist. It's also possible that NAME_MAX exists but is define to a very
57 * small value (HP-UX offers 14), so we need to check if we got a result, and
58 * if it meets a minimum standard, and create or change it if not.
60 #if !defined(LP_ENTRY_SIZE) || LP_ENTRY_SIZE<255
62 # define LP_ENTRY_SIZE 255
65 struct LP_dir_context_st {
67 char entry_name[LP_ENTRY_SIZE + 1];
70 const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
72 struct dirent *direntry = NULL;
74 if (ctx == NULL || directory == NULL) {
81 *ctx = malloc(sizeof(**ctx));
86 memset(*ctx, 0, sizeof(**ctx));
88 (*ctx)->dir = opendir(directory);
89 if ((*ctx)->dir == NULL) {
90 int save_errno = errno; /* Probably not needed, but I'm paranoid */
98 direntry = readdir((*ctx)->dir);
99 if (direntry == NULL) {
103 strncpy((*ctx)->entry_name, direntry->d_name,
104 sizeof((*ctx)->entry_name) - 1);
105 (*ctx)->entry_name[sizeof((*ctx)->entry_name) - 1] = '\0';
106 return (*ctx)->entry_name;
109 int LP_find_file_end(LP_DIR_CTX **ctx)
111 if (ctx != NULL && *ctx != NULL) {
112 int ret = closedir((*ctx)->dir);