2 safestring.c - FireTalk replacement string functions
3 Copyright (C) 2000 Ian Gulliver
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of version 2 of the GNU General Public License as
7 published by the Free Software Foundation.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "safestring.h"
26 void *safe_malloc(const size_t size
) {
28 output
= malloc(size
);
36 void *safe_realloc(void *old
, const size_t new) {
38 output
= realloc(old
,new);
46 char *safe_strdup(const char * const input
) {
51 s
= strlen(input
) + 1;
52 output
= safe_malloc(s
);
53 safe_strncpy(output
,input
,s
);
57 void safe_strncpy(char * const to
, const char * const from
, const size_t size
) {
58 strncpy(to
,from
,size
);
63 void safe_strncat(char * const to
, const char * const from
, const size_t size
) {
66 safe_strncpy(&to
[l
],from
,size
- l
);
70 void safe_snprintf(char *out
, const size_t size
, char * const format
, ...) {
72 char numbuf
[10]; /* stores shorts for printing */
73 size_t f
,o
= 0,fl
,tl
,ml
;
81 for (f
= 0; f
< fl
&& o
< ml
&& b
== 0; f
++) {
82 if (format
[f
] == '%') {
85 tempchr
= va_arg(ap
,char *);
90 memcpy(&out
[o
],tempchr
,tl
);
95 sprintf(numbuf
,"%d",va_arg(ap
,int));
100 memcpy(&out
[o
],numbuf
,tl
);
109 out
[o
++] = format
[f
];
115 int safe_strncasecmp(const char *s1
, const char *s2
, size_t n
) {
117 for (s
= 0; s
< n
; s
++) {
118 if (tolower((unsigned char) s1
[s
]) != tolower((unsigned char) s2
[s
]))