Bug Summary

File:src/usr.bin/join/join.c
Warning:line 321, column 7
Access to field 'line' results in a dereference of a null pointer (loaded from variable 'lp')

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.4 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name join.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/usr.bin/join/obj -resource-dir /usr/local/llvm16/lib/clang/16 -internal-isystem /usr/local/llvm16/lib/clang/16/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/usr.bin/join/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fcf-protection=branch -fno-jump-tables -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/scan/2024-01-11-140451-98009-1 -x c /usr/src/usr.bin/join/join.c
1/* $OpenBSD: join.c,v 1.34 2022/12/04 23:50:48 cheloha Exp $ */
2
3/*-
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Steve Hayman of the Computer Science Department, Indiana University,
9 * Michiro Hikida and David Goodenough.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <err.h>
37#include <errno(*__errno()).h>
38#include <limits.h>
39#include <locale.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <unistd.h>
44#include <wchar.h>
45
46/*
47 * There's a structure per input file which encapsulates the state of the
48 * file. We repeatedly read lines from each file until we've read in all
49 * the consecutive lines from the file with a common join field. Then we
50 * compare the set of lines with an equivalent set from the other file.
51 */
52typedef struct {
53 char *line; /* line */
54 size_t linealloc; /* bytes allocated for line */
55 char **fields; /* line field(s) */
56 u_long fieldcnt; /* line field(s) count */
57 u_long fieldalloc; /* line field(s) allocated count */
58} LINE;
59
60typedef struct {
61 FILE *fp; /* file descriptor */
62 u_long joinf; /* join field (-1, -2, -j) */
63 int unpair; /* output unpairable lines (-a) */
64 u_long number; /* 1 for file 1, 2 for file 2 */
65 LINE *set; /* set of lines with same field */
66 int pushbool; /* if pushback is set */
67 u_long pushback; /* line on the stack */
68 u_long setcnt; /* set count */
69 u_long setalloc; /* set allocated count */
70} INPUT;
71INPUT input1 = { NULL((void *)0), 0, 0, 1, NULL((void *)0), 0, 0, 0, 0 },
72 input2 = { NULL((void *)0), 0, 0, 2, NULL((void *)0), 0, 0, 0, 0 };
73
74typedef struct {
75 u_long filenum; /* file number */
76 u_long fieldno; /* field number */
77} OLIST;
78OLIST *olist; /* output field list */
79u_long olistcnt; /* output field list count */
80u_long olistalloc; /* output field allocated count */
81
82int joinout = 1; /* show lines with matched join fields (-v) */
83int needsep; /* need separator character */
84int spans = 1; /* span multiple delimiters (-t) */
85char *empty; /* empty field replacement string (-e) */
86wchar_t tabchar[] = L" \t"; /* delimiter characters (-t) */
87
88int cmp(LINE *, u_long, LINE *, u_long);
89void fieldarg(char *);
90void joinlines(INPUT *, INPUT *);
91char *mbssep(char **, const wchar_t *);
92void obsolete(char **);
93void outfield(LINE *, u_long, int);
94void outoneline(INPUT *, LINE *);
95void outtwoline(INPUT *, LINE *, INPUT *, LINE *);
96void slurp(INPUT *);
97void usage(void);
98
99int
100main(int argc, char *argv[])
101{
102 INPUT *F1, *F2;
103 int aflag, ch, cval, vflag;
104 char *end;
105
106 setlocale(LC_CTYPE2, "");
107
108 if (pledge("stdio rpath", NULL((void *)0)) == -1)
1
Assuming the condition is false
2
Taking false branch
109 err(1, "pledge");
110
111 F1 = &input1;
112 F2 = &input2;
113
114 aflag = vflag = 0;
115 obsolete(argv);
116 while ((ch = getopt(argc, argv, "\01a:e:j:1:2:o:t:v:")) != -1) {
3
Assuming the condition is false
4
Loop condition is false. Execution continues on line 194
117 switch (ch) {
118 case '\01': /* See comment in obsolete(). */
119 aflag = 1;
120 F1->unpair = F2->unpair = 1;
121 break;
122 case '1':
123 if ((F1->joinf = strtol(optarg, &end, 10)) < 1)
124 errx(1, "-1 option field number less than 1");
125 if (*end)
126 errx(1, "illegal field number -- %s", optarg);
127 --F1->joinf;
128 break;
129 case '2':
130 if ((F2->joinf = strtol(optarg, &end, 10)) < 1)
131 errx(1, "-2 option field number less than 1");
132 if (*end)
133 errx(1, "illegal field number -- %s", optarg);
134 --F2->joinf;
135 break;
136 case 'a':
137 aflag = 1;
138 switch(strtol(optarg, &end, 10)) {
139 case 1:
140 F1->unpair = 1;
141 break;
142 case 2:
143 F2->unpair = 1;
144 break;
145 default:
146 errx(1, "-a option file number not 1 or 2");
147 break;
148 }
149 if (*end)
150 errx(1, "illegal file number -- %s", optarg);
151 break;
152 case 'e':
153 empty = optarg;
154 break;
155 case 'j':
156 if ((F1->joinf = F2->joinf = strtol(optarg, &end, 10)) < 1)
157 errx(1, "-j option field number less than 1");
158 if (*end)
159 errx(1, "illegal field number -- %s", optarg);
160 --F1->joinf;
161 --F2->joinf;
162 break;
163 case 'o':
164 fieldarg(optarg);
165 break;
166 case 't':
167 spans = 0;
168 if (mbtowc(tabchar, optarg, MB_CUR_MAX__mb_cur_max()) !=
169 strlen(optarg))
170 errx(1, "illegal tab character specification");
171 tabchar[1] = L'\0';
172 break;
173 case 'v':
174 vflag = 1;
175 joinout = 0;
176 switch (strtol(optarg, &end, 10)) {
177 case 1:
178 F1->unpair = 1;
179 break;
180 case 2:
181 F2->unpair = 1;
182 break;
183 default:
184 errx(1, "-v option file number not 1 or 2");
185 break;
186 }
187 if (*end)
188 errx(1, "illegal file number -- %s", optarg);
189 break;
190 default:
191 usage();
192 }
193 }
194 argc -= optind;
195 argv += optind;
196
197 if (aflag
4.1
'aflag' is 0
&& vflag)
198 errx(1, "the -a and -v options are mutually exclusive");
199
200 if (argc != 2)
5
Assuming 'argc' is equal to 2
6
Taking false branch
201 usage();
202
203 /* Open the files; "-" means stdin. */
204 if (!strcmp(*argv, "-"))
7
Assuming the condition is false
8
Taking false branch
205 F1->fp = stdin(&__sF[0]);
206 else if ((F1->fp = fopen(*argv, "r")) == NULL((void *)0))
9
Assuming the condition is false
10
Taking false branch
207 err(1, "%s", *argv);
208 ++argv;
209 if (!strcmp(*argv, "-"))
11
Taking true branch
210 F2->fp = stdin(&__sF[0]);
211 else if ((F2->fp = fopen(*argv, "r")) == NULL((void *)0))
212 err(1, "%s", *argv);
213 if (F1->fp == stdin(&__sF[0]) && F2->fp == stdin(&__sF[0]))
12
Assuming the condition is false
214 errx(1, "only one input file may be stdin");
215
216 if (pledge("stdio", NULL((void *)0)) == -1)
13
Assuming the condition is false
14
Taking false branch
217 err(1, "pledge");
218
219 slurp(F1);
15
Calling 'slurp'
220 slurp(F2);
221
222 /*
223 * We try to let the files have the same field value, advancing
224 * whoever falls behind and always advancing the file(s) we output
225 * from.
226 */
227 while (F1->setcnt && F2->setcnt) {
228 cval = cmp(F1->set, F1->joinf, F2->set, F2->joinf);
229 if (cval == 0) {
230 /* Oh joy, oh rapture, oh beauty divine! */
231 if (joinout)
232 joinlines(F1, F2);
233 slurp(F1);
234 slurp(F2);
235 } else if (cval < 0) {
236 /* File 1 takes the lead... */
237 if (F1->unpair)
238 joinlines(F1, NULL((void *)0));
239 slurp(F1);
240 } else {
241 /* File 2 takes the lead... */
242 if (F2->unpair)
243 joinlines(F2, NULL((void *)0));
244 slurp(F2);
245 }
246 }
247
248 /*
249 * Now that one of the files is used up, optionally output any
250 * remaining lines from the other file.
251 */
252 if (F1->unpair)
253 while (F1->setcnt) {
254 joinlines(F1, NULL((void *)0));
255 slurp(F1);
256 }
257 if (F2->unpair)
258 while (F2->setcnt) {
259 joinlines(F2, NULL((void *)0));
260 slurp(F2);
261 }
262
263 return 0;
264}
265
266void
267slurp(INPUT *F)
268{
269 LINE *lp, *lastlp, tmp;
270 ssize_t len;
271 u_long cnt;
272 char *bp, *fieldp;
273
274 /*
275 * Read all of the lines from an input file that have the same
276 * join field.
277 */
278
279 F->setcnt = 0;
280 for (lastlp = NULL((void *)0); ; ++F->setcnt) {
16
Loop condition is true. Entering loop body
29
Loop condition is true. Entering loop body
281 /*
282 * If we're out of space to hold line structures, allocate
283 * more. Initialize the structure so that we know that this
284 * is new space.
285 */
286 if (F->setcnt
16.1
Field 'setcnt' is equal to field 'setalloc'
29.1
Field 'setcnt' is not equal to field 'setalloc'
== F->setalloc) {
17
Taking true branch
30
Taking false branch
287 LINE *p;
288 u_long newsize = F->setalloc + 50;
289 cnt = F->setalloc;
290 if ((p = reallocarray(F->set, newsize, sizeof(LINE)))
18
Assuming the condition is false
19
Taking false branch
291 == NULL((void *)0))
292 err(1, NULL((void *)0));
293 F->set = p;
294 F->setalloc = newsize;
295 memset(F->set + cnt, 0, 50 * sizeof(LINE));
296 /* re-set lastlp in case it moved */
297 if (lastlp
19.1
'lastlp' is equal to NULL
!= NULL((void *)0))
20
Taking false branch
298 lastlp = &F->set[F->setcnt - 1];
299 }
300 /*
301 * Get any pushed back line, else get the next line. Allocate
302 * space as necessary. If taking the line from the stack swap
303 * the two structures so that we don't lose space allocated to
304 * either structure. This could be avoided by doing another
305 * level of indirection, but it's probably okay as is.
306 */
307 lp = &F->set[F->setcnt];
31
Null pointer value stored to 'lp'
308 if (F->setcnt
20.1
Field 'setcnt' is 0
31.1
Field 'setcnt' is 1
)
21
Taking false branch
32
Taking true branch
309 lastlp = &F->set[F->setcnt - 1];
310 if (F->pushbool
21.1
Field 'pushbool' is 0
32.1
Field 'pushbool' is 0
) {
22
Taking false branch
33
Taking false branch
311 tmp = F->set[F->setcnt];
312 F->set[F->setcnt] = F->set[F->pushback];
313 F->set[F->pushback] = tmp;
314 F->pushbool = 0;
315 continue;
316 }
317 if ((len = getline(&(lp->line), &(lp->linealloc), F->fp)) == -1)
23
Assuming the condition is false
24
Taking false branch
34
Assuming the condition is false
35
Taking false branch
318 break;
319
320 /* Remove the trailing newline, if any. */
321 if (lp->line[len - 1] == '\n')
25
Assuming the condition is false
26
Taking false branch
36
Access to field 'line' results in a dereference of a null pointer (loaded from variable 'lp')
322 lp->line[--len] = '\0';
323
324 /* Split the line into fields, allocate space as necessary. */
325 lp->fieldcnt = 0;
326 bp = lp->line;
327 while ((fieldp = mbssep(&bp, tabchar)) != NULL((void *)0)) {
27
Null pointer value stored to 'input1.set'
28
Assuming the condition is false
328 if (spans && *fieldp == '\0')
329 continue;
330 if (lp->fieldcnt == lp->fieldalloc) {
331 char **p;
332 u_long newsize = lp->fieldalloc + 50;
333 if ((p = reallocarray(lp->fields, newsize,
334 sizeof(char *))) == NULL((void *)0))
335 err(1, NULL((void *)0));
336 lp->fields = p;
337 lp->fieldalloc = newsize;
338 }
339 lp->fields[lp->fieldcnt++] = fieldp;
340 }
341
342 /* See if the join field value has changed. */
343 if (lastlp
28.1
'lastlp' is equal to NULL
!= NULL((void *)0) && cmp(lp, F->joinf, lastlp, F->joinf)) {
344 F->pushbool = 1;
345 F->pushback = F->setcnt;
346 break;
347 }
348 }
349}
350
351char *
352mbssep(char **stringp, const wchar_t *wcdelim)
353{
354 char *s, *p;
355 size_t ndelim;
356 int i;
357 /* tabchar is never more than 2 */
358 char mbdelim[2][MB_LEN_MAX4 + 1];
359 size_t mblen[2];
360
361 if ((s = *stringp) == NULL((void *)0))
362 return NULL((void *)0);
363 ndelim = wcslen(wcdelim);
364 for (i = 0; i < ndelim; i++) {
365 /* wcdelim generated via mbtowc */
366 mblen[i] = wctomb(mbdelim[i], wcdelim[i]);
367 }
368 for (p = s; *p != '\0'; p++) {
369 for (i = 0; i < ndelim; i++) {
370 if (strncmp(p, mbdelim[i], mblen[i]) == 0) {
371 *p = '\0';
372 *stringp = p + mblen[i];
373 return s;
374 }
375 }
376 }
377 *stringp = NULL((void *)0);
378 return s;
379}
380
381int
382cmp(LINE *lp1, u_long fieldno1, LINE *lp2, u_long fieldno2)
383{
384 if (lp1->fieldcnt <= fieldno1)
385 return lp2->fieldcnt <= fieldno2 ? 0 : -1;
386 if (lp2->fieldcnt <= fieldno2)
387 return 1;
388 return strcmp(lp1->fields[fieldno1], lp2->fields[fieldno2]);
389}
390
391void
392joinlines(INPUT *F1, INPUT *F2)
393{
394 u_long cnt1, cnt2;
395
396 /*
397 * Output the results of a join comparison. The output may be from
398 * either file 1 or file 2 (in which case the first argument is the
399 * file from which to output) or from both.
400 */
401 if (F2 == NULL((void *)0)) {
402 for (cnt1 = 0; cnt1 < F1->setcnt; ++cnt1)
403 outoneline(F1, &F1->set[cnt1]);
404 return;
405 }
406 for (cnt1 = 0; cnt1 < F1->setcnt; ++cnt1)
407 for (cnt2 = 0; cnt2 < F2->setcnt; ++cnt2)
408 outtwoline(F1, &F1->set[cnt1], F2, &F2->set[cnt2]);
409}
410
411void
412outoneline(INPUT *F, LINE *lp)
413{
414 u_long cnt;
415
416 /*
417 * Output a single line from one of the files, according to the
418 * join rules. This happens when we are writing unmatched single
419 * lines. Output empty fields in the right places.
420 */
421 if (olist)
422 for (cnt = 0; cnt < olistcnt; ++cnt) {
423 if (olist[cnt].filenum == F->number)
424 outfield(lp, olist[cnt].fieldno, 0);
425 else if (olist[cnt].filenum == 0)
426 outfield(lp, F->joinf, 0);
427 else
428 outfield(lp, 0, 1);
429 }
430 else {
431 /*
432 * Output the join field, then the remaining fields from F
433 */
434 outfield(lp, F->joinf, 0);
435 for (cnt = 0; cnt < lp->fieldcnt; ++cnt)
436 if (F->joinf != cnt)
437 outfield(lp, cnt, 0);
438 }
439
440 putchar('\n')(!__isthreaded ? __sputc('\n', (&__sF[1])) : (putc)('\n',
(&__sF[1])))
;
441 if (ferror(stdout)(!__isthreaded ? ((((&__sF[1]))->_flags & 0x0040) !=
0) : (ferror)((&__sF[1])))
)
442 err(1, "stdout");
443 needsep = 0;
444}
445
446void
447outtwoline(INPUT *F1, LINE *lp1, INPUT *F2, LINE *lp2)
448{
449 u_long cnt;
450
451 /* Output a pair of lines according to the join list (if any). */
452 if (olist) {
453 for (cnt = 0; cnt < olistcnt; ++cnt)
454 if (olist[cnt].filenum == 0) {
455 if (lp1->fieldcnt >= F1->joinf)
456 outfield(lp1, F1->joinf, 0);
457 else
458 outfield(lp2, F2->joinf, 0);
459 } else if (olist[cnt].filenum == 1)
460 outfield(lp1, olist[cnt].fieldno, 0);
461 else /* if (olist[cnt].filenum == 2) */
462 outfield(lp2, olist[cnt].fieldno, 0);
463 } else {
464 /*
465 * Output the join field, then the remaining fields from F1
466 * and F2.
467 */
468 outfield(lp1, F1->joinf, 0);
469 for (cnt = 0; cnt < lp1->fieldcnt; ++cnt)
470 if (F1->joinf != cnt)
471 outfield(lp1, cnt, 0);
472 for (cnt = 0; cnt < lp2->fieldcnt; ++cnt)
473 if (F2->joinf != cnt)
474 outfield(lp2, cnt, 0);
475 }
476 putchar('\n')(!__isthreaded ? __sputc('\n', (&__sF[1])) : (putc)('\n',
(&__sF[1])))
;
477 if (ferror(stdout)(!__isthreaded ? ((((&__sF[1]))->_flags & 0x0040) !=
0) : (ferror)((&__sF[1])))
)
478 err(1, "stdout");
479 needsep = 0;
480}
481
482void
483outfield(LINE *lp, u_long fieldno, int out_empty)
484{
485 if (needsep++)
486 putwchar(*tabchar)fputwc(((*tabchar)), ((&__sF[1])));
487 if (!ferror(stdout)(!__isthreaded ? ((((&__sF[1]))->_flags & 0x0040) !=
0) : (ferror)((&__sF[1])))
) {
488 if (lp->fieldcnt <= fieldno || out_empty) {
489 if (empty != NULL((void *)0))
490 fputs(empty, stdout(&__sF[1]));
491 } else {
492 if (*lp->fields[fieldno] == '\0')
493 return;
494 fputs(lp->fields[fieldno], stdout(&__sF[1]));
495 }
496 }
497 if (ferror(stdout)(!__isthreaded ? ((((&__sF[1]))->_flags & 0x0040) !=
0) : (ferror)((&__sF[1])))
)
498 err(1, "stdout");
499}
500
501/*
502 * Convert an output list argument "2.1, 1.3, 2.4" into an array of output
503 * fields.
504 */
505void
506fieldarg(char *option)
507{
508 u_long fieldno, filenum;
509 char *end, *token;
510
511 while ((token = strsep(&option, ", \t")) != NULL((void *)0)) {
512 if (*token == '\0')
513 continue;
514 if (token[0] == '0')
515 filenum = fieldno = 0;
516 else if ((token[0] == '1' || token[0] == '2') &&
517 token[1] == '.') {
518 filenum = token[0] - '0';
519 fieldno = strtol(token + 2, &end, 10);
520 if (*end)
521 errx(1, "malformed -o option field");
522 if (fieldno == 0)
523 errx(1, "field numbers are 1 based");
524 --fieldno;
525 } else
526 errx(1, "malformed -o option field");
527 if (olistcnt == olistalloc) {
528 OLIST *p;
529 u_long newsize = olistalloc + 50;
530 if ((p = reallocarray(olist, newsize, sizeof(OLIST)))
531 == NULL((void *)0))
532 err(1, NULL((void *)0));
533 olist = p;
534 olistalloc = newsize;
535 }
536 olist[olistcnt].filenum = filenum;
537 olist[olistcnt].fieldno = fieldno;
538 ++olistcnt;
539 }
540}
541
542void
543obsolete(char **argv)
544{
545 size_t len;
546 char **p, *ap, *t;
547
548 while ((ap = *++argv) != NULL((void *)0)) {
549 /* Return if "--". */
550 if (ap[0] == '-' && ap[1] == '-')
551 return;
552 /* skip if not an option */
553 if (ap[0] != '-')
554 continue;
555 switch (ap[1]) {
556 case 'a':
557 /*
558 * The original join allowed "-a", which meant the
559 * same as -a1 plus -a2. POSIX 1003.2, Draft 11.2
560 * only specifies this as "-a 1" and "a -2", so we
561 * have to use another option flag, one that is
562 * unlikely to ever be used or accidentally entered
563 * on the command line. (Well, we could reallocate
564 * the argv array, but that hardly seems worthwhile.)
565 */
566 if (ap[2] == '\0' && (argv[1] == NULL((void *)0) ||
567 (strcmp(argv[1], "1") != 0 &&
568 strcmp(argv[1], "2") != 0))) {
569 ap[1] = '\01';
570 warnx("-a option used without an argument; "
571 "reverting to historical behavior");
572 }
573 break;
574 case 'j':
575 /*
576 * The original join allowed "-j[12] arg" and "-j arg".
577 * Convert the former to "-[12] arg". Don't convert
578 * the latter since getopt(3) can handle it.
579 */
580 switch(ap[2]) {
581 case '1':
582 case '2':
583 if (ap[3] != '\0')
584 goto jbad;
585 ap[1] = ap[2];
586 ap[2] = '\0';
587 break;
588 case '\0':
589 break;
590 default:
591jbad: warnx("unknown option -- %s", ap + 1);
592 usage();
593 }
594 break;
595 case 'o':
596 /*
597 * The original join allowed "-o arg arg".
598 * Convert to "-o arg -o arg".
599 */
600 if (ap[2] != '\0' || argv[1] == NULL((void *)0))
601 break;
602 for (p = argv + 2; *p != NULL((void *)0); ++p) {
603 if (p[0][0] == '0' || ((p[0][0] != '1' &&
604 p[0][0] != '2') || p[0][1] != '.'))
605 break;
606 len = strlen(*p);
607 if (len - 2 != strspn(*p + 2, "0123456789"))
608 break;
609 if ((t = malloc(len + 3)) == NULL((void *)0))
610 err(1, NULL((void *)0));
611 t[0] = '-';
612 t[1] = 'o';
613 memmove(t + 2, *p, len + 1);
614 *p = t;
615 }
616 argv = p - 1;
617 break;
618 }
619 }
620}
621
622void
623usage(void)
624{
625 int len;
626 extern char *__progname;
627
628 len = strlen(__progname) + sizeof("usage: ");
629 (void)fprintf(stderr(&__sF[2]), "usage: %s [-1 field] [-2 field] "
630 "[-a file_number | -v file_number] [-e string]\n"
631 "%*s[-o list] [-t char] file1 file2\n",
632 __progname, len, "");
633 exit(1);
634}