File: util.c

package info (click to toggle)
picasm 1.14-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 528 kB
  • ctags: 378
  • sloc: ansic: 4,481; asm: 150; makefile: 72
file content (63 lines) | stat: -rw-r--r-- 1,125 bytes parent folder | download | duplicates (2)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
 * util.c
 */

#include <stdio.h>
#include <string.h>
#include <stdarg.h>

#include "util.h"

/*
 * Special versions of some string handling functions,
 * to avoid buffer overflows.
 */


/*
 * This always NUL-terminates the buffer, even if vsnprintf does not.
 */
void p_snprintf(char *buf, size_t maxsize, const char *fmt, ...)
{
    va_list args;

    va_start(args, fmt);
    vsnprintf(buf, maxsize, fmt, args);
    buf[maxsize - 1] = '\0';
    va_end(args);
}

void p_vsnprintf(char *buf, size_t maxsize, const char *fmt, va_list args)
{
    vsnprintf(buf, maxsize, fmt, args);
    buf[maxsize - 1] = '\0';
}

void p_strcpy(char *dest, const char *src, size_t maxsize)
{
    size_t len;

    len = strlen(src);
    if(len > maxsize - 1)
	len = maxsize - 1;

    memcpy(dest, src, len);
    dest[len] = '\0';
}

void p_strcat(char *dest, const char *src, size_t maxsize)
{
    size_t len_d, len_s;

    len_d = strlen(dest);
    len_s = strlen(src);

    if(len_d + len_s > maxsize - 1)
	len_s = maxsize - 1 - len_d;

    if(len_d < 1)
	return;

    memcpy(dest + len_d, src, len_s);
    dest[len_d + len_s] = '\0';
}