-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathitstar.c
758 lines (669 loc) · 20.3 KB
/
itstar.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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
/*
Tape archive program to handle ITS DUMP tapes.
By John Wilson <[email protected]>, JOHNW.
04/02/1993 JMBW Created.
08/10/1993 JMBW Mangled to write to TCP socket (using MTS tape drives).
07/08/1994 JMBW Finished local magtape code but didn't test it.
07/15/1998 JMBW -c, -r, -t functions finished.
07/18/1998 JMBW -x function finished.
This file is part of itstar.
itstar is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
itstar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with itstar. If not, see <http://www.gnu.org/licenses/>.
*/
#include <dirent.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <utime.h>
#include <ctype.h>
#include "itstar.h"
/* variables for estimating length of tape used: */
extern unsigned long bpi; /* tape density in bits per inch */
extern unsigned long count; /* count of tape frames written */
extern int simh; /* NZ to support SIMH tape images */
int seven_track = 0; /* NZ to write 7-track tape images */
int big_endian = 0; /* NZ to read big endian record length */
int old_header = 0; /* NZ to limit file header to six words */
static void usage(), itsname(), extitsname(), changedir();
static void addfiles(), addfile(), listfiles(), listfile(),
extfiles(), extfile();
static void scantape(int argc,char **argv,void (*process)());
void save(), weenixname(), nomem();
void writevolhdr(void);
void outsix(char *s);
void insix(char *s);
int append=0; /* func=append */
int create=0; /* func=create */
int type=0; /* func=type filenames */
int extract=0; /* func=extract files */
int verify=0; /* NZ => print names of all files processed on stdout */
unsigned long tapeno=1, reelno=0; /* DUMP tape, reel number */
unsigned long islink; /* NZ => file is a link, 0 => it's a file */
char date6[7]; /* today's date in SIXBIT */
char ufd[7], fn1[7], fn2[7]; /* buffers for UFD and filename 1/2 */
char lufd[7], lfn1[7], lfn2[7]; /* same as above, for target of link */
char dev[7], author[7]; /* not currently used, but available in DIR.LIST */
struct tm cdate, rdate; /* creation, ref dates (none if tm_year=0) */
static int tape_year, tape_month, tape_day;
static char sbuf[256]; /* scratch buffer, for readlink() */
static char *dir=NULL; /* directory to change to */
static char *tape=NULL; /* name of tape drive or file */
int main(int argc,char **argv)
{
int i;
time_t t0;
struct tm *now;
argc--, argv++; /* skip our name */
for(i=0;argc;i++,argc--,argv++) { /* scan through JCL */
char *p=*argv;
if(*p=='-'||i==0) { /* 1st parm is flag even w/o '-' */
if(*p=='-') p++; /* skip the '-' if it's there */
while(*p) { /* scan next byte of option */
switch(*p++) {
case 'c': /* create archive */
create=1;
break;
case 'C': /* change directory */
if(*p) dir=p; /* -Cdir */
else { /* -C dir */
if((--argc)==0) goto msgarg;
dir=*++argv;
}
goto nxtwrd;
case 'f': /* specify archive filename */
if(*p) tape=p; /* -ffilename */
else { /* -f filename */
if((--argc)==0) goto msgarg;
tape=*++argv;
}
goto nxtwrd;
case 'h': /* help */
usage(0);
case 'r': /* append to archive */
append=1;
break;
case 't': /* type filenames */
type=1;
break;
case 'v': /* verify filenames */
verify=1;
break;
case 'x': /* extract files */
extract=1;
break;
case '7': /* 7-track tape images */
seven_track=1;
break;
case 'B': /* Big endian record lenght */
big_endian=1;
break;
case 'E': /* E-11 tape image format */
simh=0;
break;
default:
fprintf(stderr,"?Invalid option: %c\n",
*(p-1));
usage(1);
msgarg: /* missing argument */
fprintf(stderr,"?Missing argument\n");
usage(1);
}
}
}
else break; /* rest of JCL is filenames */
nxtwrd: ;
}
/* check switches */
if((append+create+type+extract)==0) {
fprintf(stderr,"?Must specify one of: -c -t -r -x\n");
exit(1);
}
if((append+create+type+extract)>1) {
fprintf(stderr,"?Switch conflict\n");
exit(1);
}
/* get local time for tape creation info */
t0=time((time_t *)0); /* secs since midnight 01-Jan-1970 */
now=localtime(&t0); /* unpack into local time */
sprintf(date6,"%2.2d%2.2d%2.2d", /* ASCII YYMMDD */
now->tm_year%100,now->tm_mon+1,now->tm_mday);
setenv("TZ","EST5EDT",1); /* ITS dates are all Cambridge, MA */
/* tzset(); */
if(append) { /* append to existing tape */
opentape(tape,0,1); /* open tape */
changedir(); /* change directory */
posneot(1); /* space to EOT */
resetbuf(); /* start a new record */
addfiles(argc,argv); /* add files onto end */
}
else if(create) { /* initialize and write tape */
opentape(tape,1,1); /* open tape */
changedir(); /* change directory */
posnbot(); /* rewind */
writevolhdr(); /* write volume header */
addfiles(argc,argv); /* add files onto end */
}
else if(type) { /* list files on tape */
if(argc!=0) usage(1); /* should we handle filenames? no. */
opentape(tape,0,0); /* open tape */
changedir(); /* change directory */
posnbot(); /* rewind */
listfiles(); /* list files */
}
else /*if(extract)*/ { /* extract files from tape */
if(argc!=0) usage(1); /* should we handle filenames? soon */
opentape(tape,0,0); /* open tape */
changedir(); /* change directory */
posnbot(); /* rewind */
extfiles(argc,argv); /* extract files */
}
closetape();
exit(0);
}
/* change directory if requested */
static void changedir(void)
{
if(!dir) return;
if(chdir(dir)<0) {
perror("?Cannot change directory");
exit(1);
}
}
/* write DUMP volume header to tape */
void writevolhdr(void)
{
resetbuf(); /* a record all to itself */
outword(-4L,0L); /* 1: AOBJN pointer giving length */
outword(tapeno,reelno); /* 2: tape,,reel */
if(verify) printf("Tape %ld, reel %ld\n",tapeno,reelno);
outsix(date6); /* 3: today's date */
outword(0L,0L); /* 4: random dump (not full/incremental) */
/* tapeflush(); */ /* write it out */
/* N.B. no tape mark between vol. header and first file label */
}
/* add files to a DUMP tape */
/* output buffer must have been initialized with resetbuf() */
static void addfiles(int argc,char **argv)
{
int c=argc;
char **v=argv;
while(c--) {
addfile(argc,argv,*v++);
}
if(verify)
printf("Approximately %lu.%lu' of tape used\n",count/bpi/12,
(count*10/bpi/12)%10);
}
/* add a single file to a DUMP tape */
/* output buffer must have been initialized with resetbuf() */
static void addfile(int argc,char **argv,char *f)
{
struct stat s;
/* look up file information */
if(lstat(f,&s)<0) {
fprintf(stderr,"?Error accessing %s\n",f);
exit(1);
}
/* if it's a bare directory, recurse for all files in that dir */
if(S_ISDIR(s.st_mode)) { /* is it a directory? */
DIR *dir;
struct dirent *d;
char *p;
/* if the dir contains DIR.LIST, use that to find files */
if(dirlist(argc,argv,f)==0) return;
/* otherwise open the directory */
if((dir=opendir(f))==NULL) {
fprintf(stderr,"?Error opening directory %s\n",f);
exit(1);
}
/* read through dir entries */
while((d=readdir(dir))!=NULL) {
if(d->d_name[0]=='.') /* ignore ./.., hidden files */
continue;
/* alloc space for path+'/'+file+NUL */
if((p=malloc(strlen(f)+1+strlen(d->d_name)+1))==NULL)
nomem();
/* combine path, /, filename, NUL */
strcpy(p,f);
strcat(p,"/");
strcat(p,d->d_name);
/* recurse */
addfile(argc,argv,p);
free(p);
}
closedir(dir);
return;
}
/* extract dir name/filename, convert to UFD/FN1/FN2 */
extitsname(f,ufd,fn1,fn2);
memcpy(&cdate,localtime(&s.st_mtime),sizeof(struct tm));
memcpy(&rdate,localtime(&s.st_atime),sizeof(struct tm));
if(S_ISLNK(s.st_mode)) { /* it's a link */
int len=readlink(f,sbuf,sizeof(sbuf)-1); /* get link name */
/* (-1 to allow for adding NUL) */
if(len<0) {
perror("?Error following link");
exit(1);
}
sbuf[len]='\0'; /* mark end (readlink() doesn't) */
extitsname(sbuf,lufd,lfn1,lfn2); /* convert to ITS style */
islink=1;
}
else islink=0;
/* now that we've gotten all the names and dates, actually save it */
save(f);
}
/* save a file based using information in UFD/FN1/FN2/CDATE etc. */
/* output buffer must have been initialized with resetbuf() */
void save(char *f)
{
long len = 7;
if(verify) printf("%s => %s;%s %s ",f,ufd,fn1,fn2);
if (old_header)
len = 6;
outword(-len,0L); /* 1: AOBJN ptr giving length */
outsix(ufd); /* 2: UFD */
outsix(fn1); /* 3: filename 1 */
outsix(fn2); /* 4: filename 2 */
outword(islink,0L); /* 5: linkf,,pack */
outword((((unsigned long)cdate.tm_year)<<9L)|
(((unsigned long)cdate.tm_mon+1L)<<5L)|
(unsigned long)cdate.tm_mday,
((((unsigned long)cdate.tm_hour*60L)+
(unsigned long)cdate.tm_min)*60L+
(unsigned long)cdate.tm_sec)*2L); /* 6: date of creation */
/* note: year field is officially only 7 bits, but the 2 bits to */
/* left of it are unused in UFD entries so hopefully it's safe to */
/* grab them */
/* tm_year and UFD year field are both YEAR-1900 */
if (len >= 7) {
outword((((unsigned long)cdate.tm_year)<<9L)|
(((unsigned long)cdate.tm_mon+1L)<<5L)|
(unsigned long)cdate.tm_mday,
((((unsigned long)cdate.tm_hour*60L)+
(unsigned long)cdate.tm_min)*60L+
(unsigned long)cdate.tm_sec)*2L);
/* 7: date of last ref */
}
/* tapeflush(); */ /* finish off label record */
if(islink) { /* it's a link, not a file */
outsix(lfn1); /* write it out (note funny order) */
outsix(lfn2);
outsix(lufd);
tapeflush(); /* end of record (just 15 bytes) */
}
else {
unpack(f); /* add the file itself */
tapeflush(); /* finish off final record */
}
tapemark(); /* write EOF */
if(verify) printf("[OK]\n");
}
/* list files on tape */
static void listfiles(int argc,char **argv)
{
scantape(argc,argv,listfile);
}
/* list a single file (called back by scantape()) */
static void listfile()
{
static const char spaces[] = " ";
int n;
n=printf("%s;%s %s",ufd,fn1,fn2); /* print ITS filename */
if (verify) {
if(islink) {
insix(lfn1);
insix(lfn2);
insix(lufd);
printf (" %s;%s %s", lufd, lfn1, lfn2);
} else if (cdate.tm_year!=0) {
fputs (spaces + n, stdout);
printf (" %4d-%02d-%02d", cdate.tm_year + 1900, cdate.tm_mon,
cdate.tm_mday);
}
}
putchar('\n');
while(taperead()==0) ; /* skip until tape mark */
}
/* extract files from tape */
static void extfiles(int argc,char **argv)
{
scantape(argc,argv,extfile);
}
/* extract a single file (called back by scantape()) */
static void extfile()
{
char newname[100];
int counter = 0;
static char fname[6+1+6+1+6+1+10]; /* filename = "ufd/fn1.fn2"<NUL> */
static char lname[6+1+6+1+6+1]; /* same, for link name */
struct stat s;
struct utimbuf u;
if(verify) printf("%s;%s %s ",ufd,fn1,fn2); /* print ITS filename */
weenixname(ufd); /* convert to WEENIX equivalent */
weenixname(fn1);
weenixname(fn2);
sprintf(fname,"%s/%s.%s",ufd,fn1,fn2); /* combine (known to fit) */
if(verify) printf("=> %s ",fname); /* print WEENIX filename */
/* create directory if it doesn't exist */
if(stat(ufd,&s)<0&&errno==ENOENT) {
if(mkdir(ufd,0755)<0) {
fflush(stdout);
perror(ufd);
exit(1);
}
}
/* renames if file already exists */
strcpy(newname, fname);
while(lstat(newname,&s)==0) {
sprintf(newname, "%s|%d", fname, counter++);
}
if(strcmp(fname, newname)) {
fprintf(stderr, "WARNING: File %s already exists; ", fname);
fprintf(stderr, "renaming to %s\n", newname);
strcpy(fname, newname);
}
/* create the file (or link) */
if(islink) { /* it's a link */
if((remaining()==0)&&(taperead()<0)) {
fprintf(stderr,"?Unexpected EOF\n");
exit(1);
}
insix(lfn1); /* read it in */
insix(lfn2);
insix(lufd);
weenixname(lufd); /* convert to WEENIX style */
weenixname(lfn1);
weenixname(lfn2);
sprintf(lname,"%s/%s.%s",lufd,lfn1,lfn2); /* combine */
if(symlink(lname,fname)<0) { /* create link */
perror(fname);
exit(1);
}
/* can't apply dates since target may not exist */
taperead(); /* read the EOF mark */
}
else { /* regular file */
pack(fname); /* pack tape file into disk file */
/* apply file dates from tape */
if(cdate.tm_year!=0) { /* creation date (if known) */
u.modtime=mktime(&cdate); /* convert to time_t */
if(rdate.tm_year!=0) /* ref date (if known) */
u.actime=mktime(&rdate);
else u.actime=u.modtime; /* use creation date if not */
if(utime(fname,&u)<0) {
perror("?Error setting file dates");
exit(1);
}
}
}
if(verify) printf("[OK]\n");
}
static void datime(unsigned long l, unsigned long r)
{
int y, m, d;
/* If the timestamp is all zeros or all ones, it's invalid. */
if ((l == 0L && r == 0L) ||
(l == 0777777L && r == 0777777L)) {
cdate.tm_year = 0;
return;
}
y = l>>9L;
m = (l>>5L)&017;
d = l&037;
/* Old tapes just store 1 bit of year. We get the full year */
/* by combining this bit with the date in the the tape header. */
if (y < 2) {
y |= tape_year & ~1;
/* Dates that appear to be later than the tape creation */
/* must be two years older. */
if (y > tape_year ||
(y == tape_year && m > tape_month) ||
(y == tape_year && m == tape_month && d > tape_day))
y -= 2;
}
/* Sanity checking the month and day values. */
if (m == 0)
m = 1;
if (d == 0)
d = 1;
cdate.tm_year=y;
cdate.tm_mon=m-1;
cdate.tm_mday=d;
cdate.tm_hour=r/(60L*60L*2L);
cdate.tm_min=(r/(60L*2L))%60L;
cdate.tm_sec=(r/2L)%60L;
cdate.tm_isdst=(-1);
}
/* scan the tape and process each file found (after setting up globals) */
static void scantape(int argc,char **argv,void (*process)())
{
unsigned long l,r,len;
if(taperead()<0) { /* read volume header */
fprintf(stderr,"?Null tape\n");
exit(1);
}
/* display volume header info */
inword(&l,&r); /* 1: AOBJN ptr giving length */
len=01000000L-l; /* length of record */
if(len>=4) {
inword(&l,&r); /* 2: tape,,real */
if(type)
printf("Tape %ld, reel %ld",l,r);
insix(ufd); /* 3: SIXBIT creation date */
inword(&l,&r); /* 4: type */
/* 0=random, >0=full, <0=incremental */
/* Remember tape creation date for 1-bit year conversion. */
tape_year = 10*(ufd[0]-'0') + ufd[1]-'0';
tape_month = 10*(ufd[2]-'0') + ufd[3]-'0';
tape_day = 10*(ufd[4]-'0') + ufd[5]-'0';
if(type)
printf(", created %c%c/%c%c/%c%c, type=%s\n",
ufd[2],ufd[3], ufd[4],ufd[5], ufd[0],ufd[1],
(l|r)==0?"random":
((l&0400000)?"incremental":"full"));
len-=4; /* eat those words */
}
while(len--) inword(&l,&r); /* eat unknown words */
if(remaining()!=0) /* file header in same rec */
goto fhead;
while(taperead()==0) { /* read file label */
fhead: inword(&l,&r); /* 1: AOBJN ptr giving length */
len=01000000L-l; /* length of record */
if(len<4) { /* must have at least filename */
fprintf(stderr,"?Invalid tape format\n");
exit(1);
}
insix(ufd); /* 2: UFD */
insix(fn1); /* 3: FN1 */
insix(fn2); /* 4: FN2 */
len-=4; /* count those words */
if(len) { /* 5: linkf,,pack */
inword(&islink,&r);
len--;
}
else islink=0; /* (assume file if missing) */
if(len) { /* 6: creation date */
inword(&l,&r);
datime(l,r);
len--;
}
else cdate.tm_year=0;
if(len) { /* 7: reference date */
inword(&l,&r);
rdate.tm_year=(l>>9L);
rdate.tm_mon=((l>>5L)&017)-1;
rdate.tm_mday=l&037;
rdate.tm_hour=r/(60L*60L*2L);
rdate.tm_min=(r/(60L*2L))%60L;
rdate.tm_sec=(r/2L)%60L;
rdate.tm_isdst=(-1);
len--;
}
else rdate.tm_year=0;
while(len--) inword(&l,&r); /* eat unknown words */
(*process)(); /* process the file */
}
}
static void usage(int rc)
{
fprintf(stderr,"\
\n\
ITSTAR V1.10 Copyright (C) By John Wilson <[email protected]>\n\
Access ITS DUMP tapes\n\
\n\
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n\
This is free software: you are free to change and redistribute it.\n\
There is NO WARRANTY, to the extent permitted by law.\n\
\n\
Usage: itstar switches file1 file2 file3 ...\n\
\n\
switches:\n\
-c create tape\n\
-C DIR change directory to DIR after opening tape\n\
-t type out tape contents\n\
-r append files to tape\n\
-x extract files from tape\n\
-f /dev/xxxx specify local tape drive name\n\
-f file use tape image file instead\n\
-f - use STDIN/STDOUT for image file\n\
-f HOST:DEV use \"rmt\" remote tape server\n\
-v verify (display) names of all files accessed\n\
-E use E-11 tape image format\n\
-O write old format tape (6 file header words)\n\
\n");
/* need some way to differentiate rmt protocol from my own weird one,
and to specify port */
exit(rc);
}
/* write a 6-character ASCII string as a word of SIXBIT */
/* it is assumed that the string contains no non-sixbit characters */
void outsix(char *s)
{
unsigned long six[6], *p;
int i;
unsigned long l, r;
for(i=6,p=six;i--;*p++=0L); /* init in case < 6 chars */
for(i=6,p=six;*s&&i--;*p++=((*s++-040)&077)); /* ASCII char -40 */
l=(six[0]<<12L)|(six[1]<<6L)|six[2]; /* pack it all up */
r=(six[3]<<12L)|(six[4]<<6L)|six[5];
outword(l,r);
}
/* read a 36-bit SIXBIT word as 0-6 ASCII characters */
void insix(char *s)
{
char *p;
int i;
unsigned long l, r;
inword(&l,&r); /* read it */
s[0]=((l>>12L)&077)+040; /* unpack all six 6-bit bytes */
s[1]=((l>>6L)&077)+040;
s[2]=(l&077)+040;
s[3]=((r>>12L)&077)+040;
s[4]=((r>>6L)&077)+040;
s[5]=(r&077)+040;
for(i=6,p=s;i--;s++)
if(*s!=' ') p=s+1; /* look for last non-blank, if any */
*p='\0'; /* put a NUL after it */
}
/* extract ITS filename components from a WEENIX filename, as much as we can */
/* (results stored in UFD/FN1/FN2 arrays, 0-6 chars each w/NUL terminator */
static void extitsname(char *f,char *ufd,char *fn1,char *fn2)
{
int len;
char *p, *q, *pfn;
/* extract dir name/filename, convert to UFD/FN1/FN2 */
pfn=rindex(f,'/'); /* filename starts after last "/" */
if(pfn==NULL) { /* if any */
ufd[0]='\0'; /* no UFD */
pfn=f;
}
else { /* extract UFD while we're here */
for(p=pfn;p>f;) /* search for preceding "/" */
if(*--p=='/') {
p++; /* first char of final path element */
break;
}
len=pfn-p; /* length */
if(len>6) len=6; /* stop at 6 */
strncpy(ufd,p,len); /* copy UFD */
ufd[len]='\0'; /* mark end */
pfn++; /* filename starts after final "/" */
}
/* split apart filename and extension */
p=index(pfn,'.'); /* extension starts after first "." */
if(p==NULL) { /* if any */
fn2[0]='\0'; /* no FN2 */
len=strlen(pfn); /* length */
}
else {
if((q=index(p+1,'.'))==NULL) /* stop at next dot */
len=strlen(p+1); /* no dot, get length of ext */
else len=(q-(p+1)); /* ext runs until next dot */
if(len>6) len=6; /* stop at 6 */
strncpy(fn2,p+1,len); /* copy FN2 */
fn2[len]='\0'; /* mark end */
len=p-pfn; /* length of FN1 */
}
if(len>6) len=6; /* stop at 6 */
strncpy(fn1,pfn,len); /* copy FN1 */
fn1[len]='\0'; /* mark end */
itsname(ufd); /* convert to ITS form */
itsname(fn1);
itsname(fn2);
/* apply stupid defaults if any are missing */
if(ufd[0]=='\0') strcpy(ufd,"UFD");
if(fn1[0]=='\0') strcpy(fn1,"FN1");
if(fn2[0]=='\0') strcpy(fn2,"FN2");
}
/* convert a filename element in-place from ITS to WEENIX form */
void weenixname(char *p)
{
register char c;
for(;c=(*p);*p++=c)
switch(c) {
case '.': c='_'; break;
case '/': c='{'; break;
case '_': c='}'; break;
case ' ': c='~'; break;
default: c=tolower(c);
}
}
/* convert a filename element in-place from WEENIX to ITS form */
static void itsname(char *p)
{
register char c;
for(;c=(*p);*p++=c)
switch(c) {
case '_': c='.'; break;
case '{': c='/'; break;
case '}': c='_'; break;
case '~': c=' '; break;
default: c=toupper(c);
}
}
/* routine to punt after a failed malloc() */
void nomem()
{
perror("?Error allocating memory");
exit(1);
}