home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / PRINTING / DVIPS54.ZIP / DVIPS / VMS / VAXVMS.C < prev    next >
C/C++ Source or Header  |  1990-11-25  |  14KB  |  470 lines

  1. /***********************************************************************
  2. This file provides alternative functions for several VMS VMS  C  library
  3. routines which either unacceptable, or incorrect, implementations.  They
  4. have  been developed and  tested under VMS Version  4.4, but indications
  5. are  that they apply  to  earlier versions, back to 3.2  at least.  They
  6. should be retested with each new release of VMS C.
  7.  
  8. Contents:
  9.     EXIT
  10.     FSEEK
  11.     FTELL
  12.     GETCHAR
  13.     GETENV
  14.     READ
  15.     UNGETC
  16.     getlogin
  17.     qsort
  18.     system
  19.     tell
  20.     unlink
  21.  
  22. The VAX VMS  file system record  structure has  unfortunate consequences
  23. for random access files.
  24.  
  25. By default, text  files written by most system  utilities, and languages
  26. other than C, have a variable  length record format,  in  which a 16-bit
  27. character count is  aligned on an  even-byte boundary in the  disk block
  28. b(always 512 bytes   in VMS, independent  of  record and  file  formats),
  29. followed by  <count> bytes of data.   Binary files, such  as .EXE, .OBJ,
  30. and  TeX .DVI  and font  files, all use a  512-byte  fixed record format
  31. which  has no explicit  length  field.  No  file  byte count  is stored;
  32. instead, the block count,  and the  offset of the  last data byte in the
  33. last block are recorded in the file header  (do ``DUMP/HEADER filespec''
  34. to see it).  For binary files with fixed-length  records, the last block
  35. is normally  assumed to be  full,  and  consequently, file   transfer of
  36. binary data from other machines  via Kermit, FTP, or DCL  COPY from ANSI
  37. tapes, generally fails because  the input file length is  not a multiple
  38. of 512.
  39.  
  40. This record organization may  be contrasted with  the STREAM, STREAM_LF,
  41. and STREAM_CR organizations supported from Version 4.0; in  these,  disk
  42. blocks contain a continuous byte stream in which nothing, or  LF, or CR,
  43. is recognized as a record terminator.  These formats are similar to  the
  44. Unix  and TOPS-20 file system  formats  which also use continuous   byte
  45. streams.
  46.  
  47. For C, this  means that a  program operating on a file  in record format
  48. cannot count input characters and expect that count to be the same value
  49. as the  offset parameter passed  to fseek(),  which  numerous C programs
  50. assume to  be the case.  The draft  ANSI C  standard,  and  Harbison and
  51. Steele's ``C Reference Manual'', emphasize that only  values returned by
  52. ftell() should be used as arguments to fseek(),  allowing the program to
  53. return to  a position previously read or  written.  UNFORTUNATELY, VMS C
  54. ftell()  DOES NOT  RETURN   A CORRECT  OFFSET VALUE FOR   RECORD  FILES.
  55. Instead, for record files, it returns the byte  offset  of the start  of
  56. the current record, no matter where in that  record the current position
  57. may  be.   This  misbehavior  is  completely unnecessary,   since    the
  58. replacements below perform correctly, and are written entirely in C.
  59.  
  60. Another problem is that ungetc(char c,  FILE*  fp) is unreliable.  VMS C
  61. implements  characters  as  signed 8-bit integers  (so  do many other  C
  62. implementations).  fgetc(FILE*  fp) returns an int,  not  a  char, whose
  63. value is EOF (-1) in the event of end-of-file;  however, this value will
  64. also  be returned for  a   character  0xFF, so  it  is essential  to use
  65. feof(FILE  *fp) to test  for a  true end-of-file condition  when  EOF is
  66. returned.   ungetc() checks the sign of  its argument c,  and  if it  is
  67. negative (which it will be for 128 of the 256 signed  bytes), REFUSES TO
  68. PUT IT BACK IN THE INPUT STREAM, on the assumption that c is really EOF.
  69. This  too can  be fixed;   ungetc()  should only  do   nothing if feof()
  70. indicates  a  true  end-of-file  condition.   The   overhead of  this is
  71. trivial, since feof() is   actually implemented  as a macro   which does
  72. nothing more than a logical AND and compare-with-zero.
  73.  
  74. getchar()  waits for a <CR> to  be typed when stdin is  a terminal;  the
  75. replacement vms_getchar() remedies this.
  76.  
  77. Undoubtedly  other  deficiencies  in   VMS  C will   reveal  themselves.
  78.  
  79. VMS read() returns   only  a  single  disk   block on  each call.    Its
  80. replacment, vms_read(), will  return  the  requested number of bytes, if
  81. possible.
  82.  
  83. There are also a  few Unix standard  functions which are  unimplemented.
  84. qsort() is not provided.  getlogin()  and unlink() have VMS  equivalents
  85. provided below.  tell() is considered obsolete, since its  functionality
  86. is available from lseek(), but it is still seen in a few programs, so is
  87. provided below.   getenv()  fails if  the  name contains  a  colon;  its
  88. replacement allows the colon.
  89.  
  90. In the interest  of  minimal source perturbation,  replacements  for VMS
  91. functions   are  given   the same  names,    but prefixed  "vms_".   For
  92. readability,   the original names  are  preserved,  but are converted to
  93. upper-case:
  94.  
  95.     #define FTELL vms_ftell
  96.     #define FSEEK vms_fseek
  97.     #define GETCHAR vms_getchar
  98.     #define GETENV vms_getenv
  99.     #define UNGETC vms_ungetc
  100.  
  101. These  are  only defined to work   correctly for fixed  length  512-byte
  102. records, and no check is made that the file has that organization (it is
  103. possible, but   not without  expensive calls to    fstat(), or access to
  104. internal library structures).
  105.  
  106. [02-Apr-87]  --    Nelson   H.F.  Beebe,  University  of Utah  Center  for
  107.         Scientific Computing
  108. ***********************************************************************/
  109.  
  110. #define EXIT    vms_exit
  111. #define FTELL    vms_ftell
  112. #define FSEEK    vms_fseek
  113. #define GETENV    vms_getenv
  114. #define GETCHAR vms_getchar
  115. #define READ    vms_read
  116. #define UNGETC    vms_ungetc
  117.  
  118. #include <stdio.h>
  119. #include <types.h>
  120. #include <ctype.h>
  121. #include <stat.h>
  122. #include <descrip.h>
  123. #include <iodef.h>        /* need for vms_getchar() */
  124. #include <ssdef.h>
  125.  
  126. /**********************************************************************/
  127. /*-->EXIT*/
  128.  
  129. void
  130. vms_exit(code)
  131. int code;
  132. {
  133.     switch (code)
  134.     {
  135.     case 0:
  136.     exit(1);            /* success */
  137.     break;
  138.  
  139.     default:
  140.     exit(2);            /* error */
  141.     break;
  142.     }
  143. }
  144.  
  145.  
  146. /**********************************************************************/
  147. /*-->FSEEK*/
  148.  
  149. /* VMS fseek() and ftell() on fixed-length record files work correctly
  150. only at block boundaries.  This replacement code patches in the offset
  151. within    the  block.  Directions     from    current      position   and  from
  152. end-of-file are converted to absolute positions, and then the code for
  153. that case is invoked. */
  154.  
  155. long
  156. FSEEK(fp,n,dir)
  157. FILE *fp;
  158. long n;
  159. long dir;
  160. {
  161.     long k,m,pos,val,oldpos;
  162.     struct stat buffer;
  163.  
  164.     for (;;)            /* loops only once or twice */
  165.     {
  166.       switch (dir)
  167.       {
  168.       case 0:            /* from BOF */
  169.       oldpos = FTELL(fp);    /* get current byte offset in file */
  170.       k = n & 511;        /* offset in 512-byte block */
  171.       m = n >> 9;        /* relative block number in file */
  172.       if (((*fp)->_cnt) && ((oldpos >> 9) == m)) /* still in same block */
  173.       {
  174.         val = 0;        /* success */
  175.         (*fp)->_ptr = ((*fp)->_base) + k; /* reset pointers to requested byte */
  176.         (*fp)->_cnt = 512 - k;
  177.       }
  178.       else
  179.       {
  180.         val = fseek(fp,m << 9,0); /* move to start of requested 512-byte block */
  181.         if (val == 0)    /* success */
  182.         {
  183.           (*fp)->_cnt = 0;    /* indicate empty buffer */
  184.           (void)fgetc(fp);    /* force refill of buffer */
  185.           (*fp)->_ptr = ((*fp)->_base) + k;    /* reset pointers to requested byte */
  186.           (*fp)->_cnt = 512 - k;
  187.         }
  188.       }
  189.       return(val);
  190.  
  191.       case 1:            /* from current pos */
  192.       pos = FTELL(fp);
  193.       if (pos == EOF)    /* then error */
  194.         return (EOF);
  195.       n += pos;
  196.       dir = 0;
  197.       break;        /* go do case 0 */
  198.  
  199.       case 2:            /* from EOF */
  200.       val = fstat(fileno(fp),&buffer);
  201.       if (val == EOF)    /* then error */
  202.         return (EOF);
  203.       n += buffer.st_size - 1; /* convert filesize to offset and */
  204.                    /* add to requested offset */
  205.       dir = 0;
  206.       break;        /* go do case 0 */
  207.  
  208.       default:            /* illegal direction parameter */
  209.       return (EOF);
  210.       }
  211.     }
  212. }
  213.  
  214. /**********************************************************************/
  215. /*-->FTELL*/
  216.  
  217. /* With fixed-length record files, ftell() returns the offset of the
  218. start of block.     To get the true position, this must be biased by
  219. the offset within the block. */
  220.  
  221. long
  222. FTELL(fp)
  223. FILE *fp;
  224. {
  225.     char c;
  226.     long pos;
  227.     long val;
  228.     if ((*fp)->_cnt == 0)    /* buffer empty--force refill */
  229.     {
  230.     c = fgetc(fp);
  231.     val = UNGETC(c,fp);
  232.     if (val != c)
  233.         return (EOF);    /* should never happen */
  234.     }
  235.     pos = ftell(fp);        /* this returns multiple of 512 (start of block) */
  236.     if (pos >= 0)        /* then success--patch in offset in block */
  237.       pos += ((*fp)->_ptr) - ((*fp)->_base);
  238.     return (pos);
  239. }
  240.   
  241. /**********************************************************************/
  242. /*-->GETCHAR*/
  243.  
  244. static int tt_channel = -1;    /* terminal channel for image QIO's */
  245.  
  246. #define FAILED(status) (~(status) & 1) /* failure if LSB is 0 */
  247.  
  248. int
  249. GETCHAR()
  250. {
  251.     int ret_char;        /* character returned */
  252.     int status;            /* system service status */
  253.     static $DESCRIPTOR(sys_in,"TT:");
  254.  
  255.     if (tt_channel == -1)    /* then first call--assign channel */
  256.     {
  257.     status = sys$assign(&sys_in,&tt_channel,0,0);
  258.     if (FAILED(status))
  259.         lib$stop(status);
  260.     }
  261.     ret_char = 0;
  262.     status = sys$qiow(0,tt_channel,IO$_TTYREADALL | IO$M_NOECHO,0,0,0,
  263.     &ret_char,1,0,0,0,0);
  264.     if (FAILED(status))
  265.         lib$stop(status);
  266.  
  267.     return (ret_char);
  268. }
  269.   
  270. /**********************************************************************/
  271. /*-->READ*/
  272. int
  273. READ(file_desc,buffer,nbytes)
  274. register int file_desc;
  275. register char *buffer;
  276. register int nbytes;
  277. {
  278.     register int ngot;
  279.     register int left;
  280.     
  281.     for ((left = nbytes, ngot = 0); left > 0; /* NOOP */)
  282.     {
  283.     ngot = read(file_desc,buffer,left);
  284.     if (ngot < 0)
  285.         return (-1);    /* error occurred */
  286.     buffer += ngot;
  287.     left -= ngot;
  288.     }
  289.     return(nbytes-left);
  290. }
  291.  
  292. /**********************************************************************/
  293. /*-->UNGETC*/
  294. long
  295. UNGETC(c,fp)    /* VMS ungetc() is a no-op if c < 0 (which is half the time!) */
  296. char c;
  297. FILE *fp;
  298. {
  299.  
  300.     if ((c == EOF) && feof(fp))
  301.     return (EOF);        /* do nothing at true end-of-file */
  302.     else if ((*fp)->_cnt >= 512)/* buffer full--no fgetc() done in this block!*/
  303.     return (EOF);        /* must be user error if this happens */
  304.     else            /* put the character back in the buffer */
  305.     {
  306.       (*fp)->_cnt++;        /* increase count of characters left */
  307.       (*fp)->_ptr--;        /* backup pointer to next available char */
  308.       *((*fp)->_ptr) = c;    /* save the character */
  309.       return (c);        /* and return it */
  310.     }
  311. }
  312.  
  313. /**********************************************************************/
  314. /*-->getenv*/
  315. char*
  316. GETENV(name)
  317. char* name;
  318. {
  319.     char* p;
  320.     char* result;
  321.     char ucname[256];
  322.  
  323.     p = ucname;
  324.     while (*name)    /* VMS logical names must be upper-case */
  325.     {
  326.       *p++ = islower(*name) ? toupper(*name) : *name;
  327.       ++name;
  328.     }
  329.     *p = '\0';
  330.  
  331.     p = strchr(ucname,':');        /* colon in name? */
  332.     if (p == (char *)NULL)        /* no colon in name */
  333.         result = getenv(ucname);
  334.     else                /* try with and without colon */
  335.     {
  336.     result = getenv(ucname);
  337.     if (result == (char *)NULL)
  338.     {
  339.         *p = '\0';
  340.         result = getenv(ucname);
  341.         *p = ':';
  342.     }
  343.     }
  344.     return (result);
  345. }
  346.  
  347. /**********************************************************************/
  348. /*-->getlogin*/
  349. char*
  350. getlogin()
  351. {
  352.     return ((char *)getenv("USER")); /* use equivalent VMS routine */
  353. }
  354.  
  355. /**********************************************************************/
  356. /*-->qsort*/
  357.  
  358. /***********************************************************************
  359. TeXindex uses  the standard  Unix  library function  qsort()  for
  360. record sorting.  Unfortunately, qsort()  is not a stable  sorting
  361. algorithm, so input order is not necessarily preserved for  equal
  362. sort  keys.    This  is   important,  because   the  sorting   is
  363. case-independent, while  the  actual  entries may  not  be.   For
  364. example, the input
  365.  
  366. \entry{i}{22}{{\CODE{i}}}
  367. \entry{i}{42}{{\CODE{i}}}
  368. \entry{I}{41}{{\CODE{I}}}
  369. \entry{I}{42}{{\CODE{I}}}
  370.  
  371. produces
  372.  
  373. \initial {I}
  374. \entry {{\CODE{i}}}{22}
  375. \entry {{\CODE{I}}}{41--42}
  376. \entry {{\CODE{i}}}{42}
  377.  
  378. instead of the correct
  379.  
  380. \initial {I}
  381. \entry {{\CODE{i}}}{22, 42}
  382. \entry {{\CODE{I}}}{41--42}
  383.  
  384. We  therefore  provide  this  stable  shellsort  replacement  for
  385. qsort() based  on the  code  given on  p.  116 of  Kernighan  and
  386. Ritchie, ``The  C Programming  Language'', Prentice-Hall  (1978).
  387. This has  order  N**1.5  average performance,  which  is  usually
  388. slower than qsort().  In the interests of simplicity, we make  no
  389. attempt to handle short sequences by alternative methods.
  390.  
  391. [07-Nov-86]
  392. ***********************************************************************/
  393.  
  394. #if VMS_QSORT
  395. #define BASE(i) &base[(i)*width]
  396.  
  397. void
  398. qsort(base, nel, width, compar)
  399.     char base[];    /* start of data in memory */
  400.     int nel;        /* number of elements to be sorted */
  401.     int width;        /* size (in bytes) of each element */
  402.     int (*compar)();    /* comparison function */
  403. {
  404.     int gap;
  405.     int i;
  406.     int j;
  407.  
  408.     register int k;    /* inner exchange loop parameters */
  409.     register char* p;
  410.     register char* q;
  411.     register char  c;
  412.  
  413.     for (gap = nel/2; gap > 0; gap /= 2)
  414.     {
  415.     for (i = gap; i < nel; i++)
  416.     {
  417.         for (j = i-gap; j >= 0; j -= gap)
  418.         {
  419.             p = BASE(j);
  420.         q = BASE(j+gap);
  421.         if ((*compar)(p,q) <= 0)
  422.             break;    /* exit j loop */
  423.         else
  424.         {
  425.             for (k = 0; k < width; (++p, ++q, ++k))
  426.             {
  427.             c = *q;
  428.             *q = *p;
  429.             *p = c;
  430.             }
  431.         }
  432.         }
  433.     }
  434.     }
  435. }
  436. #endif
  437. /**********************************************************************
  438. *-->system*
  439. int
  440. system(s)
  441. char *s;
  442. {
  443.     struct    dsc$descriptor t;
  444.  
  445.     t.dsc$w_length = strlen(s);
  446.     t.dsc$a_pointer = s;
  447.     t.dsc$b_class = DSC$K_CLASS_S;
  448.     t.dsc$b_dtype = DSC$K_DTYPE_T;
  449.     return (LIB$SPAWN(&t) == SS$_NORMAL) ? 0 : 127;
  450. }
  451.  
  452.  
  453. **********************************************************************/
  454. /*-->tell*/
  455. long
  456. tell(handle)
  457. int handle;
  458. {
  459.     return (lseek(handle,0L,1));
  460. }
  461.  
  462. /**********************************************************************/
  463. /*-->unlink*/
  464. int
  465. unlink(filename)
  466. char *filename;
  467. {
  468.     return (delete(filename)); /* use equivalent VMS routine */
  469. }
  470.