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