Autotools portability fixes and cleanups.
[cboard.git] / src / misc.c
blobabaac0bdfc7d0ed11d24c29176d516cdee4e3129
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2002-2011 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <pwd.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <err.h>
34 #ifdef HAVE_LIMITS_H
35 #include <limits.h>
36 #endif
38 #ifdef WITH_DMALLOC
39 #include <dmalloc.h>
40 #endif
42 void *Malloc(size_t size)
44 void *ptr;
46 if ((ptr = malloc(size)) == NULL)
47 err(EXIT_FAILURE, "malloc()");
49 return ptr;
52 void *Realloc(void *ptr, size_t size)
54 void *ptr2;
56 if ((ptr2 = realloc(ptr, size)) == NULL)
57 err(EXIT_FAILURE, "realloc()");
59 return ptr2;
62 void *Calloc(size_t n, size_t size)
64 void *p;
66 if ((p = calloc(n, size)) == NULL)
67 err(EXIT_FAILURE, "calloc()");
69 return p;
72 char *rtrim(char *str)
74 int i;
76 if (!*str)
77 return str;
79 for (i = strlen(str) - 1; isspace(str[i]); i--)
80 str[i] = 0;
82 return str;
85 char *trim(char *str)
87 if (!str)
88 return NULL;
90 while (isspace(*str))
91 str++;
93 return rtrim(str);
96 char *itoa(long n)
98 static char buf[16];
100 snprintf(buf, sizeof(buf), "%li", n);
101 return buf;
104 char *trim_multi(char *value)
106 char *p;
107 int lastc;
108 char *str, *s;
110 if (!value || !*value)
111 return value;
113 str = Malloc(strlen(value) + 1);
115 for (p = value, lastc = 0, s = str; *p; p++) {
116 if (isspace(lastc) && isspace(*p))
117 continue;
119 lastc = *s++ = *p;
122 *s = 0;
123 return str;
126 int integer_len(long n)
128 return strlen(itoa(n));
131 int isinteger(const char *str)
133 int i = 0;
134 int len = strlen(str);
136 if (*str == '-' && isdigit(*(str + 1)))
137 i = 1;
139 for (; i < len; i++) {
140 if (!isdigit(str[i]))
141 return 0;
144 return 1;
147 char *pathfix(const char *str)
149 static char buf[FILENAME_MAX];
150 struct passwd *pw;
152 if (*str == '~') {
153 pw = getpwuid(getuid());
154 strncpy(buf, pw->pw_dir, sizeof(buf));
155 strncat(buf, str + 1, sizeof(buf));
157 else
158 strncpy(buf, str, sizeof(buf));
160 return buf;
163 char *str_etc(const char *str, int maxlen, int rev)
165 int len;
166 static char buf[LINE_MAX], *p = buf;
167 int i;
169 if (!str)
170 return NULL;
172 len = strlen(str);
173 strncpy(buf, str, sizeof(buf));
175 if (len > maxlen) {
176 if (rev) {
177 p = buf;
178 *p++ = '.';
179 *p++ = '.';
180 *p++ = '.';
182 for (i = 0; i < maxlen + 3; i++)
183 *p++ = buf[(len - maxlen) + i + 3];
185 else {
186 p = buf + maxlen - 4;
187 *p++ = '.';
188 *p++ = '.';
189 *p++ = '.';
192 *p = '\0';
195 return buf;
198 char **split_str(char *str, char *delim, int *n, int *width, int force_trim)
200 char *tmp;
201 int total = 0;
202 char **lines = NULL;
203 int w = 0;
205 if (!str || !delim)
206 return NULL;
208 while ((tmp = strsep(&str, delim)) != NULL) {
209 char *p;
211 tmp = rtrim(tmp);
213 if (!*tmp)
214 continue;
216 lines = Realloc(lines, (total + 2) * sizeof(char *));
217 p = force_trim ? strdup(trim(tmp)) : strdup(tmp);
219 if (w < strlen(p))
220 w = strlen(p);
222 lines[total++] = p;
225 lines[total] = NULL;
226 *n += total;
228 if (*width < w + 2)
229 *width = w + 2;
231 return lines;