xref: /onnv-gate/usr/src/lib/krb5/ss/help.c (revision 2881:ea6360e7e1c5)
10Sstevel@tonic-gate /*
2*2881Smp153739  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
70Sstevel@tonic-gate 
80Sstevel@tonic-gate /*
90Sstevel@tonic-gate  * Copyright 1987, 1988 by MIT Student Information Processing Board
100Sstevel@tonic-gate  *
110Sstevel@tonic-gate  * For copyright info, see copyright.h.
120Sstevel@tonic-gate  */
130Sstevel@tonic-gate 
140Sstevel@tonic-gate #include <sys/param.h>
150Sstevel@tonic-gate #include <sys/types.h>
16*2881Smp153739 #include <errno.h>
170Sstevel@tonic-gate #include <sys/file.h>
180Sstevel@tonic-gate #include <fcntl.h>	/* just for O_* */
190Sstevel@tonic-gate #include <sys/wait.h>
200Sstevel@tonic-gate #include "ss_internal.h"
210Sstevel@tonic-gate #include "copyright.h"
220Sstevel@tonic-gate #include <libintl.h>
230Sstevel@tonic-gate 
240Sstevel@tonic-gate extern void ss_list_requests();
250Sstevel@tonic-gate 
ss_help(argc,argv,sci_idx,info_ptr)260Sstevel@tonic-gate void ss_help (argc, argv, sci_idx, info_ptr)
270Sstevel@tonic-gate     int argc;
280Sstevel@tonic-gate     char const * const *argv;
290Sstevel@tonic-gate     int sci_idx;
300Sstevel@tonic-gate     pointer info_ptr;
310Sstevel@tonic-gate {
320Sstevel@tonic-gate     char buffer[MAXPATHLEN];
330Sstevel@tonic-gate     char const *request_name;
340Sstevel@tonic-gate     int code;
350Sstevel@tonic-gate     int fd, child;
360Sstevel@tonic-gate     register int idx;
370Sstevel@tonic-gate     register ss_data *info;
380Sstevel@tonic-gate 
390Sstevel@tonic-gate     request_name = ss_current_request(sci_idx, &code);
400Sstevel@tonic-gate     if (code != 0) {
410Sstevel@tonic-gate 	ss_perror(sci_idx, code, "");
420Sstevel@tonic-gate 	return;		/* no ss_abort_line, if invalid invocation */
430Sstevel@tonic-gate     }
440Sstevel@tonic-gate     if (argc == 1) {
450Sstevel@tonic-gate 	ss_list_requests(argc, argv, sci_idx, info_ptr);
460Sstevel@tonic-gate 	return;
470Sstevel@tonic-gate     }
480Sstevel@tonic-gate     else if (argc != 2) {
490Sstevel@tonic-gate 	/* should do something better than this */
500Sstevel@tonic-gate 	snprintf(buffer, sizeof (buffer), (char *)dgettext(TEXT_DOMAIN,
510Sstevel@tonic-gate 		"usage:\n\t%s [topic|command]\nor\t%s\n"),
520Sstevel@tonic-gate 		request_name, request_name);
530Sstevel@tonic-gate 	ss_perror(sci_idx, 0, buffer);
540Sstevel@tonic-gate 	return;
550Sstevel@tonic-gate     }
560Sstevel@tonic-gate     info = ss_info(sci_idx);
570Sstevel@tonic-gate     if (info->info_dirs == (char **)NULL) {
580Sstevel@tonic-gate 	ss_perror(sci_idx, SS_ET_NO_INFO_DIR, (char *)NULL);
590Sstevel@tonic-gate 	return;
600Sstevel@tonic-gate     }
610Sstevel@tonic-gate     if (info->info_dirs[0] == (char *)NULL) {
620Sstevel@tonic-gate 	ss_perror(sci_idx, SS_ET_NO_INFO_DIR, (char *)NULL);
630Sstevel@tonic-gate 	return;
640Sstevel@tonic-gate     }
650Sstevel@tonic-gate     for (idx = 0; info->info_dirs[idx] != (char *)NULL; idx++) {
66*2881Smp153739 	(void) strncpy(buffer, info->info_dirs[idx], sizeof(buffer) - 1);
67*2881Smp153739 	buffer[sizeof(buffer) - 1] = '\0';
68*2881Smp153739 	(void) strncat(buffer, "/", sizeof(buffer) - 1 - strlen(buffer));
69*2881Smp153739 	(void) strncat(buffer, argv[1], sizeof(buffer) - 1 - strlen(buffer));
70*2881Smp153739 	(void) strncat(buffer, ".info", sizeof(buffer) - 1 - strlen(buffer));
710Sstevel@tonic-gate 	if ((fd = open(&buffer[0], O_RDONLY)) >= 0) goto got_it;
720Sstevel@tonic-gate     }
730Sstevel@tonic-gate     if ((fd = open(&buffer[0], O_RDONLY)) < 0) {
740Sstevel@tonic-gate 	char buf[MAXPATHLEN];
75*2881Smp153739 	strncpy(buf, "No info found for ", sizeof(buf) - 1);
76*2881Smp153739 	buf[sizeof(buf) - 1] = '\0';
77*2881Smp153739 	strncat(buf, argv[1], sizeof(buf) - 1 - strlen(buf));
780Sstevel@tonic-gate 	ss_perror(sci_idx, 0, buf);
790Sstevel@tonic-gate 	return;
800Sstevel@tonic-gate     }
810Sstevel@tonic-gate got_it:
820Sstevel@tonic-gate     switch (child = fork()) {
830Sstevel@tonic-gate     case -1:
840Sstevel@tonic-gate 	ss_perror(sci_idx, errno, "Can't fork for pager");
850Sstevel@tonic-gate 	return;
860Sstevel@tonic-gate     case 0:
870Sstevel@tonic-gate 	(void) dup2(fd, 0); /* put file on stdin */
880Sstevel@tonic-gate 	ss_page_stdin();
890Sstevel@tonic-gate     default:
900Sstevel@tonic-gate 	(void) close(fd); /* what can we do if it fails? */
910Sstevel@tonic-gate #ifdef WAIT_USES_INT
920Sstevel@tonic-gate 	while (wait((int *)NULL) != child) {
930Sstevel@tonic-gate #else
940Sstevel@tonic-gate 	while (wait((union wait *)NULL) != child) {
950Sstevel@tonic-gate #endif
960Sstevel@tonic-gate 	    /* do nothing if wrong pid */
970Sstevel@tonic-gate 	};
980Sstevel@tonic-gate     }
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate #ifndef USE_DIRENT_H
1020Sstevel@tonic-gate #include <sys/dir.h>
1030Sstevel@tonic-gate #else
1040Sstevel@tonic-gate #include <dirent.h>
1050Sstevel@tonic-gate #endif
1060Sstevel@tonic-gate 
ss_add_info_dir(sci_idx,info_dir,code_ptr)1070Sstevel@tonic-gate void ss_add_info_dir(sci_idx, info_dir, code_ptr)
1080Sstevel@tonic-gate     int sci_idx;
1090Sstevel@tonic-gate     char *info_dir;
1100Sstevel@tonic-gate     int *code_ptr;
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate     register ss_data *info;
1130Sstevel@tonic-gate     DIR *d;
1140Sstevel@tonic-gate     int n_dirs;
1150Sstevel@tonic-gate     register char **dirs;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate     info = ss_info(sci_idx);
1180Sstevel@tonic-gate     if (info_dir == NULL && *info_dir) {
1190Sstevel@tonic-gate 	*code_ptr = SS_ET_NO_INFO_DIR;
1200Sstevel@tonic-gate 	return;
1210Sstevel@tonic-gate     }
1220Sstevel@tonic-gate     if ((d = opendir(info_dir)) == (DIR *)NULL) {
1230Sstevel@tonic-gate 	*code_ptr = errno;
1240Sstevel@tonic-gate 	return;
1250Sstevel@tonic-gate     }
1260Sstevel@tonic-gate     closedir(d);
1270Sstevel@tonic-gate     dirs = info->info_dirs;
1280Sstevel@tonic-gate     for (n_dirs = 0; dirs[n_dirs] != (char *)NULL; n_dirs++)
1290Sstevel@tonic-gate 	;		/* get number of non-NULL dir entries */
1300Sstevel@tonic-gate     dirs = (char **)realloc((char *)dirs,
1310Sstevel@tonic-gate 			    (unsigned)(n_dirs + 2)*sizeof(char *));
1320Sstevel@tonic-gate     if (dirs == (char **)NULL) {
1330Sstevel@tonic-gate 	info->info_dirs = (char **)NULL;
1340Sstevel@tonic-gate 	*code_ptr = errno;
1350Sstevel@tonic-gate 	return;
1360Sstevel@tonic-gate     }
1370Sstevel@tonic-gate     info->info_dirs = dirs;
1380Sstevel@tonic-gate     dirs[n_dirs + 1] = (char *)NULL;
1390Sstevel@tonic-gate     dirs[n_dirs] = malloc((unsigned)strlen(info_dir)+1);
1400Sstevel@tonic-gate     strcpy(dirs[n_dirs], info_dir);
1410Sstevel@tonic-gate     *code_ptr = 0;
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate 
ss_delete_info_dir(sci_idx,info_dir,code_ptr)1440Sstevel@tonic-gate void ss_delete_info_dir(sci_idx, info_dir, code_ptr)
1450Sstevel@tonic-gate     int sci_idx;
1460Sstevel@tonic-gate     char *info_dir;
1470Sstevel@tonic-gate     int *code_ptr;
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate     register char **i_d;
1500Sstevel@tonic-gate     register char **info_dirs;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate     info_dirs = ss_info(sci_idx)->info_dirs;
1530Sstevel@tonic-gate     for (i_d = info_dirs; *i_d; i_d++) {
1540Sstevel@tonic-gate 	if (!strcmp(*i_d, info_dir)) {
1550Sstevel@tonic-gate 	    while (*i_d) {
1560Sstevel@tonic-gate 		*i_d = *(i_d+1);
1570Sstevel@tonic-gate 		i_d++;
1580Sstevel@tonic-gate 	    }
1590Sstevel@tonic-gate 	    *code_ptr = 0;
1600Sstevel@tonic-gate 	    return;
1610Sstevel@tonic-gate 	}
1620Sstevel@tonic-gate     }
1630Sstevel@tonic-gate     *code_ptr = SS_ET_NO_INFO_DIR;
1640Sstevel@tonic-gate }
165