1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2002-2018 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>
46 if ((ptr
= malloc (size
)) == NULL
)
47 err (EXIT_FAILURE
, "malloc()");
53 Realloc (void *ptr
, size_t size
)
57 if ((ptr2
= realloc (ptr
, size
)) == NULL
)
58 err (EXIT_FAILURE
, "realloc()");
64 Calloc (size_t n
, size_t size
)
68 if ((p
= calloc (n
, size
)) == NULL
)
69 err (EXIT_FAILURE
, "calloc()");
82 for (i
= strlen (str
) - 1; isspace (str
[i
]); i
--)
94 while (isspace (*str
))
101 itoa (long n
, char *buf
)
103 sprintf (buf
, "%li", n
);
108 trim_multi (char *value
)
114 if (!value
|| !*value
)
117 str
= Malloc (strlen (value
) + 1);
119 for (p
= value
, lastc
= 0, s
= str
; *p
; p
++)
121 if (isspace (lastc
) && isspace (*p
))
141 isinteger (const char *str
)
144 int len
= strlen (str
);
146 if (*str
== '-' && isdigit (*(str
+ 1)))
151 if (!isdigit (str
[i
]))
159 pathfix (const char *str
)
161 static char buf
[FILENAME_MAX
] = { 0 };
166 pw
= getpwuid (getuid ());
167 strncpy (buf
, pw
->pw_dir
, sizeof (buf
) - 1);
168 strncat (buf
, str
+ 1, sizeof (buf
) - 1);
171 strncpy (buf
, str
, sizeof (buf
) - 1);
177 str_etc (const char *str
, int maxlen
, int rev
)
185 p
= str_to_wchar (str
);
187 if (wcslen (p
) > maxlen
)
189 wchar_t *dot
= str_to_wchar (_("..."));
193 buf
= str_to_wchar_len (_("..."), maxlen
);
194 buf
[wcslen (dot
)] = 0;
195 wcsncat (buf
, p
, maxlen
- wcslen (dot
));
199 buf
= str_to_wchar_len (str
, maxlen
);
200 buf
[wcslen (buf
) - wcslen (dot
)] = 0;
214 split_str (char *str
, const char *delim
, int *n
, int *width
, int force_trim
)
224 while ((tmp
= strsep (&str
, delim
)) != NULL
)
235 lines
= Realloc (lines
, (total
+ 2) * sizeof (char *));
236 p
= force_trim
? strdup (trim (tmp
)) : strdup (tmp
);
237 wc
= str_to_wchar (p
);
258 // 'max' will always allocate that amount when not 0.
260 str_to_wchar_len (const char *str
, int max
)
267 memset (&ps
, 0, sizeof (mbstate_t));
269 len
= mbsrtowcs (NULL
, &p
, 0, &ps
);
271 wc
= Calloc (len
+ 1, sizeof (wchar_t));
273 memset (&ps
, 0, sizeof (mbstate_t));
274 len
= mbsrtowcs (wc
, &p
, len
, &ps
);
279 str_to_wchar (const char *str
)
281 return str_to_wchar_len (str
, 0);
285 wchar_to_str (const wchar_t * str
)
287 size_t len
= wcstombs (NULL
, str
, 0);
288 char *s
= Malloc (len
* sizeof (char) + 1);
289 len
= wcstombs (s
, str
, len
);
295 wcharv_length (wchar_t **s
)
299 for (n
= 0; s
&& s
[n
]; n
++);
303 /* Replace the first occurence of 's' with 'r' in 'h'. */
305 string_replace (const char *hay
, const char *n
, const char *r
)
308 const char *hp
= hay
;
310 if (!strstr (hay
, n
))
313 buf
= malloc (strlen (hay
) + strlen (r
) + 1);
315 for (hp
= hay
; hp
&& *hp
; hp
++)
317 if (!strncmp (hp
, n
, strlen (n
)))
321 for (rp
= r
; *rp
; rp
++)