/
vms2unix.c
741 lines (679 loc) · 16.5 KB
/
vms2unix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/*
* vms2unix.c
*
* Miscellaneous routines for UNIX/VMS compatibility.
*
* $Id: vms2unix.c,v 1.43 2005/06/03 00:03:29 tom Exp $
*
*/
#include "estruct.h"
#include "edef.h"
#include "dirstuff.h"
#define PERIOD '.'
#define COLON ':'
#define SEMICOLON ';'
static const char RootDir[] = "000000";
static const char DirType[] = ".DIR";
#if SYS_VMS
#include <starlet.h>
#include <unixio.h>
#include <ssdef.h>
#include <fab.h>
#define zfab dirp->dd_fab
#define znam dirp->dd_nam
#define zrsa dirp->dd_ret.d_name
#define zrsl dirp->dd_ret.d_namlen
#define zesa dirp->dd_esa
DIR *
opendir(char *filename)
{
DIR *dirp = typecalloc(DIR);
long status;
if (dirp == 0)
return (0);
zfab = cc$rms_fab;
zfab.fab$l_fop = FAB$M_NAM;
zfab.fab$l_nam = &znam; /* FAB => NAM block */
zfab.fab$l_dna = (global_g_val(GMDALL_VERSIONS)
? "*.*;*"
: "*.*;");
zfab.fab$b_dns = strlen(zfab.fab$l_dna);
zfab.fab$l_fna = filename;
zfab.fab$b_fns = strlen(filename);
znam = cc$rms_nam;
znam.nam$b_ess = NAM$C_MAXRSS;
znam.nam$l_esa = zesa;
znam.nam$b_rss = NAM$C_MAXRSS;
znam.nam$l_rsa = zrsa;
if (sys$parse(&zfab) != RMS$_NORMAL) {
(void) closedir(dirp);
dirp = 0;
}
return (dirp);
}
DIRENT *
readdir(DIR * dirp)
{
if (sys$search(&zfab) == RMS$_NORMAL) {
zrsl = znam.nam$b_rsl;
return (&(dirp->dd_ret));
}
return (0);
}
int
closedir(DIR * dirp)
{
cfree(dirp);
return 0;
}
char *
tempnam(const char *head, const char *tail)
{
char temp[NFILEN];
char leaf[NFILEN];
return mktemp(strmalloc(pathcat(temp,
head,
strcat(strcpy(leaf, tail),
"XXXXXX"))));
}
/*
* Check if the given file exists. If so, try to reuse its record-format when
* creating a new version.
*
* Note: using a '0' protection on VMS C 'open()' tells it to use an existing
* file's protection, or (if the file doesn't exist) the user's default
* protection.
*/
int
vms_creat(char *filename)
{
char rfm_option[80];
char rat_option[80];
char len_option[80];
struct stat sb;
int my_rfm = b_val(curbp, VAL_RECORD_FORMAT);
int my_rat = b_val(curbp, VAL_RECORD_ATTRS);
int my_len = b_val(curbp, VAL_RECORD_LENGTH);
int fd;
/*
* Only check for the previous file's record-format if the mode has not
* been specified.
*/
if (my_rfm == FAB$C_UDF) {
if (file_stat(filename, &sb) == 0) {
my_rfm = sb.st_fab_rfm;
my_rat = sb.st_fab_rat;
my_len = sb.st_fab_mrs;
/* Ignore the old record attributes, since 'cr' is the
* only one we know anything about.
*/
}
if (my_rfm == FAB$C_UDF)
my_rfm = FAB$C_STMLF;
}
lsprintf(rfm_option, "rfm=%s", vms_record_format(my_rfm));
lsprintf(rat_option, "rat=%s", vms_record_attrs(my_rat));
lsprintf(len_option, "mrs=%d", my_len);
switch (my_rfm) {
case FAB$C_FIX:
fd = creat(filename, 0, rfm_option, len_option, rat_option);
break;
case FAB$C_VFC:
/*
* FIXME: Must specify the fixed-length control field size
* (there's no interface for that at the moment, so I
* hardwired it at two bytes. Tom, I'm punting this one
* back to you.
*
* C. Morgan 11/29/1998.
*
* VFC files can only up to 32768 bytes/record, so the control
* field size is "always" 2 bytes -TD
*/
fd = creat(filename,
0,
rfm_option,
len_option,
"fsz=2", /* Fixed-length control field size */
rat_option);
break;
default:
/*
* The stmlf and var record-formats are really all that is
* required. Other combinations are experimental.
*/
fd = creat(filename, 0, rfm_option, "rat=cr");
break;
}
return fd;
}
/*
* Does "filename" exist? Can't use stat(), fstat(), or access() because
* they return incorrect results for DECNET-based files.
*
* Note 1: This routine will handle wildcarded filenames, but that's not it's
* purpose. Think of this routine as a replacement for
* "access(file, F_OK)".
*
* Note 2: This routine returns TRUE for "some.dir" (assuming it exists),
* but not "[.some]" .
*/
int
vms_ffexists(char *filename)
{
struct FAB fab;
struct NAM nam;
char expnd_file_buffer[NAM$C_MAXRSS + 1];
char rslt_file_buffer[NAM$C_MAXRSS + 1];
fab = cc$rms_fab;
nam = cc$rms_nam;
fab.fab$l_fna = filename;
fab.fab$b_fns = strlen(filename);
fab.fab$l_nam = &nam;
nam.nam$l_esa = expnd_file_buffer;
nam.nam$b_ess = NAM$C_MAXRSS;
nam.nam$l_rsa = rslt_file_buffer;
nam.nam$b_rss = NAM$C_MAXRSS;
return (sys$parse(&fab) == RMS$_NORMAL && sys$search(&fab) == RMS$_NORMAL);
}
#endif
#if OPT_VMS_PATH
/*
* These functions are adapted from my port2vms library -- T.Dickey
*/
/******************************************************************************
* Translate a UNIX-style name into a VMS-style name. *
******************************************************************************/
static int DotPrefix(char *s);
static char CharToVms(int c);
static int leading_uc(char *dst, char *src);
static int is_version(char *s);
static int leaf_dot; /* counts dots found in a particular leaf */
static int leaf_ver; /* set if we found a DECshell version */
/*
* If we have a dot in the second character position, force that to a dollar
* sign. Otherwise, keep the first dot in each unix leaf as a dot in the
* resulting vms name.
*/
static int
DotPrefix(char *s)
{
if (s[0] != EOS
&& s[1] == PERIOD
&& s[2] != EOS
&& strchr("sp", s[0])) /* hack for SCCS */
return (-1);
return (0);
}
static char
CharToVms(int c)
{
if (c == PERIOD) {
if (leaf_dot++)
c = '$';
} else if (!isAlnum(c) && !strchr("_-", c)) {
c = '$';
}
return (c);
}
static int
leading_uc(char *dst, char *src)
{
char *base = dst;
int c;
while ((c = *src) != EOS && c != SLASHC) {
if (isAlpha(c)) {
if (isLower(c))
return (0);
} else if (!strchr("0123456789$_", c))
return (0);
*dst++ = c;
*dst = EOS;
src++;
}
*dst = EOS;
if ((*base) && (dst = getenv(base)) != 0) {
c = strlen(base);
dst = skip_blanks(dst);
(void) strcpy(base, dst);
return (c);
}
return (0);
}
/*
* Returns true if the string points to a valid VMS version indicator, which
* may begin with a '.' or ';', e.g.,
* ;1
* ;*
* ;-1
*/
static int
is_version(char *s)
{
char *t;
if (*s == PERIOD
|| *s == SEMICOLON) {
if (*++s == EOS
|| !strcmp(s, "*")
|| !strcmp(s, "0")
|| (strtol(s, &t, 10) && *t == EOS))
return TRUE;
}
return FALSE;
}
static char *
path_suffix(char *path)
{
char *leaf = pathleaf(path);
char *type = strchr(leaf, '.');
if (type == 0)
type = skip_string(leaf);
return type;
}
static char *
path_version(char *path)
{
char *vers = strchr(path, SEMICOLON);
if (vers == 0)
vers = skip_string(path);
return vers;
}
char *
unix2vms_path(char *dst, const char *src)
{
#if !SYS_VMS
char tmp2[NFILEN];
#endif
char tmp[NFILEN];
char leading[NFILEN];
char *t;
char *s = strcpy(tmp, src); /* ... to permit src == dst */
char *d = dst;
char c = '?';
int bracket = FALSE; /* true when "[" passed. */
int on_top = FALSE; /* true when no "[." lead */
int node = FALSE; /* true when node found */
int device = FALSE; /* true when device found */
int len;
/*
* If VMS 'getenv()' is given an upper-case name, it assumes that it
* corresponds to a logical device assignment. As a special case, if
* we have a leading token of this form, translate it.
*/
if ((len = leading_uc(leading, s)) != 0) {
s += len;
len = strlen(strcpy(d, leading));
while (len > 1 && d[len - 1] == ' ')
len--;
if (*s) { /* text follows leading token */
s++; /* skip (assumed) SLASHC */
if ((len > 1)
&& (d[len - 1] == COLON)) {
on_top = TRUE;
} else if (strchr(s, SLASHC)) { /* must do a splice */
if ((len > 2)
&& (d[len - 1] == R_BLOCK)) {
bracket++;
if (d[len - 2] == PERIOD)
/* rooted-device ? */
len -= 2;
else
len--;
}
}
}
d[len] = EOS;
if ((t = strchr(d, COLON)) != NULL) {
if (t[1] == COLON) {
node = TRUE;
if ((t = strchr(t + 2, COLON)) != NULL)
device = TRUE;
} else
device = TRUE;
}
d += len;
} else if (*s == CH_TILDE) { /* process home-directory reference */
char *home = getenv("SYS$LOGIN");
#if !SYS_VMS
if (home == 0)
home = unix2vms_path(tmp2, getenv("HOME"));
#endif
node =
device = TRUE;
s++;
len = strlen(strcpy(d, home));
if (d[len - 1] == R_BLOCK) {
bracket++;
if (strcmp(s, "/")) { /* strip right-bracket to allow new levels */
if (d[len - 2] == PERIOD)
len--;
d[len - 1] = PERIOD;
} else {
s++;
len--;
}
}
d += len;
}
/* look for node-name in VMS-format */
if (!node
&& (t = strchr(s, '!')) != 0
&& (t[1] == SLASHC || t[1] == EOS)) {
leaf_dot = DotPrefix(s);
while (s < t)
*d++ = CharToVms(*s++);
*d++ = COLON;
*d++ = COLON;
s++; /* skip over '!' */
}
/* look for device-name, indicated by a leading SLASHC */
if (!device
&& (*s == SLASHC)) {
leaf_dot = DotPrefix(++s);
if ((t = strchr(s, SLASHC)) == 0)
t = skip_string(s);
else if (t[1] == EOS)
on_top = TRUE;
while (s < t)
*d++ = CharToVms(*s++);
if (d != dst)
*d++ = COLON;
}
/* permit leading "./" to simplify cases in which we concatenate */
if (!strncmp(s, "./", 2))
s += 2;
/* translate repeated leading "../" */
while (!strncmp(s, "../", 3)) {
s += 3;
if (!bracket++)
*d++ = L_BLOCK;
*d++ = '-';
}
if (!strcmp(s, "..")) {
s += 2;
if (!bracket++)
*d++ = L_BLOCK;
*d++ = '-';
}
if (strchr(s, SLASHC)) {
if (!bracket++)
*d++ = L_BLOCK;
if (*s == SLASHC) {
s++;
} else if (!on_top) {
*d++ = PERIOD;
}
while ((c = *s++) != EOS) {
if (c == PERIOD) {
c = '$';
if (*s == SLASHC) /* ignore "./" */
continue;
}
if (c == SLASHC) {
leaf_dot = DotPrefix(s);
if (strchr(s, SLASHC))
*d++ = PERIOD;
else {
break;
}
} else {
*d++ = CharToVms(c);
}
}
}
if (bracket) {
if (on_top && d[-1] == L_BLOCK) {
(void) strcpy(d, RootDir);
d += strlen(d);
}
*d++ = R_BLOCK;
}
if (c != EOS && *s) {
leaf_dot = DotPrefix(s);
while ((c = *s) != EOS) {
if ((leaf_ver = is_version(s)) == TRUE) {
leaf_dot = TRUE; /* no longer pertinent */
(void) strcpy(d, s);
*d = SEMICOLON; /* make this unambiguous */
d += strlen(d);
break;
} else {
*d++ = CharToVms(c);
}
s++;
}
if (!leaf_dot)
*d++ = PERIOD;
if (!leaf_ver)
*d++ = SEMICOLON;
}
*d = EOS;
return mkupper(dst);
}
/******************************************************************************
* Returns a pointer to a pathname's suffix iff it is likely a directory's *
******************************************************************************/
char *
is_vms_dirtype(char *path)
{
char *t = path_suffix(path);
char *v = path_version(t);
size_t len = (v - t);
if (len == sizeof(DirType) - 1
&& !strncmp(t, DirType, len)
&& (!*v || !strcmp(v, ";1"))) {
return t;
}
return 0;
}
/******************************************************************************
* Returns a pointer to a pathname's leaf iff it is the root directory *
******************************************************************************/
char *
is_vms_rootdir(char *path)
{
char *type;
if ((type = is_vms_dirtype(path)) != 0) {
char *leaf = pathleaf(path);
size_t len = (type - leaf);
if (len == sizeof(RootDir) - 1
&& !strncmp(leaf, RootDir, len))
return leaf;
}
return 0;
}
/******************************************************************************
* Convert a VMS directory-filename into the corresponding pathname *
******************************************************************************/
void
vms_dir2path(char *path)
{
char *s;
if ((s = is_vms_rootdir(path)) != 0) {
*s = EOS;
} else {
if ((s = strrchr(path, R_BLOCK)) != 0
&& (s[1] != EOS)) {
char *t;
if ((t = is_vms_dirtype(s)) != 0) {
*s = '.';
*t++ = R_BLOCK;
*t = EOS;
}
}
}
}
/******************************************************************************
* Convert a VMS pathname into the name of the corresponding directory-file. *
* *
* Note that this returns a pointer to a static buffer which is overwritten *
* by each call. *
******************************************************************************/
char *
vms_path2dir(const char *src)
{
static char buffer[NFILEN];
char *s = skip_string(strcpy(buffer, src));
if (s != buffer && *(--s) == R_BLOCK) {
(void) strcpy(s, DirType);
while (--s >= buffer) {
if (*s == PERIOD) {
*s = R_BLOCK;
if (s == buffer + 1) { /* absorb "]" */
char *t = s + 1;
s = buffer;
while ((*s++ = *t++) != EOS)
/*EMPTY */ ;
}
break;
}
if (*s == L_BLOCK) { /* absorb "[" */
char *t = s + 1;
if (is_vms_rootdir(t)
&& (s == buffer || s[-1] == COLON)) {
(void) lsprintf(t, "%s%c%s%s",
RootDir,
R_BLOCK,
RootDir,
DirType);
} else {
while ((*s++ = *t++) != EOS)
/*EMPTY */ ;
}
break;
}
}
}
return (buffer);
}
/******************************************************************************
* Translate a VMS-style pathname to a UNIX-style pathname *
******************************************************************************/
char *
vms2unix_path(char *dst, const char *src)
{
char current[NFILEN];
int need_dev = FALSE;
int have_dev = FALSE;
char tmp[NFILEN];
char *output = dst;
char *base = tmp;
char *s = strcpy(tmp, src); /* ... to permit src == dst */
char *d;
strip_version(s);
/* look for node specification */
if ((s = strchr(base, COLON)) != 0
&& (s[1] == COLON)) {
while (base < s) {
*dst++ = *base++;
}
*dst++ = '!';
base += 2;
need_dev = TRUE;
}
/*
* Look for device specification. If not found, see if the path must
* begin at the top of the device. In this case, it would be ambiguous
* if no device is supplied.
*/
if ((s = strchr(base, COLON)) != NULL) {
*dst++ = SLASHC;
while (base < s) {
*dst++ = *base++;
}
base++; /* skip over ":" */
have_dev = TRUE;
} else if (need_dev
|| ((base[0] == L_BLOCK)
&& (base[1] != '-')
&& (base[1] != PERIOD)
&& (base[1] != R_BLOCK))) { /* must supply a device */
char *a = getcwd(current, NFILEN);
char *b = strchr(a ? a : "?", COLON);
if ((b != 0)
&& (b[1] == COLON)) { /* skip over node specification */
a = b + 2;
b = strchr(a, COLON);
}
if (b != 0) {
*dst++ = SLASHC; /* begin the device */
while (a < b) {
*dst++ = *a++;
}
have_dev = TRUE;
} /* else, no device in getcwd! */
}
/* translate directory-syntax */
if ((s = strchr(base, L_BLOCK)) != NULL) {
int bracketed = TRUE;
if (s[1] == R_BLOCK) {
if (dst != output && *dst != SLASHC)
*dst++ = SLASHC;
*dst++ = PERIOD;
if (s[2] != EOS)
*dst++ = SLASHC;
s += 2;
d = s;
bracketed = FALSE;
} else if (s[1] == PERIOD) {
if (have_dev && dst[-1] != SLASHC)
*dst++ = SLASHC;
s += 2;
d = s;
} else if (s[1] == '-' && strchr("-.]", s[2])) {
s++;
while (*s == '-') {
s++;
*dst++ = PERIOD;
*dst++ = PERIOD;
if (*s == PERIOD
&& (s[1] == '-' || s[1] == R_BLOCK))
/* allow "-.-" */
s++;
if (*s == '-')
*dst++ = SLASHC;
}
d = s;
} else if (!strncmp(s + 1, RootDir, sizeof(RootDir) - 1)
&& strchr(".]", s[sizeof(RootDir)])) {
s += sizeof(RootDir);
d = s;
} else {
d = s;
*s++ = SLASHC;
}
/* expect s points to the last token before right-bracket */
if (bracketed) {
while (*s && *s != R_BLOCK) {
if (*s == PERIOD)
*s = SLASHC;
s++;
}
if (*s)
*s = s[1] ? SLASHC : EOS;
}
} else {
if (have_dev && dst[-1] != SLASHC)
*dst++ = SLASHC;
d = base;
}
/*
* Copy the remainder of the string, trimming trailing "."
*/
for (s = dst; *d; s++, d++) {
*s = *d;
if (*s == PERIOD && d[1] == EOS)
*s = EOS;
}
*s = EOS;
s = vms_pathleaf(dst);
/* SCCS hack */
if (*s == '$') {
*s++ = PERIOD;
} else if (s[0] == 's' && s[1] == '$') {
s[1] = PERIOD;
s += 2;
}
return mklower(output);
}
#endif /* OPT_VMS_PATH */