xref: /onnv-gate/usr/src/common/openssl/crypto/LPdir_unix.c (revision 2139:6243c3338933)
1*2139Sjp161948 /* $LP: LPlib/source/LPdir_unix.c,v 1.11 2004/09/23 22:07:22 _cvs_levitte Exp $ */
2*2139Sjp161948 /*
3*2139Sjp161948  * Copyright (c) 2004, Richard Levitte <richard@levitte.org>
4*2139Sjp161948  * All rights reserved.
5*2139Sjp161948  *
6*2139Sjp161948  * Redistribution and use in source and binary forms, with or without
7*2139Sjp161948  * modification, are permitted provided that the following conditions
8*2139Sjp161948  * are met:
9*2139Sjp161948  * 1. Redistributions of source code must retain the above copyright
10*2139Sjp161948  *    notice, this list of conditions and the following disclaimer.
11*2139Sjp161948  * 2. Redistributions in binary form must reproduce the above copyright
12*2139Sjp161948  *    notice, this list of conditions and the following disclaimer in the
13*2139Sjp161948  *    documentation and/or other materials provided with the distribution.
14*2139Sjp161948  *
15*2139Sjp161948  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*2139Sjp161948  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*2139Sjp161948  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18*2139Sjp161948  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19*2139Sjp161948  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20*2139Sjp161948  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21*2139Sjp161948  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22*2139Sjp161948  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23*2139Sjp161948  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24*2139Sjp161948  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25*2139Sjp161948  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*2139Sjp161948  */
27*2139Sjp161948 
28*2139Sjp161948 #include <stddef.h>
29*2139Sjp161948 #include <stdlib.h>
30*2139Sjp161948 #include <limits.h>
31*2139Sjp161948 #include <string.h>
32*2139Sjp161948 #include <sys/types.h>
33*2139Sjp161948 #include <dirent.h>
34*2139Sjp161948 #include <errno.h>
35*2139Sjp161948 #ifndef LPDIR_H
36*2139Sjp161948 #include "LPdir.h"
37*2139Sjp161948 #endif
38*2139Sjp161948 
39*2139Sjp161948 /* The POSIXly macro for the maximum number of characters in a file path
40*2139Sjp161948    is NAME_MAX.  However, some operating systems use PATH_MAX instead.
41*2139Sjp161948    Therefore, it seems natural to first check for PATH_MAX and use that,
42*2139Sjp161948    and if it doesn't exist, use NAME_MAX. */
43*2139Sjp161948 #if defined(PATH_MAX)
44*2139Sjp161948 # define LP_ENTRY_SIZE PATH_MAX
45*2139Sjp161948 #elif defined(NAME_MAX)
46*2139Sjp161948 # define LP_ENTRY_SIZE NAME_MAX
47*2139Sjp161948 #endif
48*2139Sjp161948 
49*2139Sjp161948 /* Of course, there's the possibility that neither PATH_MAX nor NAME_MAX
50*2139Sjp161948    exist.  It's also possible that NAME_MAX exists but is define to a
51*2139Sjp161948    very small value (HP-UX offers 14), so we need to check if we got a
52*2139Sjp161948    result, and if it meets a minimum standard, and create or change it
53*2139Sjp161948    if not. */
54*2139Sjp161948 #if !defined(LP_ENTRY_SIZE) || LP_ENTRY_SIZE<255
55*2139Sjp161948 # undef LP_ENTRY_SIZE
56*2139Sjp161948 # define LP_ENTRY_SIZE 255
57*2139Sjp161948 #endif
58*2139Sjp161948 
59*2139Sjp161948 struct LP_dir_context_st
60*2139Sjp161948 {
61*2139Sjp161948   DIR *dir;
62*2139Sjp161948   char entry_name[LP_ENTRY_SIZE+1];
63*2139Sjp161948 };
64*2139Sjp161948 
LP_find_file(LP_DIR_CTX ** ctx,const char * directory)65*2139Sjp161948 const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
66*2139Sjp161948 {
67*2139Sjp161948   struct dirent *direntry = NULL;
68*2139Sjp161948 
69*2139Sjp161948   if (ctx == NULL || directory == NULL)
70*2139Sjp161948     {
71*2139Sjp161948       errno = EINVAL;
72*2139Sjp161948       return 0;
73*2139Sjp161948     }
74*2139Sjp161948 
75*2139Sjp161948   errno = 0;
76*2139Sjp161948   if (*ctx == NULL)
77*2139Sjp161948     {
78*2139Sjp161948       *ctx = (LP_DIR_CTX *)malloc(sizeof(LP_DIR_CTX));
79*2139Sjp161948       if (*ctx == NULL)
80*2139Sjp161948 	{
81*2139Sjp161948 	  errno = ENOMEM;
82*2139Sjp161948 	  return 0;
83*2139Sjp161948 	}
84*2139Sjp161948       memset(*ctx, '\0', sizeof(LP_DIR_CTX));
85*2139Sjp161948 
86*2139Sjp161948       (*ctx)->dir = opendir(directory);
87*2139Sjp161948       if ((*ctx)->dir == NULL)
88*2139Sjp161948 	{
89*2139Sjp161948 	  int save_errno = errno; /* Probably not needed, but I'm paranoid */
90*2139Sjp161948 	  free(*ctx);
91*2139Sjp161948 	  *ctx = NULL;
92*2139Sjp161948 	  errno = save_errno;
93*2139Sjp161948 	  return 0;
94*2139Sjp161948 	}
95*2139Sjp161948     }
96*2139Sjp161948 
97*2139Sjp161948   direntry = readdir((*ctx)->dir);
98*2139Sjp161948   if (direntry == NULL)
99*2139Sjp161948     {
100*2139Sjp161948       return 0;
101*2139Sjp161948     }
102*2139Sjp161948 
103*2139Sjp161948   strncpy((*ctx)->entry_name, direntry->d_name, sizeof((*ctx)->entry_name) - 1);
104*2139Sjp161948   (*ctx)->entry_name[sizeof((*ctx)->entry_name) - 1] = '\0';
105*2139Sjp161948   return (*ctx)->entry_name;
106*2139Sjp161948 }
107*2139Sjp161948 
LP_find_file_end(LP_DIR_CTX ** ctx)108*2139Sjp161948 int LP_find_file_end(LP_DIR_CTX **ctx)
109*2139Sjp161948 {
110*2139Sjp161948   if (ctx != NULL && *ctx != NULL)
111*2139Sjp161948     {
112*2139Sjp161948       int ret = closedir((*ctx)->dir);
113*2139Sjp161948 
114*2139Sjp161948       free(*ctx);
115*2139Sjp161948       switch (ret)
116*2139Sjp161948 	{
117*2139Sjp161948 	case 0:
118*2139Sjp161948 	  return 1;
119*2139Sjp161948 	case -1:
120*2139Sjp161948 	  return 0;
121*2139Sjp161948 	default:
122*2139Sjp161948 	  break;
123*2139Sjp161948 	}
124*2139Sjp161948     }
125*2139Sjp161948   errno = EINVAL;
126*2139Sjp161948   return 0;
127*2139Sjp161948 }
128