1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
29
30 #include <sys/types.h>
31 #include <libgen.h>
32 #include <ctype.h>
33 #include <string.h>
34 #include <stdio.h>
35
36 /*
37 * strecpy(output, input, except)
38 * strecpy copies the input string to the output string expanding
39 * any non-graphic character with the C escape sequence. Escape
40 * sequences produced are those defined in "The C Programming
41 * Language" by Kernighan and Ritchie.
42 * Characters in the `except' string will not be expanded.
43 * Returns the first argument.
44 *
45 * streadd( output, input, except )
46 * Identical to strecpy() except returns address of null-byte at end
47 * of output. Useful for concatenating strings.
48 */
49
50
51 char *
strecpy(char * pout,const char * pin,const char * except)52 strecpy(char *pout, const char *pin, const char *except)
53 {
54 (void) streadd(pout, pin, except);
55 return (pout);
56 }
57
58
59 char *
streadd(char * pout,const char * pin,const char * except)60 streadd(char *pout, const char *pin, const char *except)
61 {
62 unsigned c;
63
64 while ((c = *pin++) != 0) {
65 if (!isprint(c) && (!except || !strchr(except, c))) {
66 *pout++ = '\\';
67 switch (c) {
68 case '\n':
69 *pout++ = 'n';
70 continue;
71 case '\t':
72 *pout++ = 't';
73 continue;
74 case '\b':
75 *pout++ = 'b';
76 continue;
77 case '\r':
78 *pout++ = 'r';
79 continue;
80 case '\f':
81 *pout++ = 'f';
82 continue;
83 case '\v':
84 *pout++ = 'v';
85 continue;
86 case '\007':
87 *pout++ = 'a';
88 continue;
89 case '\\':
90 continue;
91 default:
92 (void) sprintf(pout, "%.3o", c);
93 pout += 3;
94 continue;
95 }
96 }
97 if (c == '\\' && (!except || !strchr(except, c)))
98 *pout++ = '\\';
99 *pout++ = (char)c;
100 }
101 *pout = '\0';
102 return (pout);
103 }
104