1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
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
31 #include <sys/types.h>
42 void *Malloc(size_t size
)
46 if ((ptr
= malloc(size
)) == NULL
)
47 err(EXIT_FAILURE
, "malloc()");
52 void *Realloc(void *ptr
, size_t size
)
56 if ((ptr2
= realloc(ptr
, size
)) == NULL
)
57 err(EXIT_FAILURE
, "realloc()");
62 void *Calloc(size_t n
, size_t size
)
66 if ((p
= calloc(n
, size
)) == NULL
)
67 err(EXIT_FAILURE
, "calloc()");
72 char *rtrim(char *str
)
79 for (i
= strlen(str
) - 1; isspace(str
[i
]); i
--)
100 snprintf(buf
, sizeof(buf
), "%li", n
);
104 char *trim_multi(char *value
)
110 if (!value
|| !*value
)
113 str
= Malloc(strlen(value
) + 1);
115 for (p
= value
, lastc
= 0, s
= str
; *p
; p
++) {
116 if (isspace(lastc
) && isspace(*p
))
126 int integer_len(long n
)
128 return strlen(itoa(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
];
153 pw
= getpwuid(getuid());
154 strncpy(buf
, pw
->pw_dir
, sizeof(buf
));
155 strncat(buf
, str
+ 1, sizeof(buf
));
158 strncpy(buf
, str
, sizeof(buf
));
163 char *str_etc(const char *str
, int maxlen
, int rev
)
166 static char buf
[LINE_MAX
], *p
= buf
;
173 strncpy(buf
, str
, sizeof(buf
));
182 for (i
= 0; i
< maxlen
+ 3; i
++)
183 *p
++ = buf
[(len
- maxlen
) + i
+ 3];
186 p
= buf
+ maxlen
- 4;
198 char **split_str(char *str
, char *delim
, int *n
, int *width
, int force_trim
)
208 while ((tmp
= strsep(&str
, delim
)) != NULL
) {
216 lines
= Realloc(lines
, (total
+ 2) * sizeof(char *));
217 p
= force_trim
? strdup(trim(tmp
)) : strdup(tmp
);