LPdir_vms.c 5.8 KB

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