blob: 51043263ae6708e64e6a77987d5e97b3e211750d [file] [log] [blame]
Richard Levittea2400fc2004-07-10 13:16:02 +00001/*
Rich Salzaa6bb132016-05-17 15:38:09 -04002 * Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
Richard Levitte0e9725b2018-12-06 14:03:01 +01004 * Licensed under the Apache License 2.0 (the "License"). You may not use
Rich Salzaa6bb132016-05-17 15:38:09 -04005 * 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/*
Rich Salz8d1598b2017-06-19 22:48:25 -040011 * This file is dual-licensed and is also available under the following
Rich Salz0c3d0d42017-06-15 13:15:26 -040012 * terms:
13 *
Richard Levittea2400fc2004-07-10 13:16:02 +000014 * 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.
Matt Caswell0f113f32015-01-22 03:40:55 +000025 *
Richard Levittebb09fd22004-09-23 22:11:39 +000026 * 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.
Richard Levittea2400fc2004-07-10 13:16:02 +000037 */
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
Matt Caswell0f113f32015-01-22 03:40:55 +000052# include "LPdir.h"
Richard Levittea2400fc2004-07-10 13:16:02 +000053#endif
Richard Levitte537c9822011-03-19 10:58:14 +000054#include "vms_rms.h"
Richard Levittea2400fc2004-07-10 13:16:02 +000055
Richard Levitte537c9822011-03-19 10:58:14 +000056/* Some compiler options hide EVMSERR. */
Richard Levitteb0841342004-07-11 20:21:19 +000057#ifndef EVMSERR
Matt Caswell0f113f32015-01-22 03:40:55 +000058# define EVMSERR 65535 /* error for non-translatable VMS errors */
Richard Levitteb0841342004-07-11 20:21:19 +000059#endif
60
Matt Caswell0f113f32015-01-22 03:40:55 +000061struct 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;
Richard Levittea2400fc2004-07-10 13:16:02 +000067};
68
69const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
70{
Matt Caswell0f113f32015-01-22 03:40:55 +000071 int status;
72 char *p, *r;
73 size_t l;
74 unsigned long flags = 0;
Richard Levitte537c9822011-03-19 10:58:14 +000075
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
Matt Caswell0f113f32015-01-22 03:40:55 +000080 char *ctx_filespec_32p;
Richard Levitte537c9822011-03-19 10:58:14 +000081# pragma pointer_size restore
Matt Caswell0f113f32015-01-22 03:40:55 +000082 char ctx_filespec_32[NAMX_MAXRSS + 1];
83#endif /* __INITIAL_POINTER_SIZE == 64 */
Richard Levitte537c9822011-03-19 10:58:14 +000084
Richard Levittea2400fc2004-07-10 13:16:02 +000085#ifdef NAML$C_MAXRSS
Matt Caswell0f113f32015-01-22 03:40:55 +000086 flags |= LIB$M_FIL_LONG_NAMES;
Richard Levittea2400fc2004-07-10 13:16:02 +000087#endif
88
Matt Caswell0f113f32015-01-22 03:40:55 +000089 if (ctx == NULL || directory == NULL) {
90 errno = EINVAL;
91 return 0;
Richard Levittea2400fc2004-07-10 13:16:02 +000092 }
93
Matt Caswell0f113f32015-01-22 03:40:55 +000094 errno = 0;
95 if (*ctx == NULL) {
96 size_t filespeclen = strlen(directory);
97 char *filespec = NULL;
Richard Levittea2400fc2004-07-10 13:16:02 +000098
Matt Caswell0f113f32015-01-22 03:40:55 +000099 if (filespeclen == 0) {
100 errno = ENOENT;
101 return 0;
102 }
Richard Levitte360928b2014-08-15 01:24:34 +0200103
Matt Caswell0f113f32015-01-22 03:40:55 +0000104 /* 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 }
Richard Levittea2400fc2004-07-10 13:16:02 +0000111
Matt Caswell0f113f32015-01-22 03:40:55 +0000112 filespeclen += 4; /* "*.*;" */
Richard Levittea2400fc2004-07-10 13:16:02 +0000113
Matt Caswell0f113f32015-01-22 03:40:55 +0000114 if (filespeclen > NAMX_MAXRSS) {
115 errno = ENAMETOOLONG;
116 return 0;
117 }
Richard Levittea2400fc2004-07-10 13:16:02 +0000118
Rich Salzb4faea52015-05-01 23:10:31 -0400119 *ctx = malloc(sizeof(**ctx));
Matt Caswell0f113f32015-01-22 03:40:55 +0000120 if (*ctx == NULL) {
121 errno = ENOMEM;
122 return 0;
123 }
Rich Salz16f8d4e2015-05-04 18:00:15 -0400124 memset(*ctx, 0, sizeof(**ctx));
Richard Levittea2400fc2004-07-10 13:16:02 +0000125
Matt Caswell0f113f32015-01-22 03:40:55 +0000126 strcpy((*ctx)->filespec, directory);
127 strcat((*ctx)->filespec, "*.*;");
Richard Levitte537c9822011-03-19 10:58:14 +0000128
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;
Matt Caswell0f113f32015-01-22 03:40:55 +0000134 strcpy(ctx_filespec_32p, (*ctx)->filespec);
135#else /* __INITIAL_POINTER_SIZE == 64 */
Richard Levitte537c9822011-03-19 10:58:14 +0000136# define CTX_FILESPEC (*ctx)->filespec
Matt Caswell0f113f32015-01-22 03:40:55 +0000137#endif /* __INITIAL_POINTER_SIZE == 64 [else] */
Richard Levitte537c9822011-03-19 10:58:14 +0000138
Matt Caswell0f113f32015-01-22 03:40:55 +0000139 (*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;
Richard Levittea2400fc2004-07-10 13:16:02 +0000143 }
144
Matt Caswell0f113f32015-01-22 03:40:55 +0000145 (*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;
Richard Levittea2400fc2004-07-10 13:16:02 +0000149
Matt Caswell0f113f32015-01-22 03:40:55 +0000150 status = lib$find_file(&(*ctx)->filespec_dsc, &(*ctx)->result_dsc,
151 &(*ctx)->VMS_context, 0, 0, 0, &flags);
Richard Levittea2400fc2004-07-10 13:16:02 +0000152
Matt Caswell0f113f32015-01-22 03:40:55 +0000153 if (status == RMS$_NMF) {
154 errno = 0;
155 vaxc$errno = status;
156 return NULL;
Richard Levittea2400fc2004-07-10 13:16:02 +0000157 }
158
Matt Caswell0f113f32015-01-22 03:40:55 +0000159 if (!$VMS_STATUS_SUCCESS(status)) {
160 errno = EVMSERR;
161 vaxc$errno = status;
162 return NULL;
Richard Levittea2400fc2004-07-10 13:16:02 +0000163 }
164
Matt Caswell0f113f32015-01-22 03:40:55 +0000165 /*
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 }
Richard Levittea2400fc2004-07-10 13:16:02 +0000182 }
183
Matt Caswell0f113f32015-01-22 03:40:55 +0000184 strncpy((*ctx)->result, r, l);
185 (*ctx)->result[l] = '\0';
186 str$free1_dx(&(*ctx)->result_dsc);
Richard Levittea2400fc2004-07-10 13:16:02 +0000187
Matt Caswell0f113f32015-01-22 03:40:55 +0000188 return (*ctx)->result;
Richard Levittea2400fc2004-07-10 13:16:02 +0000189}
190
191int LP_find_file_end(LP_DIR_CTX **ctx)
192{
Matt Caswell0f113f32015-01-22 03:40:55 +0000193 if (ctx != NULL && *ctx != NULL) {
194 int status = lib$find_file_end(&(*ctx)->VMS_context);
Richard Levittea2400fc2004-07-10 13:16:02 +0000195
Matt Caswell0f113f32015-01-22 03:40:55 +0000196 free(*ctx);
Richard Levittea2400fc2004-07-10 13:16:02 +0000197
Matt Caswell0f113f32015-01-22 03:40:55 +0000198 if (!$VMS_STATUS_SUCCESS(status)) {
199 errno = EVMSERR;
200 vaxc$errno = status;
201 return 0;
202 }
203 return 1;
Richard Levittea2400fc2004-07-10 13:16:02 +0000204 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000205 errno = EINVAL;
206 return 0;
Richard Levittea2400fc2004-07-10 13:16:02 +0000207}