xref: /freebsd/crypto/krb5/src/kadmin/dbutil/t_tdumputil.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* kdc/t_tdumputil.c - test tdumputil.c functions */
3 /*
4  * Copyright (C) 2015 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  *
14  * * Redistributions in binary form must reproduce the above copyright
15  *   notice, this list of conditions and the following disclaimer in
16  *   the documentation and/or other materials provided with the
17  *   distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 
37 #include "tdumputil.h"
38 
39 static char *argv0;
40 
41 static void
usage(void)42 usage(void)
43 {
44     fprintf(stderr,
45             "usage: %s: [-T rectype] [-c] nfields {fieldnames} {fields}\n",
46             argv0);
47     exit(1);
48 }
49 
50 int
main(int argc,char ** argv)51 main(int argc, char **argv)
52 {
53     int ch, csv = 0, i, nf;
54     char **a, *rectype = NULL;
55     struct rechandle *h;
56 
57     argv0 = argv[0];
58     while ((ch = getopt(argc, argv, "T:c")) != -1) {
59         switch (ch) {
60         case 'T':
61             rectype = optarg;
62             break;
63         case 'c':
64             csv = 1;
65             break;
66         default:
67             usage();
68             break;
69         }
70     }
71     argc -= optind;
72     argv += optind;
73 
74     if (csv)
75         h = rechandle_csv(stdout, rectype);
76     else
77         h = rechandle_tabsep(stdout, rectype);
78     if (h == NULL)
79         exit(1);
80 
81     if (*argv == NULL)
82         usage();
83     nf = atoi(*argv);
84     argc--;
85     argv++;
86     a = calloc(nf + 1, sizeof(*a));
87     if (a == NULL)
88         exit(1);
89 
90     for (i = 0; argv[i] != NULL && i < nf; i++)
91         a[i] = argv[i];
92     if (i != nf)
93         usage();
94     argv += nf;
95     a[nf] = NULL;
96 
97     if (rectype == NULL && writeheader(h, a) < 0)
98         exit(1);
99     free(a);
100 
101     while (*argv != NULL) {
102         if (startrec(h) < 0)
103             exit(1);
104         for (i = 0; argv[i] != NULL && i < nf; i++) {
105             if (writefield(h, "%s", argv[i]) < 0)
106                 exit(1);
107         }
108         if (i != nf)
109             usage();
110         argv += nf;
111         if (endrec(h) < 0)
112             exit(1);
113     }
114     exit(0);
115 }
116