Line data Source code
1 : /*
2 : Authors:
3 : Jakub Hrozek <jhrozek@redhat.com>
4 :
5 : Copyright (C) 2012 Red Hat
6 :
7 : This program is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU Lesser General Public License as published by
9 : the Free Software Foundation; either version 3 of the License, or
10 : (at your option) any later version.
11 :
12 : This program is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU Lesser General Public License for more details.
16 :
17 : You should have received a copy of the GNU Lesser General Public License
18 : along with this program. If not, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : #ifdef HAVE_CONFIG_H
22 : #include <config.h>
23 : #endif
24 :
25 : #include <stdio.h>
26 : #include <stdlib.h>
27 : #include <dlfcn.h>
28 : #include <errno.h>
29 : #include <popt.h>
30 :
31 : #include "util/util.h"
32 : #include "sss_client/autofs/sss_autofs_private.h"
33 :
34 : struct automtent {
35 : const char *mapname;
36 : size_t cursor;
37 : };
38 :
39 0 : int main(int argc, const char *argv[])
40 : {
41 : void *ctx;
42 : errno_t ret;
43 : const char *mapname;
44 0 : char *key = NULL;
45 0 : char *value = NULL;
46 0 : char *pc_key = NULL;
47 0 : struct poptOption long_options[] = {
48 : POPT_AUTOHELP
49 : { "by-name", 'n', POPT_ARG_STRING, &pc_key, 0, "Request map by name", NULL },
50 : POPT_TABLEEND
51 : };
52 0 : poptContext pc = NULL;
53 :
54 0 : pc = poptGetContext(NULL, argc, argv, long_options, 0);
55 0 : poptSetOtherOptionHelp(pc, "MAPNAME");
56 :
57 0 : while ((ret = poptGetNextOpt(pc)) > 0)
58 : ;
59 :
60 0 : mapname = poptGetArg(pc);
61 0 : if (mapname == NULL) {
62 0 : poptPrintUsage(pc, stderr, 0);
63 0 : fprintf(stderr, "Please specify the automounter map name\n");
64 0 : poptFreeContext(pc);
65 0 : exit(EXIT_FAILURE);
66 : }
67 :
68 0 : poptFreeContext(pc);
69 :
70 0 : ret = _sss_setautomntent(mapname, &ctx);
71 0 : if (ret) {
72 0 : fprintf(stderr, "setautomntent failed [%d]: %s\n",
73 : ret, strerror(ret));
74 0 : exit(EXIT_FAILURE);
75 : }
76 0 : printf("setautomntent done for %s\n", mapname);
77 :
78 0 : if (!pc_key) {
79 : do {
80 0 : ret = _sss_getautomntent_r(&key, &value, ctx);
81 0 : if (ret == 0) {
82 0 : if (!key || !value) {
83 0 : fprintf(stderr,
84 : "getautomntent returned success but no data?\n");
85 0 : goto end;
86 : }
87 :
88 0 : printf("key: %s\t\tvalue: %s\n", key, value);
89 0 : free(key);
90 0 : key = NULL;
91 0 : free(value);
92 0 : value = NULL;
93 : }
94 0 : } while(ret == 0);
95 :
96 0 : if (ret != 0 && ret != ENOENT) {
97 0 : fprintf(stderr, "getautomntent_r failed [%d]: %s\n",
98 : ret, strerror(ret));
99 0 : goto end;
100 : }
101 : } else {
102 0 : ret = _sss_getautomntbyname_r(pc_key, &value, ctx);
103 0 : if (ret == ENOENT) {
104 0 : fprintf(stderr, "no such entry in map\n");
105 0 : } else if (ret != 0) {
106 0 : fprintf(stderr, "getautomntent_r failed [%d]: %s\n",
107 : ret, strerror(ret));
108 0 : goto end;
109 : } else {
110 0 : if (!value) {
111 0 : fprintf(stderr, "_sss_getautomntbyname_r "
112 : "returned success but no data?\n");
113 0 : goto end;
114 : }
115 :
116 0 : printf("key: %s\t\tvalue: %s\n", pc_key, value);
117 0 : free(value);
118 : }
119 : }
120 :
121 : end:
122 0 : ret = _sss_endautomntent(&ctx);
123 0 : if (ret) {
124 0 : fprintf(stderr, "endautomntent failed [%d]: %s\n",
125 : ret, strerror(ret));
126 0 : exit(EXIT_FAILURE);
127 : }
128 0 : printf("endautomntent done for %s\n", mapname);
129 0 : return 0;
130 : }
|