| 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 |
#ifdef __GNUC__ |
| 127 |
#include <stdlib.h> |
| 128 |
#endif |
| 129 |
|
| 130 |
void EXIT(int code); |
| 131 |
long FTELL(FILE *fp); |
| 132 |
long FSEEK(FILE *fp, long n, long dir); |
| 133 |
long UNGETC(char c, FILE *fp); |
| 134 |
int GETCHAR(void); |
| 135 |
int READ(int file_desc, char *buffer, int nbytes); |
| 136 |
char *GETENV(char *name); |
| 137 |
char *getlogin(void); |
| 138 |
long tell(int handle); |
| 139 |
int unlink(char *filename); |
| 140 |
|
| 141 |
/**********************************************************************/ |
| 142 |
/*-->EXIT*/ |
| 143 |
|
| 144 |
void |
| 145 |
vms_exit(int code) |
| 146 |
{ |
| 147 |
switch (code) |
| 148 |
{ |
| 149 |
case 0: |
| 150 |
exit(1); /* success */ |
| 151 |
break; |
| 152 |
|
| 153 |
default: |
| 154 |
exit(2); /* error */ |
| 155 |
break; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
/**********************************************************************/ |
| 161 |
/*-->FSEEK*/ |
| 162 |
|
| 163 |
/* VMS fseek() and ftell() on fixed-length record files work correctly |
| 164 |
only at block boundaries. This replacement code patches in the offset |
| 165 |
within the block. Directions from current position and from |
| 166 |
end-of-file are converted to absolute positions, and then the code for |
| 167 |
that case is invoked. */ |
| 168 |
|
| 169 |
long |
| 170 |
FSEEK(FILE *fp, long n, long dir) |
| 171 |
{ |
| 172 |
long k,m,pos,val,oldpos; |
| 173 |
struct stat buffer; |
| 174 |
|
| 175 |
for (;;) /* loops only once or twice */ |
| 176 |
{ |
| 177 |
switch (dir) |
| 178 |
{ |
| 179 |
case 0: /* from BOF */ |
| 180 |
oldpos = FTELL(fp); /* get current byte offset in file */ |
| 181 |
k = n & 511; /* offset in 512-byte block */ |
| 182 |
m = n >> 9; /* relative block number in file */ |
| 183 |
if (((*fp)->_cnt) && ((oldpos >> 9) == m)) /* still in same block */ |
| 184 |
{ |
| 185 |
val = 0; /* success */ |
| 186 |
(*fp)->_ptr = ((*fp)->_base) + k; /* reset pointers to requested byte */ |
| 187 |
(*fp)->_cnt = 512 - k; |
| 188 |
} |
| 189 |
else |
| 190 |
{ |
| 191 |
val = fseek(fp,m << 9,0); /* move to start of requested 512-byte block */ |
| 192 |
if (val == 0) /* success */ |
| 193 |
{ |
| 194 |
(*fp)->_cnt = 0; /* indicate empty buffer */ |
| 195 |
(void)fgetc(fp); /* force refill of buffer */ |
| 196 |
(*fp)->_ptr = ((*fp)->_base) + k; /* reset pointers to requested byte */ |
| 197 |
(*fp)->_cnt = 512 - k; |
| 198 |
} |
| 199 |
} |
| 200 |
return(val); |
| 201 |
|
| 202 |
case 1: /* from current pos */ |
| 203 |
pos = FTELL(fp); |
| 204 |
if (pos == EOF) /* then error */ |
| 205 |
return (EOF); |
| 206 |
n += pos; |
| 207 |
dir = 0; |
| 208 |
break; /* go do case 0 */ |
| 209 |
|
| 210 |
case 2: /* from EOF */ |
| 211 |
val = fstat(fileno(fp),&buffer); |
| 212 |
if (val == EOF) /* then error */ |
| 213 |
return (EOF); |
| 214 |
n += buffer.st_size - 1; /* convert filesize to offset and */ |
| 215 |
/* add to requested offset */ |
| 216 |
dir = 0; |
| 217 |
break; /* go do case 0 */ |
| 218 |
|
| 219 |
default: /* illegal direction parameter */ |
| 220 |
return (EOF); |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
/**********************************************************************/ |
| 226 |
/*-->FTELL*/ |
| 227 |
|
| 228 |
/* With fixed-length record files, ftell() returns the offset of the |
| 229 |
start of block. To get the true position, this must be biased by |
| 230 |
the offset within the block. */ |
| 231 |
|
| 232 |
long |
| 233 |
FTELL(FILE *fp) |
| 234 |
{ |
| 235 |
char c; |
| 236 |
long pos; |
| 237 |
long val; |
| 238 |
if ((*fp)->_cnt == 0) /* buffer empty--force refill */ |
| 239 |
{ |
| 240 |
c = fgetc(fp); |
| 241 |
val = UNGETC(c,fp); |
| 242 |
if (val != c) |
| 243 |
return (EOF); /* should never happen */ |
| 244 |
} |
| 245 |
pos = ftell(fp); /* this returns multiple of 512 (start of block) */ |
| 246 |
if (pos >= 0) /* then success--patch in offset in block */ |
| 247 |
pos += ((*fp)->_ptr) - ((*fp)->_base); |
| 248 |
return (pos); |
| 249 |
} |
| 250 |
|
| 251 |
/**********************************************************************/ |
| 252 |
/*-->GETCHAR*/ |
| 253 |
|
| 254 |
static int tt_channel = -1; /* terminal channel for image QIO's */ |
| 255 |
|
| 256 |
#define FAILED(status) (~(status) & 1) /* failure if LSB is 0 */ |
| 257 |
|
| 258 |
int |
| 259 |
GETCHAR() |
| 260 |
{ |
| 261 |
int ret_char; /* character returned */ |
| 262 |
int status; /* system service status */ |
| 263 |
static $DESCRIPTOR(sys_in,"TT:"); |
| 264 |
|
| 265 |
if (tt_channel == -1) /* then first call--assign channel */ |
| 266 |
{ |
| 267 |
status = sys$assign(&sys_in,&tt_channel,0,0); |
| 268 |
if (FAILED(status)) |
| 269 |
lib$stop(status); |
| 270 |
} |
| 271 |
ret_char = 0; |
| 272 |
status = sys$qiow(0,tt_channel,IO$_TTYREADALL | IO$M_NOECHO,0,0,0, |
| 273 |
&ret_char,1,0,0,0,0); |
| 274 |
if (FAILED(status)) |
| 275 |
lib$stop(status); |
| 276 |
|
| 277 |
return (ret_char); |
| 278 |
} |
| 279 |
|
| 280 |
/**********************************************************************/ |
| 281 |
/*-->READ*/ |
| 282 |
int |
| 283 |
READ(register int file_desc,register char *buffer,register int nbytes) |
| 284 |
{ |
| 285 |
register int ngot; |
| 286 |
register int left; |
| 287 |
|
| 288 |
for ((left = nbytes, ngot = 0); left > 0; /* NOOP */) |
| 289 |
{ |
| 290 |
ngot = read(file_desc,buffer,left); |
| 291 |
if (ngot < 0) |
| 292 |
return (-1); /* error occurred */ |
| 293 |
buffer += ngot; |
| 294 |
left -= ngot; |
| 295 |
} |
| 296 |
return(nbytes-left); |
| 297 |
} |
| 298 |
|
| 299 |
/**********************************************************************/ |
| 300 |
/*-->UNGETC*/ |
| 301 |
long |
| 302 |
UNGETC(char c, FILE *fp) /* VMS ungetc() is a no-op if c < 0 */ |
| 303 |
{ /* (which is half the time!) */ |
| 304 |
|
| 305 |
if ((c == EOF) && feof(fp)) |
| 306 |
return (EOF); /* do nothing at true end-of-file */ |
| 307 |
else if ((*fp)->_cnt >= 512)/* buffer full--no fgetc() done in this block!*/ |
| 308 |
return (EOF); /* must be user error if this happens */ |
| 309 |
else /* put the character back in the buffer */ |
| 310 |
{ |
| 311 |
(*fp)->_cnt++; /* increase count of characters left */ |
| 312 |
(*fp)->_ptr--; /* backup pointer to next available char */ |
| 313 |
*((*fp)->_ptr) = c; /* save the character */ |
| 314 |
return (c); /* and return it */ |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
/**********************************************************************/ |
| 319 |
/*-->getenv*/ |
| 320 |
char* |
| 321 |
GETENV(char *name) |
| 322 |
{ |
| 323 |
char* p; |
| 324 |
char* result; |
| 325 |
char ucname[256]; |
| 326 |
|
| 327 |
p = ucname; |
| 328 |
while (*name) /* VMS logical names must be upper-case */ |
| 329 |
{ |
| 330 |
*p++ = islower(*name) ? toupper(*name) : *name; |
| 331 |
++name; |
| 332 |
} |
| 333 |
*p = '\0'; |
| 334 |
|
| 335 |
p = strchr(ucname,':'); /* colon in name? */ |
| 336 |
if (p == (char *)NULL) /* no colon in name */ |
| 337 |
result = getenv(ucname); |
| 338 |
else /* try with and without colon */ |
| 339 |
{ |
| 340 |
result = getenv(ucname); |
| 341 |
if (result == (char *)NULL) |
| 342 |
{ |
| 343 |
*p = '\0'; |
| 344 |
result = getenv(ucname); |
| 345 |
*p = ':'; |
| 346 |
} |
| 347 |
} |
| 348 |
return (result); |
| 349 |
} |
| 350 |
|
| 351 |
/**********************************************************************/ |
| 352 |
/*-->getlogin*/ |
| 353 |
char* |
| 354 |
getlogin() |
| 355 |
{ |
| 356 |
return ((char *)getenv("USER")); /* use equivalent VMS routine */ |
| 357 |
} |
| 358 |
|
| 359 |
/**********************************************************************/ |
| 360 |
/*-->qsort*/ |
| 361 |
|
| 362 |
/*********************************************************************** |
| 363 |
TeXindex uses the standard Unix library function qsort() for |
| 364 |
record sorting. Unfortunately, qsort() is not a stable sorting |
| 365 |
algorithm, so input order is not necessarily preserved for equal |
| 366 |
sort keys. This is important, because the sorting is |
| 367 |
case-independent, while the actual entries may not be. For |
| 368 |
example, the input |
| 369 |
|
| 370 |
\entry{i}{22}{{\CODE{i}}} |
| 371 |
\entry{i}{42}{{\CODE{i}}} |
| 372 |
\entry{I}{41}{{\CODE{I}}} |
| 373 |
\entry{I}{42}{{\CODE{I}}} |
| 374 |
|
| 375 |
produces |
| 376 |
|
| 377 |
\initial {I} |
| 378 |
\entry {{\CODE{i}}}{22} |
| 379 |
\entry {{\CODE{I}}}{41--42} |
| 380 |
\entry {{\CODE{i}}}{42} |
| 381 |
|
| 382 |
instead of the correct |
| 383 |
|
| 384 |
\initial {I} |
| 385 |
\entry {{\CODE{i}}}{22, 42} |
| 386 |
\entry {{\CODE{I}}}{41--42} |
| 387 |
|
| 388 |
We therefore provide this stable shellsort replacement for |
| 389 |
qsort() based on the code given on p. 116 of Kernighan and |
| 390 |
Ritchie, ``The C Programming Language'', Prentice-Hall (1978). |
| 391 |
This has order N**1.5 average performance, which is usually |
| 392 |
slower than qsort(). In the interests of simplicity, we make no |
| 393 |
attempt to handle short sequences by alternative methods. |
| 394 |
|
| 395 |
[07-Nov-86] |
| 396 |
***********************************************************************/ |
| 397 |
|
| 398 |
#if VMS_QSORT |
| 399 |
#define BASE(i) &base[(i)*width] |
| 400 |
|
| 401 |
void |
| 402 |
qsort(base, nel, width, compar) |
| 403 |
char base[]; /* start of data in memory */ |
| 404 |
int nel; /* number of elements to be sorted */ |
| 405 |
int width; /* size (in bytes) of each element */ |
| 406 |
int (*compar)(); /* comparison function */ |
| 407 |
{ |
| 408 |
int gap; |
| 409 |
int i; |
| 410 |
int j; |
| 411 |
|
| 412 |
register int k; /* inner exchange loop parameters */ |
| 413 |
register char* p; |
| 414 |
register char* q; |
| 415 |
register char c; |
| 416 |
|
| 417 |
for (gap = nel/2; gap > 0; gap /= 2) |
| 418 |
{ |
| 419 |
for (i = gap; i < nel; i++) |
| 420 |
{ |
| 421 |
for (j = i-gap; j >= 0; j -= gap) |
| 422 |
{ |
| 423 |
p = BASE(j); |
| 424 |
q = BASE(j+gap); |
| 425 |
if ((*compar)(p,q) <= 0) |
| 426 |
break; /* exit j loop */ |
| 427 |
else |
| 428 |
{ |
| 429 |
for (k = 0; k < width; (++p, ++q, ++k)) |
| 430 |
{ |
| 431 |
c = *q; |
| 432 |
*q = *p; |
| 433 |
*p = c; |
| 434 |
} |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
#endif |
| 441 |
/********************************************************************** |
| 442 |
*-->system* |
| 443 |
int |
| 444 |
system(char *s) |
| 445 |
{ |
| 446 |
struct dsc$descriptor t; |
| 447 |
|
| 448 |
t.dsc$w_length = strlen(s); |
| 449 |
t.dsc$a_pointer = s; |
| 450 |
t.dsc$b_class = DSC$K_CLASS_S; |
| 451 |
t.dsc$b_dtype = DSC$K_DTYPE_T; |
| 452 |
return (LIB$SPAWN(&t) == SS$_NORMAL) ? 0 : 127; |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
**********************************************************************/ |
| 457 |
/*-->tell*/ |
| 458 |
long |
| 459 |
tell(int handle) |
| 460 |
{ |
| 461 |
return (lseek(handle,0L,1)); |
| 462 |
} |
| 463 |
|
| 464 |
/**********************************************************************/ |
| 465 |
/*-->unlink*/ |
| 466 |
int |
| 467 |
unlink(char *filename) |
| 468 |
{ |
| 469 |
return (delete(filename)); /* use equivalent VMS routine */ |
| 470 |
} |