comparison netdiff/renpart/rename.c @ 138:77acb816727b

netdiff: donl-rename-parts put together
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 07 Sep 2020 04:02:14 +0000
parents
children
comparison
equal deleted inserted replaced
137:6f528e2a9e23 138:77acb816727b
1 #include <ctype.h>
2 #include <string.h>
3 #include <strings.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include "struct.h"
7
8 extern struct part_rename *part_rename_list;
9
10 static char *infname;
11 static FILE *inf;
12 static char linebuf[80];
13 static int lineno;
14
15 static void
16 rename_entry(old, new)
17 char *old, *new;
18 {
19 struct part_rename *rp, **rpp;
20 char *dp;
21
22 for (rpp = &part_rename_list; rp = *rpp; rpp = &rp->next) {
23 if (!strcmp(rp->old, old)) {
24 fprintf(stderr, "%s line %d: old name %s given twice\n",
25 infname, lineno, old);
26 exit(1);
27 }
28 }
29 rp = malloc(sizeof(struct part_rename) + strlen(old) + strlen(new) + 2);
30 dp = (char *)(rp + 1);
31 rp->old = dp;
32 strcpy(dp, old);
33 dp += strlen(old) + 1;
34 rp->new = dp;
35 strcpy(dp, new);
36 rp->next = 0;
37 *rpp = rp;
38 }
39
40 static void
41 process_line()
42 {
43 char *cp, *old, *new;
44
45 for (cp = linebuf; isspace(*cp); cp++)
46 ;
47 if (*cp == '\0' || *cp == '#')
48 return;
49 for (old = cp; *cp && !isspace(*cp); cp++)
50 ;
51 if (*cp)
52 *cp++ = '\0';
53 while (isspace(*cp))
54 cp++;
55 if (*cp == '\0' || *cp == '#') {
56 fprintf(stderr, "%s line %d: too few fields\n",
57 infname, lineno);
58 exit(1);
59 }
60 for (new = cp; *cp && !isspace(*cp); cp++)
61 ;
62 if (*cp)
63 *cp++ = '\0';
64 while (isspace(*cp))
65 cp++;
66 if (*cp != '\0' && *cp != '#') {
67 fprintf(stderr, "%s line %d: too many fields\n",
68 infname, lineno);
69 exit(1);
70 }
71 rename_entry(old, new);
72 }
73
74 process_rename_file(input_filename)
75 char *input_filename;
76 {
77 infname = input_filename;
78 inf = fopen(infname, "r");
79 if (!inf) {
80 perror(infname);
81 exit(1);
82 }
83 while (fgets(linebuf, sizeof linebuf, inf)) {
84 lineno++;
85 process_line();
86 }
87 fclose(inf);
88 return(0);
89 }