* Copyright (c) 1987 Regents of the University of California.
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
"@(#) Copyright (c) 1987 Regents of the University of California.\n\
static char sccsid
[] = "@(#)apropos.c 5.6 (Berkeley) %G%";
#define DEF_PATH "/usr/man:/usr/new/man:/usr/local/man"
#define MAXLINELEN 1000 /* max line handled */
#define WHATIS "whatis" /* database name */
#define NO 0 /* no/false */
#define YES 1 /* yes/true */
register char *beg
, *end
, **C
;
int ch
, foundman
= NO
, *found
, isapropos
;
int a_match(), w_match(), (*match
)();
char *manpath
= NULL
, buf
[MAXLINELEN
+ 1], fname
[MAXPATHLEN
+ 1];
char wbuf
[MAXLINELEN
+ 1], *getenv(), *malloc();
myname
= (beg
= rindex(*argv
, '/')) ? beg
+ 1 : *argv
;
if (!strcmp(myname
, "apropos")) {
while ((ch
= getopt(argc
, argv
, "M:P:")) != EOF
)
case 'P': /* backward contemptible */
if (!(manpath
= getenv("MANPATH")))
if (!(found
= (int *)malloc((u_int
)argc
))) {
fprintf(stderr
, "%s: out of space.\n", myname
);
bzero((char *)found
, argc
* sizeof(int));
for (C
= argv
; *C
; ++C
) /* convert to lower-case */
else for (C
= argv
; *C
; ++C
) /* trim full paths */
if (beg
= rindex(*C
, '/'))
for (beg
= manpath
; beg
; beg
= end
) { /* through path list */
(void)sprintf(fname
, "%s/%s", beg
, WHATIS
);
(void)sprintf(fname
, "%.*s/%s", end
- beg
, beg
, WHATIS
);
if (!freopen(fname
, "r", stdin
))
/* for each file found */
for (foundman
= YES
; gets(buf
);) {
if ((*match
)(wbuf
, *C
)) {
/* only print line once */
fprintf(stderr
, "%s: no %s file found in %s.\n", myname
, WHATIS
, manpath
);
printf("%s: %s\n", *C
, isapropos
? "nothing appropriate" : "not found");
* match for apropos; anywhere the string appears
/* backward compatible: everything matches empty string */
for (test
= *str
++, len
= strlen(str
); *bp
;)
if (test
== *bp
++ && !strncmp(bp
, str
, len
))
* match for whatis; looks for full word match
for (len
= strlen(str
);;) {
for (; *bp
&& !isdigit(*bp
) && !isalpha(*bp
); ++bp
);
for (start
= bp
++; *bp
&& (isdigit(*bp
) || isalpha(*bp
)); ++bp
);
if (bp
- start
== len
&& !strncasecmp(start
, str
, len
))
* truncate a string at " - "
register char *from
, *to
;
if (from
[0] == ' ' && from
[1] == '-' && from
[2] == ' ')
} while (*to
++ = *from
++);
* convert a string to lower case
register char *from
, *to
;
*to
++ = isupper(*from
) ? tolower(*from
) : *from
;
* print usage message and die
fprintf(stderr
, "usage: %s [-M path] string ...\n", myname
);