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