File: utils.c

package info (click to toggle)
kbtin 1.0.15-1.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,852 kB
  • ctags: 1,074
  • sloc: ansic: 17,345; sh: 7,584; makefile: 135; perl: 118
file content (99 lines) | stat: -rw-r--r-- 2,321 bytes parent folder | download
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* Autoconf patching by David Hedbor, neotron@lysator.liu.se */
/*********************************************************************/
/* file: utils.c - some utility-functions                            */
/*                             TINTIN III                            */
/*          (T)he K(I)cki(N) (T)ickin D(I)kumud Clie(N)t             */
/*                     coded by peter unold 1992                     */
/*********************************************************************/
#include "tintin.h"
#include "ui.h"

void syserr(char *msg, ...);

/*********************************************/
/* return: TRUE if s1 is an abrevation of s2 */
/*********************************************/
int is_abrev(char *s1, char *s2)
{
    return !strncmp(s2, s1, strlen(s1));
}

/********************************/
/* strdup - duplicates a string */
/* return: address of duplicate */
/********************************/
char* mystrdup(char *s)
{
    char *dup;

    if (!s)
        return 0;
    if ((dup = MALLOC(strlen(s) + 1)) == NULL)
        syserr("Not enough memory for strdup.");
    strcpy(dup, s);
    return dup;
}


/*************************************************/
/* print system call error message and terminate */
/*************************************************/
void syserr(char *msg, ...)
{
    va_list ap;

    if (ui_own_output)
        user_done();

    if (errno)
        fprintf(stderr, "ERROR (%s):  ", strerror(errno));
    else
        fprintf(stderr, "ERROR:  ");
    va_start(ap, msg);
    vfprintf(stderr, msg, ap);
    va_end(ap);
    fprintf(stderr, "\n");
    exit(1);
}

/* Is any compiler _that_ old still alive? */
#ifndef HAVE_SNPRINTF
/* not for protos.h */ int snprintf(char *str, int len, char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    vsprintf(str, fmt, ap);
    va_end(ap);
}
#endif

#ifndef HAVE_VSNPRINTF
/* not for protos.h */ int vsnprintf(char *str, int len, char *fmt, va_list ap);
{
    vsprintf(str, fmt, ap);
}
#endif

#ifndef HAVE_STRLCPY
size_t strlcpy(char *dst, const char *src, size_t n)
{
    if (!n)
        return strlen(src);

    const char *s = src;

    while (--n > 0)
        if (!(*dst++ = *s++))
            break;

    if (!n)
    {
        *dst++ = 0;
        while (*s++)
            ;
    }

    return s - src - 1;
}
#endif