summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorume <ume@FreeBSD.org>2010-04-04 08:38:14 +0000
committerume <ume@FreeBSD.org>2010-04-04 08:38:14 +0000
commit86fb8e4f3fda90b91747e919033547769a68982f (patch)
treefa06efce38857376db13eac6acb102f93138186b
parent7915a506541b9b6b29c31dec9643b7da9649252c (diff)
downloadFreeBSD-src-86fb8e4f3fda90b91747e919033547769a68982f.zip
FreeBSD-src-86fb8e4f3fda90b91747e919033547769a68982f.tar.gz
services_mkdb; generate db file from services(5) to increase
speed of getserv*() Obtained from: NetBSD MFC after: 2 weeks
-rw-r--r--usr.sbin/Makefile1
-rw-r--r--usr.sbin/services_mkdb/Makefile10
-rw-r--r--usr.sbin/services_mkdb/services_mkdb.897
-rw-r--r--usr.sbin/services_mkdb/services_mkdb.c428
-rw-r--r--usr.sbin/services_mkdb/uniq.c159
5 files changed, 695 insertions, 0 deletions
diff --git a/usr.sbin/Makefile b/usr.sbin/Makefile
index 5df065f..c47ef76 100644
--- a/usr.sbin/Makefile
+++ b/usr.sbin/Makefile
@@ -167,6 +167,7 @@ SUBDIR= ${_ac} \
${_sade} \
${_sendmail} \
service \
+ services_mkdb \
setfib \
setfmac \
setpmac \
diff --git a/usr.sbin/services_mkdb/Makefile b/usr.sbin/services_mkdb/Makefile
new file mode 100644
index 0000000..659cdb8
--- /dev/null
+++ b/usr.sbin/services_mkdb/Makefile
@@ -0,0 +1,10 @@
+# $FreeBSD$
+
+PROG= services_mkdb
+MAN= services_mkdb.8
+SRCS= services_mkdb.c uniq.c
+
+DPADD+= ${LIBUTIL}
+LDADD+= -lutil
+
+.include <bsd.prog.mk>
diff --git a/usr.sbin/services_mkdb/services_mkdb.8 b/usr.sbin/services_mkdb/services_mkdb.8
new file mode 100644
index 0000000..6468fd2
--- /dev/null
+++ b/usr.sbin/services_mkdb/services_mkdb.8
@@ -0,0 +1,97 @@
+.\" $NetBSD: services_mkdb.8,v 1.9 2009/05/13 22:36:39 wiz Exp $
+.\"
+.\" Copyright (c) 1999 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Luke Mewburn.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\" POSSIBILITY OF SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd April 4, 2010
+.Dt SERVICES_MKDB 8
+.Os
+.Sh NAME
+.Nm services_mkdb
+.Nd generate the services databases
+.Sh SYNOPSIS
+.Nm
+.Op Fl q
+.Op Fl o Ar database
+.Op file
+.Nm
+.Fl u
+.Op file
+.Sh DESCRIPTION
+.Nm
+creates a
+.Xr db 3
+database for the specified file.
+If no file is specified, then
+.Pa /etc/services
+is used.
+The database is installed into
+.Pa /var/db/services.db .
+The file must be in the correct format (see
+.Xr services 5 ) .
+.Pp
+The options are as follows:
+.Bl -tag -width indent
+.It Fl o Ar database
+Put the output databases in the named file.
+.It Fl q
+Don't warn about duplicate services.
+.It Fl u
+Print the services file to stdout, omitting duplicate entries and comments.
+.El
+.Pp
+The databases are used by the C library services routines (see
+.Xr getservent 3 ) .
+.Pp
+.Nm
+exits zero on success, non-zero on failure.
+.Sh FILES
+.Bl -tag -width 24n -compact
+.It Pa /var/db/services.db
+The current services database.
+.It Pa /var/db/services.db.tmp
+A temporary file.
+.It Pa /etc/services
+The current services file.
+.El
+.Sh SEE ALSO
+.Xr db 3 ,
+.Xr getservent 3 ,
+.Xr services 5
+.Sh BUGS
+Because
+.Nm
+guarantees not to install a partial destination file it must
+build a temporary file in the same file system and if successful use
+.Xr rename 2
+to install over the destination file.
+.Pp
+If
+.Nm
+fails it will leave the previous version of the destination file intact.
diff --git a/usr.sbin/services_mkdb/services_mkdb.c b/usr.sbin/services_mkdb/services_mkdb.c
new file mode 100644
index 0000000..badea7c
--- /dev/null
+++ b/usr.sbin/services_mkdb/services_mkdb.c
@@ -0,0 +1,428 @@
+/* $NetBSD: services_mkdb.c,v 1.14 2008/04/28 20:24:17 martin Exp $ */
+
+/*-
+ * Copyright (c) 1999 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Luke Mewburn and Christos Zoulas.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/stat.h>
+
+#include <assert.h>
+#include <db.h>
+#include <err.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <libutil.h>
+#include <ctype.h>
+#include <errno.h>
+#include <stringlist.h>
+
+static char tname[MAXPATHLEN];
+
+#define PMASK 0xffff
+#define PROTOMAX 5
+
+extern void uniq(const char *);
+
+static void add(DB *, StringList *, size_t, const char *, size_t *, int);
+static StringList ***parseservices(const char *, StringList *);
+static void cleanup(void);
+static void store(DB *, DBT *, DBT *, int);
+static void killproto(DBT *);
+static char *getstring(const char *, size_t, char **, const char *);
+static size_t getprotoindex(StringList *, const char *);
+static const char *getprotostr(StringList *, size_t);
+static const char *mkaliases(StringList *, char *, size_t);
+static void usage(void);
+
+const HASHINFO hinfo = {
+ .bsize = 256,
+ .ffactor = 4,
+ .nelem = 32768,
+ .cachesize = 1024,
+ .hash = NULL,
+ .lorder = 0
+};
+
+
+int
+main(int argc, char *argv[])
+{
+ DB *db;
+ int ch;
+ const char *fname = _PATH_SERVICES;
+ const char *dbname = _PATH_SERVICES_DB;
+ int warndup = 1;
+ int unique = 0;
+ int otherflag = 0;
+ size_t cnt = 0;
+ StringList *sl, ***svc;
+ size_t port, proto;
+
+ setprogname(argv[0]);
+
+ while ((ch = getopt(argc, argv, "qo:u")) != -1)
+ switch (ch) {
+ case 'q':
+ otherflag = 1;
+ warndup = 0;
+ break;
+ case 'o':
+ otherflag = 1;
+ dbname = optarg;
+ break;
+ case 'u':
+ unique++;
+ break;
+ case '?':
+ default:
+ usage();
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if (argc > 1 || (unique && otherflag))
+ usage();
+ if (argc == 1)
+ fname = argv[0];
+
+ if (unique)
+ uniq(fname);
+
+ svc = parseservices(fname, sl = sl_init());
+
+ if (atexit(cleanup))
+ err(1, "Cannot install exit handler");
+
+ (void)snprintf(tname, sizeof(tname), "%s.tmp", dbname);
+ db = dbopen(tname, O_RDWR | O_CREAT | O_EXCL,
+ (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), DB_HASH, &hinfo);
+ if (!db)
+ err(1, "Error opening temporary database `%s'", tname);
+
+
+ for (port = 0; port < PMASK + 1; port++) {
+ if (svc[port] == NULL)
+ continue;
+
+ for (proto = 0; proto < PROTOMAX; proto++) {
+ StringList *s;
+ if ((s = svc[port][proto]) == NULL)
+ continue;
+ add(db, s, port, getprotostr(sl, proto), &cnt, warndup);
+ }
+
+ free(svc[port]);
+ }
+
+ free(svc);
+ sl_free(sl, 1);
+
+ if ((db->close)(db))
+ err(1, "Error closing temporary database `%s'", tname);
+
+ if (rename(tname, dbname) == -1)
+ err(1, "Cannot rename `%s' to `%s'", tname, dbname);
+
+ return 0;
+}
+
+static void
+add(DB *db, StringList *sl, size_t port, const char *proto, size_t *cnt,
+ int warndup)
+{
+ size_t i;
+ char keyb[BUFSIZ], datab[BUFSIZ], abuf[BUFSIZ];
+ DBT data, key;
+ key.data = keyb;
+ data.data = datab;
+
+#ifdef DEBUG
+ (void)printf("add %s %zu %s [ ", sl->sl_str[0], port, proto);
+ for (i = 1; i < sl->sl_cur; i++)
+ (void)printf("%s ", sl->sl_str[i]);
+ (void)printf("]\n");
+#endif
+
+ /* key `indirect key', data `full line' */
+ data.size = snprintf(datab, sizeof(datab), "%zu", (*cnt)++) + 1;
+ key.size = snprintf(keyb, sizeof(keyb), "%s %zu/%s %s",
+ sl->sl_str[0], port, proto, mkaliases(sl, abuf, sizeof(abuf))) + 1;
+ store(db, &data, &key, warndup);
+
+ /* key `\377port/proto', data = `indirect key' */
+ key.size = snprintf(keyb, sizeof(keyb), "\377%zu/%s",
+ port, proto) + 1;
+ store(db, &key, &data, warndup);
+
+ /* key `\377port', data = `indirect key' */
+ killproto(&key);
+ store(db, &key, &data, warndup);
+
+ /* add references for service and all aliases */
+ for (i = 0; i < sl->sl_cur; i++) {
+ /* key `\376service/proto', data = `indirect key' */
+ key.size = snprintf(keyb, sizeof(keyb), "\376%s/%s",
+ sl->sl_str[i], proto) + 1;
+ store(db, &key, &data, warndup);
+
+ /* key `\376service', data = `indirect key' */
+ killproto(&key);
+ store(db, &key, &data, warndup);
+ }
+ sl_free(sl, 1);
+}
+
+static StringList ***
+parseservices(const char *fname, StringList *sl)
+{
+ size_t len, line, pindex;
+ FILE *fp;
+ StringList ***svc, *s;
+ char *p, *ep;
+
+ if ((fp = fopen(fname, "r")) == NULL)
+ err(1, "Cannot open `%s'", fname);
+
+ line = 0;
+ if ((svc = calloc(PMASK + 1, sizeof(StringList **))) == NULL)
+ err(1, "Cannot allocate %zu bytes", PMASK + 1);
+
+ /* XXX: change NULL to "\0\0#" when fparseln fixed */
+ for (; (p = fparseln(fp, &len, &line, NULL, 0)) != NULL; free(p)) {
+ char *name, *port, *proto, *aliases, *cp, *alias;
+ unsigned long pnum;
+
+ if (len == 0)
+ continue;
+
+ for (cp = p; *cp && isspace((unsigned char)*cp); cp++)
+ continue;
+
+ if (*cp == '\0' || *cp == '#')
+ continue;
+
+ if ((name = getstring(fname, line, &cp, "name")) == NULL)
+ continue;
+
+ if ((port = getstring(fname, line, &cp, "port")) == NULL)
+ continue;
+
+ if (cp) {
+ for (aliases = cp; *cp && *cp != '#'; cp++)
+ continue;
+
+ if (*cp)
+ *cp = '\0';
+ } else
+ aliases = NULL;
+
+ proto = strchr(port, '/');
+ if (proto == NULL || proto[1] == '\0') {
+ warnx("%s, %zu: no protocol found", fname, line);
+ continue;
+ }
+ *proto++ = '\0';
+
+ errno = 0;
+ pnum = strtoul(port, &ep, 0);
+ if (*port == '\0' || *ep != '\0') {
+ warnx("%s, %zu: invalid port `%s'", fname, line, port);
+ continue;
+ }
+ if ((errno == ERANGE && pnum == ULONG_MAX) || pnum > PMASK) {
+ warnx("%s, %zu: port too big `%s'", fname, line, port);
+ continue;
+ }
+
+ if (svc[pnum] == NULL) {
+ svc[pnum] = calloc(PROTOMAX, sizeof(StringList *));
+ if (svc[pnum] == NULL)
+ err(1, "Cannot allocate %zu bytes", PROTOMAX);
+ }
+
+ pindex = getprotoindex(sl, proto);
+ if (svc[pnum][pindex] == NULL)
+ s = svc[pnum][pindex] = sl_init();
+ else
+ s = svc[pnum][pindex];
+
+ /* build list of aliases */
+ if (sl_find(s, name) == NULL) {
+ char *p;
+
+ if ((p = strdup(name)) == NULL)
+ err(1, "Cannot copy string");
+ (void)sl_add(s, p);
+ }
+
+ if (aliases) {
+ while ((alias = strsep(&aliases, " \t")) != NULL) {
+ if (alias[0] == '\0')
+ continue;
+ if (sl_find(s, alias) == NULL) {
+ char *p;
+
+ if ((p = strdup(alias)) == NULL)
+ err(1, "Cannot copy string");
+ (void)sl_add(s, p);
+ }
+ }
+ }
+ }
+ (void)fclose(fp);
+ return svc;
+}
+
+/*
+ * cleanup(): Remove temporary files upon exit
+ */
+static void
+cleanup(void)
+{
+ if (tname[0])
+ (void)unlink(tname);
+}
+
+static char *
+getstring(const char *fname, size_t line, char **cp, const char *tag)
+{
+ char *str;
+
+ while ((str = strsep(cp, " \t")) != NULL && *str == '\0')
+ continue;
+
+ if (str == NULL)
+ warnx("%s, %zu: no %s found", fname, line, tag);
+
+ return str;
+}
+
+static void
+killproto(DBT *key)
+{
+ char *p, *d = key->data;
+
+ if ((p = strchr(d, '/')) == NULL)
+ abort();
+ *p++ = '\0';
+ key->size = p - d;
+}
+
+static void
+store(DB *db, DBT *key, DBT *data, int warndup)
+{
+#ifdef DEBUG
+ int k = key->size - 1;
+ int d = data->size - 1;
+ (void)printf("store [%*.*s] [%*.*s]\n",
+ k, k, (char *)key->data + 1,
+ d, d, (char *)data->data + 1);
+#endif
+ switch ((db->put)(db, key, data, R_NOOVERWRITE)) {
+ case 0:
+ break;
+ case 1:
+ if (warndup)
+ warnx("duplicate service `%s'",
+ &((char *)key->data)[1]);
+ break;
+ case -1:
+ err(1, "put");
+ break;
+ default:
+ abort();
+ break;
+ }
+}
+
+static size_t
+getprotoindex(StringList *sl, const char *str)
+{
+ size_t i;
+ char *p;
+
+ for (i= 0; i < sl->sl_cur; i++)
+ if (strcmp(sl->sl_str[i], str) == 0)
+ return i;
+
+ if (i == PROTOMAX)
+ errx(1, "Ran out of protocols adding `%s';"
+ " recompile with larger PROTOMAX", str);
+ if ((p = strdup(str)) == NULL)
+ err(1, "Cannot copy string");
+ (void)sl_add(sl, p);
+ return i;
+}
+
+static const char *
+getprotostr(StringList *sl, size_t i)
+{
+ assert(i < sl->sl_cur);
+ return sl->sl_str[i];
+}
+
+static const char *
+mkaliases(StringList *sl, char *buf, size_t len)
+{
+ size_t nc, i, pos;
+
+ buf[0] = 0;
+ for (i = 1, pos = 0; i < sl->sl_cur; i++) {
+ nc = strlcpy(buf + pos, sl->sl_str[i], len);
+ if (nc >= len)
+ goto out;
+ pos += nc;
+ len -= nc;
+ nc = strlcpy(buf + pos, " ", len);
+ if (nc >= len)
+ goto out;
+ pos += nc;
+ len -= nc;
+ }
+ return buf;
+out:
+ warn("aliases for `%s' truncated", sl->sl_str[0]);
+ return buf;
+}
+
+static void
+usage(void)
+{
+ (void)fprintf(stderr, "Usage:\t%s [-q] [-o <db>] [<servicefile>]\n"
+ "\t%s -u [<servicefile>]\n", getprogname(), getprogname());
+ exit(1);
+}
diff --git a/usr.sbin/services_mkdb/uniq.c b/usr.sbin/services_mkdb/uniq.c
new file mode 100644
index 0000000..0674b4b
--- /dev/null
+++ b/usr.sbin/services_mkdb/uniq.c
@@ -0,0 +1,159 @@
+/* $NetBSD: uniq.c,v 1.4 2008/04/28 20:24:17 martin Exp $ */
+
+/*-
+ * Copyright (c) 2007 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <db.h>
+#include <err.h>
+#include <libutil.h>
+#include <ctype.h>
+#include <fcntl.h>
+
+extern const HASHINFO hinfo;
+
+void uniq(const char *);
+static int comp(const char *, char **, size_t *);
+
+/*
+ * Preserve only unique content lines in a file. Input lines that have
+ * content [alphanumeric characters before a comment] are white-space
+ * normalized and have their comments removed. Then they are placed
+ * in a hash table, and only the first instance of them is printed.
+ * Comment lines without any alphanumeric content are always printed
+ * since they are there to make the file "pretty". Comment lines with
+ * alphanumeric content are also placed into the hash table and only
+ * printed once.
+ */
+void
+uniq(const char *fname)
+{
+ DB *db;
+ DBT key;
+ static const DBT data = { NULL, 0 };
+ FILE *fp;
+ char *line;
+ size_t len;
+
+ if ((db = dbopen(NULL, O_RDWR, 0, DB_HASH, &hinfo)) == NULL)
+ err(1, "Cannot create in memory database");
+
+ if ((fp = fopen(fname, "r")) == NULL)
+ err(1, "Cannot open `%s'", fname);
+ while ((line = fgetln(fp, &len)) != NULL) {
+ size_t complen = len;
+ char *compline;
+ if (!comp(line, &compline, &complen)) {
+ (void)fprintf(stdout, "%*.*s", (int)len, (int)len,
+ line);
+ continue;
+ }
+ key.data = compline;
+ key.size = complen;
+ switch ((db->put)(db, &key, &data, R_NOOVERWRITE)) {
+ case 0:
+ (void)fprintf(stdout, "%*.*s", (int)len, (int)len,
+ line);
+ break;
+ case 1:
+ break;
+ case -1:
+ err(1, "put");
+ default:
+ abort();
+ break;
+ }
+ }
+ (void)fflush(stdout);
+ exit(0);
+}
+
+/*
+ * normalize whitespace in the original line and place a new string
+ * with whitespace converted to a single space in compline. If the line
+ * contains just comments, we preserve them. If it contains data and
+ * comments, we kill the comments. Return 1 if the line had actual
+ * contents, or 0 if it was just a comment without alphanumeric characters.
+ */
+static int
+comp(const char *origline, char **compline, size_t *len)
+{
+ const unsigned char *p;
+ unsigned char *q;
+ char *cline;
+ size_t l = *len, complen;
+ int hasalnum, iscomment;
+
+ /* Eat leading space */
+ for (p = (const unsigned char *)origline; l && *p && isspace(*p);
+ p++, l--)
+ continue;
+ if ((cline = malloc(l + 1)) == NULL)
+ err(1, "Cannot allocate %zu bytes", l + 1);
+ (void)memcpy(cline, p, l);
+ cline[l] = '\0';
+ if (*cline == '\0')
+ return 0;
+
+ complen = 0;
+ hasalnum = 0;
+ iscomment = 0;
+
+ for (q = (unsigned char *)cline; l && *p; p++, l--) {
+ if (isspace(*p)) {
+ if (complen && isspace(q[-1]))
+ continue;
+ *q++ = ' ';
+ complen++;
+ } else {
+ if (!iscomment && *p == '#') {
+ if (hasalnum)
+ break;
+ iscomment = 1;
+ } else
+ hasalnum |= isalnum(*p);
+ *q++ = *p;
+ complen++;
+ }
+ }
+
+ /* Eat trailing space */
+ while (complen && isspace(q[-1])) {
+ --q;
+ --complen;
+ }
+ *q = '\0';
+ *compline = cline;
+ *len = complen;
+ return hasalnum;
+}
OpenPOWER on IntegriCloud