1 /* usmb - mount SMB shares via FUSE and Samba
2 * Copyright (C) 2006-2010 Geoff Johnstone
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 char * concat_strings (int num
, ...)
37 base
= out
= malloc (INIT_LEN
);
41 size_t buff_size
= INIT_LEN
;
44 for (int i
= 0; i
< num
; ++i
)
46 const char *next
= va_arg (ap
, const char *);
47 assert (NULL
!= next
);
49 size_t next_len
= strlen (next
);
51 size_t required
= (out
- base
) + next_len
+ 1;
53 if (buff_size
< required
)
55 while (buff_size
< required
)
57 size_t dbl_len
= buff_size
* 2;
58 if (dbl_len
< buff_size
)
69 ptrdiff_t diff
= out
- base
;
71 char *newbase
= realloc (base
, buff_size
);
83 memcpy (out
, next
, next_len
);
93 char * xstrdup (const char *in
)
99 size_t len
= strlen (in
) + 1;
101 if ((out
= malloc (len
)))
102 memcpy (out
, in
, len
);
109 // the const here lets us pass a pointer to const
110 void xfree (const void *ptr
)
117 void clear_and_free (char *ptr
)
121 for (char *pch
= ptr
; '\0' != *pch
; ++pch
)
129 void free_errno (const void *ptr
)
137 void xfree_errno (const void *ptr
)
145 bool bsnprintf (char *str
, size_t size
, const char *format
, ...)
150 va_start (ap
, format
);
151 ret
= bvsnprintf (str
, size
, format
, ap
);
158 bool bvsnprintf (char *str
, size_t size
, const char *format
, va_list ap
)
162 assert (NULL
!= str
);
163 assert (NULL
!= format
);
165 ret
= vsnprintf (str
, size
, format
, ap
);
167 return ((ret
>= 0) && ((size_t)ret
< size
));
171 bool baprintf (char **out
, const char *format
, ...)
176 va_start (ap
, format
);
177 ret
= bvaprintf (out
, format
, ap
);
184 bool bvaprintf (char **out
, const char *format
, va_list ap
)
186 assert (NULL
!= out
);
187 assert (NULL
!= format
);
190 int bytes
= vsnprintf (NULL
, 0, format
, ap
);
191 if ((1 > bytes
) || (INT_MAX
== bytes
))
196 if (bytes
!= (int)(size_t)bytes
)
199 *out
= malloc ((size_t)bytes
);
203 if (!bvsnprintf (*out
, bytes
, format
, ap
))