-
Notifications
You must be signed in to change notification settings - Fork 18
/
utils.h
242 lines (217 loc) · 6.43 KB
/
utils.h
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* This file is part of the Green End SFTP Server.
* Copyright (C) 2007, 2011 Richard Kettlewell
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
/** @file utils.h @brief Utility functions interface */
#ifndef UTILS_H
# define UTILS_H
# include <string.h>
# include <assert.h>
/** @brief Read bytes from @p fd
* @param fd File descriptor to read from
* @param buffer Buffer to store data in
* @param size Number of bytes to read
* @return 0 on success, non-0 if EOF before @p size bytes read
*
* Loops if necessary to cope with short reads. Calls sftp_fatal() on error.
*/
int sftp_xread(int fd, void *buffer, size_t size);
/** @brief Allocate memory
* @param n Number of bytes to allocate
* @return Pointer to allocated memory
*
* Equivalent to @c malloc() but calls sftp_fatal() on error.
*
* Does not zero-fill.
*/
void *sftp_xmalloc(size_t n);
/** @brief Allocate memory
* @param n Number of objects to allocate
* @param size Size of one object
* @return Pointer to allocated memory
*
* Equivalent to @c calloc() but calls sftp_fatal() on error.
*/
void *sftp_xcalloc(size_t n, size_t size);
/** @brief Reallocate memory
* @param ptr Existing allocation
* @param n Number of bytes to allocate
* @return Pointer to allocated memory
*
* Equivalent to @c realloc() but calls sftp_fatal() on error.
*
* Does not zero-fill.
*/
void *sftp_xrealloc(void *ptr, size_t n);
/** @brief Reallocate memory
* @param ptr Existing allocation
* @param n Number of objects to allocate
* @param size Size of one object
* @return Pointer to allocated memory
*
* Equivalent to @c realloc() but calls sftp_fatal() on error.
*
* Does not zero-fill.
*/
void *sftp_xrecalloc(void *ptr, size_t n, size_t size);
/** @brief Duplicate a string
* @param s String to duplicate
* @return Duplicated string
*
* Equivalent to @c strdup() but calls sftp_fatal() on error.
*/
char *sftp_xstrdup(const char *s);
/** @brief Append to a string
* @param a Allocator
* @param s Existing string
* @param ns Where length of @p s is stored
* @param t String to append
* @return New string
*/
char *sftp_str_append(struct allocator *a, char *s, size_t *ns, const char *t);
/** @brief Append to a string
* @param a Allocator
* @param s Existing string
* @param ns Where length of @p s is stored
* @param t String to append
* @param lt Length of @p t
* @return New string
*/
char *sftp_str_appendn(struct allocator *a, char *s, size_t *ns, const char *t,
size_t lt);
/** @brief Convenient wrapper for readlink(2)
* @param a Allocator to store result
* @param path Path name to inspect
* @return Value of symbolic link or a null pointer on error
*
* Sets @c errno on erorr.
*/
char *sftp_do_readlink(struct allocator *a, const char *path);
/** @brief Return the real (canonical) name of @p path
* @param a Allocator to store result
* @param path Path name to inspect
* @param flags Flags
* @return Canonical path name or a null pointer
*
* Valid flags are:
* - @ref RP_READLINK
* - @ref RP_MUST_EXIST
*
* If @ref RP_READLINK is set then symbolic links are followed. Otherwise they
* are
* not and the transformation is purely lexical.
*
* If @ref RP_MUST_EXIST is set then the path will be converted even if it
* does
* not exist or cannot be accessed. If it is clear but the path does not exist
* or cannot be accessed then an error _may_ be returned (but this is not
* guaranteed).
*
* Setting @ref RP_MUST_EXIST is an optimization for the case where you're
* later
* going to do an existence test.
*
* Sets @c errno on erorr.
*/
char *sftp_find_realpath(struct allocator *a, const char *path, unsigned flags);
/** @brief Follow symlinks
*
* See sftp_find_realpath().
*/
# define RP_READLINK 0x0001
/** @brief Path must exist
*
* See sftp_find_realpath().
*/
# define RP_MUST_EXIST 0x0002
/** @brief Compute the name of the current directory
* @param a Allocator to store result
* @return Name of current directory, or a null pointer
*/
char *sftp_getcwd(struct allocator *a);
/** @brief Compute the directory name part of @p path
* @param a Allocator to store result
* @param path Path name to extract directory from
* @return Directory name
*/
const char *sftp_dirname(struct allocator *a, const char *path);
/** @brief Write an error and terminate
* @param msg Format string as per @c printf(3)
* @param ... Arguments
*
* The error is written either to standard error or syslog; see @ref
* sftp_log_syslog.
*
* Terminates the process.
*/
void sftp_fatal(const char *msg, ...) attribute((noreturn))
attribute((format(printf, 1, 2)));
/** @brief Fork a subprocess
* @return 0 in the child, process ID in the parent
*
* Calls sftp_fatal() on error.
*/
pid_t sftp_xfork(void);
/** @brief Called after forking
*
* Affects the way that sftp_fatal() terminates the process.
*
* sftp_xfork() already calls it, any other calls to @c fork() should call it
* explicitly.
*/
void sftp_forked(void);
/** @brief memcpy wrapper
*
* Equivalent to @c memcpy except that the prohibition on null pointer
* arguments is removed when @p n is 0.
*/
static inline void *sftp_memcpy(void *restrict dest, const void *restrict src,
size_t n) {
if(n) {
assert(dest);
assert(src);
return memcpy(dest, src, n);
}
return dest;
}
/** @brief sftp_memset wrapper
*
* Equivalent to @c memset except that the prohibition on null pointer
* arguments is removed when @p n is 0.
*/
static inline void *sftp_memset(void *s, int ch, size_t n) {
if(n) {
assert(s);
return memset(s, ch, n);
}
return s;
}
/** @brief Whether to log to syslog
*
* Affects where sftp_fatal() writes to.
*/
extern int sftp_log_syslog;
#endif /* UTILS_H */
/*
Local Variables:
c-basic-offset:2
comment-column:40
fill-column:79
indent-tabs-mode:nil
End:
*/