xref: /illumos-gate/usr/src/lib/krb5/ss/help.c (revision 0b16192f)
17c478bd9Sstevel@tonic-gate /*
2*56a424ccSmp  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright 1987, 1988 by MIT Student Information Processing Board
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * For copyright info, see copyright.h.
107c478bd9Sstevel@tonic-gate  */
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate #include <sys/param.h>
137c478bd9Sstevel@tonic-gate #include <sys/types.h>
14*56a424ccSmp #include <errno.h>
157c478bd9Sstevel@tonic-gate #include <sys/file.h>
167c478bd9Sstevel@tonic-gate #include <fcntl.h>	/* just for O_* */
177c478bd9Sstevel@tonic-gate #include <sys/wait.h>
187c478bd9Sstevel@tonic-gate #include "ss_internal.h"
197c478bd9Sstevel@tonic-gate #include "copyright.h"
207c478bd9Sstevel@tonic-gate #include <libintl.h>
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate extern void ss_list_requests();
237c478bd9Sstevel@tonic-gate 
ss_help(argc,argv,sci_idx,info_ptr)247c478bd9Sstevel@tonic-gate void ss_help (argc, argv, sci_idx, info_ptr)
257c478bd9Sstevel@tonic-gate     int argc;
267c478bd9Sstevel@tonic-gate     char const * const *argv;
277c478bd9Sstevel@tonic-gate     int sci_idx;
287c478bd9Sstevel@tonic-gate     pointer info_ptr;
297c478bd9Sstevel@tonic-gate {
307c478bd9Sstevel@tonic-gate     char buffer[MAXPATHLEN];
317c478bd9Sstevel@tonic-gate     char const *request_name;
327c478bd9Sstevel@tonic-gate     int code;
337c478bd9Sstevel@tonic-gate     int fd, child;
347c478bd9Sstevel@tonic-gate     register int idx;
357c478bd9Sstevel@tonic-gate     register ss_data *info;
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate     request_name = ss_current_request(sci_idx, &code);
387c478bd9Sstevel@tonic-gate     if (code != 0) {
397c478bd9Sstevel@tonic-gate 	ss_perror(sci_idx, code, "");
407c478bd9Sstevel@tonic-gate 	return;		/* no ss_abort_line, if invalid invocation */
417c478bd9Sstevel@tonic-gate     }
427c478bd9Sstevel@tonic-gate     if (argc == 1) {
437c478bd9Sstevel@tonic-gate 	ss_list_requests(argc, argv, sci_idx, info_ptr);
447c478bd9Sstevel@tonic-gate 	return;
457c478bd9Sstevel@tonic-gate     }
467c478bd9Sstevel@tonic-gate     else if (argc != 2) {
477c478bd9Sstevel@tonic-gate 	/* should do something better than this */
487c478bd9Sstevel@tonic-gate 	snprintf(buffer, sizeof (buffer), (char *)dgettext(TEXT_DOMAIN,
497c478bd9Sstevel@tonic-gate 		"usage:\n\t%s [topic|command]\nor\t%s\n"),
507c478bd9Sstevel@tonic-gate 		request_name, request_name);
517c478bd9Sstevel@tonic-gate 	ss_perror(sci_idx, 0, buffer);
527c478bd9Sstevel@tonic-gate 	return;
537c478bd9Sstevel@tonic-gate     }
547c478bd9Sstevel@tonic-gate     info = ss_info(sci_idx);
557c478bd9Sstevel@tonic-gate     if (info->info_dirs == (char **)NULL) {
567c478bd9Sstevel@tonic-gate 	ss_perror(sci_idx, SS_ET_NO_INFO_DIR, (char *)NULL);
577c478bd9Sstevel@tonic-gate 	return;
587c478bd9Sstevel@tonic-gate     }
597c478bd9Sstevel@tonic-gate     if (info->info_dirs[0] == (char *)NULL) {
607c478bd9Sstevel@tonic-gate 	ss_perror(sci_idx, SS_ET_NO_INFO_DIR, (char *)NULL);
617c478bd9Sstevel@tonic-gate 	return;
627c478bd9Sstevel@tonic-gate     }
637c478bd9Sstevel@tonic-gate     for (idx = 0; info->info_dirs[idx] != (char *)NULL; idx++) {
64*56a424ccSmp 	(void) strncpy(buffer, info->info_dirs[idx], sizeof(buffer) - 1);
65*56a424ccSmp 	buffer[sizeof(buffer) - 1] = '\0';
66*56a424ccSmp 	(void) strncat(buffer, "/", sizeof(buffer) - 1 - strlen(buffer));
67*56a424ccSmp 	(void) strncat(buffer, argv[1], sizeof(buffer) - 1 - strlen(buffer));
68*56a424ccSmp 	(void) strncat(buffer, ".info", sizeof(buffer) - 1 - strlen(buffer));
697c478bd9Sstevel@tonic-gate 	if ((fd = open(&buffer[0], O_RDONLY)) >= 0) goto got_it;
707c478bd9Sstevel@tonic-gate     }
717c478bd9Sstevel@tonic-gate     if ((fd = open(&buffer[0], O_RDONLY)) < 0) {
727c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];
73*56a424ccSmp 	strncpy(buf, "No info found for ", sizeof(buf) - 1);
74*56a424ccSmp 	buf[sizeof(buf) - 1] = '\0';
75*56a424ccSmp 	strncat(buf, argv[1], sizeof(buf) - 1 - strlen(buf));
767c478bd9Sstevel@tonic-gate 	ss_perror(sci_idx, 0, buf);
777c478bd9Sstevel@tonic-gate 	return;
787c478bd9Sstevel@tonic-gate     }
797c478bd9Sstevel@tonic-gate got_it:
807c478bd9Sstevel@tonic-gate     switch (child = fork()) {
817c478bd9Sstevel@tonic-gate     case -1:
827c478bd9Sstevel@tonic-gate 	ss_perror(sci_idx, errno, "Can't fork for pager");
837c478bd9Sstevel@tonic-gate 	return;
847c478bd9Sstevel@tonic-gate     case 0:
857c478bd9Sstevel@tonic-gate 	(void) dup2(fd, 0); /* put file on stdin */
867c478bd9Sstevel@tonic-gate 	ss_page_stdin();
877c478bd9Sstevel@tonic-gate     default:
887c478bd9Sstevel@tonic-gate 	(void) close(fd); /* what can we do if it fails? */
897c478bd9Sstevel@tonic-gate #ifdef WAIT_USES_INT
907c478bd9Sstevel@tonic-gate 	while (wait((int *)NULL) != child) {
917c478bd9Sstevel@tonic-gate #else
927c478bd9Sstevel@tonic-gate 	while (wait((union wait *)NULL) != child) {
937c478bd9Sstevel@tonic-gate #endif
947c478bd9Sstevel@tonic-gate 	    /* do nothing if wrong pid */
957c478bd9Sstevel@tonic-gate 	};
967c478bd9Sstevel@tonic-gate     }
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate #ifndef USE_DIRENT_H
1007c478bd9Sstevel@tonic-gate #include <sys/dir.h>
1017c478bd9Sstevel@tonic-gate #else
1027c478bd9Sstevel@tonic-gate #include <dirent.h>
1037c478bd9Sstevel@tonic-gate #endif
1047c478bd9Sstevel@tonic-gate 
ss_add_info_dir(sci_idx,info_dir,code_ptr)1057c478bd9Sstevel@tonic-gate void ss_add_info_dir(sci_idx, info_dir, code_ptr)
1067c478bd9Sstevel@tonic-gate     int sci_idx;
1077c478bd9Sstevel@tonic-gate     char *info_dir;
1087c478bd9Sstevel@tonic-gate     int *code_ptr;
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate     register ss_data *info;
1117c478bd9Sstevel@tonic-gate     DIR *d;
1127c478bd9Sstevel@tonic-gate     int n_dirs;
1137c478bd9Sstevel@tonic-gate     register char **dirs;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate     info = ss_info(sci_idx);
1167c478bd9Sstevel@tonic-gate     if (info_dir == NULL && *info_dir) {
1177c478bd9Sstevel@tonic-gate 	*code_ptr = SS_ET_NO_INFO_DIR;
1187c478bd9Sstevel@tonic-gate 	return;
1197c478bd9Sstevel@tonic-gate     }
1207c478bd9Sstevel@tonic-gate     if ((d = opendir(info_dir)) == (DIR *)NULL) {
1217c478bd9Sstevel@tonic-gate 	*code_ptr = errno;
1227c478bd9Sstevel@tonic-gate 	return;
1237c478bd9Sstevel@tonic-gate     }
1247c478bd9Sstevel@tonic-gate     closedir(d);
1257c478bd9Sstevel@tonic-gate     dirs = info->info_dirs;
1267c478bd9Sstevel@tonic-gate     for (n_dirs = 0; dirs[n_dirs] != (char *)NULL; n_dirs++)
1277c478bd9Sstevel@tonic-gate 	;		/* get number of non-NULL dir entries */
1287c478bd9Sstevel@tonic-gate     dirs = (char **)realloc((char *)dirs,
1297c478bd9Sstevel@tonic-gate 			    (unsigned)(n_dirs + 2)*sizeof(char *));
1307c478bd9Sstevel@tonic-gate     if (dirs == (char **)NULL) {
1317c478bd9Sstevel@tonic-gate 	info->info_dirs = (char **)NULL;
1327c478bd9Sstevel@tonic-gate 	*code_ptr = errno;
1337c478bd9Sstevel@tonic-gate 	return;
1347c478bd9Sstevel@tonic-gate     }
1357c478bd9Sstevel@tonic-gate     info->info_dirs = dirs;
1367c478bd9Sstevel@tonic-gate     dirs[n_dirs + 1] = (char *)NULL;
1377c478bd9Sstevel@tonic-gate     dirs[n_dirs] = malloc((unsigned)strlen(info_dir)+1);
1387c478bd9Sstevel@tonic-gate     strcpy(dirs[n_dirs], info_dir);
1397c478bd9Sstevel@tonic-gate     *code_ptr = 0;
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
ss_delete_info_dir(sci_idx,info_dir,code_ptr)1427c478bd9Sstevel@tonic-gate void ss_delete_info_dir(sci_idx, info_dir, code_ptr)
1437c478bd9Sstevel@tonic-gate     int sci_idx;
1447c478bd9Sstevel@tonic-gate     char *info_dir;
1457c478bd9Sstevel@tonic-gate     int *code_ptr;
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate     register char **i_d;
1487c478bd9Sstevel@tonic-gate     register char **info_dirs;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate     info_dirs = ss_info(sci_idx)->info_dirs;
1517c478bd9Sstevel@tonic-gate     for (i_d = info_dirs; *i_d; i_d++) {
1527c478bd9Sstevel@tonic-gate 	if (!strcmp(*i_d, info_dir)) {
1537c478bd9Sstevel@tonic-gate 	    while (*i_d) {
1547c478bd9Sstevel@tonic-gate 		*i_d = *(i_d+1);
1557c478bd9Sstevel@tonic-gate 		i_d++;
1567c478bd9Sstevel@tonic-gate 	    }
1577c478bd9Sstevel@tonic-gate 	    *code_ptr = 0;
1587c478bd9Sstevel@tonic-gate 	    return;
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate     }
1617c478bd9Sstevel@tonic-gate     *code_ptr = SS_ET_NO_INFO_DIR;
1627c478bd9Sstevel@tonic-gate }
163