Sun Nov 16 06:26:12 2008 UTC ()
Minor KNF and whitespace nits. No functional change.


(dholland)
diff -r1.45 -r1.46 src/libexec/makewhatis/makewhatis.c

cvs diff -r1.45 -r1.46 src/libexec/makewhatis/makewhatis.c (expand / switch to unified diff)

--- src/libexec/makewhatis/makewhatis.c 2008/11/16 06:17:05 1.45
+++ src/libexec/makewhatis/makewhatis.c 2008/11/16 06:26:12 1.46
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: makewhatis.c,v 1.45 2008/11/16 06:17:05 dholland Exp $ */ 1/* $NetBSD: makewhatis.c,v 1.46 2008/11/16 06:26:12 dholland Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc. 4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matthias Scheler. 8 * by Matthias Scheler.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -27,27 +27,27 @@ @@ -27,27 +27,27 @@
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32#if HAVE_NBTOOL_CONFIG_H 32#if HAVE_NBTOOL_CONFIG_H
33#include "nbtool_config.h" 33#include "nbtool_config.h"
34#endif 34#endif
35 35
36#include <sys/cdefs.h> 36#include <sys/cdefs.h>
37#if !defined(lint) 37#if !defined(lint)
38__COPYRIGHT("@(#) Copyright (c) 1999\ 38__COPYRIGHT("@(#) Copyright (c) 1999\
39 The NetBSD Foundation, Inc. All rights reserved."); 39 The NetBSD Foundation, Inc. All rights reserved.");
40__RCSID("$NetBSD: makewhatis.c,v 1.45 2008/11/16 06:17:05 dholland Exp $"); 40__RCSID("$NetBSD: makewhatis.c,v 1.46 2008/11/16 06:26:12 dholland Exp $");
41#endif /* not lint */ 41#endif /* not lint */
42 42
43#include <sys/types.h> 43#include <sys/types.h>
44#include <sys/param.h> 44#include <sys/param.h>
45#include <sys/queue.h> 45#include <sys/queue.h>
46#include <sys/stat.h> 46#include <sys/stat.h>
47#include <sys/wait.h> 47#include <sys/wait.h>
48 48
49#include <ctype.h> 49#include <ctype.h>
50#include <err.h> 50#include <err.h>
51#include <errno.h> 51#include <errno.h>
52#include <fcntl.h> 52#include <fcntl.h>
53#include <fts.h> 53#include <fts.h>
@@ -61,58 +61,58 @@ __RCSID("$NetBSD: makewhatis.c,v 1.45 20 @@ -61,58 +61,58 @@ __RCSID("$NetBSD: makewhatis.c,v 1.45 20
61#include <unistd.h> 61#include <unistd.h>
62#include <zlib.h> 62#include <zlib.h>
63#include <util.h> 63#include <util.h>
64 64
65#include <man/manconf.h> 65#include <man/manconf.h>
66#include <man/pathnames.h> 66#include <man/pathnames.h>
67 67
68#ifndef NROFF 68#ifndef NROFF
69#define NROFF "nroff" 69#define NROFF "nroff"
70#endif 70#endif
71 71
72typedef struct manpagestruct manpage; 72typedef struct manpagestruct manpage;
73struct manpagestruct { 73struct manpagestruct {
74 manpage *mp_left,*mp_right; 74 manpage *mp_left, *mp_right;
75 ino_t mp_inode; 75 ino_t mp_inode;
76 size_t mp_sdoff; 76 size_t mp_sdoff;
77 size_t mp_sdlen; 77 size_t mp_sdlen;
78 char mp_name[1]; 78 char mp_name[1];
79}; 79};
80 80
81typedef struct whatisstruct whatis; 81typedef struct whatisstruct whatis;
82struct whatisstruct { 82struct whatisstruct {
83 whatis *wi_left,*wi_right; 83 whatis *wi_left, *wi_right;
84 char *wi_data; 84 char *wi_data;
85 char wi_prefix[1]; 85 char wi_prefix[1];
86}; 86};
87 87
88int main(int, char * const *); 88int main(int, char * const *);
89static char *findwhitespace(char *); 89static char *findwhitespace(char *);
90static char *strmove(char *,char *); 90static char *strmove(char *, char *);
91static char *GetS(gzFile, char *, size_t); 91static char *GetS(gzFile, char *, size_t);
92static int pathnamesection(const char *, const char *); 92static int pathnamesection(const char *, const char *);
93static int manpagesection(char *); 93static int manpagesection(char *);
94static char *createsectionstring(char *); 94static char *createsectionstring(char *);
95static void addmanpage(manpage **, ino_t, char *, size_t, size_t); 95static void addmanpage(manpage **, ino_t, char *, size_t, size_t);
96static void addwhatis(whatis **, char *, char *); 96static void addwhatis(whatis **, char *, char *);
97static char *makesection(int); 97static char *makesection(int);
98static char *makewhatisline(const char *, const char *, const char *); 98static char *makewhatisline(const char *, const char *, const char *);
99static void catpreprocess(char *); 99static void catpreprocess(char *);
100static char *parsecatpage(const char *, gzFile *); 100static char *parsecatpage(const char *, gzFile *);
101static int manpreprocess(char *); 101static int manpreprocess(char *);
102static char *nroff(const char *, gzFile *); 102static char *nroff(const char *, gzFile *);
103static char *parsemanpage(const char *, gzFile *, int); 103static char *parsemanpage(const char *, gzFile *, int);
104static char *getwhatisdata(char *); 104static char *getwhatisdata(char *);
105static void processmanpages(manpage **,whatis **); 105static void processmanpages(manpage **, whatis **);
106static void dumpwhatis(FILE *, whatis *); 106static void dumpwhatis(FILE *, whatis *);
107static int makewhatis(char * const *manpath); 107static int makewhatis(char * const *manpath);
108 108
109static char * const default_manpath[] = { 109static char * const default_manpath[] = {
110 "/usr/share/man", 110 "/usr/share/man",
111 NULL 111 NULL
112}; 112};
113 113
114static const char *sectionext = "0123456789ln"; 114static const char *sectionext = "0123456789ln";
115static const char *whatisdb = _PATH_WHATIS; 115static const char *whatisdb = _PATH_WHATIS;
116static const char *whatisdb_new = _PATH_WHATIS ".new"; 116static const char *whatisdb_new = _PATH_WHATIS ".new";
117static int dowarn = 0; 117static int dowarn = 0;
118 118
@@ -150,30 +150,30 @@ main(int argc, char *const *argv) @@ -150,30 +150,30 @@ main(int argc, char *const *argv)
150 dofork = 0; 150 dofork = 0;
151 break; 151 break;
152 case 'w': 152 case 'w':
153 dowarn++; 153 dowarn++;
154 break; 154 break;
155 default: 155 default:
156 fprintf(stderr, "Usage: %s [-fw] [-C file] [manpath ...]\n", 156 fprintf(stderr, "Usage: %s [-fw] [-C file] [manpath ...]\n",
157 getprogname()); 157 getprogname());
158 exit(EXIT_FAILURE); 158 exit(EXIT_FAILURE);
159 } 159 }
160 } 160 }
161 argc -= optind; 161 argc -= optind;
162 argv += optind; 162 argv += optind;
163  163
164 if (argc >= 1) { 164 if (argc >= 1) {
165 manpath = &argv[0]; 165 manpath = &argv[0];
166  166
167 mkwhatis: 167 mkwhatis:
168 return makewhatis(manpath); 168 return makewhatis(manpath);
169 } 169 }
170 170
171 /* 171 /*
172 * Try read config file, fallback to default_manpath[] 172 * Try read config file, fallback to default_manpath[]
173 * if man.conf not available. 173 * if man.conf not available.
174 */ 174 */
175 config(conffile); 175 config(conffile);
176 if ((tp = gettag("_whatdb", 0)) == NULL) { 176 if ((tp = gettag("_whatdb", 0)) == NULL) {
177 manpath = default_manpath; 177 manpath = default_manpath;
178 goto mkwhatis; 178 goto mkwhatis;
179 } 179 }
@@ -212,40 +212,40 @@ main(int argc, char *const *argv) @@ -212,40 +212,40 @@ main(int argc, char *const *argv)
212 212
213 switch (fork()) { 213 switch (fork()) {
214 case 0: 214 case 0:
215 exit(makewhatis(paths)); 215 exit(makewhatis(paths));
216 break; 216 break;
217 case -1: 217 case -1:
218 warn("fork"); 218 warn("fork");
219 makewhatis(paths); 219 makewhatis(paths);
220 break; 220 break;
221 default: 221 default:
222 jobs++; 222 jobs++;
223 break; 223 break;
224 } 224 }
225  225
226 } 226 }
227 227
228 globfree(&pg); 228 globfree(&pg);
229 } 229 }
230 230
231 /* Wait for the childern to finish */ 231 /* Wait for the childern to finish */
232 while (jobs > 0) { 232 while (jobs > 0) {
233 (void)wait(&status); 233 (void)wait(&status);
234 if (!WIFEXITED(status) || WEXITSTATUS(status) != EXIT_SUCCESS) 234 if (!WIFEXITED(status) || WEXITSTATUS(status) != EXIT_SUCCESS)
235 retval = EXIT_FAILURE; 235 retval = EXIT_FAILURE;
236 jobs--; 236 jobs--;
237 } 237 }
238  238
239 return retval; 239 return retval;
240} 240}
241 241
242static int 242static int
243makewhatis(char * const * manpath) 243makewhatis(char * const * manpath)
244{ 244{
245 FTS *fts; 245 FTS *fts;
246 FTSENT *fe; 246 FTSENT *fe;
247 manpage *source; 247 manpage *source;
248 whatis *dest; 248 whatis *dest;
249 FILE *out; 249 FILE *out;
250 size_t sdoff, sdlen; 250 size_t sdoff, sdlen;
251 int outfd; 251 int outfd;
@@ -267,29 +267,31 @@ makewhatis(char * const * manpath) @@ -267,29 +267,31 @@ makewhatis(char * const * manpath)
267 int sl; 267 int sl;
268 const char *s, *lsl; 268 const char *s, *lsl;
269 269
270 lsl = NULL; 270 lsl = NULL;
271 s = &fe->fts_path[fe->fts_pathlen - 1]; 271 s = &fe->fts_path[fe->fts_pathlen - 1];
272 for(sl = fe->fts_level - 1; sl > 0; 272 for(sl = fe->fts_level - 1; sl > 0;
273 sl--) { 273 sl--) {
274 s--; 274 s--;
275 while (s[0] != '/') 275 while (s[0] != '/')
276 s--; 276 s--;
277 if (lsl == NULL) 277 if (lsl == NULL)
278 lsl = s; 278 lsl = s;
279 } 279 }
280  280
281 /* Include trailing '/', so we get 281 /*
282 * 'arch/'. */ 282 * Include trailing '/', so we get
 283 * 'arch/'.
 284 */
283 sdoff = s + 1 - fe->fts_path; 285 sdoff = s + 1 - fe->fts_path;
284 sdlen = lsl - s + 1; 286 sdlen = lsl - s + 1;
285 } else { 287 } else {
286 sdoff = 0; 288 sdoff = 0;
287 sdlen = 0; 289 sdlen = 0;
288 } 290 }
289 291
290 addmanpage(&source, fe->fts_statp->st_ino, 292 addmanpage(&source, fe->fts_statp->st_ino,
291 fe->fts_path, sdoff, sdlen); 293 fe->fts_path, sdoff, sdlen);
292 } 294 }
293 /*FALLTHROUGH*/ 295 /*FALLTHROUGH*/
294 case FTS_D: 296 case FTS_D:
295 case FTS_DC: 297 case FTS_DC:
@@ -408,28 +410,28 @@ makewhatis(char * const * manpath) @@ -408,28 +410,28 @@ makewhatis(char * const * manpath)
408 410
409static char * 411static char *
410findwhitespace(char *str) 412findwhitespace(char *str)
411{ 413{
412 while (!ISSPACE(*str)) 414 while (!ISSPACE(*str))
413 if (*str++ == '\0') { 415 if (*str++ == '\0') {
414 str = NULL; 416 str = NULL;
415 break; 417 break;
416 } 418 }
417 419
418 return str; 420 return str;
419} 421}
420 422
421static char 423static char *
422*strmove(char *dest,char *src) 424strmove(char *dest, char *src)
423{ 425{
424 return memmove(dest, src, strlen(src) + 1); 426 return memmove(dest, src, strlen(src) + 1);
425} 427}
426 428
427static char * 429static char *
428GetS(gzFile in, char *buffer, size_t length) 430GetS(gzFile in, char *buffer, size_t length)
429{ 431{
430 char *ptr; 432 char *ptr;
431 433
432 if (((ptr = gzgets(in, buffer, (int)length)) != NULL) && (*ptr == '\0')) 434 if (((ptr = gzgets(in, buffer, (int)length)) != NULL) && (*ptr == '\0'))
433 ptr = NULL; 435 ptr = NULL;
434 436
435 return ptr; 437 return ptr;
@@ -488,27 +490,27 @@ manpagesection(char *name) @@ -488,27 +490,27 @@ manpagesection(char *name)
488} 490}
489 491
490static char * 492static char *
491createsectionstring(char *section_id) 493createsectionstring(char *section_id)
492{ 494{
493 char *section; 495 char *section;
494 496
495 if (asprintf(&section, " (%s) - ", section_id) < 0) 497 if (asprintf(&section, " (%s) - ", section_id) < 0)
496 err(EXIT_FAILURE, "malloc failed"); 498 err(EXIT_FAILURE, "malloc failed");
497 return section; 499 return section;
498} 500}
499 501
500static void 502static void
501addmanpage(manpage **tree,ino_t inode,char *name, size_t sdoff, size_t sdlen) 503addmanpage(manpage **tree, ino_t inode, char *name, size_t sdoff, size_t sdlen)
502{ 504{
503 manpage *mp; 505 manpage *mp;
504 506
505 while ((mp = *tree) != NULL) { 507 while ((mp = *tree) != NULL) {
506 if (mp->mp_inode == inode) 508 if (mp->mp_inode == inode)
507 return; 509 return;
508 tree = inode < mp->mp_inode ? &mp->mp_left : &mp->mp_right; 510 tree = inode < mp->mp_inode ? &mp->mp_left : &mp->mp_right;
509 } 511 }
510 512
511 mp = emalloc(sizeof(manpage) + strlen(name)); 513 mp = emalloc(sizeof(manpage) + strlen(name));
512 mp->mp_left = NULL; 514 mp->mp_left = NULL;
513 mp->mp_right = NULL; 515 mp->mp_right = NULL;
514 mp->mp_inode = inode; 516 mp->mp_inode = inode;
@@ -1105,27 +1107,27 @@ static void @@ -1105,27 +1107,27 @@ static void
1105processmanpages(manpage **source, whatis **dest) 1107processmanpages(manpage **source, whatis **dest)
1106{ 1108{
1107 manpage *mp; 1109 manpage *mp;
1108 char sd[128]; 1110 char sd[128];
1109 1111
1110 mp = *source; 1112 mp = *source;
1111 *source = NULL; 1113 *source = NULL;
1112 1114
1113 while (mp != NULL) { 1115 while (mp != NULL) {
1114 manpage *obsolete; 1116 manpage *obsolete;
1115 char *data; 1117 char *data;
1116 1118
1117 if (mp->mp_left != NULL) 1119 if (mp->mp_left != NULL)
1118 processmanpages(&mp->mp_left,dest); 1120 processmanpages(&mp->mp_left, dest);
1119 1121
1120 if ((data = getwhatisdata(mp->mp_name)) != NULL) { 1122 if ((data = getwhatisdata(mp->mp_name)) != NULL) {
1121 /* Pass eventual directory prefix to addwhatis() */ 1123 /* Pass eventual directory prefix to addwhatis() */
1122 if (mp->mp_sdlen > 0 && mp->mp_sdlen < sizeof(sd)-1) 1124 if (mp->mp_sdlen > 0 && mp->mp_sdlen < sizeof(sd)-1)
1123 strlcpy(sd, &mp->mp_name[mp->mp_sdoff], 1125 strlcpy(sd, &mp->mp_name[mp->mp_sdoff],
1124 mp->mp_sdlen); 1126 mp->mp_sdlen);
1125 else 1127 else
1126 sd[0] = '\0'; 1128 sd[0] = '\0';
1127 1129
1128 addwhatis(dest, data, sd); 1130 addwhatis(dest, data, sd);
1129 } 1131 }
1130 1132
1131 obsolete = mp; 1133 obsolete = mp;