depmod: use real regex matching on depmod "overrides" entries
[mit.git] / zlibsupport.c
blobb159765cf92163391013a3e1d162839fb1450e13
1 /* Support for compressed modules. Willy Tarreau <willy@meta-x.org>
2 * did the support for modutils, Andrey Borzenkov <arvidjaar@mail.ru>
3 * ported it to module-init-tools, and I said it was too ugly to live
4 * and rewrote it 8).
6 * (C) 2003 Rusty Russell, IBM Corporation.
7 */
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <errno.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <sys/mman.h>
16 #include "zlibsupport.h"
17 #include "logging.h"
18 #include "testing.h"
20 #ifdef CONFIG_USE_ZLIB
21 #include <zlib.h>
23 void *grab_contents(gzFile *gzfd, unsigned long *size)
25 unsigned int max = 16384;
26 void *buffer = NOFAIL(malloc(max));
27 int ret;
29 *size = 0;
30 while ((ret = gzread(gzfd, buffer + *size, max - *size)) > 0) {
31 *size += ret;
32 if (*size == max)
33 buffer = NOFAIL(realloc(buffer, max *= 2));
35 if (ret < 0) {
36 free(buffer);
37 buffer = NULL;
40 return buffer;
43 void *grab_fd(int fd, unsigned long *size)
45 gzFile gzfd;
47 gzfd = gzdopen(fd, "rb");
48 if (!gzfd) {
49 if (errno == ENOMEM)
50 fatal("Memory allocation failure in gzdopen\n");
51 return NULL;
54 /* gzclose(gzfd) would close fd, which would drop locks.
55 Don't blame zlib: POSIX locking semantics are so horribly
56 broken that they should be ripped out. */
57 return grab_contents(gzfd, size);
60 /* gzopen handles uncompressed files transparently. */
61 void *grab_file(const char *filename, unsigned long *size)
63 gzFile gzfd;
64 void *buffer;
66 errno = 0;
67 gzfd = gzopen(filename, "rb");
68 if (!gzfd) {
69 if (errno == ENOMEM)
70 fatal("Memory allocation failure in gzopen\n");
71 return NULL;
73 buffer = grab_contents(gzfd, size);
74 gzclose(gzfd);
75 return buffer;
78 void release_file(void *data, unsigned long size)
80 free(data);
82 #else /* ... !CONFIG_USE_ZLIB */
84 void *grab_fd(int fd, unsigned long *size)
86 struct stat st;
87 void *map;
88 int ret;
90 ret = fstat(fd, &st);
91 if (ret < 0)
92 return NULL;
93 *size = st.st_size;
94 map = mmap(0, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
95 if (map == MAP_FAILED)
96 map = NULL;
97 return map;
100 void *grab_file(const char *filename, unsigned long *size)
102 int fd;
103 void *map;
105 fd = open(filename, O_RDONLY, 0);
106 if (fd < 0)
107 return NULL;
108 map = grab_fd(fd, size);
109 close(fd);
110 return map;
113 void release_file(void *data, unsigned long size)
115 munmap(data, size);
117 #endif