1 /* $LP: LPlib/source/LPdir_vms.c,v 1.20 2004/08/26 13:36:05 _cvs_levitte Exp $ */
3 * Copyright (c) 2004, Richard Levitte <richard@levitte.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <libfildef.h>
36 #include <lib$routines.h>
38 #include <str$routines.h>
45 /* Some compiler options hide EVMSERR. */
47 # define EVMSERR 65535 /* error for non-translatable VMS errors */
50 struct LP_dir_context_st
52 unsigned long VMS_context;
53 char filespec[ NAMX_MAXRSS+ 1];
54 char result[ NAMX_MAXRSS+ 1];
55 struct dsc$descriptor_d filespec_dsc;
56 struct dsc$descriptor_d result_dsc;
59 const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
64 unsigned long flags = 0;
66 /* Arrange 32-bit pointer to (copied) string storage, if needed. */
67 #if __INITIAL_POINTER_SIZE == 64
68 # pragma pointer_size save
69 # pragma pointer_size 32
70 char *ctx_filespec_32p;
71 # pragma pointer_size restore
72 char ctx_filespec_32[ NAMX_MAXRSS+ 1];
73 #endif /* __INITIAL_POINTER_SIZE == 64 */
76 flags |= LIB$M_FIL_LONG_NAMES;
79 if (ctx == NULL || directory == NULL)
88 size_t filespeclen = strlen(directory);
89 char *filespec = NULL;
91 /* MUST be a VMS directory specification! Let's estimate if it is. */
92 if (directory[filespeclen-1] != ']'
93 && directory[filespeclen-1] != '>'
94 && directory[filespeclen-1] != ':')
100 filespeclen += 4; /* "*.*;" */
102 if (filespeclen > NAMX_MAXRSS)
104 errno = ENAMETOOLONG;
108 *ctx = (LP_DIR_CTX *)malloc(sizeof(LP_DIR_CTX));
114 memset(*ctx, '\0', sizeof(LP_DIR_CTX));
116 strcpy((*ctx)->filespec,directory);
117 strcat((*ctx)->filespec,"*.*;");
119 /* Arrange 32-bit pointer to (copied) string storage, if needed. */
120 #if __INITIAL_POINTER_SIZE == 64
121 # define CTX_FILESPEC ctx_filespec_32p
122 /* Copy the file name to storage with a 32-bit pointer. */
123 ctx_filespec_32p = ctx_filespec_32;
124 strcpy( ctx_filespec_32p, (*ctx)->filespec);
125 #else /* __INITIAL_POINTER_SIZE == 64 */
126 # define CTX_FILESPEC (*ctx)->filespec
127 #endif /* __INITIAL_POINTER_SIZE == 64 [else] */
129 (*ctx)->filespec_dsc.dsc$w_length = filespeclen;
130 (*ctx)->filespec_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
131 (*ctx)->filespec_dsc.dsc$b_class = DSC$K_CLASS_S;
132 (*ctx)->filespec_dsc.dsc$a_pointer = CTX_FILESPEC;
135 (*ctx)->result_dsc.dsc$w_length = 0;
136 (*ctx)->result_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
137 (*ctx)->result_dsc.dsc$b_class = DSC$K_CLASS_D;
138 (*ctx)->result_dsc.dsc$a_pointer = 0;
140 status = lib$find_file(&(*ctx)->filespec_dsc, &(*ctx)->result_dsc,
141 &(*ctx)->VMS_context, 0, 0, 0, &flags);
143 if (status == RMS$_NMF)
150 if(!$VMS_STATUS_SUCCESS(status))
157 /* Quick, cheap and dirty way to discard any device and directory,
158 since we only want file names */
159 l = (*ctx)->result_dsc.dsc$w_length;
160 p = (*ctx)->result_dsc.dsc$a_pointer;
164 if (*p == '^' && p[1] != '\0') /* Take care of ODS-5 escapes */
168 else if (*p == ':' || *p == '>' || *p == ']')
180 strncpy((*ctx)->result, r, l);
181 (*ctx)->result[l] = '\0';
182 str$free1_dx(&(*ctx)->result_dsc);
184 return (*ctx)->result;
187 int LP_find_file_end(LP_DIR_CTX **ctx)
189 if (ctx != NULL && *ctx != NULL)
191 int status = lib$find_file_end(&(*ctx)->VMS_context);
195 if(!$VMS_STATUS_SUCCESS(status))