-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap.c
767 lines (711 loc) · 21.8 KB
/
map.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
759
760
761
762
763
764
765
766
767
/*
This software was developed by employees of the National Institute
of Standards and Technology (NIST), an agency of the Federal
Government and is being made available as a public service. Pursuant
to title 17 United States Code Section 105, works of NIST employees
are not subject to copyright protection in the United States. This
software may be subject to foreign copyright. Permission in the
United States and in foreign countries, to the extent that NIST may
hold copyright, to use, copy, modify, create derivative works, and
distribute this software and its documentation without fee is hereby
granted on a non-exclusive basis, provided that this notice and
disclaimer of warranty appears in all copies.
THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND,
EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED
TO, ANY WARRANTY THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS,
ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, AND FREEDOM FROM INFRINGEMENT, AND ANY WARRANTY THAT THE
DOCUMENTATION WILL CONFORM TO THE SOFTWARE, OR ANY WARRANTY THAT THE
SOFTWARE WILL BE ERROR FREE. IN NO EVENT SHALL NIST BE LIABLE FOR
ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, INDIRECT, SPECIAL
OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, OR IN ANY
WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY
PERSONS OR PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS
SUSTAINED FROM, OR AROSE OUT OF THE RESULTS OF, OR USE OF, THE
SOFTWARE OR SERVICES PROVIDED HEREUNDER.
*/
/*
*******************************************************************
* *
* Unravel sysgen map *
* *
* Jim Lyle 25 Jan 1994 *
* *
*******************************************************************
*/
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
/*
static char sccsid[] = "@(#)map.c 1.6 8/11/95";
*/
int v_opt = 0;
typedef struct file_struct file_rec, *file_ptr;
typedef struct proc_struct proc_rec, *proc_ptr;
typedef struct main_struct main_rec, *main_ptr;
typedef struct proc_list_struct proc_list_rec, *proc_list;
typedef struct file_list_struct file_list_rec, *file_list;
struct file_list_struct
{
file_list next;
file_ptr file;
};
struct proc_list_struct
{
proc_list next;
proc_ptr proc;
};
struct file_struct
{
int n_procs;
char *name;
proc_list procs; /* procs defined (with body) */
proc_list not_defined; /* called only */
file_ptr next;
};
struct proc_struct
{
int proc_id, entry;
char *name;
char s_or_x; /* static or extern */
file_ptr in_file;
proc_ptr same_name;
proc_ptr next;
};
struct main_struct
{
file_list files;
file_ptr main_file;
proc_list done;
proc_list ambigous_procs;
proc_list lib_procs;
main_ptr next;
};
proc_list
proc_entry(proc_ptr proc)
{
proc_list new;
new = (proc_list)malloc(sizeof(proc_list_rec));
new->next = NULL;
new->proc = proc;
return new;
}
file_list
file_entry(file_ptr file)
{
file_list new;
new = (file_list)malloc(sizeof(file_list_rec));
new->next = NULL;
new->file = file;
return new;
}
file_ptr
find_file(file_ptr list, file_ptr file)
{
file_ptr at;
at = list;
while (at)
{
if (strcmp(at->name, file->name) == 0)
return at;
at = at->next;
}
return NULL;
}
file_list
find_file_list(file_list list, file_ptr file)
{
file_list at;
at = list;
while (at)
{
if (strcmp(at->file->name, file->name) == 0)
return at;
at = at->next;
}
return NULL;
}
proc_ptr
find_proc_list(proc_list list, proc_ptr proc)
{
proc_list at;
at = list;
while (at)
{
if (strcmp(at->proc->name, proc->name) == 0)
return at->proc;
at = at->next;
}
return NULL;
}
proc_ptr
find_proc(proc_ptr list, proc_ptr proc)
{
proc_ptr at;
at = list;
while (at)
{
if (strcmp(at->name, proc->name) == 0)
return at;
at = at->next;
}
return NULL;
}
void
match_procs_to_files(main_ptr mp, file_ptr fp, proc_ptr pp)
{
main_ptr m;
file_ptr f;
file_list add_f;
proc_ptr p, found;
proc_list not_list;
proc_list new_proc_entry, p_list;
file_list files, new_file_entry;
m = mp;
while (m)
{
if (v_opt)
printf("\nmatch main %s\n", m->main_file->name);
m->done = NULL;
m->files = file_entry(m->main_file);
files = m->files;
while (files)
{
f = files->file;
if (v_opt)
printf("File: %s\n", f->name);
not_list = f->not_defined;
p_list = f->procs;
while (p_list)
{
if (v_opt)
printf("\tAdd proc %s\n", p_list->proc->name);
found = find_proc_list(m->done, p_list->proc);
if (found)
{
if (v_opt)
printf("\t\tfound %s in %s\n", found->name, f->name);
found = find_proc(pp, found);
if (f != m->main_file)
if (found->same_name)
{
if (v_opt)
printf("\t\tambig %s in %s\n", found->name, f->name);
if (!find_proc_list(m->ambigous_procs, found))
{
new_proc_entry = proc_entry(/*p*/ found);
new_proc_entry->next = m->ambigous_procs;
m->ambigous_procs = new_proc_entry;
}
}
}
else
{
found = find_proc(pp, p_list->proc);
if ((f != m->main_file) && (found->same_name))
{
if (v_opt)
printf("\t\tambig %s in %s\n", found->name, f->name);
if (!find_proc_list(m->ambigous_procs, found))
{
new_proc_entry = proc_entry(/*p*/ found);
new_proc_entry->next = m->ambigous_procs;
m->ambigous_procs = new_proc_entry;
}
}
else
{
new_proc_entry = proc_entry(p_list->proc);
new_proc_entry->next = m->done;
m->done = new_proc_entry;
}
}
p_list = p_list->next;
}
while (not_list)
{
p = not_list->proc;
found = find_proc_list(m->done, p);
if (!found)
{
new_proc_entry = proc_entry(p);
new_proc_entry->next = m->done;
m->done = new_proc_entry;
found = find_proc(pp, p);
if (found)
{
if (found->same_name)
{
new_proc_entry = proc_entry(/*p*/ found);
new_proc_entry->next = m->ambigous_procs;
m->ambigous_procs = new_proc_entry;
}
else if (!find_file_list(m->files, found->in_file))
{
new_file_entry = file_entry(found->in_file);
add_f = files;
while (add_f->next)
add_f = add_f->next;
add_f->next = new_file_entry;
/*
p_list = found->in_file->procs;
while (p_list){
new_proc_entry =
proc_entry (p_list->proc);
new_proc_entry -> next = m -> done;
m -> done = new_proc_entry;
p_list = p_list -> next;
}
*/
}
}
else
{
new_proc_entry = proc_entry(p);
new_proc_entry->next = m->lib_procs;
m->lib_procs = new_proc_entry;
}
}
not_list = not_list->next;
}
files = files->next;
}
m = m->next;
}
}
void
print_match_results(main_ptr mp)
{
file_list need_files;
proc_list p;
proc_ptr same;
FILE *sys;
int n;
int col;
sys = stdout;
while (mp)
{
col = fprintf(sys, "Main program file %s requires", mp->main_file->name);
need_files = mp->files;
n = 0;
while (need_files)
{
n++;
need_files = need_files->next;
}
col += fprintf(sys, " %d files: ", n);
need_files = mp->files;
while (need_files)
{
if (col > 60)
{
fprintf(sys, "\n");
col = 0;
}
col += fprintf(sys, "%s ", need_files->file->name);
need_files = need_files->next;
}
p = mp->ambigous_procs;
if (p)
fprintf(sys, "\nThe following procedures are defined in more than one file:\n");
else
fprintf(sys, "\n");
while (p)
{
fprintf(sys, "\t%s ", p->proc->name);
same = p->proc->same_name;
col = fprintf(sys, "%s ", p->proc->in_file->name);
while (same)
{
col += fprintf(sys, "%s ", same->in_file->name);
same = same->same_name;
if (same && col + strlen(same->in_file->name) > 60)
{
fprintf(sys, "...");
same = NULL;
}
}
fprintf(sys, "\n");
p = p->next;
}
mp = mp->next;
}
fclose(sys);
}
void
output_match_results(char *dir_name, main_ptr mp)
{
file_list need_files;
proc_list p;
proc_ptr same;
FILE *sys;
int n;
char buff[2000];
int col;
sprintf(buff, "%s/SYSTEM", dir_name);
sys = fopen(buff, "w");
if (!sys)
{
fprintf(stderr, "Could not open 'SYSTEM' file\n");
fprintf(stderr, "check current directory write permissions,\n");
fprintf(stderr, "\t\tls -ld .\n");
fprintf(stderr, "current directory not being writable or ");
fprintf(stderr, "existing SYSTEM file with\n");
fprintf(stderr, "no write permission ");
fprintf(stderr, "is most likely cause of write failure\n");
exit(1);
}
while (mp)
{
fprintf(sys, "MAIN %s", mp->main_file->name);
need_files = mp->files;
n = 0;
while (need_files)
{
n++;
need_files = need_files->next;
}
fprintf(sys, " %d\n", n);
fprintf(sys, "FILES\n");
need_files = mp->files;
while (need_files)
{
fprintf(sys, "\t%s\n", need_files->file->name);
need_files = need_files->next;
}
fprintf(sys, "AMBIG\n");
p = mp->ambigous_procs;
while (p)
{
fprintf(sys, "\t%s ", p->proc->name);
same = p->proc->same_name;
col = fprintf(sys, "%s ", p->proc->in_file->name);
while (same)
{
col += fprintf(sys, "%s ", same->in_file->name);
same = same->same_name;
if (same && col + strlen(same->in_file->name) > 60)
{
fprintf(sys, "...");
same = NULL;
}
}
fprintf(sys, "\n");
p = p->next;
}
fprintf(sys, "LIB\n");
p = mp->lib_procs;
while (p)
{
fprintf(sys, "\t%s\n", p->proc->name);
p = p->next;
}
mp = mp->next;
}
fclose(sys);
}
/*
print_match_results (mp)
main_ptr mp;
{
file_list need_files;
proc_list p;
while (mp){
printf ("\n\nmatching for main in %s\n",mp->main_file->name);
printf ("AMBIG\n");
p = mp->ambigous_procs;
while(p){
printf ("\t%s\n",p->proc->name);
p = p -> next;
}
printf ("LIB\n");
p = mp->lib_procs;
while(p){
printf ("\t%s\n",p->proc->name);
p = p -> next;
}
printf ("FILES\n");
need_files = mp->files;
while(need_files){
printf ("\t%s\n",need_files->file->name);
need_files = need_files -> next;
}
mp = mp -> next;
}
}
*/
int
file_selector(struct dirent *entry)
{
int ix;
ix = strlen(entry->d_name) - 2;
if (ix > 0)
if ((entry->d_name[ix] == '.') && (entry->d_name[ix + 1] == 'T'))
{
return 1;
}
return 0;
}
int
compare(struct dirent **a, struct dirent **b)
{
if (strcmp((*a)->d_name, (*b)->d_name) > 0)
return 1;
return 0;
}
int
myscandir(char *dir_name, struct dirent ***list, int (*select)(), int (*compare)())
{
DIR *dir;
struct dirent *entry;
typedef struct l_struct ll_node, *ll;
struct l_struct
{
ll next;
struct dirent *e;
};
int i, n_match = 0;
ll new_ll;
ll nodes, node_list = NULL;
dir = opendir(dir_name);
if (!dir)
{
fprintf(stderr, "directory open failed for %s\n", dir_name);
exit(1);
}
for (entry = readdir(dir); entry != NULL; entry = readdir(dir))
{
if (select(entry))
{
new_ll = (ll)malloc(sizeof(ll_node));
new_ll->next = node_list;
node_list = new_ll;
new_ll->e = (struct dirent *)malloc(sizeof(struct dirent) + strlen(entry->d_name) + 3);
strcpy(new_ll->e->d_name, entry->d_name);
n_match++;
}
}
*list = (struct dirent **)malloc((n_match + 1) * sizeof(struct dirent *));
nodes = node_list;
for (i = 0; i < n_match; i++)
{
(*list)[i] = nodes->e;
nodes = nodes->next;
}
qsort(*list, n_match, sizeof(struct dirent *), compare);
return n_match;
}
int
scan_dir(char *dir, struct dirent ***f)
{
struct dirent **files;
int count;
int alphasort();
count = myscandir(dir, &files, file_selector, compare);
if (count == -1)
{
fprintf(
stderr, "map: could not open %s\n", strcmp(dir, ".") == 0 ? "current directory" : dir);
exit(1);
}
files[count] = NULL;
*f = files;
return count;
}
int
main(int np, char **p)
{
/* struct direct **files; */
struct dirent **files;
int n_files, i, j, length;
char *dir_name;
FILE *t_file;
char buff[3000];
proc_ptr same;
file_ptr fp = NULL, new_fp;
proc_ptr pp = NULL, new_pp;
main_ptr mp = NULL, new_main;
proc_list new_proc_list;
proc_ptr new_proc;
int n_procs = 0;
int fx;
if (np > 3)
{
fprintf(stderr, "map: too many parameters (%d)\n", np - 1);
fprintf(stderr, "%s usage: %s [-v] [directory_name]\n", "map", "map");
exit(1);
}
files = NULL;
fx = 0;
for (i = 1; i < np; i++)
{
if (p[i][0] == '-')
{
if (strcmp("-v", p[i]) == 0)
v_opt = 1;
else
{
fprintf(stderr, "%s: option (%s) is not valid\n", "map", p[i]);
fprintf(stderr, "%s usage: %s [-v] [directory_name]\n", "map", "map");
exit(1);
}
}
else if (fx == 0)
fx = i;
else
{
fprintf(stderr, "map: only one directory name allowed\n");
fprintf(stderr, "%s usage: %s [-v] [directory_name]\n", "map", "map");
exit(1);
}
}
if (fx != 0)
dir_name = p[fx];
else
dir_name = ".";
n_files = scan_dir(dir_name, &files);
for (i = 0; i < n_files; i++)
{
sprintf(buff, "%s/%s", dir_name, files[i]->d_name);
t_file = fopen(buff, "r");
if (t_file == NULL)
{
fprintf(stderr, "map: could not open file %s\n", files[i]->d_name);
return 1;
}
fscanf(t_file, "%d %*d", &n_procs);
new_fp = (file_ptr)malloc(sizeof(file_rec));
new_fp->next = fp;
new_fp->name = files[i]->d_name;
length = strlen(new_fp->name);
new_fp->name[length - 1] = 'c';
new_fp->n_procs = n_procs;
new_fp->procs = NULL;
fp = new_fp;
if (v_opt)
printf("File %s (%d procs)\n", files[i]->d_name, n_procs);
for (j = 0; j < n_procs; j++)
{
new_pp = (proc_ptr)malloc(sizeof(proc_rec));
new_pp->in_file = new_fp;
new_pp->same_name = NULL;
new_proc_list = (proc_list)malloc(sizeof(proc_list_rec));
new_proc_list->proc = new_pp;
fscanf(t_file,
"%d %d %*d %*d %c %s",
&new_pp->proc_id,
&new_pp->entry,
&new_pp->s_or_x,
buff);
new_pp->name = strcpy(malloc(1 + strlen(buff)), buff);
if (v_opt)
printf("\tproc %s %s ", buff, new_pp->s_or_x == 'S' ? "static" : "extern");
if (strcmp("main", buff) == 0)
{
new_main = (main_ptr)malloc(sizeof(main_rec));
new_main->next = mp;
mp = new_main;
new_main->main_file = new_fp;
new_main->files = NULL;
new_main->ambigous_procs = NULL;
new_main->lib_procs = NULL;
}
if (new_pp->entry == -1)
{ /* not defined */
new_proc_list->next = new_fp->not_defined;
new_fp->not_defined = new_proc_list;
if (v_opt)
printf("called, but not defined");
}
else if (new_pp->s_or_x != 'S')
{
new_proc_list->next = new_fp->procs;
new_fp->procs = new_proc_list;
same = find_proc(pp, new_pp);
if (same)
{
if (v_opt)
printf("overloaded name");
while (same->same_name)
same = same->same_name;
same->same_name = new_pp;
}
else
{
new_pp->next = pp;
pp = new_pp;
}
}
if (v_opt)
printf("\n");
}
fclose(t_file);
}
if (v_opt)
{
new_fp = fp;
printf("\nFiles:\n");
while (new_fp)
{
new_proc_list = new_fp->procs;
printf("File %s (%d procs)\n", new_fp->name, new_fp->n_procs);
if (new_proc_list)
printf("\tDefined procs:\n");
while (new_proc_list)
{
new_proc = new_proc_list->proc;
printf("\t%20.20s %c %c\n",
new_proc->name,
new_proc->entry == -1 ? 'C' : 'D',
new_proc->s_or_x);
new_proc_list = new_proc_list->next;
}
new_proc_list = new_fp->not_defined;
if (new_proc_list)
printf("\tCalled but not defined procs:\n");
while (new_proc_list)
{
new_proc = new_proc_list->proc;
printf("\t%20.20s %c %c\n",
new_proc->name,
new_proc->entry == -1 ? 'C' : 'D',
new_proc->s_or_x);
new_proc_list = new_proc_list->next;
}
new_fp = new_fp->next;
}
new_pp = pp;
printf("\nProcs:\n");
while (new_pp)
{
new_proc = new_pp;
printf("\t%s:", new_proc->name);
printf(" (%s)", new_proc->in_file->name);
if (new_pp->same_name)
{
new_proc = new_pp->same_name;
while (new_proc)
{
printf(" %s", new_proc->in_file->name);
new_proc = new_proc->same_name;
}
}
printf("\n");
new_pp = new_pp->next;
}
new_main = mp;
printf("\nMain procs:\n");
while (new_main)
{
new_main = new_main->next;
}
}
match_procs_to_files(mp, fp, pp);
print_match_results(mp);
output_match_results(dir_name, mp);
return 0;
}