1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2002-2015 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
31 #include <sys/types.h>
41 void *Malloc(size_t size
)
45 if ((ptr
= malloc(size
)) == NULL
)
46 err(EXIT_FAILURE
, "malloc()");
51 void *Realloc(void *ptr
, size_t size
)
55 if ((ptr2
= realloc(ptr
, size
)) == NULL
)
56 err(EXIT_FAILURE
, "realloc()");
61 void *Calloc(size_t n
, size_t size
)
65 if ((p
= calloc(n
, size
)) == NULL
)
66 err(EXIT_FAILURE
, "calloc()");
71 char *rtrim(char *str
)
78 for (i
= strlen(str
) - 1; isspace(str
[i
]); i
--)
95 char *itoa(long n
, char *buf
)
97 sprintf(buf
, "%li", n
);
101 char *trim_multi(char *value
)
107 if (!value
|| !*value
)
110 str
= Malloc(strlen(value
) + 1);
112 for (p
= value
, lastc
= 0, s
= str
; *p
; p
++) {
113 if (isspace(lastc
) && isspace(*p
))
123 int integer_len(long n
)
131 int isinteger(const char *str
)
134 int len
= strlen(str
);
136 if (*str
== '-' && isdigit(*(str
+ 1)))
139 for (; i
< len
; i
++) {
140 if (!isdigit(str
[i
]))
147 char *pathfix(const char *str
)
149 static char buf
[FILENAME_MAX
] = {0};
153 pw
= getpwuid(getuid());
154 strncpy(buf
, pw
->pw_dir
, sizeof(buf
)-1);
155 strncat(buf
, str
+ 1, sizeof(buf
)-1);
158 strncpy(buf
, str
, sizeof(buf
)-1);
163 wchar_t *str_etc(const char *str
, int maxlen
, int rev
)
171 p
= str_to_wchar (str
);
173 if (wcslen (p
) > maxlen
) {
174 wchar_t *dot
= str_to_wchar (_("..."));
177 buf
= str_to_wchar_len (_("..."), maxlen
);
178 buf
[wcslen (dot
)] = 0;
179 wcsncat (buf
, p
, maxlen
-wcslen (dot
));
182 buf
= str_to_wchar_len (str
, maxlen
);
183 buf
[wcslen (buf
)-wcslen (dot
)]=0;
196 char **split_str(char *str
, char *delim
, int *n
, int *width
, int force_trim
)
206 while ((tmp
= strsep(&str
, delim
)) != NULL
) {
216 lines
= Realloc(lines
, (total
+ 2) * sizeof(char *));
217 p
= force_trim
? strdup(trim(tmp
)) : strdup(tmp
);
218 wc
= str_to_wchar (p
);
238 // 'max' will always allocate that amount when not 0.
239 wchar_t *str_to_wchar_len (const char *str
, int max
)
246 memset (&ps
, 0, sizeof(mbstate_t));
248 len
= mbsrtowcs (NULL
, &p
, 0, &ps
);
250 wc
= Calloc (len
+1, sizeof(wchar_t));
252 memset (&ps
, 0, sizeof(mbstate_t));
253 len
= mbsrtowcs (wc
, &p
, len
, &ps
);
257 wchar_t *str_to_wchar (const char *str
)
259 return str_to_wchar_len (str
, 0);
262 char *wchar_to_str (const wchar_t *str
)
264 size_t len
= wcstombs (NULL
, str
, 0);
265 char *s
= Malloc (len
*sizeof(char)+1);
266 len
= wcstombs (s
, str
, len
);