xref: /freebsd/crypto/krb5/src/util/ss/help.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * Copyright 1987, 1988 by MIT Student Information Processing Board
4  *
5  * For copyright info, see copyright.h.
6  */
7 
8 #include <sys/param.h>
9 #include <sys/types.h>
10 #include <errno.h>
11 #include <sys/file.h>
12 #include <fcntl.h>      /* just for O_* */
13 #include <sys/wait.h>
14 #include "ss_internal.h"
15 #include "copyright.h"
16 
17 
ss_help(argc,argv,sci_idx,info_ptr)18 void ss_help (argc, argv, sci_idx, info_ptr)
19     int argc;
20     char const * const *argv;
21     int sci_idx;
22     pointer info_ptr;
23 {
24     char buffer[MAXPATHLEN];
25     char const *request_name;
26     int code;
27     int fd, child;
28     int idx;
29     ss_data *info;
30 
31     request_name = ss_current_request(sci_idx, &code);
32     if (code != 0) {
33         ss_perror(sci_idx, code, "");
34         return;         /* no ss_abort_line, if invalid invocation */
35     }
36     if (argc == 1) {
37         ss_list_requests(argc, argv, sci_idx, info_ptr);
38         return;
39     }
40     else if (argc != 2) {
41         /* should do something better than this */
42         snprintf(buffer, sizeof(buffer),
43                  "usage:\n\t%s [topic|command]\nor\t%s\n",
44                  request_name, request_name);
45         ss_perror(sci_idx, 0, buffer);
46         return;
47     }
48     info = ss_info(sci_idx);
49     if (info->info_dirs == (char **)NULL) {
50         ss_perror(sci_idx, SS_ET_NO_INFO_DIR, (char *)NULL);
51         return;
52     }
53     if (info->info_dirs[0] == (char *)NULL) {
54         ss_perror(sci_idx, SS_ET_NO_INFO_DIR, (char *)NULL);
55         return;
56     }
57     for (idx = 0; info->info_dirs[idx] != (char *)NULL; idx++) {
58         (void) strncpy(buffer, info->info_dirs[idx], sizeof(buffer) - 1);
59         buffer[sizeof(buffer) - 1] = '\0';
60         (void) strncat(buffer, "/", sizeof(buffer) - 1 - strlen(buffer));
61         (void) strncat(buffer, argv[1], sizeof(buffer) - 1 - strlen(buffer));
62         (void) strncat(buffer, ".info", sizeof(buffer) - 1 - strlen(buffer));
63         if ((fd = open(&buffer[0], O_RDONLY)) >= 0) goto got_it;
64     }
65     if ((fd = open(&buffer[0], O_RDONLY)) < 0) {
66         char buf[MAXPATHLEN];
67         strncpy(buf, "No info found for ", sizeof(buf) - 1);
68         buf[sizeof(buf) - 1] = '\0';
69         strncat(buf, argv[1], sizeof(buf) - 1 - strlen(buf));
70         ss_perror(sci_idx, 0, buf);
71         return;
72     }
73 got_it:
74     switch (child = fork()) {
75     case -1:
76         ss_perror(sci_idx, errno, "Can't fork for pager");
77         close(fd);
78         return;
79     case 0:
80         (void) dup2(fd, 0); /* put file on stdin */
81         ss_page_stdin();
82     default:
83         (void) close(fd); /* what can we do if it fails? */
84 #ifdef WAIT_USES_INT
85         while (wait((int *)NULL) != child) {
86 #else
87             while (wait((union wait *)NULL) != child) {
88 #endif
89                 /* do nothing if wrong pid */
90             };
91         }
92     }
93 
94 #ifndef USE_DIRENT_H
95 #include <sys/dir.h>
96 #else
97 #include <dirent.h>
98 #endif
99 
ss_add_info_dir(sci_idx,info_dir,code_ptr)100     void ss_add_info_dir(sci_idx, info_dir, code_ptr)
101         int sci_idx;
102     char *info_dir;
103     int *code_ptr;
104     {
105         ss_data *info;
106         DIR *d;
107         int n_dirs;
108         char **dirs;
109 
110         info = ss_info(sci_idx);
111         if ((info_dir == NULL) || (*info_dir == '\0')) {
112             *code_ptr = SS_ET_NO_INFO_DIR;
113             return;
114         }
115         if ((d = opendir(info_dir)) == (DIR *)NULL) {
116             *code_ptr = errno;
117             return;
118         }
119         closedir(d);
120         dirs = info->info_dirs;
121         for (n_dirs = 0; dirs[n_dirs] != (char *)NULL; n_dirs++)
122             ;               /* get number of non-NULL dir entries */
123         dirs = (char **)realloc((char *)dirs,
124                                 (unsigned)(n_dirs + 2)*sizeof(char *));
125         if (dirs == (char **)NULL) {
126             info->info_dirs = (char **)NULL;
127             *code_ptr = errno;
128             return;
129         }
130         info->info_dirs = dirs;
131         dirs[n_dirs + 1] = (char *)NULL;
132         dirs[n_dirs] = strdup(info_dir);
133         *code_ptr = 0;
134     }
135 
ss_delete_info_dir(sci_idx,info_dir,code_ptr)136     void ss_delete_info_dir(sci_idx, info_dir, code_ptr)
137         int sci_idx;
138     char *info_dir;
139     int *code_ptr;
140     {
141         char **i_d;
142         char **info_dirs;
143 
144         info_dirs = ss_info(sci_idx)->info_dirs;
145         for (i_d = info_dirs; *i_d; i_d++) {
146             if (!strcmp(*i_d, info_dir)) {
147                 while (*i_d) {
148                     *i_d = *(i_d+1);
149                     i_d++;
150                 }
151                 *code_ptr = 0;
152                 return;
153             }
154         }
155         *code_ptr = SS_ET_NO_INFO_DIR;
156     }
157