OSDN Git Service

ChangeLog, dumpe2fs.8.in, dumpe2fs.c, mke2fs.8.in, mke2fs.c, partinfo.c:
[android-x86/external-e2fsprogs.git] / lib / ss / help.c
1 /*
2  * Copyright 1987, 1988 by MIT Student Information Processing Board
3  *
4  * This file may be copied under the terms of the GNU Public License.
5  */
6
7 #ifdef HAVE_UNISTD_H
8 #include <unistd.h>
9 #endif
10 #ifdef HAVE_STDLIB_H
11 #include <stdlib.h>
12 #endif
13 #ifdef HAVE_ERRNO_H
14 #include <errno.h>
15 #else
16 extern int errno;
17 #endif
18 #include <fcntl.h>
19 #include <sys/param.h>
20 #include <sys/types.h>
21 #include <sys/file.h>
22 #ifdef NEED_SYS_FCNTL_H
23 /* just for O_* */
24 #include <sys/fcntl.h>
25 #endif
26 #include <sys/wait.h>
27 #include "ss_internal.h"
28
29 void ss_help (argc, argv, sci_idx, info_ptr)
30     int argc;
31     char const * const *argv;
32     int sci_idx;
33     pointer info_ptr;
34 {
35     char *buffer;
36     char const *request_name;
37     int code;
38     int fd, child;
39     register int idx;
40     register ss_data *info;
41
42     request_name = ss_current_request(sci_idx, &code);
43     if (code != 0) {
44         ss_perror(sci_idx, code, "");
45         return;         /* no ss_abort_line, if invalid invocation */
46     }
47     if (argc == 1) {
48         ss_list_requests(argc, argv, sci_idx, info_ptr);
49         return;
50     }
51     else if (argc != 2) {
52         /* should do something better than this */
53         buffer = malloc(80+2*strlen(request_name));
54         if (!buffer) {
55                 ss_perror(sci_idx, 0,
56                           "couldn't allocate memory to print usage message");
57                 return;
58         }
59         sprintf(buffer, "usage:\n\t%s [topic|command]\nor\t%s\n",
60                 request_name, request_name);
61         ss_perror(sci_idx, 0, buffer);
62         free(buffer);
63         return;
64     }
65     info = ss_info(sci_idx);
66     if (info->info_dirs == (char **)NULL) {
67         ss_perror(sci_idx, SS_ET_NO_INFO_DIR, (char *)NULL);
68         return;
69     }
70     if (info->info_dirs[0] == (char *)NULL) {
71         ss_perror(sci_idx, SS_ET_NO_INFO_DIR, (char *)NULL);
72         return;
73     }
74     for (fd = -1, idx = 0; info->info_dirs[idx] != (char *)NULL; idx++) {
75         buffer = malloc(strlen (info->info_dirs[idx]) + 1 +
76                         strlen (argv[1]) + 6);
77         if (!buffer) {
78             ss_perror(sci_idx, 0,
79                       "couldn't allocate memory for help filename");
80             return;
81         }
82         (void) strcpy(buffer, info->info_dirs[idx]);
83         (void) strcat(buffer, "/");
84         (void) strcat(buffer, argv[1]);
85         (void) strcat(buffer, ".info");
86         fd = open(buffer, O_RDONLY);
87         free(buffer);
88         if (fd >= 0)
89             break;
90     }
91     if (fd < 0) {
92 #define MSG "No info found for "
93         char *buf = malloc(strlen (MSG) + strlen (argv[1]) + 1);
94         strcpy(buf, MSG);
95         strcat(buf, argv[1]);
96         ss_perror(sci_idx, 0, buf);
97         free(buf);
98         return;
99     }
100     switch (child = fork()) {
101     case -1:
102         ss_perror(sci_idx, errno, "Can't fork for pager");
103         return;
104     case 0:
105         (void) dup2(fd, 0); /* put file on stdin */
106         ss_page_stdin();
107     default:
108         (void) close(fd); /* what can we do if it fails? */
109         while (wait(0) != child) {
110             /* do nothing if wrong pid */
111         };
112     }
113 }
114
115 #ifndef HAVE_DIRENT_H
116 #include <sys/dir.h>
117 #else
118 #include <dirent.h>
119 #endif
120
121 void ss_add_info_dir(sci_idx, info_dir, code_ptr)
122     int sci_idx;
123     char *info_dir;
124     int *code_ptr;
125 {
126     register ss_data *info;
127     DIR *d;
128     int n_dirs;
129     register char **dirs;
130
131     info = ss_info(sci_idx);
132     if (info_dir == NULL && *info_dir) {
133         *code_ptr = SS_ET_NO_INFO_DIR;
134         return;
135     }
136     if ((d = opendir(info_dir)) == (DIR *)NULL) {
137         *code_ptr = errno;
138         return;
139     }
140     closedir(d);
141     dirs = info->info_dirs;
142     for (n_dirs = 0; dirs[n_dirs] != (char *)NULL; n_dirs++)
143         ;               /* get number of non-NULL dir entries */
144     dirs = (char **)realloc((char *)dirs,
145                             (unsigned)(n_dirs + 2)*sizeof(char *));
146     if (dirs == (char **)NULL) {
147         info->info_dirs = (char **)NULL;
148         *code_ptr = errno;
149         return;
150     }
151     info->info_dirs = dirs;
152     dirs[n_dirs + 1] = (char *)NULL;
153     dirs[n_dirs] = malloc((unsigned)strlen(info_dir)+1);
154     strcpy(dirs[n_dirs], info_dir);
155     *code_ptr = 0;
156 }
157
158 void ss_delete_info_dir(sci_idx, info_dir, code_ptr)
159     int sci_idx;
160     char *info_dir;
161     int *code_ptr;
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 }