10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
22132Srobinson
230Sstevel@tonic-gate /*
24*1219Sraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25132Srobinson * Use is subject to license terms.
260Sstevel@tonic-gate */
270Sstevel@tonic-gate
280Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
290Sstevel@tonic-gate
30*1219Sraf #include "mt.h"
310Sstevel@tonic-gate #include <stdio.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <strings.h>
340Sstevel@tonic-gate
350Sstevel@tonic-gate char *_strpbrk_escape(char *, char *);
360Sstevel@tonic-gate
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate * _strtok_escape()
390Sstevel@tonic-gate * Like strtok_r, except we don't break on a token if it is escaped
400Sstevel@tonic-gate * with the escape character (\).
410Sstevel@tonic-gate */
420Sstevel@tonic-gate char *
_strtok_escape(char * string,char * sepset,char ** lasts)430Sstevel@tonic-gate _strtok_escape(char *string, char *sepset, char **lasts)
440Sstevel@tonic-gate {
45132Srobinson char *r;
460Sstevel@tonic-gate
470Sstevel@tonic-gate /* first or subsequent call */
480Sstevel@tonic-gate if (string == NULL)
490Sstevel@tonic-gate string = *lasts;
500Sstevel@tonic-gate
510Sstevel@tonic-gate if (string == 0) /* return if no tokens remaining */
520Sstevel@tonic-gate return (NULL);
530Sstevel@tonic-gate
540Sstevel@tonic-gate if (*string == '\0') /* return if no tokens remaining */
550Sstevel@tonic-gate return (NULL);
560Sstevel@tonic-gate
57132Srobinson /* move past token */
58132Srobinson if ((r = _strpbrk_escape(string, sepset)) == NULL)
590Sstevel@tonic-gate *lasts = 0; /* indicate this is last token */
600Sstevel@tonic-gate else {
610Sstevel@tonic-gate *r = '\0';
620Sstevel@tonic-gate *lasts = r+1;
630Sstevel@tonic-gate }
640Sstevel@tonic-gate return (string);
650Sstevel@tonic-gate }
660Sstevel@tonic-gate
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate * Return ptr to first occurrence of any non-escaped character from `brkset'
690Sstevel@tonic-gate * in the character string `string'; NULL if none exists.
700Sstevel@tonic-gate */
710Sstevel@tonic-gate char *
_strpbrk_escape(char * string,char * brkset)720Sstevel@tonic-gate _strpbrk_escape(char *string, char *brkset)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate const char *p;
750Sstevel@tonic-gate
760Sstevel@tonic-gate do {
770Sstevel@tonic-gate for (p = brkset; *p != '\0' && *p != *string; ++p)
780Sstevel@tonic-gate ;
790Sstevel@tonic-gate if (p == string)
800Sstevel@tonic-gate return ((char *)string);
810Sstevel@tonic-gate if (*p != '\0') {
820Sstevel@tonic-gate if (*(string-1) != '\\')
830Sstevel@tonic-gate return ((char *)string);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate } while (*string++);
860Sstevel@tonic-gate
870Sstevel@tonic-gate return (NULL);
880Sstevel@tonic-gate }
890Sstevel@tonic-gate
900Sstevel@tonic-gate
910Sstevel@tonic-gate char *
_escape(char * s,char * esc)920Sstevel@tonic-gate _escape(char *s, char *esc)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate int nescs = 0; /* number of escapes to place in s */
950Sstevel@tonic-gate int i, j;
960Sstevel@tonic-gate int len_s;
970Sstevel@tonic-gate char *tmp;
980Sstevel@tonic-gate
990Sstevel@tonic-gate if (s == NULL || esc == NULL)
1000Sstevel@tonic-gate return (NULL);
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate len_s = strlen(s);
1030Sstevel@tonic-gate for (i = 0; i < len_s; i++)
1040Sstevel@tonic-gate if (strchr(esc, s[i]))
1050Sstevel@tonic-gate nescs++;
106132Srobinson if ((tmp = malloc(nescs + len_s + 1)) == NULL)
1070Sstevel@tonic-gate return (NULL);
1080Sstevel@tonic-gate for (i = 0, j = 0; i < len_s; i++) {
1090Sstevel@tonic-gate if (strchr(esc, s[i])) {
1100Sstevel@tonic-gate tmp[j++] = '\\';
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate tmp[j++] = s[i];
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate tmp[len_s + nescs] = '\0';
1150Sstevel@tonic-gate return (tmp);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate char *
_unescape(char * s,char * esc)1200Sstevel@tonic-gate _unescape(char *s, char *esc)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate int len_s;
1230Sstevel@tonic-gate int i, j;
1240Sstevel@tonic-gate char *tmp;
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate if (s == NULL || esc == NULL)
127132Srobinson return (NULL);
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate len_s = strlen(s);
130132Srobinson if ((tmp = malloc(len_s + 1)) == NULL)
131132Srobinson return (NULL);
1320Sstevel@tonic-gate for (i = 0, j = 0; i < len_s; i++) {
1330Sstevel@tonic-gate if (s[i] == '\\' && strchr(esc, s[i + 1]))
1340Sstevel@tonic-gate tmp[j++] = s[++i];
1350Sstevel@tonic-gate else
1360Sstevel@tonic-gate tmp[j++] = s[i];
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate tmp[j] = NULL;
1390Sstevel@tonic-gate return (tmp);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate char *
_strdup_null(char * s)1430Sstevel@tonic-gate _strdup_null(char *s)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate return (strdup(s ? s : ""));
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate /*
1500Sstevel@tonic-gate * read a line into buffer from a mmap'ed file.
1510Sstevel@tonic-gate * return length of line read.
1520Sstevel@tonic-gate */
1530Sstevel@tonic-gate int
_readbufline(char * mapbuf,int mapsize,char * buffer,int buflen,int * lastlen)1540Sstevel@tonic-gate _readbufline(char *mapbuf, /* input mmap buffer */
1550Sstevel@tonic-gate int mapsize, /* input size */
1560Sstevel@tonic-gate char *buffer, /* output storage */
1570Sstevel@tonic-gate int buflen, /* output size */
1580Sstevel@tonic-gate int *lastlen) /* input read till here last time */
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate int linelen;
1610Sstevel@tonic-gate
162132Srobinson for (;;) {
1630Sstevel@tonic-gate linelen = 0;
1640Sstevel@tonic-gate while (linelen < buflen - 1) { /* "- 1" saves room for \n\0 */
1650Sstevel@tonic-gate if (*lastlen >= mapsize) {
1660Sstevel@tonic-gate if (linelen == 0 ||
1670Sstevel@tonic-gate buffer[linelen - 1] == '\\') {
1680Sstevel@tonic-gate return (-1);
1690Sstevel@tonic-gate } else {
1700Sstevel@tonic-gate buffer[linelen] = '\n';
1710Sstevel@tonic-gate buffer[linelen + 1] = '\0';
1720Sstevel@tonic-gate return (linelen);
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate switch (mapbuf[*lastlen]) {
1760Sstevel@tonic-gate case '\n':
1770Sstevel@tonic-gate (*lastlen)++;
1780Sstevel@tonic-gate if (linelen > 0 &&
1790Sstevel@tonic-gate buffer[linelen - 1] == '\\') {
1800Sstevel@tonic-gate --linelen; /* remove the '\\' */
1810Sstevel@tonic-gate } else {
1820Sstevel@tonic-gate buffer[linelen] = '\n';
1830Sstevel@tonic-gate buffer[linelen + 1] = '\0';
1840Sstevel@tonic-gate return (linelen);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate break;
1870Sstevel@tonic-gate default:
1880Sstevel@tonic-gate buffer[linelen] = mapbuf[*lastlen];
1890Sstevel@tonic-gate (*lastlen)++;
1900Sstevel@tonic-gate linelen++;
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate /* Buffer overflow -- eat rest of line and loop again */
1940Sstevel@tonic-gate while (mapbuf[*lastlen] != '\n') {
1950Sstevel@tonic-gate if (mapbuf[*lastlen] == EOF) {
1960Sstevel@tonic-gate return (-1);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate (*lastlen)++;
1990Sstevel@tonic-gate };
2000Sstevel@tonic-gate }
201132Srobinson /* NOTREACHED */
2020Sstevel@tonic-gate }
203