2 * Copyright (c) 2004, Richard Levitte <richard@levitte.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <libfildef.h>
35 #include <lib$routines.h>
37 #include <str$routines.h>
43 /* Because some compiler options hide this macor */
45 #define EVMSERR 65535 /* error for non-translatable VMS errors */
48 struct LP_dir_context_st
50 unsigned long VMS_context;
52 char filespec[NAML$C_MAXRSS+1];
53 char result[NAML$C_MAXRSS+1];
58 struct dsc$descriptor_d filespec_dsc;
59 struct dsc$descriptor_d result_dsc;
62 const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
67 unsigned long flags = 0;
69 flags |= LIB$M_FIL_LONG_NAMES;
72 if (ctx == NULL || directory == NULL)
81 size_t filespeclen = strlen(directory);
82 char *filespec = NULL;
90 /* MUST be a VMS directory specification! Let's estimate if it is. */
91 if (directory[filespeclen-1] != ']'
92 && directory[filespeclen-1] != '>'
93 && directory[filespeclen-1] != ':')
99 filespeclen += 4; /* "*.*;" */
109 errno = ENAMETOOLONG;
113 *ctx = (LP_DIR_CTX *)malloc(sizeof(LP_DIR_CTX));
119 memset(*ctx, '\0', sizeof(LP_DIR_CTX));
121 strcpy((*ctx)->filespec,directory);
122 strcat((*ctx)->filespec,"*.*;");
123 (*ctx)->filespec_dsc.dsc$w_length = filespeclen;
124 (*ctx)->filespec_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
125 (*ctx)->filespec_dsc.dsc$b_class = DSC$K_CLASS_S;
126 (*ctx)->filespec_dsc.dsc$a_pointer = (*ctx)->filespec;
127 (*ctx)->result_dsc.dsc$w_length = 0;
128 (*ctx)->result_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
129 (*ctx)->result_dsc.dsc$b_class = DSC$K_CLASS_D;
130 (*ctx)->result_dsc.dsc$a_pointer = 0;
133 (*ctx)->result_dsc.dsc$w_length = 0;
134 (*ctx)->result_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
135 (*ctx)->result_dsc.dsc$b_class = DSC$K_CLASS_D;
136 (*ctx)->result_dsc.dsc$a_pointer = 0;
138 status = lib$find_file(&(*ctx)->filespec_dsc, &(*ctx)->result_dsc,
139 &(*ctx)->VMS_context, 0, 0, 0, &flags);
141 if (status == RMS$_NMF)
148 if(!$VMS_STATUS_SUCCESS(status))
155 /* Quick, cheap and dirty way to discard any device and directory,
156 since we only want file names */
157 l = (*ctx)->result_dsc.dsc$w_length;
158 p = (*ctx)->result_dsc.dsc$a_pointer;
162 if (*p == '^' && p[1] != '\0') /* Take care of ODS-5 escapes */
166 else if (*p == ':' || *p == '>' || *p == ']')
178 strncpy((*ctx)->result, r, l);
179 (*ctx)->result[l] = '\0';
180 str$free1_dx(&(*ctx)->result_dsc);
182 return (*ctx)->result;
185 int LP_find_file_end(LP_DIR_CTX **ctx)
187 if (ctx != NULL && *ctx != NULL)
189 int status = lib$find_file_end(&(*ctx)->VMS_context);
193 if(!$VMS_STATUS_SUCCESS(status))