4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
34 char *_strpbrk_escape(char *, char *);
38 * Like strtok_r, except we don't break on a token if it is escaped
39 * with the escape character (\).
42 _strtok_escape(char *string
, char *sepset
, char **lasts
)
46 /* first or subsequent call */
50 if (string
== 0) /* return if no tokens remaining */
53 if (*string
== '\0') /* return if no tokens remaining */
57 if ((r
= _strpbrk_escape(string
, sepset
)) == NULL
)
58 *lasts
= 0; /* indicate this is last token */
67 * Return ptr to first occurrence of any non-escaped character from `brkset'
68 * in the character string `string'; NULL if none exists.
71 _strpbrk_escape(char *string
, char *brkset
)
76 for (p
= brkset
; *p
!= '\0' && *p
!= *string
; ++p
)
79 return ((char *)string
);
81 if (*(string
-1) != '\\')
82 return ((char *)string
);
91 _escape(char *s
, char *esc
)
93 int nescs
= 0; /* number of escapes to place in s */
98 if (s
== NULL
|| esc
== NULL
)
102 for (i
= 0; i
< len_s
; i
++)
103 if (strchr(esc
, s
[i
]))
105 if ((tmp
= malloc(nescs
+ len_s
+ 1)) == NULL
)
107 for (i
= 0, j
= 0; i
< len_s
; i
++) {
108 if (strchr(esc
, s
[i
])) {
113 tmp
[len_s
+ nescs
] = '\0';
119 _unescape(char *s
, char *esc
)
125 if (s
== NULL
|| esc
== NULL
)
129 if ((tmp
= malloc(len_s
+ 1)) == NULL
)
131 for (i
= 0, j
= 0; i
< len_s
; i
++) {
132 if (s
[i
] == '\\' && strchr(esc
, s
[i
+ 1]))
142 _strdup_null(char *s
)
144 return (strdup(s
? s
: ""));
149 * read a line into buffer from a mmap'ed file.
150 * return length of line read.
153 _readbufline(char *mapbuf
, /* input mmap buffer */
154 int mapsize
, /* input size */
155 char *buffer
, /* output storage */
156 int buflen
, /* output size */
157 int *lastlen
) /* input read till here last time */
163 while (linelen
< buflen
- 1) { /* "- 1" saves room for \n\0 */
164 if (*lastlen
>= mapsize
) {
166 buffer
[linelen
- 1] == '\\') {
169 buffer
[linelen
] = '\n';
170 buffer
[linelen
+ 1] = '\0';
174 switch (mapbuf
[*lastlen
]) {
178 buffer
[linelen
- 1] == '\\') {
179 --linelen
; /* remove the '\\' */
181 buffer
[linelen
] = '\n';
182 buffer
[linelen
+ 1] = '\0';
187 buffer
[linelen
] = mapbuf
[*lastlen
];
192 /* Buffer overflow -- eat rest of line and loop again */
193 while (mapbuf
[*lastlen
] != '\n') {
194 if (mapbuf
[*lastlen
] == EOF
) {