xref: /titanic_41/usr/src/lib/krb5/ss/help.c (revision ba2e4443695ee6a6f420a35cd4fc3d3346d22932)
1 /*
2  * Copyright 2000-2003 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 <sys/file.h>
17 #include <fcntl.h>	/* just for O_* */
18 #include <sys/wait.h>
19 #include "ss_internal.h"
20 #include "copyright.h"
21 #include <libintl.h>
22 #include <errno.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) strcpy(buffer, info->info_dirs[idx]);
67 	(void) strcat(buffer, "/");
68 	(void) strcat(buffer, argv[1]);
69 	(void) strcat(buffer, ".info");
70 	if ((fd = open(&buffer[0], O_RDONLY)) >= 0) goto got_it;
71     }
72     if ((fd = open(&buffer[0], O_RDONLY)) < 0) {
73 	char buf[MAXPATHLEN];
74 	strcpy(buf, "No info found for ");
75 	strcat(buf, argv[1]);
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