vms.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. /*
  2. Copyright (c) 1990-2007 Info-ZIP. All rights reserved.
  3. See the accompanying file LICENSE, version 2007-Mar-4 or later
  4. (the contents of which are also included in zip.h) for terms of use.
  5. If, for some reason, all these files are missing, the Info-ZIP license
  6. also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
  7. */
  8. /*
  9. * vms.c (zip) by Igor Mandrichenko Version 2.2-2
  10. *
  11. * Revision history:
  12. * ...
  13. * 2.2-2 18-jan-1993 I.Mandrichenko
  14. * vms_stat() added - version of stat() that handles special
  15. * case when end-of-file-block == 0
  16. *
  17. * 3.0 11-oct-2004 SMS
  18. * It would be nice to know why vms_stat() is needed. If EOF can't
  19. * be trusted for a zero-length file, why trust it for any file?
  20. * Anyway, I removed the (int) cast on ->st_size, which may now be
  21. * bigger than an int, just in case this code ever does get used.
  22. * (A true zero-length file should still report zero length, even
  23. * after the long fight with RMS.)
  24. * Moved the VMS_PK_EXTRA test(s) into VMS_IM.C and VMS_PK.C to
  25. * allow more general automatic dependency generation.
  26. */
  27. #ifdef VMS /* For VMS only ! */
  28. #define NO_ZIPUP_H /* Prevent full inclusion of vms/zipup.h. */
  29. #include "zip.h"
  30. #include "zipup.h" /* Only partial. */
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <jpidef.h>
  34. #include <fab.h> /* Needed only in old environments. */
  35. #include <nam.h> /* Needed only in old environments. */
  36. #include <starlet.h>
  37. #include <ssdef.h>
  38. #include <stsdef.h>
  39. /* On VAX, define Goofy VAX Type-Cast to obviate /standard = vaxc.
  40. Otherwise, lame system headers on VAX cause compiler warnings.
  41. (GNU C may define vax but not __VAX.)
  42. */
  43. #ifdef vax
  44. # define __VAX 1
  45. #endif /* def vax */
  46. #ifdef __VAX
  47. # define GVTC (unsigned int)
  48. #else /* def __VAX */
  49. # define GVTC
  50. #endif /* def __VAX */
  51. #ifdef UTIL
  52. /* For utilities, include only vms.h, as either of the vms_XX.c files
  53. * would do.
  54. */
  55. # include "vms.h"
  56. #else /* not UTIL */
  57. /* Include the `VMS attributes' preserving file-io code. We distinguish
  58. between two incompatible flavours of storing VMS attributes in the
  59. Zip archive:
  60. a) The "PKware" style follows the extra field specification for
  61. PKware's VMS Zip.
  62. b) The "IM (Info-ZIP)" flavour was defined from scratch by
  63. Igor Mandrichenko. This version has be used in official Info-ZIP
  64. releases for several years and is known to work well.
  65. */
  66. /* Note that only one of these #include directives will include any
  67. * active code, depending on VMS_PK_EXTRA. Both are included here (and
  68. * tested there) to allow more general automatic dependency generation.
  69. */
  70. #include "vms_pk.c"
  71. #include "vms_im.c"
  72. #endif /* not UTIL [else] */
  73. #ifndef ERR
  74. #define ERR(x) (((x)&1)==0)
  75. #endif
  76. #ifndef NULL
  77. #define NULL (void*)(0L)
  78. #endif
  79. int vms_stat( char *file, stat_t *s)
  80. {
  81. int status;
  82. int staterr;
  83. struct FAB fab;
  84. struct NAM_STRUCT nam;
  85. struct XABFHC fhc;
  86. /*
  87. * In simplest case when stat() returns "ok" and file size is
  88. * nonzero or this is directory, finish with this
  89. */
  90. if( (staterr=stat(file,s)) == 0
  91. && ( s->st_size >= 0 /* Size - ok */
  92. || (s->st_mode & S_IFREG) == 0 /* Not a plain file */
  93. )
  94. ) return staterr;
  95. /*
  96. * Get here to handle the special case when stat() returns
  97. * invalid file size. Use RMS to compute the size.
  98. * When EOF block is zero, set file size to its physical size.
  99. * One more case to get here is when this is remote file accessed
  100. * via DECnet.
  101. */
  102. fab = cc$rms_fab;
  103. nam = CC_RMS_NAM;
  104. fhc = cc$rms_xabfhc;
  105. fab.FAB_NAM = &nam;
  106. fab.fab$l_xab = (char*)(&fhc);
  107. #ifdef NAML$C_MAXRSS
  108. fab.fab$l_dna = (char *) -1; /* Using NAML for default name. */
  109. fab.fab$l_fna = (char *) -1; /* Using NAML for file name. */
  110. #endif /* def NAML$C_MAXRSS */
  111. FAB_OR_NAML( fab, nam).FAB_OR_NAML_FNA = file;
  112. FAB_OR_NAML( fab, nam).FAB_OR_NAML_FNS = strlen( file);
  113. fab.fab$b_fac = FAB$M_GET;
  114. status = sys$open(&fab);
  115. fab.fab$l_xab = (char*)0L;
  116. sys$close(&fab);
  117. if( !ERR(status) )
  118. {
  119. if( fhc.xab$l_ebk > 0 )
  120. s->st_size = ( fhc.xab$l_ebk-1 ) * 512 + fhc.xab$w_ffb;
  121. else if( fab.fab$b_org == FAB$C_IDX
  122. || fab.fab$b_org == FAB$C_REL
  123. || fab.fab$b_org == FAB$C_HSH )
  124. /* Special case, when ebk=0: save entire allocated space */
  125. s->st_size = fhc.xab$l_hbk * 512;
  126. else
  127. s->st_size = fhc.xab$w_ffb;
  128. return 0; /* stat() success code */
  129. }
  130. else
  131. return status;
  132. }
  133. /*
  134. * 2007-01-29 SMS.
  135. *
  136. * VMS Status Code Summary (See STSDEF.H for details.)
  137. *
  138. * Bits: 31:28 27:16 15:3 2:0
  139. * Field: Control Facility Message Severity
  140. *
  141. * In the Control field, bits 31:29 are reserved. Bit 28 inhibits
  142. * printing the message. In the Facility field, bit 27 means
  143. * customer-defined (not HP-assigned, like us). In the Message field,
  144. * bit 15 means facility-specific (which our messages are). The
  145. * Severity codes are 0 = Warning, 1 = Success, 2 = Error, 3 = Info,
  146. * 4 = Severe (fatal).
  147. *
  148. * Previous versions of Info-ZIP programs used a generic ("chosen (by
  149. * experimentation)") Control+Facility code of 0x7FFF, which included
  150. * some reserved control bits, the inhibit-printing bit, and the
  151. * customer-defined bit.
  152. *
  153. * HP has now assigned official Facility names and corresponding
  154. * Facility codes for the Info-ZIP products:
  155. *
  156. * Facility Name Facility Code
  157. * IZ_UNZIP 1954 = 0x7A2
  158. * IZ_ZIP 1955 = 0x7A3
  159. *
  160. * Now, unless the CTL_FAC_IZ_ZIP macro is defined at build-time, we
  161. * will use the official Facility code.
  162. *
  163. */
  164. /* Official HP-assigned Info-ZIP Zip Facility code. */
  165. #define FAC_IZ_ZIP 1955 /* 0x7A3 */
  166. #ifndef CTL_FAC_IZ_ZIP
  167. /*
  168. * Default is inhibit-printing with the official Facility code.
  169. */
  170. # define CTL_FAC_IZ_ZIP ((0x1 << 12)| FAC_IZ_ZIP)
  171. # define MSG_FAC_SPEC 0x8000 /* Facility-specific code. */
  172. #else /* ndef CTL_FAC_IZ_ZIP */
  173. /* Use the user-supplied Control+Facility code for err or warn. */
  174. # define OLD_STATUS
  175. # ifndef MSG_FAC_SPEC /* Old default is not Facility-specific. */
  176. # define MSG_FAC_SPEC 0x0 /* Facility-specific code. Or 0x8000. */
  177. # endif /* ndef MSG_FAC_SPEC */
  178. #endif /* ndef CTL_FAC_IZ_ZIP [else] */
  179. /* Return an intelligent status/severity code. */
  180. void vms_exit(e)
  181. int e;
  182. {
  183. {
  184. #ifndef OLD_STATUS
  185. /*
  186. * Exit with code comprising Control, Facility, (facility-specific)
  187. * Message, and Severity.
  188. */
  189. exit( (CTL_FAC_IZ_ZIP << 16) | /* Facility */
  190. MSG_FAC_SPEC | /* Facility-specific */
  191. (e << 4) | /* Message code */
  192. (ziperrors[ e].severity & 0x07) /* Severity */
  193. );
  194. #else /* ndef OLD_STATUS */
  195. /* 2007-01-17 SMS.
  196. * Defining OLD_STATUS provides the same behavior as in Zip versions
  197. * before an official VMS Facility code had been assigned, which
  198. * means that Success (ZE_OK) gives a status value of 1 (SS$_NORMAL)
  199. * with no Facility code, while any error or warning gives a status
  200. * value which includes a Facility code. (Curiously, under the old
  201. * scheme, message codes were left-shifted by 4 instead of 3,
  202. * resulting in all-even message codes.) I don't like this, but I
  203. * was afraid to remove it, as someone, somewhere may be depending
  204. * on it. Define CTL_FAC_IZ_ZIP as 0x7FFF to get the old behavior.
  205. * Define only OLD_STATUS to get the old behavior for Success
  206. * (ZE_OK), but using the official HP-assigned Facility code for an
  207. * error or warning. Define MSG_FAC_SPEC to get the desired
  208. * behavior.
  209. *
  210. * Exit with simple SS$_NORMAL for ZE_OK. Otherwise, exit with code
  211. * comprising Control, Facility, Message, and Severity.
  212. */
  213. exit(
  214. (e == ZE_OK) ? SS$_NORMAL : /* Success (others below) */
  215. ((CTL_FAC_IZ_ZIP << 16) | /* Facility */
  216. MSG_FAC_SPEC | /* Facility-specific (?) */
  217. (e << 4) | /* Message code */
  218. (ziperrors[ e].severity & 0x07) /* Severity */
  219. )
  220. );
  221. #endif /* ndef OLD_STATUS */
  222. }
  223. }
  224. /******************************/
  225. /* Function version_local() */
  226. /******************************/
  227. void version_local()
  228. {
  229. static ZCONST char CompiledWith[] = "Compiled with %s%s for %s%s%s%s.\n\n";
  230. #ifdef VMS_VERSION
  231. char *chrp1;
  232. char *chrp2;
  233. char buf[40];
  234. char vms_vers[ 16];
  235. int ver_maj;
  236. #endif
  237. #ifdef __DECC_VER
  238. char buf2[40];
  239. int vtyp;
  240. #endif
  241. #ifdef VMS_VERSION
  242. /* Truncate the version string at the first (trailing) space. */
  243. strncpy( vms_vers, VMS_VERSION, sizeof( vms_vers));
  244. chrp1 = strchr( vms_vers, ' ');
  245. if (chrp1 != NULL)
  246. *chrp1 = '\0';
  247. /* Determine the major version number. */
  248. ver_maj = 0;
  249. chrp1 = strchr( &vms_vers[ 1], '.');
  250. for (chrp2 = &vms_vers[ 1];
  251. chrp2 < chrp1;
  252. ver_maj = ver_maj* 10+ *(chrp2++)- '0');
  253. #endif /* def VMS_VERSION */
  254. /* DEC C in ANSI mode does not like "#ifdef MACRO" inside another
  255. macro when MACRO is equated to a value (by "#define MACRO 1"). */
  256. printf(CompiledWith,
  257. #ifdef __GNUC__
  258. "gcc ", __VERSION__,
  259. #else
  260. # if defined(DECC) || defined(__DECC) || defined (__DECC__)
  261. "DEC C",
  262. # ifdef __DECC_VER
  263. (sprintf(buf2, " %c%d.%d-%03d",
  264. ((vtyp = (__DECC_VER / 10000) % 10) == 6 ? 'T' :
  265. (vtyp == 8 ? 'S' : 'V')),
  266. __DECC_VER / 10000000,
  267. (__DECC_VER % 10000000) / 100000, __DECC_VER % 1000), buf2),
  268. # else
  269. "",
  270. # endif
  271. # else
  272. # ifdef VAXC
  273. "VAX C", "",
  274. # else
  275. "unknown compiler", "",
  276. # endif
  277. # endif
  278. #endif
  279. #ifdef VMS_VERSION
  280. # if defined( __alpha)
  281. "OpenVMS",
  282. (sprintf( buf, " (%s Alpha)", vms_vers), buf),
  283. # elif defined( __ia64) /* defined( __alpha) */
  284. "OpenVMS",
  285. (sprintf( buf, " (%s IA64)", vms_vers), buf),
  286. # else /* defined( __alpha) */
  287. (ver_maj >= 6) ? "OpenVMS" : "VMS",
  288. (sprintf( buf, " (%s VAX)", vms_vers), buf),
  289. # endif /* defined( __alpha) */
  290. #else
  291. "VMS",
  292. "",
  293. #endif /* def VMS_VERSION */
  294. #ifdef __DATE__
  295. " on ", __DATE__
  296. #else
  297. "", ""
  298. #endif
  299. );
  300. } /* end function version_local() */
  301. /* 2004-10-08 SMS.
  302. *
  303. * tempname() for VMS.
  304. *
  305. * Generate a temporary Zip archive file name, near the actual
  306. * destination Zip archive file, or at "tempath", if specified.
  307. *
  308. * Using sys$parse() is probably more work than it's worth, but it
  309. * should also be ODS5-safe.
  310. *
  311. * Note that the generic method using tmpnam() (in FILEIO.C)
  312. * produces "ziXXXXXX", where "XXXXXX" is the low six digits of the
  313. * decimal representation of the process ID. This method produces
  314. * "ZIxxxxxxxx", where "xxxxxxxx" is the (whole) eight-digit
  315. * hexadecimal representation of the process ID. More important, it
  316. * actually uses the directory part of the argument or "tempath".
  317. */
  318. char *tempname( char *zip)
  319. /* char *zip; */ /* Path name of Zip archive. */
  320. {
  321. char *temp_name; /* Return value. */
  322. int sts; /* System service status. */
  323. static int pid; /* Process ID. */
  324. static int pid_len; /* Returned size of process ID. */
  325. struct /* Item list for GETJPIW. */
  326. {
  327. short buf_len; /* Buffer length. */
  328. short itm_cod; /* Item code. */
  329. int *buf; /* Buffer address. */
  330. int *ret_len; /* Returned length. */
  331. int term; /* Item list terminator. */
  332. } jpi_itm_lst = { sizeof( pid), JPI$_PID, &pid, &pid_len };
  333. /* ZI<UNIQUE> name storage. */
  334. static char zip_tmp_nam[ 16] = "ZI<unique>.;";
  335. struct FAB fab; /* FAB structure. */
  336. struct NAM_STRUCT nam; /* NAM[L] structure. */
  337. char exp_str[ NAM_MAXRSS+ 1]; /* Expanded name storage. */
  338. #ifdef VMS_UNIQUE_TEMP_BY_TIME
  339. /* Use alternate time-based scheme to generate a unique temporary name. */
  340. sprintf( &zip_tmp_nam[ 2], "%08X", time( NULL));
  341. #else /* def VMS_UNIQUE_TEMP_BY_TIME */
  342. /* Use the process ID to generate a unique temporary name. */
  343. sts = sys$getjpiw( 0, 0, 0, &jpi_itm_lst, 0, 0, 0);
  344. sprintf( &zip_tmp_nam[ 2], "%08X", pid);
  345. #endif /* def VMS_UNIQUE_TEMP_BY_TIME */
  346. /* Smoosh the unique temporary name against the actual Zip archive
  347. name (or "tempath") to create the full temporary path name.
  348. (Truncate it at the file type to remove any file type.)
  349. */
  350. if (tempath != NULL) /* Use "tempath", if it's been specified. */
  351. zip = tempath;
  352. /* Initialize the FAB and NAM[L], and link the NAM[L] to the FAB. */
  353. fab = cc$rms_fab;
  354. nam = CC_RMS_NAM;
  355. fab.FAB_NAM = &nam;
  356. /* Point the FAB/NAM[L] fields to the actual name and default name. */
  357. #ifdef NAML$C_MAXRSS
  358. fab.fab$l_dna = (char *) -1; /* Using NAML for default name. */
  359. fab.fab$l_fna = (char *) -1; /* Using NAML for file name. */
  360. #endif /* def NAML$C_MAXRSS */
  361. /* Default name = Zip archive name. */
  362. FAB_OR_NAML( fab, nam).FAB_OR_NAML_DNA = zip;
  363. FAB_OR_NAML( fab, nam).FAB_OR_NAML_DNS = strlen( zip);
  364. /* File name = "ZI<unique>,;". */
  365. FAB_OR_NAML( fab, nam).FAB_OR_NAML_FNA = zip_tmp_nam;
  366. FAB_OR_NAML( fab, nam).FAB_OR_NAML_FNS = strlen( zip_tmp_nam);
  367. nam.NAM_ESA = exp_str; /* Expanded name (result) storage. */
  368. nam.NAM_ESS = NAM_MAXRSS; /* Size of expanded name storage. */
  369. nam.NAM_NOP = NAM_M_SYNCHK; /* Syntax-only analysis. */
  370. temp_name = NULL; /* Prepare for failure (unlikely). */
  371. sts = sys$parse( &fab, 0, 0); /* Parse the name(s). */
  372. if ((sts& STS$M_SEVERITY) == STS$M_SUCCESS)
  373. {
  374. /* Overlay any resulting file type (typically ".ZIP") with none. */
  375. strcpy( nam.NAM_L_TYPE, ".;");
  376. /* Allocate temp name storage (as caller expects), and copy the
  377. (truncated) temp name into the new location.
  378. */
  379. temp_name = malloc( strlen( nam.NAM_ESA)+ 1);
  380. if (temp_name != NULL)
  381. {
  382. strcpy( temp_name, nam.NAM_ESA);
  383. }
  384. }
  385. return temp_name;
  386. } /* tempname() for VMS. */
  387. /* 2005-02-17 SMS.
  388. *
  389. * ziptyp() for VMS.
  390. *
  391. * Generate a real Zip archive file name (exact, if it exists), using
  392. * a default file name.
  393. *
  394. * 2005-02-17 SMS. Moved to here from [-]ZIPFILE.C, to segregate
  395. * better the RMS stuff.
  396. *
  397. * Before 2005-02-17, if sys$parse() failed, ziptyp() returned a null
  398. * string ("&zero", where "static char zero = '\0';"). This
  399. * typically caused Zip to proceed, but then the final rename() of
  400. * the temporary archive would (silently) fail (null file name, after
  401. * all), leaving only the temporary archive file, and providing no
  402. * warning message to the victim. Now, when sys$parse() fails,
  403. * ziptyp() returns the original string, so a later open() fails, and
  404. * a relatively informative message is provided. (A VMS-specific
  405. * message could also be provided here, if desired.)
  406. *
  407. * 2005-09-16 SMS.
  408. * Changed name parsing in ziptyp() to solve a problem with a
  409. * search-list logical name device-directory spec for the zipfile.
  410. * Previously, when the zipfile did not exist (so sys$search()
  411. * failed), the expanded name was used, but as it was
  412. * post-sys$search(), it was based on the _last_ member of the search
  413. * list instead of the first. Now, the expanded name from the
  414. * original sys$parse() (pre-sys$search()) is retained, and it is
  415. * used if sys$search() fails. This name is based on the first
  416. * member of the search list, as a user might expect.
  417. */
  418. /* Default Zip archive file spec. */
  419. #define DEF_DEVDIRNAM "SYS$DISK:[].zip"
  420. char *ziptyp( char *s)
  421. {
  422. int status;
  423. int exp_len;
  424. struct FAB fab;
  425. struct NAM_STRUCT nam;
  426. char result[ NAM_MAXRSS+ 1];
  427. char exp[ NAM_MAXRSS+ 1];
  428. char *p;
  429. fab = cc$rms_fab; /* Initialize FAB. */
  430. nam = CC_RMS_NAM; /* Initialize NAM[L]. */
  431. fab.FAB_NAM = &nam; /* FAB -> NAM[L] */
  432. #ifdef NAML$C_MAXRSS
  433. fab.fab$l_dna =(char *) -1; /* Using NAML for default name. */
  434. fab.fab$l_fna = (char *) -1; /* Using NAML for file name. */
  435. #endif /* def NAML$C_MAXRSS */
  436. /* Argument file name and length. */
  437. FAB_OR_NAML( fab, nam).FAB_OR_NAML_FNA = s;
  438. FAB_OR_NAML( fab, nam).FAB_OR_NAML_FNS = strlen( s);
  439. /* Default file spec and length. */
  440. FAB_OR_NAML( fab, nam).FAB_OR_NAML_DNA = DEF_DEVDIRNAM;
  441. FAB_OR_NAML( fab, nam).FAB_OR_NAML_DNS = sizeof( DEF_DEVDIRNAM)- 1;
  442. nam.NAM_ESA = exp; /* Expanded name, */
  443. nam.NAM_ESS = NAM_MAXRSS; /* storage size. */
  444. nam.NAM_RSA = result; /* Resultant name, */
  445. nam.NAM_RSS = NAM_MAXRSS; /* storage size. */
  446. status = sys$parse(&fab);
  447. if ((status & 1) == 0)
  448. {
  449. /* Invalid file name. Return (re-allocated) original, and hope
  450. for a later error message.
  451. */
  452. if ((p = malloc( strlen( s)+ 1)) != NULL )
  453. {
  454. strcpy( p, s);
  455. }
  456. return p;
  457. }
  458. /* Save expanded name length from sys$parse(). */
  459. exp_len = nam.NAM_ESL;
  460. /* Leave expanded name as-is, in case of search failure. */
  461. nam.NAM_ESA = NULL; /* Expanded name, */
  462. nam.NAM_ESS = 0; /* storage size. */
  463. status = sys$search(&fab);
  464. if (status & 1)
  465. { /* Zip file exists. Use resultant (complete, exact) name. */
  466. if ((p = malloc( nam.NAM_RSL+ 1)) != NULL )
  467. {
  468. result[ nam.NAM_RSL] = '\0';
  469. strcpy( p, result);
  470. }
  471. }
  472. else
  473. { /* New Zip file. Use pre-search expanded name. */
  474. if ((p = malloc( exp_len+ 1)) != NULL )
  475. {
  476. exp[ exp_len] = '\0';
  477. strcpy( p, exp);
  478. }
  479. }
  480. return p;
  481. } /* ziptyp() for VMS. */
  482. /* 2005-12-30 SMS.
  483. *
  484. * vms_file_version().
  485. *
  486. * Return the ";version" part of a VMS file specification.
  487. */
  488. char *vms_file_version( char *s)
  489. {
  490. int status;
  491. struct FAB fab;
  492. struct NAM_STRUCT nam;
  493. char *p;
  494. static char exp[ NAM_MAXRSS+ 1]; /* Expanded name storage. */
  495. fab = cc$rms_fab; /* Initialize FAB. */
  496. nam = CC_RMS_NAM; /* Initialize NAM[L]. */
  497. fab.FAB_NAM = &nam; /* FAB -> NAM[L] */
  498. #ifdef NAML$C_MAXRSS
  499. fab.fab$l_dna =(char *) -1; /* Using NAML for default name. */
  500. fab.fab$l_fna = (char *) -1; /* Using NAML for file name. */
  501. #endif /* def NAML$C_MAXRSS */
  502. /* Argument file name and length. */
  503. FAB_OR_NAML( fab, nam).FAB_OR_NAML_FNA = s;
  504. FAB_OR_NAML( fab, nam).FAB_OR_NAML_FNS = strlen( s);
  505. nam.NAM_ESA = exp; /* Expanded name, */
  506. nam.NAM_ESS = NAM_MAXRSS; /* storage size. */
  507. nam.NAM_NOP = NAM_M_SYNCHK; /* Syntax-only analysis. */
  508. status = sys$parse(&fab);
  509. if ((status & 1) == 0)
  510. {
  511. /* Invalid file name. Return "". */
  512. exp[ 0] = '\0';
  513. p = exp;
  514. }
  515. else
  516. {
  517. /* Success. NUL-terminate, and return a pointer to the ";" in
  518. the expanded name storage buffer.
  519. */
  520. p = nam.NAM_L_VER;
  521. p[ nam.NAM_B_VER] = '\0';
  522. }
  523. return p;
  524. } /* vms_file_version(). */
  525. /* 2004-11-23 SMS.
  526. *
  527. * get_rms_defaults().
  528. *
  529. * Get user-specified values from (DCL) SET RMS_DEFAULT. FAB/RAB
  530. * items of particular interest are:
  531. *
  532. * fab$w_deq default extension quantity (blocks) (write).
  533. * rab$b_mbc multi-block count.
  534. * rab$b_mbf multi-buffer count (used with rah and wbh).
  535. */
  536. #define DIAG_FLAG (verbose >= 2)
  537. /* Default RMS parameter values. */
  538. #define RMS_DEQ_DEFAULT 16384 /* About 1/4 the max (65535 blocks). */
  539. #define RMS_MBC_DEFAULT 127 /* The max, */
  540. #define RMS_MBF_DEFAULT 2 /* Enough to enable rah and wbh. */
  541. /* GETJPI item descriptor structure. */
  542. typedef struct
  543. {
  544. short buf_len;
  545. short itm_cod;
  546. void *buf;
  547. int *ret_len;
  548. } jpi_item_t;
  549. /* Durable storage */
  550. static int rms_defaults_known = 0;
  551. /* JPI item buffers. */
  552. static unsigned short rms_ext;
  553. static char rms_mbc;
  554. static unsigned char rms_mbf;
  555. /* Active RMS item values. */
  556. unsigned short rms_ext_active;
  557. char rms_mbc_active;
  558. unsigned char rms_mbf_active;
  559. /* GETJPI item lengths. */
  560. static int rms_ext_len; /* Should come back 2. */
  561. static int rms_mbc_len; /* Should come back 1. */
  562. static int rms_mbf_len; /* Should come back 1. */
  563. /* Desperation attempts to define unknown macros. Probably doomed.
  564. * If these get used, expect sys$getjpiw() to return %x00000014 =
  565. * %SYSTEM-F-BADPARAM, bad parameter value.
  566. * They keep compilers with old header files quiet, though.
  567. */
  568. #ifndef JPI$_RMS_EXTEND_SIZE
  569. # define JPI$_RMS_EXTEND_SIZE 542
  570. #endif /* ndef JPI$_RMS_EXTEND_SIZE */
  571. #ifndef JPI$_RMS_DFMBC
  572. # define JPI$_RMS_DFMBC 535
  573. #endif /* ndef JPI$_RMS_DFMBC */
  574. #ifndef JPI$_RMS_DFMBFSDK
  575. # define JPI$_RMS_DFMBFSDK 536
  576. #endif /* ndef JPI$_RMS_DFMBFSDK */
  577. /* GETJPI item descriptor set. */
  578. struct
  579. {
  580. jpi_item_t rms_ext_itm;
  581. jpi_item_t rms_mbc_itm;
  582. jpi_item_t rms_mbf_itm;
  583. int term;
  584. } jpi_itm_lst =
  585. { { 2, JPI$_RMS_EXTEND_SIZE, &rms_ext, &rms_ext_len },
  586. { 1, JPI$_RMS_DFMBC, &rms_mbc, &rms_mbc_len },
  587. { 1, JPI$_RMS_DFMBFSDK, &rms_mbf, &rms_mbf_len },
  588. 0
  589. };
  590. int get_rms_defaults()
  591. {
  592. int sts;
  593. /* Get process RMS_DEFAULT values. */
  594. sts = sys$getjpiw( 0, 0, 0, &jpi_itm_lst, 0, 0, 0);
  595. if ((sts& STS$M_SEVERITY) != STS$M_SUCCESS)
  596. {
  597. /* Failed. Don't try again. */
  598. rms_defaults_known = -1;
  599. }
  600. else
  601. {
  602. /* Fine, but don't come back. */
  603. rms_defaults_known = 1;
  604. }
  605. /* Limit the active values according to the RMS_DEFAULT values. */
  606. if (rms_defaults_known > 0)
  607. {
  608. /* Set the default values. */
  609. rms_ext_active = RMS_DEQ_DEFAULT;
  610. rms_mbc_active = RMS_MBC_DEFAULT;
  611. rms_mbf_active = RMS_MBF_DEFAULT;
  612. /* Default extend quantity. Use the user value, if set. */
  613. if (rms_ext > 0)
  614. {
  615. rms_ext_active = rms_ext;
  616. }
  617. /* Default multi-block count. Use the user value, if set. */
  618. if (rms_mbc > 0)
  619. {
  620. rms_mbc_active = rms_mbc;
  621. }
  622. /* Default multi-buffer count. Use the user value, if set. */
  623. if (rms_mbf > 0)
  624. {
  625. rms_mbf_active = rms_mbf;
  626. }
  627. }
  628. if (DIAG_FLAG)
  629. {
  630. fprintf( stderr,
  631. "Get RMS defaults. getjpi sts = %%x%08x.\n",
  632. sts);
  633. if (rms_defaults_known > 0)
  634. {
  635. fprintf( stderr,
  636. " Default: deq = %6d, mbc = %3d, mbf = %3d.\n",
  637. rms_ext, rms_mbc, rms_mbf);
  638. }
  639. }
  640. return sts;
  641. }
  642. #ifdef __DECC
  643. /* 2004-11-23 SMS.
  644. *
  645. * acc_cb(), access callback function for DEC C zfopen().
  646. *
  647. * Set some RMS FAB/RAB items, with consideration of user-specified
  648. * values from (DCL) SET RMS_DEFAULT. Items of particular interest are:
  649. *
  650. * fab$w_deq default extension quantity (blocks).
  651. * rab$b_mbc multi-block count.
  652. * rab$b_mbf multi-buffer count (used with rah and wbh).
  653. *
  654. * See also the FOP* macros in OSDEP.H. Currently, no notice is
  655. * taken of the caller-ID value, but options could be set differently
  656. * for read versus write access. (I assume that specifying fab$w_deq,
  657. * for example, for a read-only file has no ill effects.)
  658. */
  659. /* Global storage. */
  660. int fopm_id = FOPM_ID; /* Callback id storage, modify. */
  661. int fopr_id = FOPR_ID; /* Callback id storage, read. */
  662. int fopw_id = FOPW_ID; /* Callback id storage, write. */
  663. int fhow_id = FHOW_ID; /* Callback id storage, in read. */
  664. /* acc_cb() */
  665. int acc_cb( int *id_arg, struct FAB *fab, struct RAB *rab)
  666. {
  667. int sts;
  668. /* Get process RMS_DEFAULT values, if not already done. */
  669. if (rms_defaults_known == 0)
  670. {
  671. get_rms_defaults();
  672. }
  673. /* If RMS_DEFAULT (and adjusted active) values are available, then set
  674. * the FAB/RAB parameters. If RMS_DEFAULT values are not available,
  675. * suffer with the default parameters.
  676. */
  677. if (rms_defaults_known > 0)
  678. {
  679. /* Set the FAB/RAB parameters accordingly. */
  680. fab-> fab$w_deq = rms_ext_active;
  681. rab-> rab$b_mbc = rms_mbc_active;
  682. rab-> rab$b_mbf = rms_mbf_active;
  683. /* Truncate at EOF on close, as we'll probably over-extend. */
  684. fab-> fab$v_tef = 1;
  685. /* If using multiple buffers, enable read-ahead and write-behind. */
  686. if (rms_mbf_active > 1)
  687. {
  688. rab-> rab$v_rah = 1;
  689. rab-> rab$v_wbh = 1;
  690. }
  691. if (DIAG_FLAG)
  692. {
  693. fprintf( mesg,
  694. "Open callback. ID = %d, deq = %6d, mbc = %3d, mbf = %3d.\n",
  695. *id_arg, fab-> fab$w_deq, rab-> rab$b_mbc, rab-> rab$b_mbf);
  696. }
  697. }
  698. /* Declare success. */
  699. return 0;
  700. }
  701. #endif /* def __DECC */
  702. /*
  703. * 2004-09-19 SMS.
  704. *
  705. *----------------------------------------------------------------------
  706. *
  707. * decc_init()
  708. *
  709. * On non-VAX systems, uses LIB$INITIALIZE to set a collection of C
  710. * RTL features without using the DECC$* logical name method.
  711. *
  712. *----------------------------------------------------------------------
  713. */
  714. #ifdef __DECC
  715. #ifdef __CRTL_VER
  716. #if !defined( __VAX) && (__CRTL_VER >= 70301000)
  717. #include <unixlib.h>
  718. /*--------------------------------------------------------------------*/
  719. /* Global storage. */
  720. /* Flag to sense if decc_init() was called. */
  721. int decc_init_done = -1;
  722. /*--------------------------------------------------------------------*/
  723. /* decc_init()
  724. Uses LIB$INITIALIZE to set a collection of C RTL features without
  725. requiring the user to define the corresponding logical names.
  726. */
  727. /* Structure to hold a DECC$* feature name and its desired value. */
  728. typedef struct
  729. {
  730. char *name;
  731. int value;
  732. } decc_feat_t;
  733. /* Array of DECC$* feature names and their desired values. */
  734. decc_feat_t decc_feat_array[] = {
  735. /* Preserve command-line case with SET PROCESS/PARSE_STYLE=EXTENDED */
  736. { "DECC$ARGV_PARSE_STYLE", 1 },
  737. /* Preserve case for file names on ODS5 disks. */
  738. { "DECC$EFS_CASE_PRESERVE", 1 },
  739. /* Enable multiple dots (and most characters) in ODS5 file names,
  740. while preserving VMS-ness of ";version". */
  741. { "DECC$EFS_CHARSET", 1 },
  742. /* List terminator. */
  743. { (char *)NULL, 0 } };
  744. /* LIB$INITIALIZE initialization function. */
  745. static void decc_init( void)
  746. {
  747. int feat_index;
  748. int feat_value;
  749. int feat_value_max;
  750. int feat_value_min;
  751. int i;
  752. int sts;
  753. /* Set the global flag to indicate that LIB$INITIALIZE worked. */
  754. decc_init_done = 1;
  755. /* Loop through all items in the decc_feat_array[]. */
  756. for (i = 0; decc_feat_array[ i].name != NULL; i++)
  757. {
  758. /* Get the feature index. */
  759. feat_index = decc$feature_get_index( decc_feat_array[ i].name);
  760. if (feat_index >= 0)
  761. {
  762. /* Valid item. Collect its properties. */
  763. feat_value = decc$feature_get_value( feat_index, 1);
  764. feat_value_min = decc$feature_get_value( feat_index, 2);
  765. feat_value_max = decc$feature_get_value( feat_index, 3);
  766. if ((decc_feat_array[ i].value >= feat_value_min) &&
  767. (decc_feat_array[ i].value <= feat_value_max))
  768. {
  769. /* Valid value. Set it if necessary. */
  770. if (feat_value != decc_feat_array[ i].value)
  771. {
  772. sts = decc$feature_set_value( feat_index,
  773. 1,
  774. decc_feat_array[ i].value);
  775. }
  776. }
  777. else
  778. {
  779. /* Invalid DECC feature value. */
  780. printf( " INVALID DECC FEATURE VALUE, %d: %d <= %s <= %d.\n",
  781. feat_value,
  782. feat_value_min, decc_feat_array[ i].name, feat_value_max);
  783. }
  784. }
  785. else
  786. {
  787. /* Invalid DECC feature name. */
  788. printf( " UNKNOWN DECC FEATURE: %s.\n", decc_feat_array[ i].name);
  789. }
  790. }
  791. }
  792. /* Get "decc_init()" into a valid, loaded LIB$INITIALIZE PSECT. */
  793. #pragma nostandard
  794. /* Establish the LIB$INITIALIZE PSECTs, with proper alignment and
  795. other attributes. Note that "nopic" is significant only on VAX.
  796. */
  797. #pragma extern_model save
  798. #pragma extern_model strict_refdef "LIB$INITIALIZ" 2, nopic, nowrt
  799. const int spare[ 8] = { 0 };
  800. #pragma extern_model strict_refdef "LIB$INITIALIZE" 2, nopic, nowrt
  801. void (*const x_decc_init)() = decc_init;
  802. #pragma extern_model restore
  803. /* Fake reference to ensure loading the LIB$INITIALIZE PSECT. */
  804. #pragma extern_model save
  805. int LIB$INITIALIZE( void);
  806. #pragma extern_model strict_refdef
  807. int dmy_lib$initialize = (int) LIB$INITIALIZE;
  808. #pragma extern_model restore
  809. #pragma standard
  810. #endif /* !defined( __VAX) && (__CRTL_VER >= 70301000) */
  811. #endif /* def __CRTL_VER */
  812. #endif /* def __DECC */
  813. #endif /* VMS */