1 /**************************************************************************************************
2 $Id: string.c,v 1.22 2005/04/20 16:49:11 bboy Exp $
3
4 string.c: Typical generic string manipulation routines.
5
6 Copyright (C) 2002-2005 Don Moore <bboy@bboy.net>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at Your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 **************************************************************************************************/
22
23 #include "mydnsutil.h"
24
25
26 /**************************************************************************************************
27 STRTRIMLEAD
28 Remove trailing spaces, etc.
29 **************************************************************************************************/
30 char *
strtrimlead(char * str)31 strtrimlead(char *str) {
32 char *obuf;
33
34 if (str) {
35 for (obuf = str; *obuf && isspace((int)(*obuf)); ++obuf)
36 ;
37 if (str != obuf)
38 memmove(str, obuf, strlen(obuf) + 1);
39 }
40 return (str);
41 }
42 /*--- strtrimlead() -----------------------------------------------------------------------------*/
43
44
45 /**************************************************************************************************
46 STRTRIMTRAIL
47 **************************************************************************************************/
48 char *
strtrimtrail(char * str)49 strtrimtrail(char *str) {
50 int i;
51
52 if (str && 0 != (i = strlen(str))) {
53 while (--i >= 0) {
54 if (!isspace((int)(str[i])))
55 break;
56 }
57 str[++i] = '\0';
58 }
59 return (str);
60 }
61 /*--- strtrimtrail() ----------------------------------------------------------------------------*/
62
63
64 /**************************************************************************************************
65 STRTRIM
66 Removes leading and trailing whitespace from a string. Converts tabs and newlines to spaces.
67 **************************************************************************************************/
68 char *
strtrim(char * str)69 strtrim(char *str) {
70 strtrimlead(str);
71 strtrimtrail(str);
72 return (str);
73 }
74 /*--- strtrim() ---------------------------------------------------------------------------------*/
75
76
77 /**************************************************************************************************
78 STRTOUPPER
79 Converts a string to uppercase.
80 **************************************************************************************************/
81 char *
strtoupper(char * str)82 strtoupper(char *str) {
83 register char *c;
84
85 if (!str || !*str)
86 return (NULL);
87 for (c = str; *c; c++)
88 *c = toupper(*c);
89 return (str);
90 }
91 /*--- strtoupper() ------------------------------------------------------------------------------*/
92
93
94 /**************************************************************************************************
95 STRTOLOWER
96 Converts a string to lowercase.
97 **************************************************************************************************/
98 char *
strtolower(char * str)99 strtolower(char *str) {
100 register char *c;
101
102 if (!str || !*str)
103 return (NULL);
104 for (c = str; *c; c++)
105 *c = tolower(*c);
106 return (str);
107 }
108 /*--- strtolower() ------------------------------------------------------------------------------*/
109
110
111 /**************************************************************************************************
112 STRSECS
113 Outputs a number of seconds in a more human-friendly format.
114 **************************************************************************************************/
115 char *
strsecs(time_t seconds)116 strsecs(time_t seconds) {
117 int weeks, days, hours, minutes;
118 static char *str = NULL;
119 char *weekstr = NULL, *daystr = NULL, *hourstr = NULL, *minutestr = NULL, *secondstr = NULL;
120
121 weeks = seconds / 604800; seconds -= (weeks * 604800);
122 days = seconds / 86400; seconds -= (days * 86400);
123 hours = seconds / 3600; seconds -= (hours * 3600);
124 minutes = seconds / 60; seconds -= (minutes * 60);
125
126 if (weeks) { ASPRINTF(&weekstr, "%dw", weeks); }
127 if (days) { ASPRINTF(&daystr, "%dd", days); }
128 if (hours) { ASPRINTF(&hourstr, "%dh", hours); }
129 if (minutes) { ASPRINTF(&minutestr, "%dm", minutes); }
130 if (seconds) { ASPRINTF(&secondstr, "%ds", (int)seconds); }
131
132 RELEASE(str);
133 ASPRINTF(&str,
134 "%s%s%s%s%s",
135 (weekstr)?weekstr:"",
136 (daystr)?daystr:"",
137 (hourstr)?hourstr:"",
138 (minutestr)?minutestr:"",
139 (secondstr)?secondstr:(!(weekstr||daystr||hourstr||minutestr))?"0s":"");
140
141 RELEASE(weekstr);
142 RELEASE(daystr);
143 RELEASE(hourstr);
144 RELEASE(minutestr);
145 RELEASE(secondstr);
146 return (str);
147 }
148 /*--- strsecs() ---------------------------------------------------------------------------------*/
149
150
151 /**************************************************************************************************
152 STRDCAT
153 Dynamically-allocated strcat(3).
154 **************************************************************************************************/
155 char *
strdcat(char ** dest,const char * src)156 strdcat(char **dest, const char *src) {
157 register int srclen; /* Length of src */
158 register int destlen; /* Length of dest */
159 char *d = *dest; /* Ptr to dest */
160
161 /* If we pass a length of 0 to realloc, it frees memory: just return */
162 if ((srclen = strlen(src)) == 0)
163 return (d);
164 destlen = (d) ? strlen(d) : 0;
165
166 /* Allocate/reallocate the storage in dest */
167 if (!d) {
168 d = ALLOCATE(destlen + srclen + 1, char[]);
169 } else {
170 d = REALLOCATE(d, destlen + srclen + 1, char[]);
171 }
172
173 memcpy(d + destlen, src, srclen);
174 d[destlen + srclen] = '\0';
175
176 *dest = d;
177 return (d);
178 }
179 /*--- strdcat() ---------------------------------------------------------------------------------*/
180
181
182 /**************************************************************************************************
183 SDPRINTF
184 Dynamically-allocated sprintf(3).
185 **************************************************************************************************/
186 int
sdprintf(char ** dest,const char * fmt,...)187 sdprintf(char **dest, const char *fmt, ...) {
188 va_list ap;
189 int len;
190
191 va_start(ap, fmt);
192 len = VASPRINTF(dest, fmt, ap);
193 va_end(ap);
194
195 return (len);
196 }
197 /*--- sdprintf() --------------------------------------------------------------------------------*/
198
199
200 /**************************************************************************************************
201 Given a string such as "10MB" returns the size represented, in bytes.
202 **************************************************************************************************/
203 size_t
human_file_size(const char * str)204 human_file_size(const char *str) {
205 size_t numeric = 0; /* Numeric part of `str' */
206 register char *c; /* Ptr to first nonalpha char */
207
208 numeric = (size_t)strtoul(str, (char **)NULL, 10);
209
210 for (c = (char *)str; *c && isdigit((int)(*c)); c++)
211 /* DONOTHING */;
212
213 if (!*c)
214 return (numeric);
215
216 switch (tolower(*c)) {
217 case 'k': return (numeric * 1024);
218 case 'm': return (numeric * 1048576);
219 case 'g': return (numeric * 1073741824);
220 default:
221 break;
222 }
223 return (numeric);
224 }
225 /*--- human_file_size() -------------------------------------------------------------------------*/
226
227 /* vi:set ts=3: */
228