17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
2261961e0fSrobinson
237c478bd9Sstevel@tonic-gate /*
24*e8031f0aSraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2561961e0fSrobinson * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
297c478bd9Sstevel@tonic-gate
30*e8031f0aSraf #include "mt.h"
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <strings.h>
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate char *_strpbrk_escape(char *, char *);
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate * _strtok_escape()
397c478bd9Sstevel@tonic-gate * Like strtok_r, except we don't break on a token if it is escaped
407c478bd9Sstevel@tonic-gate * with the escape character (\).
417c478bd9Sstevel@tonic-gate */
427c478bd9Sstevel@tonic-gate char *
_strtok_escape(char * string,char * sepset,char ** lasts)437c478bd9Sstevel@tonic-gate _strtok_escape(char *string, char *sepset, char **lasts)
447c478bd9Sstevel@tonic-gate {
4561961e0fSrobinson char *r;
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate /* first or subsequent call */
487c478bd9Sstevel@tonic-gate if (string == NULL)
497c478bd9Sstevel@tonic-gate string = *lasts;
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate if (string == 0) /* return if no tokens remaining */
527c478bd9Sstevel@tonic-gate return (NULL);
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate if (*string == '\0') /* return if no tokens remaining */
557c478bd9Sstevel@tonic-gate return (NULL);
567c478bd9Sstevel@tonic-gate
5761961e0fSrobinson /* move past token */
5861961e0fSrobinson if ((r = _strpbrk_escape(string, sepset)) == NULL)
597c478bd9Sstevel@tonic-gate *lasts = 0; /* indicate this is last token */
607c478bd9Sstevel@tonic-gate else {
617c478bd9Sstevel@tonic-gate *r = '\0';
627c478bd9Sstevel@tonic-gate *lasts = r+1;
637c478bd9Sstevel@tonic-gate }
647c478bd9Sstevel@tonic-gate return (string);
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate * Return ptr to first occurrence of any non-escaped character from `brkset'
697c478bd9Sstevel@tonic-gate * in the character string `string'; NULL if none exists.
707c478bd9Sstevel@tonic-gate */
717c478bd9Sstevel@tonic-gate char *
_strpbrk_escape(char * string,char * brkset)727c478bd9Sstevel@tonic-gate _strpbrk_escape(char *string, char *brkset)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate const char *p;
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate do {
777c478bd9Sstevel@tonic-gate for (p = brkset; *p != '\0' && *p != *string; ++p)
787c478bd9Sstevel@tonic-gate ;
797c478bd9Sstevel@tonic-gate if (p == string)
807c478bd9Sstevel@tonic-gate return ((char *)string);
817c478bd9Sstevel@tonic-gate if (*p != '\0') {
827c478bd9Sstevel@tonic-gate if (*(string-1) != '\\')
837c478bd9Sstevel@tonic-gate return ((char *)string);
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate } while (*string++);
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate return (NULL);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate char *
_escape(char * s,char * esc)927c478bd9Sstevel@tonic-gate _escape(char *s, char *esc)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate int nescs = 0; /* number of escapes to place in s */
957c478bd9Sstevel@tonic-gate int i, j;
967c478bd9Sstevel@tonic-gate int len_s;
977c478bd9Sstevel@tonic-gate char *tmp;
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate if (s == NULL || esc == NULL)
1007c478bd9Sstevel@tonic-gate return (NULL);
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate len_s = strlen(s);
1037c478bd9Sstevel@tonic-gate for (i = 0; i < len_s; i++)
1047c478bd9Sstevel@tonic-gate if (strchr(esc, s[i]))
1057c478bd9Sstevel@tonic-gate nescs++;
10661961e0fSrobinson if ((tmp = malloc(nescs + len_s + 1)) == NULL)
1077c478bd9Sstevel@tonic-gate return (NULL);
1087c478bd9Sstevel@tonic-gate for (i = 0, j = 0; i < len_s; i++) {
1097c478bd9Sstevel@tonic-gate if (strchr(esc, s[i])) {
1107c478bd9Sstevel@tonic-gate tmp[j++] = '\\';
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate tmp[j++] = s[i];
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate tmp[len_s + nescs] = '\0';
1157c478bd9Sstevel@tonic-gate return (tmp);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate char *
_unescape(char * s,char * esc)1207c478bd9Sstevel@tonic-gate _unescape(char *s, char *esc)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate int len_s;
1237c478bd9Sstevel@tonic-gate int i, j;
1247c478bd9Sstevel@tonic-gate char *tmp;
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate if (s == NULL || esc == NULL)
12761961e0fSrobinson return (NULL);
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate len_s = strlen(s);
13061961e0fSrobinson if ((tmp = malloc(len_s + 1)) == NULL)
13161961e0fSrobinson return (NULL);
1327c478bd9Sstevel@tonic-gate for (i = 0, j = 0; i < len_s; i++) {
1337c478bd9Sstevel@tonic-gate if (s[i] == '\\' && strchr(esc, s[i + 1]))
1347c478bd9Sstevel@tonic-gate tmp[j++] = s[++i];
1357c478bd9Sstevel@tonic-gate else
1367c478bd9Sstevel@tonic-gate tmp[j++] = s[i];
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate tmp[j] = NULL;
1397c478bd9Sstevel@tonic-gate return (tmp);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate char *
_strdup_null(char * s)1437c478bd9Sstevel@tonic-gate _strdup_null(char *s)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate return (strdup(s ? s : ""));
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate /*
1507c478bd9Sstevel@tonic-gate * read a line into buffer from a mmap'ed file.
1517c478bd9Sstevel@tonic-gate * return length of line read.
1527c478bd9Sstevel@tonic-gate */
1537c478bd9Sstevel@tonic-gate int
_readbufline(char * mapbuf,int mapsize,char * buffer,int buflen,int * lastlen)1547c478bd9Sstevel@tonic-gate _readbufline(char *mapbuf, /* input mmap buffer */
1557c478bd9Sstevel@tonic-gate int mapsize, /* input size */
1567c478bd9Sstevel@tonic-gate char *buffer, /* output storage */
1577c478bd9Sstevel@tonic-gate int buflen, /* output size */
1587c478bd9Sstevel@tonic-gate int *lastlen) /* input read till here last time */
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate int linelen;
1617c478bd9Sstevel@tonic-gate
16261961e0fSrobinson for (;;) {
1637c478bd9Sstevel@tonic-gate linelen = 0;
1647c478bd9Sstevel@tonic-gate while (linelen < buflen - 1) { /* "- 1" saves room for \n\0 */
1657c478bd9Sstevel@tonic-gate if (*lastlen >= mapsize) {
1667c478bd9Sstevel@tonic-gate if (linelen == 0 ||
1677c478bd9Sstevel@tonic-gate buffer[linelen - 1] == '\\') {
1687c478bd9Sstevel@tonic-gate return (-1);
1697c478bd9Sstevel@tonic-gate } else {
1707c478bd9Sstevel@tonic-gate buffer[linelen] = '\n';
1717c478bd9Sstevel@tonic-gate buffer[linelen + 1] = '\0';
1727c478bd9Sstevel@tonic-gate return (linelen);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate switch (mapbuf[*lastlen]) {
1767c478bd9Sstevel@tonic-gate case '\n':
1777c478bd9Sstevel@tonic-gate (*lastlen)++;
1787c478bd9Sstevel@tonic-gate if (linelen > 0 &&
1797c478bd9Sstevel@tonic-gate buffer[linelen - 1] == '\\') {
1807c478bd9Sstevel@tonic-gate --linelen; /* remove the '\\' */
1817c478bd9Sstevel@tonic-gate } else {
1827c478bd9Sstevel@tonic-gate buffer[linelen] = '\n';
1837c478bd9Sstevel@tonic-gate buffer[linelen + 1] = '\0';
1847c478bd9Sstevel@tonic-gate return (linelen);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate break;
1877c478bd9Sstevel@tonic-gate default:
1887c478bd9Sstevel@tonic-gate buffer[linelen] = mapbuf[*lastlen];
1897c478bd9Sstevel@tonic-gate (*lastlen)++;
1907c478bd9Sstevel@tonic-gate linelen++;
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate /* Buffer overflow -- eat rest of line and loop again */
1947c478bd9Sstevel@tonic-gate while (mapbuf[*lastlen] != '\n') {
1957c478bd9Sstevel@tonic-gate if (mapbuf[*lastlen] == EOF) {
1967c478bd9Sstevel@tonic-gate return (-1);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate (*lastlen)++;
1997c478bd9Sstevel@tonic-gate };
2007c478bd9Sstevel@tonic-gate }
20161961e0fSrobinson /* NOTREACHED */
2027c478bd9Sstevel@tonic-gate }
203