Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[5693c9]: src / manpath.c Maximize Restore History

Download this file

manpath.c    438 lines (375 with data), 9.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
* manpath.c
*
* Copyright (c) 1990, 1991, John W. Eaton.
*
* You may distribute under the terms of the GNU General Public
* License as specified in the file COPYING that comes with the man
* distribution.
*
* John W. Eaton
* jwe@che.utexas.edu
* Department of Chemical Engineering
* The University of Texas at Austin
* Austin, Texas 78712
*
* Changed PATH->manpath algorithm
* Added: an empty string in MANPATH denotes the system path
* Added: use LANG to search in /usr/man/<locale>
* Lots of other minor things, including spoiling the indentation.
* aeb - 940315
*/
#include "compat.h"
#include <stdio.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <string.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
/* if we don't have the strchr() and strrchr() functions,
* then we must fall back to index() and rindex() respectively,
* but (apparently) these aren't always prototyped in string.h
*/
#ifndef index
/* i.e. NOT using strchr()
*/
extern char *index (const char *, int);
#endif
#ifndef rindex
/* i.e. NOT using strrchr()
*/
extern char *rindex (const char *, int);
#endif
#include "defs.h"
#include "gripes.h"
#include "man.h" /* for debug */
#include "man-config.h" /* for cfdirlist */
#include "man-getopt.h" /* for alt_system, opt_manpath */
#include "manpath.h"
#include "util.h" /* my_malloc, my_strdup */
char **mandirlist;
static int mandirlistlth = 0;
static int mandirlistmax = 0;
/*
* Input: a string, with PATH_SEPARATOR_CHAR as separator
* For each entry in the string, call fn.
*/
static void
split (char *string, void (*fn)(char *, int), int perrs) {
char *p, *q, *r;
if (string) {
p = my_strdup (POSIX_STYLE_PATH (string));
for (q = p; ; ) {
if ((r = index(q, PATH_SEPARATOR_CHAR)) == (char*)(0))
r=index(q, '\01');
if (r) {
*r = 0;
fn (q, perrs);
q = r+1;
} else {
fn (q, perrs);
break;
}
}
free (p);
}
}
static void
split2 (char *s, char *string, void (*fn)(char *, char *, int), int perrs) {
char *p, *q, *r;
if (string) {
p = my_strdup (POSIX_STYLE_PATH (string));
for (q = p; ; ) {
r = index(q, PATH_SEPARATOR_CHAR);
if (r) {
*r = 0;
fn (s, q, perrs);
q = r+1;
} else {
fn (s, q, perrs);
break;
}
}
free (p);
}
}
/*
* Is path a directory?
* -1: error, 0: no, 1: yes.
*/
static int
is_directory (char *path) {
struct stat sb;
if (stat (path, &sb) != 0)
return -1;
return ((sb.st_mode & S_IFDIR) == S_IFDIR);
}
/*
* Check to see if the current directory has man or MAN
* or ../man or ../man1 or ../man8 subdirectories.
*/
static char *
find_man_subdir (char *p) {
int len;
char *t, *sp;
len = strlen (p);
t = my_malloc ((unsigned) len + 20);
memcpy (t, p, len);
strcpy (t + len, "/man");
if (is_directory (t) == 1)
return t;
strcpy (t + len, "/MAN");
if (is_directory (t) == 1)
return t;
/* find parent directory */
t[len] = 0;
if ((sp = rindex (t, '/')) != NULL) {
*sp = 0;
len = sp - t;
} else {
strcpy (t + len, "/..");
len += 3;
}
/* look for the situation with packagedir/bin and packagedir/man */
strcpy (t + len, "/man");
if (is_directory (t) == 1)
return t;
/* look for the situation with pkg/bin and pkg/man1 or pkg/man8 */
/* (looking for all man[1-9] would probably be a waste of stats) */
strcpy (t + len, "/man1");
if (is_directory (t) == 1) {
t[len] = 0;
return t;
}
strcpy (t + len, "/man8");
if (is_directory (t) == 1) {
t[len] = 0;
return t;
}
free (t);
return NULL;
}
/*
* Add a directory to the manpath list if it isn't already there.
*/
static void
add_to_list (char *dir, char *lang, int perrs) {
int status;
char cwd[BUFSIZ];
char **dp;
if (!lang)
lang = "";
/* only add absolute paths */
if (! IS_ABSOLUTE_PATH (dir)) {
if (!getcwd(cwd, sizeof(cwd)))
return; /* cwd not readable, or pathname very long */
if (! IS_ABSOLUTE_PATH (cwd))
return; /* strange.. */
if (strlen(dir) + strlen(lang) + strlen(cwd) + 3 > sizeof(cwd))
return;
if (!strncmp (dir, "./", 2))
dir += 2;
while (!strncmp (dir, "../", 3)) {
char *p = rindex (cwd, '/');
if (p > cwd)
*p = 0;
else
cwd[1] = 0;
dir += 3;
}
strcat (cwd, "/");
strcat (cwd, dir);
if (*lang) {
strcat (cwd, "/");
strcat (cwd, lang);
}
dir = cwd;
} else if (*lang) {
if (strlen(dir) + strlen(lang) + 2 > sizeof(cwd))
return;
strcpy (cwd, dir);
strcat (cwd, "/");
strcat (cwd, lang);
dir = cwd;
}
if (mandirlist) {
for (dp = mandirlist; *dp; dp++) {
if (!strcmp (*dp, dir))
return;
}
}
/*
* Avoid trickery: no /../ in path.
*/
if (strstr(dir, "/../"))
return;
/*
* Not found -- add it.
*/
status = is_directory(dir);
if (status < 0 && perrs) {
gripe (CANNOT_STAT, dir);
} else if (status == 0 && perrs) {
gripe (IS_NO_DIR, dir);
} else if (status == 1) {
if (debug)
gripe (ADDING_TO_MANPATH, dir);
if (!mandirlist || mandirlistlth+1 >= mandirlistmax) {
int i, ct = mandirlistmax + 100;
char **p = (char **) my_malloc(ct * sizeof(char *));
if (mandirlist) {
for (i=0; i<mandirlistlth; i++)
p[i] = mandirlist[i];
free(mandirlist);
}
mandirlistmax = ct;
mandirlist = p;
}
mandirlist[mandirlistlth++] = my_strdup (dir);
mandirlist[mandirlistlth] = 0;
}
}
static void
add_to_mandirlist_x (char *dir, char *lang, int perrs) {
add_to_list(dir, lang, perrs);
if (lang && strlen(lang) > 5 && lang[5] == '.') {
char lang2[6]; /* e.g. zh_CN from zh_CN.GB2312 */
strncpy(lang2,lang,5);
lang2[5] = 0;
add_to_list(dir, lang2, perrs);
}
if (lang && strlen(lang) > 2) {
char lang2[3];
strncpy(lang2,lang,2);
lang2[2] = 0;
add_to_list(dir, lang2, perrs);
}
}
static void
add_to_mandirlist (char *dir, int perrs) {
char *lang;
if (alt_system) {
add_to_list(dir, alt_system_name, perrs);
} else {
/* We cannot use "lang = setlocale(LC_MESSAGES, NULL)" or so:
the return value of setlocale is an opaque string. */
/* POSIX prescribes the order: LC_ALL, LC_MESSAGES, LANG */
if((lang = getenv("LC_ALL")) != NULL)
split2(dir, lang, add_to_mandirlist_x, perrs);
if((lang = getenv("LC_MESSAGES")) != NULL)
split2(dir, lang, add_to_mandirlist_x, perrs);
if((lang = getenv("LANG")) != NULL)
split2(dir, lang, add_to_mandirlist_x, perrs);
if((lang = getenv("LANGUAGE")) != NULL)
split2(dir, lang, add_to_mandirlist_x, perrs);
add_to_mandirlist_x(dir, 0, perrs);
}
}
/*
* For each directory in the user's path, see if it is one of the
* directories listed in the man.conf file. If so, and it is
* not already in the manpath, add it. If the directory is not listed
* in the man.conf file, see if there is a subdirectory `man' or
* `MAN'. If so, and it is not already in the manpath, add it.
*
* Example: user has <dir>/bin in his path and the directory
* <dir>/bin/man exists -- the directory <dir>/bin/man will be added
* to the manpath.
* Try also <dir>/man ?and <dir>?, and, if LANG is set, <dir>/$LANG/man.
* aeb - 940320
*/
static void
get_manpath_from_pathdir (char *dir, int perrs) {
char *t;
struct dirs *dlp;
if (debug)
gripe (PATH_DIR, dir);
/*
* The directory we're working on is in the config file.
* If we haven't added it to the list yet, do.
*/
if (*dir) {
for (dlp = cfdirlist.nxt; dlp; dlp = dlp->nxt) {
if (!strcmp (dir, dlp->bindir)) {
if (debug)
gripe (IS_IN_CONFIG);
add_to_mandirlist (dlp->mandir, perrs);
return;
}
}
}
if (!noautopath) {
/*
* The directory we're working on isn't in the config file.
* See if it has man or MAN subdirectories. If so, and this
* subdirectory hasn't been added to the list, do. (Try also
* a few other places nearby.)
*/
if (debug)
gripe (IS_NOT_IN_CONFIG);
t = find_man_subdir (dir);
if (t != NULL) {
if (debug)
gripe (MAN_NEARBY);
add_to_mandirlist (t, perrs);
free (t);
} else {
if (debug)
gripe (NO_MAN_NEARBY);
}
}
}
static void
add_default_manpath (int perrs) {
struct dirs *dlp;
if (debug)
gripe (ADDING_MANDIRS);
for (dlp = cfdirlist.nxt; dlp; dlp = dlp->nxt)
if (dlp->mandatory)
add_to_mandirlist (dlp->mandir, perrs);
}
static void
to_mandirlist(char *s, int perrs) {
char *path;
if (*s) {
add_to_mandirlist (s, perrs);
} else {
/* empty substring: insert default path */
if((path = getenv ("PATH")) != NULL)
split (path, get_manpath_from_pathdir, perrs);
add_default_manpath (perrs);
}
}
void
init_manpath () {
static int done = 0;
if (!done) {
char *manp;
if ((manp = opt_manpath) == NULL &&
(manp = getenv ("manpath")) == NULL &&
(manp = getenv ("MANPATH")) == NULL)
manp = ""; /* default path */
split (manp, to_mandirlist, 0);
done = 1;
}
}
void
prmanpath () {
char **dp, **dp0;
if (mandirlist) {
for (dp0 = dp = mandirlist; *dp; dp++) {
if (dp != dp0)
putchar (PATH_SEPARATOR_CHAR);
printf("%s", *dp);
}
}
printf("\n");
}