-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.c
More file actions
152 lines (129 loc) · 3.42 KB
/
util.c
File metadata and controls
152 lines (129 loc) · 3.42 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/** Fichier util.c
**
** Diverses routines utilitaires pour calife.
**
** Copyright (c) 1991-2010 by Ollivier ROBERT
** A distribuer selon les regles de la GNU GPL General Public Licence
** Voir le fichier COPYING fourni.
**/
#ifndef lint
static __attribute__((unused)) const char * rcsid = "@(#) $Id: util.c,v 0849dfe2ac8e 2014/09/05 19:06:54 roberto $";
#endif
#include "config.h" /* configure */
#include "conf.h"
/** Renvoie la derniere composante (le nom du fichier) d'un nom complet
**
** Parametre : filename char * nom a analyser
**
** Retourne : p char * le nom de fichier
**/
#ifndef HAVE_BASENAME /* linux as already one it seems */
char *
basename (char * filename)
{
char * p;
p = filename + (strlen (filename) * sizeof (char));
while (*p != '/')
{
if (p == filename)
return (filename);
p--;
}
return (char *) (p + 1);
}
#endif /* HAVE_BASENAME */
/** Efface la mémoire de manière sécurisée
**
** cf. http://www.daemonology.net/blog/2014-09-04-how-to-zero-a-buffer.html
**
** Parametres: buf void * pointeur vers la zone à effacer
** size size_t longueur à effacer
**
** Retourne : rien
**/
static void * (* const volatile memset_ptr)(void *, int, size_t) = memset;
void
secure_memzero (void * buf, size_t size)
{
(memset_ptr)(buf, '\0', size);
}
/** Alloue de la mémoire par malloc, la met à zéro et renvoie le pointeur.
** Sort si plus de mémoire.
**
** Parametres: num size_t nombre de blocs à allouer
** size size_t taille d'un bloc
**
** Retourne : ptr void * un pointeur sur la zone
**/
void *
xalloc (size_t size)
{
void * ptr;
ptr = (void *) malloc (size);
if (ptr == NULL)
die (1, "No more memory.");
secure_memzero ((char *) ptr, size);
return ptr;
}
/** Sort du programme avec le message et le code de retour spécifiés.
**
** Contribué par Nat Makarévitch <nat@nataa.frmug.fr.net>
** Modifiée pour se conformer à mes usages :-)
**
** Paramètres: err code d'erreur de exit(3)
** fmt format du message
** ... les arguments
**
** Retourne: rien
*/
#ifdef HAVE_STDARG_H
#include <stdarg.h>
void
die (int err, const char * fmt, ...)
{
va_list ap;
fflush (stdout);
closelog ();
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
fprintf (stderr, "\n");
exit (err);
}
#elif defined(HAVE_VARARGS_H)
void
die (int err, const char * fmt, ...)
{
va_list ap;
fflush (stdout);
closelog ();
va_start (ap);
vfprintf (stderr, fmt, ap);
va_end (ap);
fprintf (stderr, "\n");
exit (err);
}
#else
#error You should have at least one of stdarg/varargs
#endif /* neither */
#ifndef HAVE_STRLCPY
/** Copy src to string dst of size dsize.
** At most dsize-1 chars will be copied.
** Always NUL terminates (unless dsize == 0).
**
** Parameters: dst char * destination buffer
** src char * source string
** dsize size_t size of destination buffer
**
** Returns: strlen(src) size_t length of string tried to create
**/
size_t
strlcpy(char *dst, const char *src, size_t dsize)
{
size_t srclen = strlen(src);
if (dsize != 0) {
snprintf(dst, dsize, "%s", src);
}
return srclen;
}
#endif /* !HAVE_STRLCPY */