00001 /* 00002 00003 Copyright (C) 2000, 2001 Silicon Graphics, Inc. All Rights Reserved. 00004 00005 This program is free software; you can redistribute it and/or modify it 00006 under the terms of version 2.1 of the GNU Lesser General Public License 00007 as published by the Free Software Foundation. 00008 00009 This program is distributed in the hope that it would be useful, but 00010 WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00012 00013 Further, this software is distributed without any warranty that it is 00014 free of the rightful claim of any third person regarding infringement 00015 or the like. Any license provided herein, whether implied or 00016 otherwise, applies only to this software file. Patent licenses, if 00017 any, provided herein do not apply to combinations of this program with 00018 other software, or any other product whatsoever. 00019 00020 You should have received a copy of the GNU Lesser General Public 00021 License along with this program; if not, write the Free Software 00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, 00023 USA. 00024 00025 Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pky, 00026 Mountain View, CA 94043, or: 00027 00028 http://www.sgi.com 00029 00030 For further information regarding this notice, see: 00031 00032 http://oss.sgi.com/projects/GenInfo/NoticeExplan 00033 00034 */ 00035 00036 00037 #pragma ident "@(#) libf/pxf/pxfopendir.c 92.1 06/29/99 11:36:06" 00038 00039 #include <errno.h> 00040 #include <liberrno.h> 00041 #include <fortran.h> 00042 #include <string.h> 00043 #include <malloc.h> 00044 #include <sys/types.h> 00045 #include <dirent.h> 00046 #include "table.h" 00047 00048 /* PXFOPENDIR -- open directory 00049 * (section 5.1.2 of Posix 1003.9-1992) 00050 * 00051 * Synopsis: 00052 * SUBROUTINE PXFOPENDIR(DIRNAME,LENDIRNAME,IOPENDIRID,IERROR) 00053 * CHARACTER*(*) DIRNAME 00054 * INTEGER LENDIRNAME,IOPENDIR,IERROR 00055 * 00056 * Function Description: 00057 * The routine PXFOPENDIR uses opendir() (3C) to open a directory 00058 * directory stream for the directory DIRNAME and positions the 00059 * stream at the first directory entry. 00060 * 00061 * Description of arguments: 00062 * DIRNAME is an input character array variable containing the path for 00063 * directory to be opened. 00064 * 00065 * LENDIRNAME is an input integer variable containing the length of 00066 * DIRNAME. 00067 * 00068 * IOPENDIRID is an output integer variable for the directory ID. 00069 * 00070 * IERROR is an output integer variable that will contain 00071 * the status: 00072 * 00073 * zero - PXFOPENDIR was successful. 00074 * 00075 * nonzero - PXFOPENDIR was not successful. 00076 * 00077 * PXFOPENDIR may return any of the following error 00078 * values: 00079 * 00080 * EACCES If a component of DIRNAME denies search permission. 00081 * 00082 * ENAMETOOLONG If the length of the DIRNAME argument exceeds 00083 * PATH_MAX found in <limits.h>. (IRIX and Solaris only) 00084 * 00085 * ENOENT If the directory in the DIRNAME argument does not exist. 00086 * 00087 * ENOTDIR If a component of DIRNAME is not a directory. 00088 * 00089 * EINVAL If LENDIRNAME < 0 or LENDIRNAME > LEN(DIRNAME). 00090 * 00091 * ENOMEM If memory needed by PXFOPENDIR could not be allocated. 00092 * 00093 * ELOOP If the the DIRNAME argument contains too many symbolic 00094 * links. (Solaris only) 00095 * 00096 * EMFILE If too many file descriptors are currently open for the process. 00097 * 00098 * ENFILE If too many file descriptors are currently open for the system. 00099 * (Solaris and IRIX only) 00100 * 00101 */ 00102 00103 #ifdef _UNICOS 00104 void 00105 PXFOPENDIR( 00106 #else 00107 void 00108 _PXFOPENDIR( 00109 #endif 00110 _fcd DIRNAME, 00111 _f_int *LENDIRNAME, 00112 _f_int *IOPENDIRID, 00113 _f_int *IERROR 00114 ) 00115 { 00116 int ilendirname; 00117 char *cdirname; 00118 DIR *dirptr; 00119 _f_int copendirid; 00120 00121 ilendirname = *LENDIRNAME; 00122 00123 /* check DIRNAME length */ 00124 if (ilendirname < 0 || ilendirname > __fcdlen(DIRNAME)) { 00125 *IERROR = EINVAL; 00126 return; 00127 } 00128 00129 if (*LENDIRNAME == 0) { 00130 /* 00131 * If length is zero, user wants trailing blanks stripped. 00132 * Otherwise, malloc memory and copy the string adding a 00133 * NULL terminator. 00134 */ 00135 00136 cdirname = _fc_acopy(DIRNAME); 00137 if (cdirname == NULL) { 00138 *IERROR = ENOMEM; 00139 return; 00140 } 00141 00142 } else { 00143 00144 cdirname = (char *)malloc(ilendirname + 1); 00145 if (cdirname != NULL) { 00146 (void)strncpy(cdirname, _fcdtocp(DIRNAME), ilendirname); 00147 cdirname[ilendirname] = '\0'; 00148 } else { 00149 *IERROR = ENOMEM; 00150 return; 00151 } 00152 00153 } 00154 00155 if ((dirptr = opendir(cdirname)) == NULL) { 00156 free(cdirname); 00157 *IERROR = errno; 00158 return; 00159 } 00160 00161 /* assign the dirptr. NOTE: The user is now responsible to manage the 00162 * the memory */ 00163 copendirid = _table_add(&_pxfdir_table, dirptr); 00164 if (copendirid > 0) { 00165 /* negate the id value so readdir can determine if the corresponding 00166 * DIR structure has been used to read directory entries. 00167 */ 00168 *IOPENDIRID = -copendirid; 00169 *IERROR = 0; 00170 } else { 00171 *IERROR = ENOMEM; 00172 } 00173 00174 free(cdirname); 00175 } 00176 00177 #ifndef _UNICOS 00178 void 00179 pxfopendir_( 00180 char *DIRNAME, 00181 _f_int *LENDIRNAME, 00182 _f_int *IOPENDIRID, 00183 _f_int *IERROR, 00184 _f_int dirnamelen 00185 ) 00186 { 00187 _PXFOPENDIR(_cptofcd(DIRNAME,dirnamelen),LENDIRNAME, 00188 IOPENDIRID,IERROR); 00189 } 00190 #endif 00191
1.5.6