]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/LPdir_vms.c
Copyright consolidation 05/10
[thirdparty/openssl.git] / crypto / LPdir_vms.c
1 /*
2 * Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 /*
11 * Copyright (c) 2004, Richard Levitte <richard@levitte.org>
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
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.
22 *
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.
34 */
35
36 #include <stddef.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <descrip.h>
41 #include <namdef.h>
42 #include <rmsdef.h>
43 #include <libfildef.h>
44 #include <lib$routines.h>
45 #include <strdef.h>
46 #include <str$routines.h>
47 #include <stsdef.h>
48 #ifndef LPDIR_H
49 # include "LPdir.h"
50 #endif
51 #include "vms_rms.h"
52
53 /* Some compiler options hide EVMSERR. */
54 #ifndef EVMSERR
55 # define EVMSERR 65535 /* error for non-translatable VMS errors */
56 #endif
57
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;
64 };
65
66 const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
67 {
68 int status;
69 char *p, *r;
70 size_t l;
71 unsigned long flags = 0;
72
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 */
81
82 #ifdef NAML$C_MAXRSS
83 flags |= LIB$M_FIL_LONG_NAMES;
84 #endif
85
86 if (ctx == NULL || directory == NULL) {
87 errno = EINVAL;
88 return 0;
89 }
90
91 errno = 0;
92 if (*ctx == NULL) {
93 size_t filespeclen = strlen(directory);
94 char *filespec = NULL;
95
96 if (filespeclen == 0) {
97 errno = ENOENT;
98 return 0;
99 }
100
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] != ':') {
105 errno = EINVAL;
106 return 0;
107 }
108
109 filespeclen += 4; /* "*.*;" */
110
111 if (filespeclen > NAMX_MAXRSS) {
112 errno = ENAMETOOLONG;
113 return 0;
114 }
115
116 *ctx = malloc(sizeof(**ctx));
117 if (*ctx == NULL) {
118 errno = ENOMEM;
119 return 0;
120 }
121 memset(*ctx, 0, sizeof(**ctx));
122
123 strcpy((*ctx)->filespec, directory);
124 strcat((*ctx)->filespec, "*.*;");
125
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] */
135
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;
140 }
141
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;
146
147 status = lib$find_file(&(*ctx)->filespec_dsc, &(*ctx)->result_dsc,
148 &(*ctx)->VMS_context, 0, 0, 0, &flags);
149
150 if (status == RMS$_NMF) {
151 errno = 0;
152 vaxc$errno = status;
153 return NULL;
154 }
155
156 if (!$VMS_STATUS_SUCCESS(status)) {
157 errno = EVMSERR;
158 vaxc$errno = status;
159 return NULL;
160 }
161
162 /*
163 * Quick, cheap and dirty way to discard any device and directory, since
164 * we only want file names
165 */
166 l = (*ctx)->result_dsc.dsc$w_length;
167 p = (*ctx)->result_dsc.dsc$a_pointer;
168 r = p;
169 for (; *p; p++) {
170 if (*p == '^' && p[1] != '\0') { /* Take care of ODS-5 escapes */
171 p++;
172 } else if (*p == ':' || *p == '>' || *p == ']') {
173 l -= p + 1 - r;
174 r = p + 1;
175 } else if (*p == ';') {
176 l = p - r;
177 break;
178 }
179 }
180
181 strncpy((*ctx)->result, r, l);
182 (*ctx)->result[l] = '\0';
183 str$free1_dx(&(*ctx)->result_dsc);
184
185 return (*ctx)->result;
186 }
187
188 int LP_find_file_end(LP_DIR_CTX **ctx)
189 {
190 if (ctx != NULL && *ctx != NULL) {
191 int status = lib$find_file_end(&(*ctx)->VMS_context);
192
193 free(*ctx);
194
195 if (!$VMS_STATUS_SUCCESS(status)) {
196 errno = EVMSERR;
197 vaxc$errno = status;
198 return 0;
199 }
200 return 1;
201 }
202 errno = EINVAL;
203 return 0;
204 }