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