Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1987, 1988 by MIT Student Information Processing Board |
| 3 | * |
Theodore Ts'o | 06cefee | 1999-10-23 01:16:22 +0000 | [diff] [blame] | 4 | * Permission to use, copy, modify, and distribute this software and |
| 5 | * its documentation for any purpose is hereby granted, provided that |
| 6 | * the names of M.I.T. and the M.I.T. S.I.P.B. not be used in |
| 7 | * advertising or publicity pertaining to distribution of the software |
| 8 | * without specific, written prior permission. M.I.T. and the |
| 9 | * M.I.T. S.I.P.B. make no representations about the suitability of |
| 10 | * this software for any purpose. It is provided "as is" without |
| 11 | * express or implied warranty. |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 14 | #ifdef HAVE_UNISTD_H |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 15 | #include <unistd.h> |
| 16 | #endif |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 17 | #ifdef HAVE_STDLIB_H |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 18 | #include <stdlib.h> |
| 19 | #endif |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 20 | #ifdef HAVE_ERRNO_H |
| 21 | #include <errno.h> |
| 22 | #else |
| 23 | extern int errno; |
| 24 | #endif |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 25 | #include <fcntl.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 26 | #include <sys/param.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/file.h> |
| 29 | #ifdef NEED_SYS_FCNTL_H |
| 30 | /* just for O_* */ |
| 31 | #include <sys/fcntl.h> |
| 32 | #endif |
Theodore Ts'o | fff4548 | 2003-04-13 00:44:19 -0400 | [diff] [blame] | 33 | #ifdef HAVE_SYS_WAIT_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 34 | #include <sys/wait.h> |
Theodore Ts'o | fff4548 | 2003-04-13 00:44:19 -0400 | [diff] [blame] | 35 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 36 | #include "ss_internal.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 37 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 38 | void ss_help(int argc, char const * const *argv, int sci_idx, pointer info_ptr) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 39 | { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 40 | char *buffer; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 41 | char const *request_name; |
| 42 | int code; |
| 43 | int fd, child; |
| 44 | register int idx; |
| 45 | register ss_data *info; |
| 46 | |
| 47 | request_name = ss_current_request(sci_idx, &code); |
| 48 | if (code != 0) { |
| 49 | ss_perror(sci_idx, code, ""); |
| 50 | return; /* no ss_abort_line, if invalid invocation */ |
| 51 | } |
| 52 | if (argc == 1) { |
| 53 | ss_list_requests(argc, argv, sci_idx, info_ptr); |
| 54 | return; |
| 55 | } |
| 56 | else if (argc != 2) { |
| 57 | /* should do something better than this */ |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 58 | buffer = malloc(80+2*strlen(request_name)); |
| 59 | if (!buffer) { |
| 60 | ss_perror(sci_idx, 0, |
| 61 | "couldn't allocate memory to print usage message"); |
| 62 | return; |
| 63 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 64 | sprintf(buffer, "usage:\n\t%s [topic|command]\nor\t%s\n", |
| 65 | request_name, request_name); |
| 66 | ss_perror(sci_idx, 0, buffer); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 67 | free(buffer); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 68 | return; |
| 69 | } |
| 70 | info = ss_info(sci_idx); |
| 71 | if (info->info_dirs == (char **)NULL) { |
| 72 | ss_perror(sci_idx, SS_ET_NO_INFO_DIR, (char *)NULL); |
| 73 | return; |
| 74 | } |
| 75 | if (info->info_dirs[0] == (char *)NULL) { |
| 76 | ss_perror(sci_idx, SS_ET_NO_INFO_DIR, (char *)NULL); |
| 77 | return; |
| 78 | } |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 79 | for (fd = -1, idx = 0; info->info_dirs[idx] != (char *)NULL; idx++) { |
| 80 | buffer = malloc(strlen (info->info_dirs[idx]) + 1 + |
| 81 | strlen (argv[1]) + 6); |
| 82 | if (!buffer) { |
| 83 | ss_perror(sci_idx, 0, |
| 84 | "couldn't allocate memory for help filename"); |
| 85 | return; |
| 86 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 87 | (void) strcpy(buffer, info->info_dirs[idx]); |
| 88 | (void) strcat(buffer, "/"); |
| 89 | (void) strcat(buffer, argv[1]); |
| 90 | (void) strcat(buffer, ".info"); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 91 | fd = open(buffer, O_RDONLY); |
| 92 | free(buffer); |
| 93 | if (fd >= 0) |
| 94 | break; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 95 | } |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 96 | if (fd < 0) { |
| 97 | #define MSG "No info found for " |
| 98 | char *buf = malloc(strlen (MSG) + strlen (argv[1]) + 1); |
| 99 | strcpy(buf, MSG); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 100 | strcat(buf, argv[1]); |
| 101 | ss_perror(sci_idx, 0, buf); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 102 | free(buf); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 103 | return; |
| 104 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 105 | switch (child = fork()) { |
| 106 | case -1: |
| 107 | ss_perror(sci_idx, errno, "Can't fork for pager"); |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 108 | (void) close(fd); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 109 | return; |
| 110 | case 0: |
| 111 | (void) dup2(fd, 0); /* put file on stdin */ |
| 112 | ss_page_stdin(); |
| 113 | default: |
| 114 | (void) close(fd); /* what can we do if it fails? */ |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 115 | while (wait(NULL) != child) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 116 | /* do nothing if wrong pid */ |
| 117 | }; |
| 118 | } |
| 119 | } |
| 120 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 121 | #ifndef HAVE_DIRENT_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 122 | #include <sys/dir.h> |
| 123 | #else |
| 124 | #include <dirent.h> |
| 125 | #endif |
| 126 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 127 | void ss_add_info_dir(int sci_idx, char *info_dir, int *code_ptr) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 128 | { |
| 129 | register ss_data *info; |
| 130 | DIR *d; |
| 131 | int n_dirs; |
| 132 | register char **dirs; |
| 133 | |
| 134 | info = ss_info(sci_idx); |
Brian Behlendorf | d2021de | 2007-03-19 08:39:32 -0400 | [diff] [blame] | 135 | if (info_dir == NULL || *info_dir == '\0') { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 136 | *code_ptr = SS_ET_NO_INFO_DIR; |
| 137 | return; |
| 138 | } |
| 139 | if ((d = opendir(info_dir)) == (DIR *)NULL) { |
| 140 | *code_ptr = errno; |
| 141 | return; |
| 142 | } |
| 143 | closedir(d); |
| 144 | dirs = info->info_dirs; |
| 145 | for (n_dirs = 0; dirs[n_dirs] != (char *)NULL; n_dirs++) |
| 146 | ; /* get number of non-NULL dir entries */ |
| 147 | dirs = (char **)realloc((char *)dirs, |
| 148 | (unsigned)(n_dirs + 2)*sizeof(char *)); |
| 149 | if (dirs == (char **)NULL) { |
| 150 | info->info_dirs = (char **)NULL; |
| 151 | *code_ptr = errno; |
| 152 | return; |
| 153 | } |
| 154 | info->info_dirs = dirs; |
| 155 | dirs[n_dirs + 1] = (char *)NULL; |
| 156 | dirs[n_dirs] = malloc((unsigned)strlen(info_dir)+1); |
| 157 | strcpy(dirs[n_dirs], info_dir); |
| 158 | *code_ptr = 0; |
| 159 | } |
| 160 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 161 | void ss_delete_info_dir(int sci_idx, char *info_dir, int *code_ptr) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 162 | { |
| 163 | register char **i_d; |
| 164 | register char **info_dirs; |
| 165 | |
| 166 | info_dirs = ss_info(sci_idx)->info_dirs; |
| 167 | for (i_d = info_dirs; *i_d; i_d++) { |
| 168 | if (!strcmp(*i_d, info_dir)) { |
| 169 | while (*i_d) { |
| 170 | *i_d = *(i_d+1); |
| 171 | i_d++; |
| 172 | } |
| 173 | *code_ptr = 0; |
| 174 | return; |
| 175 | } |
| 176 | } |
| 177 | *code_ptr = SS_ET_NO_INFO_DIR; |
| 178 | } |