1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2002-2013 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>
44 void *Malloc(size_t size
)
48 if ((ptr
= malloc(size
)) == NULL
)
49 err(EXIT_FAILURE
, "malloc()");
54 void *Realloc(void *ptr
, size_t size
)
58 if ((ptr2
= realloc(ptr
, size
)) == NULL
)
59 err(EXIT_FAILURE
, "realloc()");
64 void *Calloc(size_t n
, size_t size
)
68 if ((p
= calloc(n
, size
)) == NULL
)
69 err(EXIT_FAILURE
, "calloc()");
74 char *rtrim(char *str
)
81 for (i
= strlen(str
) - 1; isspace(str
[i
]); i
--)
102 snprintf(buf
, sizeof(buf
), "%li", n
);
106 char *trim_multi(char *value
)
112 if (!value
|| !*value
)
115 str
= Malloc(strlen(value
) + 1);
117 for (p
= value
, lastc
= 0, s
= str
; *p
; p
++) {
118 if (isspace(lastc
) && isspace(*p
))
128 int integer_len(long n
)
130 return strlen(itoa(n
));
133 int isinteger(const char *str
)
136 int len
= strlen(str
);
138 if (*str
== '-' && isdigit(*(str
+ 1)))
141 for (; i
< len
; i
++) {
142 if (!isdigit(str
[i
]))
149 char *pathfix(const char *str
)
151 static char buf
[FILENAME_MAX
];
155 pw
= getpwuid(getuid());
156 strncpy(buf
, pw
->pw_dir
, sizeof(buf
));
157 strncat(buf
, str
+ 1, sizeof(buf
));
160 strncpy(buf
, str
, sizeof(buf
));
165 wchar_t *str_etc(const char *str
, int maxlen
, int rev
)
174 p
= str_to_wchar (str
);
178 wchar_t *dot
= str_to_wchar ("...");
180 buf
= Malloc (maxlen
+4*sizeof(wchar_t));
184 wcsncat (buf
, dot
, maxlen
);
185 wcsncat (buf
, p
, maxlen
);
188 wcsncat (buf
, p
, maxlen
);
189 wcsncat (buf
, dot
, maxlen
);
201 char **split_str(char *str
, char *delim
, int *n
, int *width
, int force_trim
)
211 while ((tmp
= strsep(&str
, delim
)) != NULL
) {
219 lines
= Realloc(lines
, (total
+ 2) * sizeof(char *));
220 p
= force_trim
? strdup(trim(tmp
)) : strdup(tmp
);
238 str_to_wchar (const char *str
)
244 memset (&ps
, 0, sizeof(mbstate_t));
245 size_t len
= mbsrtowcs (NULL
, &p
, 0, &ps
)+1;
246 wc
= Calloc (1, len
* sizeof(wchar_t));
248 memset (&ps
, 0, sizeof(mbstate_t));
249 len
= mbsrtowcs (wc
, &p
, len
, &ps
);