2
* libdpkg - Debian packaging suite library routines
3
* mlib.c - `must' library: routines will succeed or longjmp
5
* Copyright © 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
7
* This is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as
9
* published by the Free Software Foundation; either version 2,
10
* or (at your option) any later version.
12
* This is distributed in the hope that it will be useful, but
13
* WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public
18
* License along with dpkg; if not, write to the Free Software
19
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
#include <dpkg/i18n.h>
35
#include <sys/types.h>
37
#include <dpkg/dpkg.h>
38
#include <dpkg/dpkg-db.h>
40
void *m_malloc(size_t amount) {
42
unsigned short *r2, x;
48
if (!r) ohshite(_("malloc failed (%ld bytes)"),(long)amount);
52
r2= r; x= (unsigned short)amount ^ 0xf000;
53
while (amount >= 2) { *r2++= x; amount -= 2; }
58
void *m_realloc(void *r, size_t amount) {
61
if (!r) ohshite(_("realloc failed (%ld bytes)"),(long)amount);
68
m_strdup(const char *str)
73
new_str = strdup(str);
75
ohshite(_("failed to allocate memory"));
81
static void print_error_forked(const char *emsg, const char *contextstring) {
82
fprintf(stderr, _("%s (subprocess): %s\n"), thisname, emsg);
85
static void cu_m_fork(int argc, void **argv) DPKG_ATTR_NORET;
86
static void cu_m_fork(int argc, void **argv) {
88
/* Don't do the other cleanups, because they'll be done by/in the parent
96
if (r == -1) { onerr_abort++; ohshite(_("fork failed")); }
98
push_cleanup(cu_m_fork,~0, NULL,0, 0);
99
set_error_display(print_error_forked,NULL);
103
void m_dup2(int oldfd, int newfd) {
104
const char *const stdstrings[]= { "in", "out", "err" };
106
if (dup2(oldfd,newfd) == newfd) return;
109
if (newfd < 3) ohshite(_("failed to dup for std%s"),stdstrings[newfd]);
110
ohshite(_("failed to dup for fd %d"),newfd);
113
void m_pipe(int *fds) {
114
if (!pipe(fds)) return;
116
ohshite(_("failed to create pipe"));
120
m_output(FILE *f, const char *name)
124
ohshite(_("error writing to '%s'"), name);
127
void setcloexec(int fd, const char* fn) {
130
if ((f=fcntl(fd, F_GETFD))==-1)
131
ohshite(_("unable to read filedescriptor flags for %.250s"),fn);
132
if (fcntl(fd, F_SETFD, (f|FD_CLOEXEC))==-1)
133
ohshite(_("unable to set close-on-exec flag for %.250s"),fn);