aboutsummaryrefslogtreecommitdiff
path: root/lib/cmyth/libcmyth/safe_string.h
blob: 8110ef658baf10d90f6d5e227e00c5a62a0e907f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
 *  Copyright (C) 2006-2009, Simon Hyde
 *  http://www.mvpmc.org/
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/** \file mvp_string.h
 * Some basic string handling routines to help avoid segfaults/buffer overflows
 */

#ifndef __MVP_STRING_H
#define __MVP_STRING_H

#include <stdio.h>

static inline char * safe_strncpy(char *dest,const char *src,size_t n)
{
    if(src == NULL)
    {
	dest[0] = '\0';
    }
    else
    {
	dest[n-1] = '\0';
	strncpy(dest,src,n-1);
    }
    return dest;
}

#define sizeof_strncpy(dest,src) (safe_strncpy(dest,src,sizeof(dest)))

#define safe_atol(str) (((str) == NULL)? (long)0: atol(str))
#define safe_atoll(str) (((str) == NULL)? (long long)0: atoll(str))
#define safe_atoi(str) (((str) == NULL)? (int)0: atoi(str))

#endif /* __MVP_STRING_H */