blob: 01b2f87bcd2504e538db3b3fd502a3ecd7f62cdd [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenaad1a882001-03-16 22:47:14 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Eric Andersenaad1a882001-03-16 22:47:14 +000020 */
21
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000022#include <sys/types.h>
23#include <sys/stat.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000024#include <stdio.h>
25#include <string.h>
26#include <stdlib.h>
27#include <unistd.h>
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000028#include <fcntl.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000029#include "libbb.h"
30
Eric Andersenaad1a882001-03-16 22:47:14 +000031
32#ifndef DMALLOC
Manuel Novoa III cad53642003-03-19 09:13:01 +000033#ifdef L_xmalloc
Eric Andersenaad1a882001-03-16 22:47:14 +000034extern void *xmalloc(size_t size)
35{
36 void *ptr = malloc(size);
Matt Kraaia99b1942002-02-26 15:28:22 +000037 if (ptr == NULL && size != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000038 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersenaad1a882001-03-16 22:47:14 +000039 return ptr;
40}
Manuel Novoa III cad53642003-03-19 09:13:01 +000041#endif
Eric Andersenaad1a882001-03-16 22:47:14 +000042
Manuel Novoa III cad53642003-03-19 09:13:01 +000043#ifdef L_xrealloc
Matt Kraaia99b1942002-02-26 15:28:22 +000044extern void *xrealloc(void *ptr, size_t size)
Eric Andersenaad1a882001-03-16 22:47:14 +000045{
Matt Kraaia99b1942002-02-26 15:28:22 +000046 ptr = realloc(ptr, size);
47 if (ptr == NULL && size != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000048 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersenaad1a882001-03-16 22:47:14 +000049 return ptr;
50}
Manuel Novoa III cad53642003-03-19 09:13:01 +000051#endif
Eric Andersenaad1a882001-03-16 22:47:14 +000052
Manuel Novoa III cad53642003-03-19 09:13:01 +000053#ifdef L_xcalloc
Eric Andersenaad1a882001-03-16 22:47:14 +000054extern void *xcalloc(size_t nmemb, size_t size)
55{
56 void *ptr = calloc(nmemb, size);
Matt Kraaia99b1942002-02-26 15:28:22 +000057 if (ptr == NULL && nmemb != 0 && size != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000058 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersenaad1a882001-03-16 22:47:14 +000059 return ptr;
60}
Manuel Novoa III cad53642003-03-19 09:13:01 +000061#endif
Eric Andersen9f894f42003-07-05 22:15:43 +000062#endif /* DMALLOC */
Eric Andersenaad1a882001-03-16 22:47:14 +000063
Manuel Novoa III cad53642003-03-19 09:13:01 +000064#ifdef L_xstrdup
65extern char * bb_xstrdup (const char *s) {
Eric Andersenaad1a882001-03-16 22:47:14 +000066 char *t;
67
68 if (s == NULL)
69 return NULL;
70
71 t = strdup (s);
72
73 if (t == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersenaad1a882001-03-16 22:47:14 +000075
76 return t;
77}
78#endif
79
Manuel Novoa III cad53642003-03-19 09:13:01 +000080#ifdef L_xstrndup
81extern char * bb_xstrndup (const char *s, int n) {
Eric Andersenaad1a882001-03-16 22:47:14 +000082 char *t;
83
84 if (s == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000085 bb_error_msg_and_die("bb_xstrndup bug");
Eric Andersenaad1a882001-03-16 22:47:14 +000086
87 t = xmalloc(++n);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000088
Eric Andersenaad1a882001-03-16 22:47:14 +000089 return safe_strncpy(t,s,n);
90}
Manuel Novoa III cad53642003-03-19 09:13:01 +000091#endif
Eric Andersenaad1a882001-03-16 22:47:14 +000092
Manuel Novoa III cad53642003-03-19 09:13:01 +000093#ifdef L_xfopen
94FILE *bb_xfopen(const char *path, const char *mode)
Eric Andersenaad1a882001-03-16 22:47:14 +000095{
96 FILE *fp;
97 if ((fp = fopen(path, mode)) == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000098 bb_perror_msg_and_die("%s", path);
Eric Andersenaad1a882001-03-16 22:47:14 +000099 return fp;
100}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101#endif
Eric Andersenaad1a882001-03-16 22:47:14 +0000102
Manuel Novoa III cad53642003-03-19 09:13:01 +0000103#ifdef L_xopen
104extern int bb_xopen(const char *pathname, int flags)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000105{
106 int ret;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000107
Glenn L McGrath237ae422002-11-03 14:05:15 +0000108 ret = open(pathname, flags, 0777);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000109 if (ret == -1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000110 bb_perror_msg_and_die("%s", pathname);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000111 }
112 return ret;
113}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000114#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000115
Manuel Novoa III cad53642003-03-19 09:13:01 +0000116#ifdef L_xread
117extern ssize_t bb_xread(int fd, void *buf, size_t count)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000118{
119 ssize_t size;
120
121 size = read(fd, buf, count);
122 if (size == -1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000123 bb_perror_msg_and_die("Read error");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000124 }
125 return(size);
126}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000127#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000128
Manuel Novoa III cad53642003-03-19 09:13:01 +0000129#ifdef L_xread_all
130extern void bb_xread_all(int fd, void *buf, size_t count)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000131{
132 ssize_t size;
133
Manuel Novoa III cad53642003-03-19 09:13:01 +0000134 while (count) {
135 if ((size = bb_xread(fd, buf, count)) == 0) { /* EOF */
136 bb_error_msg_and_die("Short read");
137 }
138 count -= size;
Manuel Novoa III 948d4902004-03-08 05:44:30 +0000139 buf = ((char *) buf) + size;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000140 }
141 return;
142}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000143#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000144
Manuel Novoa III cad53642003-03-19 09:13:01 +0000145#ifdef L_xread_char
146extern unsigned char bb_xread_char(int fd)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000147{
148 char tmp;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000149
Manuel Novoa III cad53642003-03-19 09:13:01 +0000150 bb_xread_all(fd, &tmp, 1);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000151
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000152 return(tmp);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000153}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000154#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000155
Manuel Novoa III cad53642003-03-19 09:13:01 +0000156#ifdef L_xferror
157extern void bb_xferror(FILE *fp, const char *fn)
158{
159 if (ferror(fp)) {
160 bb_error_msg_and_die("%s", fn);
161 }
162}
163#endif
164
165#ifdef L_xferror_stdout
166extern void bb_xferror_stdout(void)
167{
168 bb_xferror(stdout, bb_msg_standard_output);
169}
170#endif
171
172#ifdef L_xfflush_stdout
173extern void bb_xfflush_stdout(void)
174{
175 if (fflush(stdout)) {
176 bb_perror_msg_and_die(bb_msg_standard_output);
177 }
178}
179#endif
180
181#ifdef L_strlen
Eric Andersendb7d5fc2002-04-13 13:02:03 +0000182/* Stupid gcc always includes its own builtin strlen()... */
Eric Andersenc8459a52002-04-13 14:44:42 +0000183#undef strlen
Manuel Novoa III cad53642003-03-19 09:13:01 +0000184size_t bb_strlen(const char *string)
Eric Andersendb7d5fc2002-04-13 13:02:03 +0000185{
186 return(strlen(string));
187}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000188#endif
Eric Andersendb7d5fc2002-04-13 13:02:03 +0000189
Eric Andersenaad1a882001-03-16 22:47:14 +0000190/* END CODE */
191/*
192Local Variables:
193c-file-style: "linux"
194c-basic-offset: 4
195tab-width: 4
196End:
197*/