retire nonsymbolic rootdev, dev2name
[minix.git] / commands / join / join.c
blob5f8f59f0e9218e49f5344f4b2c824e8e3ac99adb
1 /* join - relation data base operator Author: Saeko Hirabayashi */
3 /* Written by Saeko Hirabayashi, 1989.
4 * 1992-01-28 Modified by Kouichi Hirabayashi to add some POSIX1003.2 options.
6 * This a free program.
7 */
9 #include <string.h>
10 #include <stdio.h>
12 #define MAXFLD 200 /* maximum # of fields to accept */
14 int main(int argc, char **argv);
15 void error(char *s, char *t);
16 void usage(void);
17 void match(void);
18 void f1_only(void);
19 void f2_only(void);
20 void output(int flag);
21 void outfld(int file);
22 void outputf(int flag);
23 int compare(void);
24 int get1(void);
25 int get2(int back);
26 int getrec(int file);
27 int split(int file);
28 int atoi(char *str);
29 int exit(int val);
30 FILE * efopen(char *file, char *mode);
31 void(*outfun) (int file);
33 #define F1 1
34 #define F2 2
35 #define SEP (sep ? sep : ' ')
37 FILE *fp[2]; /* file pointer for file1 and file2 */
38 long head; /* head of the current (same)key group of the
39 * file2 */
41 char buf[2][BUFSIZ]; /* input buffer for file1 and file2 */
42 char *fld[2][MAXFLD]; /* field vector for file1 and file2 */
43 int nfld[2]; /* # of fields for file1 and file2 */
45 int kpos[2]; /* key field position for file1 and file2
46 * (from 0) */
47 char oldkey[BUFSIZ]; /* previous key of the file1 */
49 struct { /* output list by -o option */
50 int o_file; /* file #: 0 or 1 */
51 int o_field; /* field #: 0, 1, 2, .. */
52 } olist[MAXFLD];
53 int nout; /* # of output filed */
55 int aflag; /* n for '-an': F1 or F2 or both */
56 int vflag; /* n for '-vn': F1 or F2 or both */
57 char *es; /* s for '-e s' */
58 char sep; /* c for -tc: filed separator */
59 char *cmd; /* name of this program */
61 int main(argc, argv)
62 int argc;
63 char **argv;
65 register char *s;
66 int c, i, j;
68 cmd = argv[0];
69 outfun = output; /* default output form */
71 while (--argc > 0 && (*++argv)[0] == '-' && (*argv)[1]) {
72 /* "-" is a file name (stdin) */
73 s = argv[0] + 1;
74 if ((c = *s) == '-' && !s[1]) {
75 ++argv;
76 --argc;
77 break; /* -- */
79 if (*++s == '\0') {
80 s = *++argv;
81 --argc;
83 switch (c) {
84 case 'a': /* add unpairable line to output */
85 vflag = 0;
86 switch (*s) {
87 case '1': aflag |= F1; break;
88 case '2': aflag |= F2; break;
89 default: aflag |= (F1 | F2); break;
91 break;
93 case 'e': /* replace empty field by es */
94 es = s;
95 break;
97 case 'j': /* key field (obsolute) */
98 c = *s++;
99 if (*s == '\0') {
100 s = *++argv;
101 --argc;
104 case '1': /* key field of file1 */
105 case '2': /* key field of file2 */
106 i = atoi(s) - 1;
108 switch (c) {
109 case '1': kpos[0] = i; break;
110 case '2': kpos[1] = i; break;
111 default: kpos[0] = kpos[1] = i;
112 break;
114 break;
116 case 'o': /* specify output format */
117 do {
118 i = j = 0;
119 sscanf(s, "%d.%d", &i, &j);
120 if (i < 1 || j < 1 || i > 2) usage();
121 olist[nout].o_file = i - 1;
122 olist[nout].o_field = j - 1;
123 nout++;
124 if ((s = strchr(s, ',')) != (char *) 0)
125 s++;
126 else {
127 s = *++argv;
128 --argc;
130 } while (argc > 2 && *s != '-');
131 ++argc;
132 --argv; /* compensation */
133 outfun = outputf;
134 break;
136 case 't': /* tab char */
137 sep = *s;
138 break;
140 case 'v': /* output unpairable line only */
141 aflag = 0;
142 switch (*s) {
143 case '1': vflag |= F1; break;
144 case '2': vflag |= F2; break;
145 default: vflag |= (F1 | F2); break;
147 break;
149 default: usage();
152 if (argc != 2) usage();
154 fp[0] = strcmp(argv[0], "-") ? efopen(argv[0], "r") : stdin;
155 fp[1] = efopen(argv[1], "r");
157 nfld[0] = get1(); /* read file1 */
158 nfld[1] = get2(0); /* read file2 */
160 while (nfld[0] || nfld[1]) {
161 if ((i = compare()) == 0)
162 match();
163 else if (i < 0)
164 f1_only();
165 else
166 f2_only();
168 fflush(stdout);
170 exit(0);
173 void usage()
175 fprintf(stderr,
176 "Usage: %s [-an|-vn] [-e str] [-o list] [-tc] [-1 f] [-2 f] file1 file2\n",
177 cmd);
178 exit(1);
181 int compare()
182 { /* compare key field */
183 register int r;
185 if (nfld[1] == 0) /* file2 EOF */
186 r = -1;
187 else if (nfld[0] == 0) /* file1 EOF */
188 r = 1;
189 else {
190 if (nfld[0] <= kpos[0])
191 error("missing key field in file1", (char *) 0);
192 if (nfld[1] <= kpos[1])
193 error("missing key field in file2", (char *) 0);
195 r = strcmp(fld[0][kpos[0]], fld[1][kpos[1]]);
197 return r;
200 void match()
202 long p;
204 if (!vflag) (*outfun) (F1 | F2);
206 p = ftell(fp[1]);
207 nfld[1] = get2(0); /* check key order */
208 if (nfld[1] == 0 || strcmp(fld[0][kpos[0]], fld[1][kpos[1]])) {
209 nfld[0] = get1();
210 if (strcmp(fld[0][kpos[0]], oldkey) == 0) {
211 fseek(fp[1], head, 0); /* re-do from head */
212 nfld[1] = get2(1); /* don't check key order */
213 } else
214 head = p; /* mark here */
218 void f1_only()
220 if ((aflag & F1) || (vflag & F1)) (*outfun) (F1);
221 nfld[0] = get1();
224 void f2_only()
226 if ((aflag & F2) || (vflag & F2)) (*outfun) (F2);
227 head = ftell(fp[1]); /* mark */
228 nfld[1] = get2(0); /* check key order */
231 void output(f)
232 { /* default output form */
233 if (f & F1)
234 fputs(fld[0][kpos[0]], stdout);
235 else
236 fputs(fld[1][kpos[1]], stdout);
237 if (f & F1) outfld(0);
238 if (f & F2) outfld(1);
239 fputc('\n', stdout);
242 void outfld(file)
243 { /* output all fields except key_field */
244 register int i;
245 int k, n;
247 k = kpos[file];
248 n = nfld[file];
249 for (i = 0; i < n; i++)
250 if (i != k) {
251 fputc(SEP, stdout);
252 fputs(fld[file][i], stdout);
256 void outputf(f)
257 { /* output by '-o list' */
258 int i, j, k;
259 register char *s;
261 for (i = k = 0; i < nout; i++) {
262 j = olist[i].o_file;
263 if ((f & (j + 1)) && (olist[i].o_field < nfld[j]))
264 s = fld[j][olist[i].o_field];
265 else
266 s = es;
267 if (s) {
268 if (k++) fputc(SEP, stdout);
269 fputs(s, stdout);
272 fputc('\n', stdout);
275 int get1()
276 { /* read file1 */
277 int r;
278 static char oldkey1[BUFSIZ];
280 if (fld[0][kpos[0]]) {
281 strcpy(oldkey, fld[0][kpos[0]]); /* save previous key for control */
283 r = getrec(0);
285 if (r) {
286 if (strcmp(oldkey1, fld[0][kpos[0]]) > 0)
287 error("file1 is not sorted", (char *) 0);
288 strcpy(oldkey1, fld[0][kpos[0]]); /* save prev key for sort check */
290 return r;
293 int get2(back)
294 { /* read file2 */
295 static char oldkey2[BUFSIZ];
296 int r;
298 r = getrec(1);
300 if (r) {
301 if (!back && strcmp(oldkey2, fld[1][kpos[1]]) > 0)
302 error("file2 is not sorted", (char *) 0);
303 strcpy(oldkey2, fld[1][kpos[1]]); /* save prev key for sort check */
305 return r;
308 int getrec(file)
309 { /* read one line to split it */
310 if (fgets(buf[file], BUFSIZ, fp[file]) == (char *) 0)
311 *buf[file] = '\0';
312 else if (*buf[file] == '\n' || *buf[file] == '\r')
313 error("null line in file%s", file ? "1" : "0");
315 return split(file);
318 int split(file)
319 { /* setup fields */
320 register int n;
321 register char *s, *t;
323 for (n = 0, s = buf[file]; *s && *s != '\n' && *s != '\r';) {
324 if (sep) {
325 for (t = s; *s && *s != sep && *s != '\n' && *s != '\r'; s++);
326 } else {
327 while (*s == ' ' || *s == '\t')
328 s++; /* skip leading white space */
329 for (t = s; *s && *s != ' ' && *s != '\t'
330 && *s != '\n' && *s != '\r'; s++);
331 /* We will treat trailing white space as NULL field */
333 if (*s) *s++ = '\0';
334 fld[file][n++] = t;
335 if (n == MAXFLD) error("too many filed in file%s", file ? "1" : "0");
337 fld[file][n] = (char *) 0;
339 return n;
342 FILE *efopen(file, mode)
343 char *file, *mode;
345 FILE *fp;
347 if ((fp = fopen(file, mode)) == (FILE *) 0) error("can't open %s", file);
349 return fp;
352 void error(s, t)
353 char *s, *t;
355 fprintf(stderr, "%s: ", cmd);
356 fprintf(stderr, s, t);
357 fprintf(stderr, "\n");
359 exit(1);