LPdir_vms.c 5.4 KB

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