1 /* $LP: LPlib/source/LPdir_vms.c,v 1.17 2004/06/30 11:36:43 _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 REGENTS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <libfildef.h>
36 #include <lib$routines.h>
38 #include <str$routines.h>
44 struct LP_dir_context_st
46 unsigned long VMS_context;
48 char filespec[NAML$C_MAXRSS+1];
49 char result[NAML$C_MAXRSS+1];
54 struct dsc$descriptor_d filespec_dsc;
55 struct dsc$descriptor_d result_dsc;
58 const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
63 const unsigned long flags = 0;
65 flags |= LIB$M_FIL_LONG_NAMES;
68 if (ctx == NULL || directory == NULL)
77 size_t filespeclen = strlen(directory);
78 char *filespec = NULL;
80 /* MUST be a VMS directory specification! Let's estimate if it is. */
81 if (directory[filespeclen-1] != ']'
82 && directory[filespeclen-1] != '>'
83 && directory[filespeclen-1] != ':')
89 filespeclen += 4; /* "*.*;" */
103 *ctx = (LP_DIR_CTX *)malloc(sizeof(LP_DIR_CTX));
109 memset(*ctx, '\0', sizeof(LP_DIR_CTX));
111 strcpy((*ctx)->filespec,directory);
112 strcat((*ctx)->filespec,"*.*;");
113 (*ctx)->filespec_dsc.dsc$w_length = filespeclen;
114 (*ctx)->filespec_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
115 (*ctx)->filespec_dsc.dsc$b_class = DSC$K_CLASS_S;
116 (*ctx)->filespec_dsc.dsc$a_pointer = (*ctx)->filespec;
117 (*ctx)->result_dsc.dsc$w_length = 0;
118 (*ctx)->result_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
119 (*ctx)->result_dsc.dsc$b_class = DSC$K_CLASS_D;
120 (*ctx)->result_dsc.dsc$a_pointer = 0;
123 (*ctx)->result_dsc.dsc$w_length = 0;
124 (*ctx)->result_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
125 (*ctx)->result_dsc.dsc$b_class = DSC$K_CLASS_D;
126 (*ctx)->result_dsc.dsc$a_pointer = 0;
128 status = lib$find_file(&(*ctx)->filespec_dsc, &(*ctx)->result_dsc,
129 &(*ctx)->VMS_context, 0, 0, 0, &flags);
131 if (status == RMS$_NMF)
138 if(!$VMS_STATUS_SUCCESS(status))
145 /* Quick, cheap and dirty way to discard any device and directory,
146 since we only want file names */
147 l = (*ctx)->result_dsc.dsc$w_length;
148 p = (*ctx)->result_dsc.dsc$a_pointer;
152 if (*p == '^' && p[1] != '\0') /* Take care of ODS-5 escapes */
156 else if (*p == ':' || *p == '>' || *p == ']')
168 strncpy((*ctx)->result, r, l);
169 (*ctx)->result[l] = '\0';
170 str$free1_dx(&(*ctx)->result_dsc);
172 return (*ctx)->result;
175 int LP_find_file_end(LP_DIR_CTX **ctx)
177 if (ctx != NULL && *ctx != NULL)
179 int status = lib$find_file_end(&(*ctx)->VMS_context);
183 if(!$VMS_STATUS_SUCCESS(status))