2 * Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * Copyright (c) 2004, Richard Levitte <richard@levitte.org>
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 #include <libfildef.h>
44 #include <lib$routines.h>
46 #include <str$routines.h>
53 /* Some compiler options hide EVMSERR. */
55 # define EVMSERR 65535 /* error for non-translatable VMS errors */
58 struct LP_dir_context_st
{
59 unsigned long VMS_context
;
60 char filespec
[NAMX_MAXRSS
+ 1];
61 char result
[NAMX_MAXRSS
+ 1];
62 struct dsc$descriptor_d filespec_dsc
;
63 struct dsc$descriptor_d result_dsc
;
66 const char *LP_find_file(LP_DIR_CTX
**ctx
, const char *directory
)
71 unsigned long flags
= 0;
73 /* Arrange 32-bit pointer to (copied) string storage, if needed. */
74 #if __INITIAL_POINTER_SIZE == 64
75 # pragma pointer_size save
76 # pragma pointer_size 32
77 char *ctx_filespec_32p
;
78 # pragma pointer_size restore
79 char ctx_filespec_32
[NAMX_MAXRSS
+ 1];
80 #endif /* __INITIAL_POINTER_SIZE == 64 */
83 flags
|= LIB$M_FIL_LONG_NAMES
;
86 if (ctx
== NULL
|| directory
== NULL
) {
93 size_t filespeclen
= strlen(directory
);
94 char *filespec
= NULL
;
96 if (filespeclen
== 0) {
101 /* MUST be a VMS directory specification! Let's estimate if it is. */
102 if (directory
[filespeclen
- 1] != ']'
103 && directory
[filespeclen
- 1] != '>'
104 && directory
[filespeclen
- 1] != ':') {
109 filespeclen
+= 4; /* "*.*;" */
111 if (filespeclen
> NAMX_MAXRSS
) {
112 errno
= ENAMETOOLONG
;
116 *ctx
= malloc(sizeof(**ctx
));
121 memset(*ctx
, 0, sizeof(**ctx
));
123 strcpy((*ctx
)->filespec
, directory
);
124 strcat((*ctx
)->filespec
, "*.*;");
126 /* Arrange 32-bit pointer to (copied) string storage, if needed. */
127 #if __INITIAL_POINTER_SIZE == 64
128 # define CTX_FILESPEC ctx_filespec_32p
129 /* Copy the file name to storage with a 32-bit pointer. */
130 ctx_filespec_32p
= ctx_filespec_32
;
131 strcpy(ctx_filespec_32p
, (*ctx
)->filespec
);
132 #else /* __INITIAL_POINTER_SIZE == 64 */
133 # define CTX_FILESPEC (*ctx)->filespec
134 #endif /* __INITIAL_POINTER_SIZE == 64 [else] */
136 (*ctx
)->filespec_dsc
.dsc$w_length
= filespeclen
;
137 (*ctx
)->filespec_dsc
.dsc$b_dtype
= DSC$K_DTYPE_T
;
138 (*ctx
)->filespec_dsc
.dsc$b_class
= DSC$K_CLASS_S
;
139 (*ctx
)->filespec_dsc
.dsc$a_pointer
= CTX_FILESPEC
;
142 (*ctx
)->result_dsc
.dsc$w_length
= 0;
143 (*ctx
)->result_dsc
.dsc$b_dtype
= DSC$K_DTYPE_T
;
144 (*ctx
)->result_dsc
.dsc$b_class
= DSC$K_CLASS_D
;
145 (*ctx
)->result_dsc
.dsc$a_pointer
= 0;
147 status
= lib$
find_file(&(*ctx
)->filespec_dsc
, &(*ctx
)->result_dsc
,
148 &(*ctx
)->VMS_context
, 0, 0, 0, &flags
);
150 if (status
== RMS$_NMF
) {
156 if (!$
VMS_STATUS_SUCCESS(status
)) {
163 * Quick, cheap and dirty way to discard any device and directory, since
164 * we only want file names
166 l
= (*ctx
)->result_dsc
.dsc$w_length
;
167 p
= (*ctx
)->result_dsc
.dsc$a_pointer
;
170 if (*p
== '^' && p
[1] != '\0') { /* Take care of ODS-5 escapes */
172 } else if (*p
== ':' || *p
== '>' || *p
== ']') {
175 } else if (*p
== ';') {
181 strncpy((*ctx
)->result
, r
, l
);
182 (*ctx
)->result
[l
] = '\0';
183 str$
free1_dx(&(*ctx
)->result_dsc
);
185 return (*ctx
)->result
;
188 int LP_find_file_end(LP_DIR_CTX
**ctx
)
190 if (ctx
!= NULL
&& *ctx
!= NULL
) {
191 int status
= lib$
find_file_end(&(*ctx
)->VMS_context
);
195 if (!$
VMS_STATUS_SUCCESS(status
)) {