LPdir_vms.c 6.2 KB

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