LPdir_vms.c 5.0 KB

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