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