/[gentoo-projects]/pax-utils/xfuncs.c
Gentoo

Contents of /pax-utils/xfuncs.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (hide annotations) (download) (as text)
Sun Nov 4 07:26:24 2012 UTC (6 years ago) by vapier
Branch: MAIN
Changes since 1.10: +4 -4 lines
File MIME type: text/x-csrc
update copyright years

1 vapier 1.1 /*
2 vapier 1.11 * Copyright 2003-2012 Gentoo Foundation
3 vapier 1.1 * Distributed under the terms of the GNU General Public License v2
4 vapier 1.11 * $Header: /var/cvsroot/gentoo-projects/pax-utils/xfuncs.c,v 1.10 2011/09/27 18:37:22 vapier Exp $
5 vapier 1.1 *
6 vapier 1.11 * Copyright 2003-2012 Ned Ludd - <solar@gentoo.org>
7     * Copyright 2004-2012 Mike Frysinger - <vapier@gentoo.org>
8 vapier 1.1 */
9    
10     #include "paxinc.h"
11    
12     char *xstrdup(const char *s)
13     {
14     char *ret = strdup(s);
15     if (!ret) err("Could not strdup(): %s", strerror(errno));
16     return ret;
17     }
18    
19     void *xmalloc(size_t size)
20     {
21     void *ret = malloc(size);
22 vapier 1.8 if (!ret) err("Could not malloc() %zu bytes", size);
23 vapier 1.1 return ret;
24     }
25    
26 vapier 1.2 void *xzalloc(size_t size)
27     {
28     return memset(xmalloc(size), 0, size);
29     }
30    
31 vapier 1.1 void *xrealloc(void *ptr, size_t size)
32     {
33     void *ret = realloc(ptr, size);
34 vapier 1.8 if (!ret) err("Could not realloc() %zu bytes", size);
35 vapier 1.1 return ret;
36     }
37    
38     void xstrncat(char **dst, const char *src, size_t *curr_len, size_t n)
39     {
40 vapier 1.9 bool init;
41 vapier 1.1 size_t new_len;
42    
43 vapier 1.9 init = *curr_len ? false : true;
44     new_len = (init ? 0 : strlen(*dst)) + strlen(src);
45 vapier 1.1 if (*curr_len <= new_len) {
46 vapier 1.9 *curr_len = new_len + (*curr_len / 2) + 1;
47 vapier 1.1 *dst = realloc(*dst, *curr_len);
48     if (!*dst)
49 vapier 1.8 err("could not realloc() %zu bytes", *curr_len);
50 vapier 1.9 if (init)
51     *dst[0] = '\0';
52 vapier 1.1 }
53    
54     if (n)
55     strncat(*dst, src, n);
56     else
57     strcat(*dst, src);
58     }
59    
60     void xchrcat(char **dst, const char append, size_t *curr_len)
61     {
62     static char my_app[2];
63     my_app[0] = append;
64     my_app[1] = '\0';
65     xstrcat(dst, my_app, curr_len);
66     }
67 vapier 1.10
68     void *xmemdup(const void *src, size_t n)
69     {
70     void *ret = xmalloc(n);
71     memcpy(ret, src, n);
72     return ret;
73     }
74    
75     void xarraypush(array_t *arr, const void *ele, size_t ele_len)
76     {
77     size_t n = arr->num++;
78     arr->eles = xrealloc_array(arr->eles, arr->num, sizeof(ele));
79     arr->eles[n] = xmemdup(ele, ele_len);
80     }
81    
82     void xarrayfree(array_t *arr)
83     {
84     array_t blank = array_init_decl;
85     size_t n;
86    
87     for (n = 0; n < arr->num; ++n)
88     free(arr->eles[n]);
89     free(arr->eles);
90    
91     *arr = blank;
92     }
93    
94     char *array_flatten_str(array_t *array)
95     {
96     size_t n, len = 0;
97     char *str, *ret = NULL;
98    
99     array_for_each(array, n, str) {
100     if (ret)
101     xchrcat(&ret, ',', &len);
102     xstrcat(&ret, str, &len);
103     }
104    
105     return ret;
106     }

  ViewVC Help
Powered by ViewVC 1.1.20