]>
Commit | Line | Data |
---|---|---|
aa6bb135 RS |
1 | /* |
2 | * Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * | |
0e9725bc | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
aa6bb135 RS |
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 | |
8 | */ | |
9 | ||
a2400fca | 10 | /* |
8d1598b0 | 11 | * This file is dual-licensed and is also available under the following |
0c3d0d4a RS |
12 | * terms: |
13 | * | |
a2400fca RL |
14 | * Copyright (c) 2004, Richard Levitte <richard@levitte.org> |
15 | * All rights reserved. | |
16 | * | |
17 | * Redistribution and use in source and binary forms, with or without | |
18 | * modification, are permitted provided that the following conditions | |
19 | * are met: | |
20 | * 1. Redistributions of source code must retain the above copyright | |
21 | * notice, this list of conditions and the following disclaimer. | |
22 | * 2. Redistributions in binary form must reproduce the above copyright | |
23 | * notice, this list of conditions and the following disclaimer in the | |
24 | * documentation and/or other materials provided with the distribution. | |
0f113f3e | 25 | * |
bb09fd2b RL |
26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
27 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
28 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
29 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
30 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
31 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
32 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
33 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
34 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
35 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
36 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
a2400fca RL |
37 | */ |
38 | ||
39 | #include <stddef.h> | |
40 | #include <stdlib.h> | |
41 | #include <string.h> | |
42 | #include <errno.h> | |
43 | #include <descrip.h> | |
44 | #include <namdef.h> | |
45 | #include <rmsdef.h> | |
46 | #include <libfildef.h> | |
47 | #include <lib$routines.h> | |
48 | #include <strdef.h> | |
49 | #include <str$routines.h> | |
50 | #include <stsdef.h> | |
51 | #ifndef LPDIR_H | |
0f113f3e | 52 | # include "LPdir.h" |
a2400fca | 53 | #endif |
537c9823 | 54 | #include "vms_rms.h" |
a2400fca | 55 | |
537c9823 | 56 | /* Some compiler options hide EVMSERR. */ |
b0841348 | 57 | #ifndef EVMSERR |
0f113f3e | 58 | # define EVMSERR 65535 /* error for non-translatable VMS errors */ |
b0841348 RL |
59 | #endif |
60 | ||
0f113f3e MC |
61 | struct LP_dir_context_st { |
62 | unsigned long VMS_context; | |
63 | char filespec[NAMX_MAXRSS + 1]; | |
64 | char result[NAMX_MAXRSS + 1]; | |
65 | struct dsc$descriptor_d filespec_dsc; | |
66 | struct dsc$descriptor_d result_dsc; | |
a2400fca RL |
67 | }; |
68 | ||
69 | const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory) | |
70 | { | |
0f113f3e MC |
71 | int status; |
72 | char *p, *r; | |
73 | size_t l; | |
74 | unsigned long flags = 0; | |
537c9823 RL |
75 | |
76 | /* Arrange 32-bit pointer to (copied) string storage, if needed. */ | |
77 | #if __INITIAL_POINTER_SIZE == 64 | |
78 | # pragma pointer_size save | |
79 | # pragma pointer_size 32 | |
0f113f3e | 80 | char *ctx_filespec_32p; |
537c9823 | 81 | # pragma pointer_size restore |
0f113f3e MC |
82 | char ctx_filespec_32[NAMX_MAXRSS + 1]; |
83 | #endif /* __INITIAL_POINTER_SIZE == 64 */ | |
537c9823 | 84 | |
a2400fca | 85 | #ifdef NAML$C_MAXRSS |
0f113f3e | 86 | flags |= LIB$M_FIL_LONG_NAMES; |
a2400fca RL |
87 | #endif |
88 | ||
0f113f3e MC |
89 | if (ctx == NULL || directory == NULL) { |
90 | errno = EINVAL; | |
91 | return 0; | |
a2400fca RL |
92 | } |
93 | ||
0f113f3e MC |
94 | errno = 0; |
95 | if (*ctx == NULL) { | |
96 | size_t filespeclen = strlen(directory); | |
97 | char *filespec = NULL; | |
98 | ||
99 | if (filespeclen == 0) { | |
100 | errno = ENOENT; | |
101 | return 0; | |
102 | } | |
103 | ||
104 | /* MUST be a VMS directory specification! Let's estimate if it is. */ | |
105 | if (directory[filespeclen - 1] != ']' | |
106 | && directory[filespeclen - 1] != '>' | |
107 | && directory[filespeclen - 1] != ':') { | |
108 | errno = EINVAL; | |
109 | return 0; | |
110 | } | |
111 | ||
112 | filespeclen += 4; /* "*.*;" */ | |
113 | ||
114 | if (filespeclen > NAMX_MAXRSS) { | |
115 | errno = ENAMETOOLONG; | |
116 | return 0; | |
117 | } | |
118 | ||
b4faea50 | 119 | *ctx = malloc(sizeof(**ctx)); |
0f113f3e MC |
120 | if (*ctx == NULL) { |
121 | errno = ENOMEM; | |
122 | return 0; | |
123 | } | |
16f8d4eb | 124 | memset(*ctx, 0, sizeof(**ctx)); |
0f113f3e MC |
125 | |
126 | strcpy((*ctx)->filespec, directory); | |
127 | strcat((*ctx)->filespec, "*.*;"); | |
537c9823 RL |
128 | |
129 | /* Arrange 32-bit pointer to (copied) string storage, if needed. */ | |
130 | #if __INITIAL_POINTER_SIZE == 64 | |
131 | # define CTX_FILESPEC ctx_filespec_32p | |
132 | /* Copy the file name to storage with a 32-bit pointer. */ | |
133 | ctx_filespec_32p = ctx_filespec_32; | |
0f113f3e MC |
134 | strcpy(ctx_filespec_32p, (*ctx)->filespec); |
135 | #else /* __INITIAL_POINTER_SIZE == 64 */ | |
537c9823 | 136 | # define CTX_FILESPEC (*ctx)->filespec |
0f113f3e | 137 | #endif /* __INITIAL_POINTER_SIZE == 64 [else] */ |
537c9823 | 138 | |
0f113f3e MC |
139 | (*ctx)->filespec_dsc.dsc$w_length = filespeclen; |
140 | (*ctx)->filespec_dsc.dsc$b_dtype = DSC$K_DTYPE_T; | |
141 | (*ctx)->filespec_dsc.dsc$b_class = DSC$K_CLASS_S; | |
142 | (*ctx)->filespec_dsc.dsc$a_pointer = CTX_FILESPEC; | |
a2400fca RL |
143 | } |
144 | ||
0f113f3e MC |
145 | (*ctx)->result_dsc.dsc$w_length = 0; |
146 | (*ctx)->result_dsc.dsc$b_dtype = DSC$K_DTYPE_T; | |
147 | (*ctx)->result_dsc.dsc$b_class = DSC$K_CLASS_D; | |
148 | (*ctx)->result_dsc.dsc$a_pointer = 0; | |
a2400fca | 149 | |
0f113f3e MC |
150 | status = lib$find_file(&(*ctx)->filespec_dsc, &(*ctx)->result_dsc, |
151 | &(*ctx)->VMS_context, 0, 0, 0, &flags); | |
a2400fca | 152 | |
0f113f3e MC |
153 | if (status == RMS$_NMF) { |
154 | errno = 0; | |
155 | vaxc$errno = status; | |
156 | return NULL; | |
a2400fca RL |
157 | } |
158 | ||
0f113f3e MC |
159 | if (!$VMS_STATUS_SUCCESS(status)) { |
160 | errno = EVMSERR; | |
161 | vaxc$errno = status; | |
162 | return NULL; | |
a2400fca RL |
163 | } |
164 | ||
0f113f3e MC |
165 | /* |
166 | * Quick, cheap and dirty way to discard any device and directory, since | |
167 | * we only want file names | |
168 | */ | |
169 | l = (*ctx)->result_dsc.dsc$w_length; | |
170 | p = (*ctx)->result_dsc.dsc$a_pointer; | |
171 | r = p; | |
172 | for (; *p; p++) { | |
173 | if (*p == '^' && p[1] != '\0') { /* Take care of ODS-5 escapes */ | |
174 | p++; | |
175 | } else if (*p == ':' || *p == '>' || *p == ']') { | |
176 | l -= p + 1 - r; | |
177 | r = p + 1; | |
178 | } else if (*p == ';') { | |
179 | l = p - r; | |
180 | break; | |
181 | } | |
a2400fca RL |
182 | } |
183 | ||
0f113f3e MC |
184 | strncpy((*ctx)->result, r, l); |
185 | (*ctx)->result[l] = '\0'; | |
186 | str$free1_dx(&(*ctx)->result_dsc); | |
a2400fca | 187 | |
0f113f3e | 188 | return (*ctx)->result; |
a2400fca RL |
189 | } |
190 | ||
191 | int LP_find_file_end(LP_DIR_CTX **ctx) | |
192 | { | |
0f113f3e MC |
193 | if (ctx != NULL && *ctx != NULL) { |
194 | int status = lib$find_file_end(&(*ctx)->VMS_context); | |
195 | ||
196 | free(*ctx); | |
197 | ||
198 | if (!$VMS_STATUS_SUCCESS(status)) { | |
199 | errno = EVMSERR; | |
200 | vaxc$errno = status; | |
201 | return 0; | |
202 | } | |
203 | return 1; | |
a2400fca | 204 | } |
0f113f3e MC |
205 | errno = EINVAL; |
206 | return 0; | |
a2400fca | 207 | } |