utils.c (7c478bd9) utils.c (24da5b34)
1/*
2 * utils.c - various utility functions used in pppd.
3 *
1/*
2 * utils.c - various utility functions used in pppd.
3 *
4 * Copyright (c) 2000-2001 by Sun Microsystems, Inc.
5 * All rights reserved.
4 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
5 * Use is subject to license terms.
6 *
7 * Permission to use, copy, modify, and distribute this software and its
8 * documentation is hereby granted, provided that the above copyright
9 * notice appears in all copies.
10 *
11 * SUN MAKES NO REPRESENTATION OR WARRANTIES ABOUT THE SUITABILITY OF
12 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
13 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A

--- 83 unchanged lines hidden (view full) ---

97
98 for (ret = 0; ret < len; ret++)
99 if (*str++ == '\0')
100 break;
101 return (ret);
102}
103
104/*
6 *
7 * Permission to use, copy, modify, and distribute this software and its
8 * documentation is hereby granted, provided that the above copyright
9 * notice appears in all copies.
10 *
11 * SUN MAKES NO REPRESENTATION OR WARRANTIES ABOUT THE SUITABILITY OF
12 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
13 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A

--- 83 unchanged lines hidden (view full) ---

97
98 for (ret = 0; ret < len; ret++)
99 if (*str++ == '\0')
100 break;
101 return (ret);
102}
103
104/*
105 * strlcpy - like strcpy/strncpy, doesn't overflow destination buffer,
106 * always leaves destination null-terminated (for len > 0).
107 */
108size_t
109strlcpy(dest, src, len)
110 char *dest;
111 const char *src;
112 size_t len;
113{
114 size_t ret = strlen(src);
115
116 if (len != 0) {
117 if (ret < len)
118 (void) strcpy(dest, src);
119 else {
120 (void) strncpy(dest, src, len - 1);
121 dest[len-1] = 0;
122 }
123 }
124 return (ret);
125}
126
127/*
128 * strlcat - like strcat/strncat, doesn't overflow destination buffer,
129 * always leaves destination null-terminated (for len > 0).
130 */
131size_t
132strlcat(dest, src, len)
133 char *dest;
134 const char *src;
135 size_t len;
136{
137 size_t dlen = strlen(dest);
138
139 return (dlen + strlcpy(dest + dlen, src, (len > dlen? len - dlen: 0)));
140}
141
142
143/*
144 * slprintf - format a message into a buffer. Like sprintf except we
145 * also specify the length of the output buffer, and we handle %m
146 * (error message), %v (visible string), %q (quoted string), %t
147 * (current time), %I (IP address), %P (PPP packet), and %B (sequence
148 * of bytes) formats. Doesn't do floating-point formats. Returns the
149 * number of chars put into buf.
150 */
151int

--- 903 unchanged lines hidden ---
105 * slprintf - format a message into a buffer. Like sprintf except we
106 * also specify the length of the output buffer, and we handle %m
107 * (error message), %v (visible string), %q (quoted string), %t
108 * (current time), %I (IP address), %P (PPP packet), and %B (sequence
109 * of bytes) formats. Doesn't do floating-point formats. Returns the
110 * number of chars put into buf.
111 */
112int

--- 903 unchanged lines hidden ---