LPdir_vms.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. /* Because some compiler options hide this macor */
  42. #ifndef EVMSERR
  43. # define EVMSERR 65535 /* error for non-translatable VMS errors */
  44. #endif
  45. struct LP_dir_context_st {
  46. unsigned long VMS_context;
  47. #ifdef NAML$C_MAXRSS
  48. char filespec[NAML$C_MAXRSS + 1];
  49. char result[NAML$C_MAXRSS + 1];
  50. #else
  51. char filespec[256];
  52. char result[256];
  53. #endif
  54. struct dsc$descriptor_d filespec_dsc;
  55. struct dsc$descriptor_d result_dsc;
  56. };
  57. const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
  58. {
  59. int status;
  60. char *p, *r;
  61. size_t l;
  62. unsigned long flags = 0;
  63. #ifdef NAML$C_MAXRSS
  64. flags |= LIB$M_FIL_LONG_NAMES;
  65. #endif
  66. if (ctx == NULL || directory == NULL) {
  67. errno = EINVAL;
  68. return 0;
  69. }
  70. errno = 0;
  71. if (*ctx == NULL) {
  72. size_t filespeclen = strlen(directory);
  73. char *filespec = NULL;
  74. if (filespeclen == 0) {
  75. errno = ENOENT;
  76. return 0;
  77. }
  78. /* MUST be a VMS directory specification! Let's estimate if it is. */
  79. if (directory[filespeclen - 1] != ']'
  80. && directory[filespeclen - 1] != '>'
  81. && directory[filespeclen - 1] != ':') {
  82. errno = EINVAL;
  83. return 0;
  84. }
  85. filespeclen += 4; /* "*.*;" */
  86. if (filespeclen >
  87. #ifdef NAML$C_MAXRSS
  88. NAML$C_MAXRSS
  89. #else
  90. 255
  91. #endif
  92. ) {
  93. errno = ENAMETOOLONG;
  94. return 0;
  95. }
  96. *ctx = (LP_DIR_CTX *)malloc(sizeof(LP_DIR_CTX));
  97. if (*ctx == NULL) {
  98. errno = ENOMEM;
  99. return 0;
  100. }
  101. memset(*ctx, '\0', sizeof(LP_DIR_CTX));
  102. strcpy((*ctx)->filespec, directory);
  103. strcat((*ctx)->filespec, "*.*;");
  104. (*ctx)->filespec_dsc.dsc$w_length = filespeclen;
  105. (*ctx)->filespec_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
  106. (*ctx)->filespec_dsc.dsc$b_class = DSC$K_CLASS_S;
  107. (*ctx)->filespec_dsc.dsc$a_pointer = (*ctx)->filespec;
  108. (*ctx)->result_dsc.dsc$w_length = 0;
  109. (*ctx)->result_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
  110. (*ctx)->result_dsc.dsc$b_class = DSC$K_CLASS_D;
  111. (*ctx)->result_dsc.dsc$a_pointer = 0;
  112. }
  113. (*ctx)->result_dsc.dsc$w_length = 0;
  114. (*ctx)->result_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
  115. (*ctx)->result_dsc.dsc$b_class = DSC$K_CLASS_D;
  116. (*ctx)->result_dsc.dsc$a_pointer = 0;
  117. status = lib$find_file(&(*ctx)->filespec_dsc, &(*ctx)->result_dsc,
  118. &(*ctx)->VMS_context, 0, 0, 0, &flags);
  119. if (status == RMS$_NMF) {
  120. errno = 0;
  121. vaxc$errno = status;
  122. return NULL;
  123. }
  124. if (!$VMS_STATUS_SUCCESS(status)) {
  125. errno = EVMSERR;
  126. vaxc$errno = status;
  127. return NULL;
  128. }
  129. /*
  130. * Quick, cheap and dirty way to discard any device and directory, since
  131. * we only want file names
  132. */
  133. l = (*ctx)->result_dsc.dsc$w_length;
  134. p = (*ctx)->result_dsc.dsc$a_pointer;
  135. r = p;
  136. for (; *p; p++) {
  137. if (*p == '^' && p[1] != '\0') { /* Take care of ODS-5 escapes */
  138. p++;
  139. } else if (*p == ':' || *p == '>' || *p == ']') {
  140. l -= p + 1 - r;
  141. r = p + 1;
  142. } else if (*p == ';') {
  143. l = p - r;
  144. break;
  145. }
  146. }
  147. strncpy((*ctx)->result, r, l);
  148. (*ctx)->result[l] = '\0';
  149. str$free1_dx(&(*ctx)->result_dsc);
  150. return (*ctx)->result;
  151. }
  152. int LP_find_file_end(LP_DIR_CTX **ctx)
  153. {
  154. if (ctx != NULL && *ctx != NULL) {
  155. int status = lib$find_file_end(&(*ctx)->VMS_context);
  156. free(*ctx);
  157. if (!$VMS_STATUS_SUCCESS(status)) {
  158. errno = EVMSERR;
  159. vaxc$errno = status;
  160. return 0;
  161. }
  162. return 1;
  163. }
  164. errno = EINVAL;
  165. return 0;
  166. }