LPdir_vms.c 6.1 KB

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