-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlar.c
More file actions
647 lines (558 loc) · 14.8 KB
/
Copy pathlar.c
File metadata and controls
647 lines (558 loc) · 14.8 KB
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
/*
* Lar - LU format library file maintainer
* by Stephen C. Hemminger
* linus!sch or sch@Mitre-Bedford
*
* Usage: lar key library [files] ...
*
* Key functions are:
* u - Update, add files to library
* t - Table of contents
* e - Extract files from library
* p - Print files in library
* d - Delete files in library
* r - Reorginize library
* Other keys:
* v - Verbose
*
* This program is public domain software, no warranty intended or
* implied.
*
* DESCRPTION
* Lar is a Unix program to manipulate CP/M LU format libraries.
* The original CP/M library program LU is the product
* of Gary P. Novosielski. The primary use of lar is to combine several
* files together for upload/download to a personal computer.
*
* PORTABILITY
* The code is modeled after the Software tools archive program,
* and is setup for Version 7 Unix. It does not make any assumptions
* about byte ordering, explict and's and shift's are used.
* If you have a dumber C compiler, you may have to recode new features
* like structure assignment, typedef's and enumerated types.
*
* BUGS/MISFEATURES
* The biggest problem is text files, the programs tries to detect
* text files vs. binaries by checking for non-Ascii (8th bit set) chars.
* If the file is text then it will throw away Control-Z chars which
* CP/M puts on the end. All files in library are padded with Control-Z
* at the end to the CP/M sector size if necessary.
*
* No effort is made to handle the difference between CP/M and Unix
* end of line chars. CP/M uses Cr/Lf and Unix just uses Lf.
* The solution is just to use the Unix command sed when necessary.
*
* * Unix is a trademark of Bell Labs.
* ** CP/M is a trademark of Digital Research.
*
* -------------------------------------------------------------------------
* Modified by Simon Kissane, November 2022.
* Modernised syntax to ISO/ANSI C.
* I place my changes in the public domain (same terms as the original).
*/
#include <stdnoreturn.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#define ACTIVE 00
#define UNUSED 0xff
#define DELETED 0xfe
#define CTRLZ 0x1a
#define MAXFILES 256
#define SECTOR 128
#define DSIZE ( sizeof(struct ludir) )
#define SLOTS_SEC (SECTOR/DSIZE)
#define equal(s1, s2) ( strcmp(s1,s2) == 0 )
/* if you don't have void type just define as blank */
#define VOID (void)
/* if no enum's then define false as 0 and true as 1 and bool as int */
typedef enum {false=0, true=1} bool;
/* Globals */
char *fname[MAXFILES];
bool ftouched[MAXFILES];
typedef struct {
unsigned char lobyte;
unsigned char hibyte;
} word;
/* convert word to int */
#define wtoi(w) ( (w.hibyte<<8) + w.lobyte)
#define itow(dst,src) dst.hibyte = (src & 0xff00) >> 8;\
dst.lobyte = src & 0xff;
struct ludir { /* Internal library ldir structure */
unsigned char l_stat; /* status of file */
char l_name[8]; /* name */
char l_ext[3]; /* extension */
word l_off; /* offset in library */
word l_len; /* lengty of file */
char l_fill[16]; /* pad to 32 bytes */
} ldir[MAXFILES];
int errcnt, nfiles, nslots;
bool verbose = false;
char *cmdname;
char *getname(char *nm, char *ex); //, *sprintf();
void update(char *name), reorg(char *name), table(char *lib), extract(char *name), print(char *name), delete(char *lname);
void help(void);
void filenames (int ac, char **av);
noreturn void conflict(void);
void getdir (FILE *f);
int filarg (char *name);
void not_found (void);
void getfiles (char *name, bool pflag);
void acopy (FILE *fdi, FILE *fdo, register unsigned int nsecs);
void addfil (char *name, FILE *lfd);
int fcopy (FILE *ifd, FILE *ofd);
void copymem(register char *dst, register char *src, register unsigned int n);
void copyentry(struct ludir *old, FILE *of, struct ludir *new, FILE *nf);
int main (int argc, char **argv)
{
register char *flagp;
char *aname; /* name of library file */
void (*function)(char*) = NULL; /* function to do on library */
/* set the function to be performed, but detect conflicts */
#define setfunc(val) if(function != NULL) conflict(); else function = val
cmdname = argv[0];
if (argc < 3)
help ();
aname = argv[2];
filenames (argc, argv);
for(flagp = argv[1]; *flagp; flagp++)
switch (*flagp) {
case 'u':
setfunc(update);
break;
case 't':
setfunc(table);
break;
case 'e':
setfunc(extract);
break;
case 'p':
setfunc(print);
break;
case 'd':
setfunc(delete);
break;
case 'r':
setfunc(reorg);
break;
case 'v':
verbose = true;
break;
default:
help ();
}
if(function == NULL) {
fprintf(stderr,"No function key letter specified\n");
help();
}
(*function)(aname);
return 0;
}
/* print error message and exit */
noreturn void help (void) {
fprintf (stderr, "Usage: %s {utepdr}[v] library [files] ...\n", cmdname);
fprintf (stderr, "Functions are:\n\tu - Update, add files to library\n");
fprintf (stderr, "\tt - Table of contents\n");
fprintf (stderr, "\te - Extract files from library\n");
fprintf (stderr, "\tp - Print files in library\n");
fprintf (stderr, "\td - Delete files in library\n");
fprintf (stderr, "\tr - Reorginize library\n");
fprintf (stderr, "Flags are:\n\tv - Verbose\n");
exit (1);
}
noreturn void conflict(void) {
fprintf(stderr,"Conficting keys\n");
help();
}
noreturn void error (char *str)
{
fprintf (stderr, "%s: %s\n", cmdname, str);
exit (1);
}
noreturn void cant (char *name)
{
fprintf (stderr, "%s: %s\n", name, sys_errlist[errno]);
exit (1);
}
/* Get file names, check for dups, and initialize */
void filenames (int ac, char **av)
{
register int i, j;
errcnt = 0;
for (i = 0; i < ac - 3; i++) {
fname[i] = av[i + 3];
ftouched[i] = false;
if (i == MAXFILES)
error ("Too many file names.");
}
fname[i] = NULL;
nfiles = i;
for (i = 0; i < nfiles; i++)
for (j = i + 1; j < nfiles; j++)
if (equal (fname[i], fname[j])) {
fprintf (stderr, "%s", fname[i]);
error (": duplicate file name");
}
}
void table (char *lib)
{
FILE *lfd;
register int i, total;
int active = 0, unused = 0, deleted = 0;
char *uname;
if ((lfd = fopen (lib, "r")) == NULL)
cant (lib);
getdir (lfd);
total = wtoi(ldir[0].l_len);
if(verbose) {
printf("Name Index Length\n");
printf("Directory %4d\n", total);
}
for (i = 1; i < nslots; i++)
switch(ldir[i].l_stat) {
case ACTIVE:
active++;
uname = getname(ldir[i].l_name, ldir[i].l_ext);
if (filarg (uname)) {
if(verbose)
printf ("%-12s %4d %4d\n", uname,
wtoi (ldir[i].l_off), wtoi (ldir[i].l_len));
else
printf ("%s\n", uname);
}
total += wtoi(ldir[i].l_len);
break;
case UNUSED:
unused++;
break;
default:
deleted++;
}
if(verbose) {
printf("--------------------------\n");
printf("Total sectors %4d\n", total);
printf("\nLibrary %s has %d slots, %d deleted %d active, %d unused\n",
lib, nslots, deleted, active, unused);
}
VOID fclose (lfd);
not_found ();
}
void getdir (FILE *f)
{
rewind(f);
if (fread ((char *) & ldir[0], DSIZE, 1, f) != 1)
error ("No directory\n");
nslots = wtoi (ldir[0].l_len) * SLOTS_SEC;
if (fread ((char *) & ldir[1], DSIZE, nslots, f) != nslots)
error ("Can't read directory - is it a library?");
}
void putdir (FILE *f)
{
rewind(f);
if (fwrite ((char *) ldir, DSIZE, nslots, f) != nslots)
error ("Can't write directory - library may be botched");
}
void initdir (FILE *f)
{
register int i;
int numsecs;
char line[80];
static struct ludir blankentry = {
UNUSED,
{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ' },
};
for (;;) {
printf ("Number of slots to allocate: ");
if (fgets (line, 80, stdin) == NULL)
error ("Eof when reading input");
nslots = atoi (line);
if (nslots < 1)
printf ("Must have at least one!\n");
else if (nslots > MAXFILES)
printf ("Too many slots\n");
else
break;
}
numsecs = nslots / SLOTS_SEC;
nslots = numsecs * SLOTS_SEC;
for (i = 0; i < nslots; i++)
ldir[i] = blankentry;
ldir[0].l_stat = ACTIVE;
itow (ldir[0].l_len, numsecs);
putdir (f);
}
/* convert nm.ex to a Unix style string */
char *getname (char *nm, char *ex)
{
static char namebuf[14];
register char *cp, *dp;
for (cp = namebuf, dp = nm; *dp != ' ' && dp != &nm[8];)
*cp++ = isupper (*dp) ? tolower (*dp++) : *dp++;
*cp++ = '.';
for (dp = ex; *dp != ' ' && dp != &ex[3];)
*cp++ = isupper (*dp) ? tolower (*dp++) : *dp++;
*cp = '\0';
return namebuf;
}
void putname (char *cpmname, char *unixname)
{
register char *p1, *p2;
for (p1 = unixname, p2 = cpmname; *p1; p1++, p2++) {
while (*p1 == '.') {
p2 = cpmname + 8;
p1++;
}
if (p2 - cpmname < 11)
*p2 = islower(*p1) ? toupper(*p1) : *p1;
else {
fprintf (stderr, "%s: name truncated\n", unixname);
break;
}
}
while (p2 - cpmname < 11)
*p2++ = ' ';
}
/* filarg - check if name matches argument list */
int filarg (char *name)
{
register int i;
if (nfiles <= 0)
return 1;
for (i = 0; i < nfiles; i++)
if (equal (name, fname[i])) {
ftouched[i] = true;
return 1;
}
return 0;
}
void not_found (void) {
register int i;
for (i = 0; i < nfiles; i++)
if (!ftouched[i]) {
fprintf (stderr, "%s: not in library.\n", fname[i]);
errcnt++;
}
}
void extract(char *name)
{
getfiles(name, false);
}
void print(char *name)
{
getfiles(name, true);
}
void getfiles (char *name, bool pflag)
{
FILE *lfd, *ofd;
register int i;
char *unixname;
if ((lfd = fopen (name, "r")) == NULL)
cant (name);
ofd = pflag ? stdout : NULL;
getdir (lfd);
for (i = 1; i < nslots; i++) {
if(ldir[i].l_stat != ACTIVE)
continue;
unixname = getname (ldir[i].l_name, ldir[i].l_ext);
if (!filarg (unixname))
continue;
fprintf(stderr,"%s", unixname);
if (ofd != stdout)
ofd = fopen (unixname, "w");
if (ofd == NULL) {
fprintf (stderr, " - can't create");
errcnt++;
}
else {
VOID fseek (lfd, (long) wtoi (ldir[i].l_off) * SECTOR, 0);
acopy (lfd, ofd, wtoi (ldir[i].l_len));
if (ofd != stdout)
VOID fclose (ofd);
}
putc('\n', stderr);
}
VOID fclose (lfd);
not_found ();
}
void acopy (FILE *fdi, FILE *fdo, register unsigned int nsecs)
{
register int i, c;
int textfile = 1;
while( nsecs-- != 0)
for(i=0; i<SECTOR; i++) {
c = getc(fdi);
if( feof(fdi) )
error("Premature EOF\n");
if( ferror(fdi) )
error ("Can't read");
if( !isascii(c) )
textfile = 0;
if( nsecs != 0 || !textfile || c != CTRLZ) {
putc(c, fdo);
if ( ferror(fdo) )
error ("write error");
}
}
}
void update (char *name)
{
FILE *lfd;
register int i;
if ((lfd = fopen (name, "r+")) == NULL) {
if ((lfd = fopen (name, "w+")) == NULL)
cant (name);
initdir (lfd);
}
else
getdir (lfd); /* read old directory */
if(verbose)
fprintf (stderr,"Updating files:\n");
for (i = 0; i < nfiles; i++)
addfil (fname[i], lfd);
if (errcnt == 0)
putdir (lfd);
else
fprintf (stderr, "fatal errors - library not changed\n");
VOID fclose (lfd);
}
void addfil (char *name, FILE *lfd)
{
FILE *ifd;
register int secoffs, numsecs;
register int i;
if ((ifd = fopen (name, "r")) == NULL) {
fprintf (stderr, "%s: can't find to add\n",name);
errcnt++;
return;
}
if(verbose)
fprintf(stderr, "%s\n", name);
for (i = 0; i < nslots; i++) {
if (equal( getname (ldir[i].l_name, ldir[i].l_ext), name) ) /* update */
break;
if (ldir[i].l_stat != ACTIVE)
break;
}
if (i >= nslots) {
fprintf (stderr, "%s: can't add library is full\n",name);
errcnt++;
return;
}
ldir[i].l_stat = ACTIVE;
putname (ldir[i].l_name, name);
VOID fseek(lfd, 0L, 2); /* append to end */
secoffs = ftell(lfd) / SECTOR;
itow (ldir[i].l_off, secoffs);
numsecs = fcopy (ifd, lfd);
itow (ldir[i].l_len, numsecs);
VOID fclose (ifd);
}
int fcopy (FILE *ifd, FILE *ofd)
{
register int total = 0;
register int i, n;
char sectorbuf[SECTOR];
while ( (n = fread( sectorbuf, 1, SECTOR, ifd)) != 0) {
if (n != SECTOR)
for (i = n; i < SECTOR; i++)
sectorbuf[i] = CTRLZ;
if (fwrite( sectorbuf, 1, SECTOR, ofd ) != SECTOR)
error("write error");
++total;
}
return total;
}
void delete (char *lname)
{
FILE *f;
register int i;
if ((f = fopen (lname, "r+")) == NULL)
cant (lname);
if (nfiles <= 0)
error("delete by name only");
getdir (f);
for (i = 0; i < nslots; i++) {
if (!filarg ( getname (ldir[i].l_name, ldir[i].l_ext)))
continue;
ldir[i].l_stat = DELETED;
}
not_found();
if (errcnt > 0)
fprintf (stderr, "errors - library not updated\n");
else
putdir (f);
VOID fclose (f);
}
void reorg (char *name)
{
FILE *olib, *nlib;
int oldsize;
register int i, j;
struct ludir odir[MAXFILES];
char tmpname[SECTOR];
VOID sprintf(tmpname,"%-10.10s.TMP", name);
if( (olib = fopen(name,"r")) == NULL)
cant(name);
if( (nlib = fopen(tmpname, "w")) == NULL)
cant(tmpname);
getdir(olib);
printf("Old library has %d slots\n", oldsize = nslots);
for(i = 0; i < nslots ; i++)
copymem( (char *) &odir[i], (char *) &ldir[i],
sizeof(struct ludir));
initdir(nlib);
errcnt = 0;
for (i = j = 1; i < oldsize; i++)
if( odir[i].l_stat == ACTIVE ) {
if(verbose)
fprintf(stderr, "Copying: %-8.8s.%3.3s\n",
odir[i].l_name, odir[i].l_ext);
copyentry( &odir[i], olib, &ldir[j], nlib);
if (++j >= nslots) {
errcnt++;
fprintf(stderr, "Not enough room in new library\n");
break;
}
}
VOID fclose(olib);
putdir(nlib);
VOID fclose (nlib);
if(errcnt == 0) {
if ( unlink(name) < 0 || link(tmpname, name) < 0) {
VOID unlink(tmpname);
cant(name);
}
}
else
fprintf(stderr,"Errors, library not updated\n");
VOID unlink(tmpname);
}
void copyentry(struct ludir *old, FILE *of, struct ludir *new, FILE *nf)
{
register int secoffs, numsecs;
char buf[SECTOR];
new->l_stat = ACTIVE;
copymem(new->l_name, old->l_name, 8);
copymem(new->l_ext, old->l_ext, 3);
VOID fseek(of, (long) wtoi(old->l_off)*SECTOR, 0);
VOID fseek(nf, 0L, 2);
secoffs = ftell(nf) / SECTOR;
itow (new->l_off, secoffs);
numsecs = wtoi(old->l_len);
itow (new->l_len, numsecs);
while(numsecs-- != 0) {
if( fread( buf, 1, SECTOR, of) != SECTOR)
error("read error");
if( fwrite( buf, 1, SECTOR, nf) != SECTOR)
error("write error");
}
}
void copymem(register char *dst, register char *src, register unsigned int n)
{
while(n-- != 0)
*dst++ = *src++;
}