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
768
769
770
771
772
773
774
775
776
|
/*++
/* NAME
/* argv 3
/* SUMMARY
/* string array utilities
/* SYNOPSIS
/* #include <argv.h>
/*
/* typedef int (*ARGV_COMPAR_FN)(const void *, const void *);
/*
/* ARGV *argv_alloc(len)
/* ssize_t len;
/*
/* ARGV *argv_qsort(argvp, compar)
/* ARGV *argvp;
/* ARGV_COMPAR_FN compar;
/*
/* void argv_uniq(argvp, compar)
/* ARGV *argvp;
/* ARGV_COMPAR_FN compar;
/*
/* ARGV *argv_free(argvp)
/* ARGV *argvp;
/*
/* void argv_add(argvp, arg, ..., ARGV_END)
/* ARGV *argvp;
/* char *arg;
/*
/* void argv_addn(argvp, arg, arg_len, ..., ARGV_END)
/* ARGV *argvp;
/* char *arg;
/* ssize_t arg_len;
/*
/* void argv_terminate(argvp);
/* ARGV *argvp;
/*
/* void argv_truncate(argvp, len);
/* ARGV *argvp;
/* ssize_t len;
/*
/* void argv_insert_one(argvp, pos, arg)
/* ARGV *argvp;
/* ssize_t pos;
/* const char *arg;
/*
/* void argv_replace_one(argvp, pos, arg)
/* ARGV *argvp;
/* ssize_t pos;
/* const char *arg;
/*
/* void argv_delete(argvp, pos, how_many)
/* ARGV *argvp;
/* ssize_t pos;
/* ssize_t how_many;
/*
/* char *argv_join(buf, argvp, delim)
/* VSTRING *buf;
/* ARGV *argvp;
/* int delim;
/*
/* void ARGV_FAKE_BEGIN(argv, arg)
/* const char *arg;
/*
/* void ARGV_FAKE_END
/* DESCRIPTION
/* The functions in this module manipulate arrays of string
/* pointers. An ARGV structure contains the following members:
/* .IP len
/* The length of the \fIargv\fR array member.
/* .IP argc
/* The number of \fIargv\fR elements used.
/* .IP argv
/* An array of pointers to null-terminated strings.
/* .PP
/* argv_alloc() returns an empty string array of the requested
/* length. The result is ready for use by argv_add(). The array
/* is null terminated.
/*
/* argv_qsort() sorts the elements of argvp in place, and
/* returns its first argument. If the compar argument specifies
/* a null pointer, then argv_qsort() will use byte-by-byte
/* comparison.
/*
/* argv_uniq() reduces adjacent same-value elements to one
/* element, and returns its first argument. If the compar
/* argument specifies a null pointer, then argv_uniq() will
/* use byte-by-byte comparison.
/*
/* argv_add() copies zero or more strings and adds them to the
/* specified string array. The array is null terminated.
/* Terminate the argument list with a null pointer. The manifest
/* constant ARGV_END provides a convenient notation for this.
/*
/* argv_addn() is like argv_add(), but each string is followed
/* by a string length argument.
/*
/* argv_free() releases storage for a string array, and conveniently
/* returns a null pointer.
/*
/* argv_terminate() null-terminates its string array argument.
/*
/* argv_truncate() truncates its argument to the specified
/* number of entries, but does not reallocate memory. The
/* result is null-terminated.
/*
/* argv_insert_one() inserts one string at the specified array
/* position.
/*
/* argv_replace_one() replaces one string at the specified
/* position. The old string is destroyed after the update is
/* made.
/*
/* argv_delete() deletes the specified number of elements
/* starting at the specified array position. The result is
/* null-terminated.
/*
/* argv_join() joins all elements in an array using the
/* specified delimiter value, and appends the result to the
/* specified buffer.
/*
/* ARGV_FAKE_BEGIN/END are an optimization for the case where
/* a single string needs to be passed into an ARGV-based
/* interface. ARGV_FAKE_BEGIN() opens a statement block and
/* allocates a stack-based ARGV structure named after the first
/* argument, that encapsulates the second argument. This
/* implementation allocates no heap memory and creates no copy
/* of the second argument. ARGV_FAKE_END closes the statement
/* block and thereby releases storage.
/* SEE ALSO
/* msg(3) diagnostics interface
/* DIAGNOSTICS
/* Fatal errors: memory allocation problem.
/* LICENSE
/* .ad
/* .fi
/* The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/* Wietse Venema
/* IBM T.J. Watson Research
/* P.O. Box 704
/* Yorktown Heights, NY 10598, USA
/*
/* Wietse Venema
/* Google, Inc.
/* 111 8th Avenue
/* New York, NY 10011, USA
/*--*/
/* System libraries. */
#include <sys_defs.h>
#include <stdlib.h> /* 44BSD stdarg.h uses abort() */
#include <stdarg.h>
#include <string.h>
/* Application-specific. */
#include "mymalloc.h"
#include "msg.h"
#include "vstring.h"
#include "argv.h"
#ifdef TEST
extern NORETURN PRINTFLIKE(1, 2) test_msg_panic(const char *,...);
#define msg_panic test_msg_panic
#endif
/* argv_free - destroy string array */
ARGV *argv_free(ARGV *argvp)
{
char **cpp;
for (cpp = argvp->argv; cpp < argvp->argv + argvp->argc; cpp++)
myfree(*cpp);
myfree((void *) argvp->argv);
myfree((void *) argvp);
return (0);
}
/* argv_alloc - initialize string array */
ARGV *argv_alloc(ssize_t len)
{
ARGV *argvp;
ssize_t sane_len;
/*
* Make sure that always argvp->argc < argvp->len.
*/
argvp = (ARGV *) mymalloc(sizeof(*argvp));
argvp->len = 0;
sane_len = (len < 2 ? 2 : len);
argvp->argv = (char **) mymalloc((sane_len + 1) * sizeof(char *));
argvp->len = sane_len;
argvp->argc = 0;
argvp->argv[0] = 0;
return (argvp);
}
static int argv_cmp(const void *e1, const void *e2)
{
const char *s1 = *(const char **) e1;
const char *s2 = *(const char **) e2;
return strcmp(s1, s2);
}
/* argv_qsort - sort array in place */
ARGV *argv_qsort(ARGV *argvp, ARGV_COMPAR_FN compar)
{
qsort(argvp->argv, argvp->argc, sizeof(argvp->argv[0]),
compar ? compar : argv_cmp);
return (argvp);
}
/* argv_sort - binary compatibility */
ARGV *argv_sort(ARGV *argvp)
{
qsort(argvp->argv, argvp->argc, sizeof(argvp->argv[0]), argv_cmp);
return (argvp);
}
/* argv_uniq - deduplicate adjacent array elements */
ARGV *argv_uniq(ARGV *argvp, ARGV_COMPAR_FN compar)
{
char **cpp;
char **prev;
if (compar == 0)
compar = argv_cmp;
for (prev = 0, cpp = argvp->argv; cpp < argvp->argv + argvp->argc; cpp++) {
if (prev != 0 && compar(prev, cpp) == 0) {
argv_delete(argvp, cpp - argvp->argv, 1);
cpp = prev;
} else {
prev = cpp;
}
}
return (argvp);
}
/* argv_extend - extend array */
static void argv_extend(ARGV *argvp)
{
ssize_t new_len;
new_len = argvp->len * 2;
argvp->argv = (char **)
myrealloc((void *) argvp->argv, (new_len + 1) * sizeof(char *));
argvp->len = new_len;
}
/* argv_add - add string to vector */
void argv_add(ARGV *argvp,...)
{
char *arg;
va_list ap;
/*
* Make sure that always argvp->argc < argvp->len.
*/
#define ARGV_SPACE_LEFT(a) ((a)->len - (a)->argc - 1)
va_start(ap, argvp);
while ((arg = va_arg(ap, char *)) != 0) {
if (ARGV_SPACE_LEFT(argvp) <= 0)
argv_extend(argvp);
argvp->argv[argvp->argc++] = mystrdup(arg);
}
va_end(ap);
argvp->argv[argvp->argc] = 0;
}
/* argv_addn - add string to vector */
void argv_addn(ARGV *argvp,...)
{
char *arg;
ssize_t len;
va_list ap;
/*
* Make sure that always argvp->argc < argvp->len.
*/
va_start(ap, argvp);
while ((arg = va_arg(ap, char *)) != 0) {
if ((len = va_arg(ap, ssize_t)) < 0)
msg_panic("argv_addn: bad string length %ld", (long) len);
if (ARGV_SPACE_LEFT(argvp) <= 0)
argv_extend(argvp);
argvp->argv[argvp->argc++] = mystrndup(arg, len);
}
va_end(ap);
argvp->argv[argvp->argc] = 0;
}
/* argv_terminate - terminate string array */
void argv_terminate(ARGV *argvp)
{
/*
* Trust that argvp->argc < argvp->len.
*/
argvp->argv[argvp->argc] = 0;
}
/* argv_truncate - truncate string array */
void argv_truncate(ARGV *argvp, ssize_t len)
{
char **cpp;
/*
* Sanity check.
*/
if (len < 0)
msg_panic("argv_truncate: bad length %ld", (long) len);
if (len < argvp->argc) {
for (cpp = argvp->argv + len; cpp < argvp->argv + argvp->argc; cpp++)
myfree(*cpp);
argvp->argc = len;
argvp->argv[argvp->argc] = 0;
}
}
/* argv_insert_one - insert one string into array */
void argv_insert_one(ARGV *argvp, ssize_t where, const char *arg)
{
ssize_t pos;
/*
* Sanity check.
*/
if (where < 0 || where > argvp->argc)
msg_panic("argv_insert_one bad position: %ld", (long) where);
if (ARGV_SPACE_LEFT(argvp) <= 0)
argv_extend(argvp);
for (pos = argvp->argc; pos >= where; pos--)
argvp->argv[pos + 1] = argvp->argv[pos];
argvp->argv[where] = mystrdup(arg);
argvp->argc += 1;
}
/* argv_replace_one - replace one string in array */
void argv_replace_one(ARGV *argvp, ssize_t where, const char *arg)
{
char *temp;
/*
* Sanity check.
*/
if (where < 0 || where >= argvp->argc)
msg_panic("argv_replace_one bad position: %ld", (long) where);
temp = argvp->argv[where];
argvp->argv[where] = mystrdup(arg);
myfree(temp);
}
/* argv_delete - remove string(s) from array */
void argv_delete(ARGV *argvp, ssize_t first, ssize_t how_many)
{
ssize_t pos;
/*
* Sanity check.
*/
if (first < 0 || how_many < 0 || first + how_many > argvp->argc)
msg_panic("argv_delete bad range: (start=%ld count=%ld)",
(long) first, (long) how_many);
for (pos = first; pos < first + how_many; pos++)
myfree(argvp->argv[pos]);
for (pos = first; pos <= argvp->argc - how_many; pos++)
argvp->argv[pos] = argvp->argv[pos + how_many];
argvp->argc -= how_many;
}
/* argv_join - concatenate array elements with delimiter */
char *argv_join(VSTRING *buf, ARGV *argv, int delim)
{
char **cpp;
for (cpp = argv->argv; *cpp; cpp++) {
vstring_strcat(buf, *cpp);
if (cpp[1])
VSTRING_ADDCH(buf, delim);
}
return (vstring_str(buf));
}
#ifdef TEST
/*
* System library.
*/
#include <setjmp.h>
/*
* Utility library.
*/
#include <msg_vstream.h>
#include <stringops.h>
#define ARRAY_LEN (10)
typedef struct TEST_CASE {
const char *label; /* identifies test case */
const char *inputs[ARRAY_LEN]; /* input strings */
int terminate; /* terminate result */
ARGV *(*populate_fn) (const struct TEST_CASE *, ARGV *);
const char *exp_panic_msg; /* expected panic */
int exp_argc; /* expected array length */
const char *exp_argv[ARRAY_LEN]; /* expected array content */
int join_delim; /* argv_join() delimiter */
} TEST_CASE;
#define TERMINATE_ARRAY (1)
#define PASS (0)
#define FAIL (1)
VSTRING *test_panic_str;
jmp_buf test_panic_jbuf;
/* test_msg_panic - does not return, and does not terminate */
void test_msg_panic(const char *fmt,...)
{
va_list ap;
va_start(ap, fmt);
test_panic_str = vstring_alloc(100);
vstring_vsprintf(test_panic_str, fmt, ap);
va_end(ap);
longjmp(test_panic_jbuf, 1);
}
/* test_argv_populate - populate result, optionally terminate */
static ARGV *test_argv_populate(const TEST_CASE *tp, ARGV *argvp)
{
const char *const * cpp;
for (cpp = tp->inputs; *cpp; cpp++)
argv_add(argvp, *cpp, (char *) 0);
if (tp->terminate)
argv_terminate(argvp);
return (argvp);
}
/* test_argv_sort - populate and sort result */
static ARGV *test_argv_sort(const TEST_CASE *tp, ARGV *argvp)
{
test_argv_populate(tp, argvp);
argv_qsort(argvp, (ARGV_COMPAR_FN) 0);
return (argvp);
}
/* test_argv_sort_uniq - populate, sort, uniq result */
static ARGV *test_argv_sort_uniq(const TEST_CASE *tp, ARGV *argvp)
{
/*
* This also tests argv_delete().
*/
test_argv_sort(tp, argvp);
argv_uniq(argvp, (ARGV_COMPAR_FN) 0);
return (argvp);
}
/* test_argv_good_truncate - populate and truncate to good size */
static ARGV *test_argv_good_truncate(const TEST_CASE *tp, ARGV *argvp)
{
test_argv_populate(tp, argvp);
argv_truncate(argvp, tp->exp_argc);
return (argvp);
}
/* test_argv_bad_truncate - populate and truncate to bad size */
static ARGV *test_argv_bad_truncate(const TEST_CASE *tp, ARGV *argvp)
{
test_argv_populate(tp, argvp);
argv_truncate(argvp, -1);
return (argvp);
}
/* test_argv_good_insert - populate and insert at good position */
static ARGV *test_argv_good_insert(const TEST_CASE *tp, ARGV *argvp)
{
test_argv_populate(tp, argvp);
argv_insert_one(argvp, 1, "new");
return (argvp);
}
/* test_argv_bad_insert1 - populate and insert at bad position */
static ARGV *test_argv_bad_insert1(const TEST_CASE *tp, ARGV *argvp)
{
test_argv_populate(tp, argvp);
argv_insert_one(argvp, -1, "new");
return (argvp);
}
/* test_argv_bad_insert2 - populate and insert at bad position */
static ARGV *test_argv_bad_insert2(const TEST_CASE *tp, ARGV *argvp)
{
test_argv_populate(tp, argvp);
argv_insert_one(argvp, 100, "new");
return (argvp);
}
/* test_argv_good_replace - populate and replace at good position */
static ARGV *test_argv_good_replace(const TEST_CASE *tp, ARGV *argvp)
{
test_argv_populate(tp, argvp);
argv_replace_one(argvp, 1, "new");
return (argvp);
}
/* test_argv_bad_replace1 - populate and replace at bad position */
static ARGV *test_argv_bad_replace1(const TEST_CASE *tp, ARGV *argvp)
{
test_argv_populate(tp, argvp);
argv_replace_one(argvp, -1, "new");
return (argvp);
}
/* test_argv_bad_replace2 - populate and replace at bad position */
static ARGV *test_argv_bad_replace2(const TEST_CASE *tp, ARGV *argvp)
{
test_argv_populate(tp, argvp);
argv_replace_one(argvp, 100, "new");
return (argvp);
}
/* test_argv_bad_delete1 - populate and delete at bad position */
static ARGV *test_argv_bad_delete1(const TEST_CASE *tp, ARGV *argvp)
{
test_argv_populate(tp, argvp);
argv_delete(argvp, -1, 1);
return (argvp);
}
/* test_argv_bad_delete2 - populate and delete at bad position */
static ARGV *test_argv_bad_delete2(const TEST_CASE *tp, ARGV *argvp)
{
test_argv_populate(tp, argvp);
argv_delete(argvp, 0, -1);
return (argvp);
}
/* test_argv_bad_delete3 - populate and delete at bad position */
static ARGV *test_argv_bad_delete3(const TEST_CASE *tp, ARGV *argvp)
{
test_argv_populate(tp, argvp);
argv_delete(argvp, 100, 1);
return (argvp);
}
/* test_argv_join - populate, join, and overwrite */
static ARGV *test_argv_join(const TEST_CASE *tp, ARGV *argvp)
{
VSTRING *buf = vstring_alloc(100);
/*
* Impedance mismatch: argv_join() produces output to VSTRING, but the
* test fixture wants output to ARGV.
*/
test_argv_populate(tp, argvp);
argv_join(buf, argvp, tp->join_delim);
argv_delete(argvp, 0, argvp->argc);
argv_add(argvp, vstring_str(buf), ARGV_END);
vstring_free(buf);
return (argvp);
}
/* test_argv_verify - verify result */
static int test_argv_verify(const TEST_CASE *tp, ARGV *argvp)
{
int idx;
if (tp->exp_panic_msg != 0) {
if (test_panic_str == 0) {
msg_warn("test case '%s': got no panic, want: '%s'",
tp->label, tp->exp_panic_msg);
return (FAIL);
}
if (strcmp(vstring_str(test_panic_str), tp->exp_panic_msg) != 0) {
msg_warn("test case '%s': got '%s', want: '%s'",
tp->label, vstring_str(test_panic_str), tp->exp_panic_msg);
return (FAIL);
}
return (PASS);
}
if (test_panic_str != 0) {
msg_warn("test case '%s': got '%s', want: no panic",
tp->label, vstring_str(test_panic_str));
return (FAIL);
}
if (argvp->argc != tp->exp_argc) {
msg_warn("test case '%s': got argc: %ld, want: %d",
tp->label, (long) argvp->argc, tp->exp_argc);
return (FAIL);
}
if (argvp->argv[argvp->argc] != 0 && tp->terminate) {
msg_warn("test case '%s': got unterminated, want: terminated", tp->label);
return (FAIL);
}
for (idx = 0; idx < argvp->argc; idx++) {
if (strcmp(argvp->argv[idx], tp->exp_argv[idx]) != 0) {
msg_warn("test case '%s': index %d: got '%s', want: '%s'",
tp->label, idx, argvp->argv[idx], tp->exp_argv[idx]);
return (FAIL);
}
}
return (PASS);
}
/*
* The test cases. TODO: argv_addn with good and bad string length.
*/
static const TEST_CASE test_cases[] = {
{"multiple strings, unterminated array",
{"foo", "baz", "bar", 0}, 0, test_argv_populate,
0, 3, {"foo", "baz", "bar", 0}
},
{"multiple strings, terminated array",
{"foo", "baz", "bar", 0}, TERMINATE_ARRAY, test_argv_populate,
0, 3, {"foo", "baz", "bar", 0}
},
{"distinct strings, sorted array",
{"foo", "baz", "bar", 0}, 0, test_argv_sort,
0, 3, {"bar", "baz", "foo", 0}
},
{"duplicate strings, sorted array",
{"foo", "baz", "baz", "bar", 0}, 0, test_argv_sort,
0, 4, {"bar", "baz", "baz", "foo", 0}
},
{"duplicate strings, sorted, uniqued-middle elements",
{"foo", "baz", "baz", "bar", 0}, 0, test_argv_sort_uniq,
0, 3, {"bar", "baz", "foo", 0}
},
{"duplicate strings, sorted, uniqued-first elements",
{"foo", "bar", "baz", "bar", 0}, 0, test_argv_sort_uniq,
0, 3, {"bar", "baz", "foo", 0}
},
{"duplicate strings, sorted, uniqued-last elements",
{"foo", "foo", "baz", "bar", 0}, 0, test_argv_sort_uniq,
0, 3, {"bar", "baz", "foo", 0}
},
{"multiple strings, truncate array by one",
{"foo", "baz", "bar", 0}, 0, test_argv_good_truncate,
0, 2, {"foo", "baz", 0}
},
{"multiple strings, truncate whole array",
{"foo", "baz", "bar", 0}, 0, test_argv_good_truncate,
0, 0, {"foo", "baz", 0}
},
{"multiple strings, bad truncate",
{"foo", "baz", "bar", 0}, 0, test_argv_bad_truncate,
"argv_truncate: bad length -1"
},
{"multiple strings, insert one at good position",
{"foo", "baz", "bar", 0}, 0, test_argv_good_insert,
0, 4, {"foo", "new", "baz", "bar", 0}
},
{"multiple strings, insert one at bad position",
{"foo", "baz", "bar", 0}, 0, test_argv_bad_insert1,
"argv_insert_one bad position: -1"
},
{"multiple strings, insert one at bad position",
{"foo", "baz", "bar", 0}, 0, test_argv_bad_insert2,
"argv_insert_one bad position: 100"
},
{"multiple strings, replace one at good position",
{"foo", "baz", "bar", 0}, 0, test_argv_good_replace,
0, 3, {"foo", "new", "bar", 0}
},
{"multiple strings, replace one at bad position",
{"foo", "baz", "bar", 0}, 0, test_argv_bad_replace1,
"argv_replace_one bad position: -1"
},
{"multiple strings, replace one at bad position",
{"foo", "baz", "bar", 0}, 0, test_argv_bad_replace2,
"argv_replace_one bad position: 100"
},
{"multiple strings, delete one at negative position",
{"foo", "baz", "bar", 0}, 0, test_argv_bad_delete1,
"argv_delete bad range: (start=-1 count=1)"
},
{"multiple strings, delete with bad range end",
{"foo", "baz", "bar", 0}, 0, test_argv_bad_delete2,
"argv_delete bad range: (start=0 count=-1)"
},
{"multiple strings, delete at too large position",
{"foo", "baz", "bar", 0}, 0, test_argv_bad_delete3,
"argv_delete bad range: (start=100 count=1)"
},
{"argv_join, multiple strings",
{"foo", "baz", "bar", 0}, 0, test_argv_join,
0, 1, {"foo:baz:bar", 0}, ':'
},
{"argv_join, one string",
{"foo", 0}, 0, test_argv_join,
0, 1, {"foo", 0}, ':'
},
{"argv_join, empty",
{0}, 0, test_argv_join,
0, 1, {"", 0}, ':'
},
0,
};
int main(int argc, char **argv)
{
const TEST_CASE *tp;
int pass = 0;
int fail = 0;
msg_vstream_init(sane_basename((VSTRING *) 0, argv[0]), VSTREAM_ERR);
for (tp = test_cases; tp->label != 0; tp++) {
int test_failed;
ARGV *argvp;
argvp = argv_alloc(1);
if (setjmp(test_panic_jbuf) == 0)
tp->populate_fn(tp, argvp);
test_failed = test_argv_verify(tp, argvp);
if (test_failed) {
msg_info("%s: FAIL", tp->label);
fail++;
} else {
msg_info("%s: PASS", tp->label);
pass++;
}
argv_free(argvp);
if (test_panic_str) {
vstring_free(test_panic_str);
test_panic_str = 0;
}
}
msg_info("PASS=%d FAIL=%d", pass, fail);
exit(fail != 0);
}
#endif
|