opkg: fix some initialisation and double free issues in libbb
[oweals/opkg-lede.git] / libbb / libbb.h
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Busybox main internal header file
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19
20 #ifndef __LIBBB_H__
21 #define __LIBBB_H__    1
22
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27
28 #include <netdb.h>
29
30 // Include because of single use with opkg
31 #define _BB_INTERNAL_H_
32 #define L_archive_offset
33 #define L_get_header_ar
34 #define L_free_header_tar
35 #define L_get_header_tar
36 #define L_unarchive
37 #define L_seek_sub_file
38 #define L_memory_exhausted
39 #define L_deb_extract
40 #define L_extract_archive
41 #define L_parse_mode
42 #define L_time_string
43 #define L_mode_string
44
45 #ifndef _BB_INTERNAL_H_
46 #include "../busybox.h"
47 #endif
48
49 #ifdef DMALLOC
50 #include "dmalloc.h"
51 #endif
52
53 #include <features.h>
54
55 #if (__GNU_LIBRARY__ < 5) && (!defined __dietlibc__)
56 /* libc5 doesn't define socklen_t */
57 typedef unsigned int socklen_t;
58 /* libc5 doesn't implement BSD 4.4 daemon() */
59 extern int daemon (int nochdir, int noclose);
60 /* libc5 doesn't implement strtok_r */
61 char *strtok_r(char *s, const char *delim, char **ptrptr);
62 #endif  
63
64 /* Some useful definitions */
65 #define FALSE   ((int) 0)
66 #define TRUE    ((int) 1)
67 #define SKIP    ((int) 2)
68
69 /* for mtab.c */
70 #define MTAB_GETMOUNTPT '1'
71 #define MTAB_GETDEVICE  '2'
72
73 #define BUF_SIZE        8192
74 #define EXPAND_ALLOC    1024
75
76 static inline int is_decimal(int ch) { return ((ch >= '0') && (ch <= '9')); }
77 static inline int is_octal(int ch)   { return ((ch >= '0') && (ch <= '7')); }
78
79 /* Macros for min/max.  */
80 #ifndef MIN
81 #define MIN(a,b) (((a)<(b))?(a):(b))
82 #endif
83
84 #ifndef MAX
85 #define MAX(a,b) (((a)>(b))?(a):(b))
86 #endif
87
88
89
90 extern void show_usage(void) __attribute__ ((noreturn));
91 extern void error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
92 extern void error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
93 extern void perror_msg(const char *s, ...);
94 extern void perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn));
95 extern void vherror_msg(const char *s, va_list p);
96 extern void herror_msg(const char *s, ...);
97 extern void herror_msg_and_die(const char *s, ...) __attribute__ ((noreturn));
98 extern void archive_xread_all(int fd, char *buf, size_t count);
99
100 /* These two are used internally -- you shouldn't need to use them */
101 extern void verror_msg(const char *s, va_list p);
102 extern void vperror_msg(const char *s, va_list p);
103
104 const char *mode_string(int mode);
105 const char *time_string(time_t timeVal);
106 int is_directory(const char *name, int followLinks, struct stat *statBuf);
107 int isDevice(const char *name);
108
109 int remove_file(const char *path, int flags);
110 int copy_file(const char *source, const char *dest, int flags);
111 int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize);
112 char *buildName(const char *dirName, const char *fileName);
113 int makeString(int argc, const char **argv, char *buf, int bufLen);
114 char *getChunk(int size);
115 char *chunkstrdup(const char *str);
116 void freeChunks(void);
117 ssize_t safe_read(int fd, void *buf, size_t count);
118 ssize_t full_write(int fd, const char *buf, int len);
119 ssize_t full_read(int fd, char *buf, int len);
120 int recursive_action(const char *fileName, int recurse, int followLinks, int depthFirst,
121           int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData),
122           int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData),
123           void* userData);
124
125 extern int parse_mode( const char* s, mode_t* theMode);
126
127 extern int get_kernel_revision(void);
128
129 extern int get_console_fd(char* tty_name);
130 extern struct mntent *find_mount_point(const char *name, const char *table);
131 extern void write_mtab(char* blockDevice, char* directory, 
132         char* filesystemType, long flags, char* string_flags);
133 extern void erase_mtab(const char * name);
134 extern long atoi_w_units (const char *cp);
135 extern long* find_pid_by_name( char* pidName);
136 extern char *find_real_root_device_name(const char* name);
137 extern char *get_line_from_file(FILE *file);
138 extern void print_file(FILE *file);
139 extern int copyfd(int fd1, int fd2);
140 extern int print_file_by_name(char *filename);
141 extern char process_escape_sequence(const char **ptr);
142 extern char *get_last_path_component(char *path);
143 extern FILE *wfopen(const char *path, const char *mode);
144 extern FILE *xfopen(const char *path, const char *mode);
145 extern void chomp(char *s);
146 extern struct BB_applet *find_applet_by_name(const char *name);
147 void run_applet_by_name(const char *name, int argc, char **argv);
148
149 #ifndef DMALLOC
150 extern void *xmalloc (size_t size);
151 extern void *xrealloc(void *old, size_t size);
152 extern void *xcalloc(size_t nmemb, size_t size);
153 extern char *xstrdup (const char *s);
154 #endif
155 extern char *xstrndup (const char *s, int n);
156 extern char * safe_strncpy(char *dst, const char *src, size_t size);
157
158 struct suffix_mult {
159         const char *suffix;
160         int mult;
161 };
162
163 extern unsigned long parse_number(const char *numstr,
164                 const struct suffix_mult *suffixes);
165
166
167 /* These parse entries in /etc/passwd and /etc/group.  This is desirable
168  * for BusyBox since we want to avoid using the glibc NSS stuff, which
169  * increases target size and is often not needed embedded systems.  */
170 extern long my_getpwnam(const char *name);
171 extern long my_getgrnam(const char *name);
172 extern void my_getpwuid(char *name, long uid);
173 extern void my_getgrgid(char *group, long gid);
174 extern long my_getpwnamegid(const char *name);
175
176 extern int device_open(char *device, int mode);
177
178 extern int del_loop(const char *device);
179 extern int set_loop(const char *device, const char *file, int offset, int *loopro);
180 extern char *find_unused_loop_device (void);
181
182
183 #if (__GLIBC__ < 2)
184 extern int vdprintf(int d, const char *format, va_list ap);
185 #endif
186
187 int nfsmount(const char *spec, const char *node, int *flags,
188              char **extra_opts, char **mount_opts, int running_bg);
189
190 void syslog_msg_with_name(const char *name, int facility, int pri, const char *msg);
191 void syslog_msg(int facility, int pri, const char *msg);
192
193 /* Include our own copy of struct sysinfo to avoid binary compatability
194  * problems with Linux 2.4, which changed things.  Grumble, grumble. */
195 struct sysinfo {
196         long uptime;                    /* Seconds since boot */
197         unsigned long loads[3];         /* 1, 5, and 15 minute load averages */
198         unsigned long totalram;         /* Total usable main memory size */
199         unsigned long freeram;          /* Available memory size */
200         unsigned long sharedram;        /* Amount of shared memory */
201         unsigned long bufferram;        /* Memory used by buffers */
202         unsigned long totalswap;        /* Total swap space size */
203         unsigned long freeswap;         /* swap space still available */
204         unsigned short procs;           /* Number of current processes */
205         unsigned short pad;                     /* Padding needed for m68k */
206         unsigned long totalhigh;        /* Total high memory size */
207         unsigned long freehigh;         /* Available high memory size */
208         unsigned int mem_unit;          /* Memory unit size in bytes */
209         char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
210 };
211 extern int sysinfo (struct sysinfo* info);
212
213 enum {
214         KILOBYTE = 1024,
215         MEGABYTE = (KILOBYTE*1024),
216         GIGABYTE = (MEGABYTE*1024)
217 };
218 const char *make_human_readable_str(unsigned long size, unsigned long block_size, unsigned long display_unit);
219
220 int ask_confirmation(void);
221 int klogctl(int type, char * b, int len);
222
223 char *xgetcwd(char *cwd);
224 char *xreadlink(const char *path);
225 char *concat_path_file(const char *path, const char *filename);
226 char *last_char_is(const char *s, int c);
227
228 extern long arith (const char *startbuf, int *errcode);
229
230 typedef struct file_headers_s {
231         char *name;
232         char *link_name;
233         off_t size;
234         uid_t uid;
235         gid_t gid;
236         mode_t mode;
237         time_t mtime;
238         dev_t device;
239 } file_header_t;
240 file_header_t *get_header_ar(FILE *in_file);
241 void free_header_ar(file_header_t *ar_entry);
242 file_header_t *get_header_cpio(FILE *src_stream);
243 file_header_t *get_header_tar(FILE *tar_stream);
244 void free_header_tar(file_header_t *tar_entry);
245
246 enum extract_functions_e {
247         extract_verbose_list = 1,
248         extract_list = 2,
249         extract_one_to_buffer = 4,
250         extract_to_stdout = 8,
251         extract_all_to_fs = 16,
252         extract_preserve_date = 32,
253         extract_data_tar_gz = 64,
254         extract_control_tar_gz = 128,
255         extract_unzip_only = 256,
256         extract_unconditional = 512,
257         extract_create_leading_dirs = 1024,
258         extract_quiet = 2048,
259         extract_exclude_list = 4096
260 };
261 char *unarchive(FILE *src_stream, FILE *out_stream,
262                 file_header_t *(*get_header)(FILE *),
263                 void (*free_headers)(file_header_t *),
264                 const int extract_function, const char *prefix, char **extract_names);
265 char *deb_extract(const char *package_filename, FILE *out_stream, const int extract_function,
266         const char *prefix, const char *filename);
267 int read_package_field(const char *package_buffer, char **field_name, char **field_value);
268 char *fgets_str(FILE *file, const char *terminating_string);
269
270 extern int unzip(FILE *l_in_file, FILE *l_out_file);
271 extern void gz_close(int gunzip_pid);
272 extern FILE *gz_open(FILE *compressed_file, int *pid);
273
274 extern struct hostent *xgethostbyname(const char *name);
275 extern int create_icmp_socket(void);
276
277 char *dirname (char *path);
278
279 int make_directory (const char *path, long mode, int flags);
280
281 const char *u_signal_names(const char *str_sig, int *signo, int startnum);
282 char *simplify_path(const char *path);
283
284 #define CT_AUTO 0
285 #define CT_UNIX2DOS     1
286 #define CT_DOS2UNIX     2
287 /* extern int convert(char *fn, int ConvType); */
288
289 enum {
290         FILEUTILS_PRESERVE_STATUS = 1,
291         FILEUTILS_PRESERVE_SYMLINKS = 2,
292         FILEUTILS_RECUR = 4,
293         FILEUTILS_FORCE = 8,
294         FILEUTILS_INTERACTIVE = 16
295 };
296
297 extern const char *applet_name;
298 extern const char * const full_version;
299 extern const char * const name_too_long;
300 extern const char * const omitting_directory;
301 extern const char * const not_a_directory;
302 extern const char * const memory_exhausted;
303 extern const char * const invalid_date;
304 extern const char * const invalid_option;
305 extern const char * const io_error;
306 extern const char * const dash_dash_help;
307 extern const char * const write_error;
308 extern const char * const too_few_args;
309 extern const char * const name_longer_than_foo;
310 extern const char * const unknown;
311 extern const char * const can_not_create_raw_socket;
312
313 #ifdef BB_FEATURE_DEVFS
314 # define CURRENT_VC "/dev/vc/0"
315 # define VC_1 "/dev/vc/1"
316 # define VC_2 "/dev/vc/2"
317 # define VC_3 "/dev/vc/3"
318 # define VC_4 "/dev/vc/4"
319 # define VC_5 "/dev/vc/5"
320 # define SC_0 "/dev/tts/0"
321 # define SC_1 "/dev/tts/1"
322 # define VC_FORMAT "/dev/vc/%d"
323 # define SC_FORMAT "/dev/tts/%d"
324 # define LOOP_FORMAT "/dev/loop/%d"
325 #else
326 # define CURRENT_VC "/dev/tty0"
327 # define VC_1 "/dev/tty1"
328 # define VC_2 "/dev/tty2"
329 # define VC_3 "/dev/tty3"
330 # define VC_4 "/dev/tty4"
331 # define VC_5 "/dev/tty5"
332 # define SC_0 "/dev/ttyS0"
333 # define SC_1 "/dev/ttyS1"
334 # define VC_FORMAT "/dev/tty%d"
335 # define SC_FORMAT "/dev/ttyS%d"
336 # define LOOP_FORMAT "/dev/loop%d"
337 #endif
338
339 /* The following devices are the same on devfs and non-devfs systems.  */
340 #define CURRENT_TTY "/dev/tty"
341 #define CONSOLE_DEV "/dev/console"
342
343 /* Cope with mmu-less systems somewhat gracefully */
344 #if defined(__UCLIBC__) && !defined(__ARCH_HAS_MMU__)
345 #define fork    vfork
346 #endif
347
348 /* Stupid gcc always includes its own builtin strlen()... */
349 extern size_t xstrlen(const char *string);
350 #define strlen(x)       xstrlen(x)
351
352 #endif /* __LIBBB_H__ */