blob: 6d54c1a794f931e5919fee4042dadf69e15061f1 [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 *
Rob Landley4e9deec2006-02-20 02:44:30 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenaad1a882001-03-16 22:47:14 +00008 */
9
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000010#include <sys/types.h>
11#include <sys/stat.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000012#include <stdio.h>
13#include <string.h>
14#include <stdlib.h>
15#include <unistd.h>
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000016#include <fcntl.h>
"Vladimir N. Oleynik"0c6ff432006-02-20 12:15:10 +000017
18/* Since gcc always inlines strlen(), this saves a byte or two, but we need
19 * the #undef here to avoid endless loop from #define strlen bb_strlen */
20#ifdef L_strlen
21#define BB_STRLEN_IMPLEMENTATION
22#endif
23
Eric Andersenaad1a882001-03-16 22:47:14 +000024#include "libbb.h"
25
Eric Andersenaad1a882001-03-16 22:47:14 +000026
27#ifndef DMALLOC
Manuel Novoa III cad53642003-03-19 09:13:01 +000028#ifdef L_xmalloc
Eric Andersenaad1a882001-03-16 22:47:14 +000029extern void *xmalloc(size_t size)
30{
31 void *ptr = malloc(size);
Matt Kraaia99b1942002-02-26 15:28:22 +000032 if (ptr == NULL && size != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000033 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersenaad1a882001-03-16 22:47:14 +000034 return ptr;
35}
Manuel Novoa III cad53642003-03-19 09:13:01 +000036#endif
Eric Andersenaad1a882001-03-16 22:47:14 +000037
Manuel Novoa III cad53642003-03-19 09:13:01 +000038#ifdef L_xrealloc
Matt Kraaia99b1942002-02-26 15:28:22 +000039extern void *xrealloc(void *ptr, size_t size)
Eric Andersenaad1a882001-03-16 22:47:14 +000040{
Matt Kraaia99b1942002-02-26 15:28:22 +000041 ptr = realloc(ptr, size);
42 if (ptr == NULL && size != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000043 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersenaad1a882001-03-16 22:47:14 +000044 return ptr;
45}
Manuel Novoa III cad53642003-03-19 09:13:01 +000046#endif
Eric Andersenaad1a882001-03-16 22:47:14 +000047
Manuel Novoa III cad53642003-03-19 09:13:01 +000048#ifdef L_xcalloc
Eric Andersenaad1a882001-03-16 22:47:14 +000049extern void *xcalloc(size_t nmemb, size_t size)
50{
51 void *ptr = calloc(nmemb, size);
Matt Kraaia99b1942002-02-26 15:28:22 +000052 if (ptr == NULL && nmemb != 0 && size != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000053 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersenaad1a882001-03-16 22:47:14 +000054 return ptr;
55}
Manuel Novoa III cad53642003-03-19 09:13:01 +000056#endif
Eric Andersen9f894f42003-07-05 22:15:43 +000057#endif /* DMALLOC */
Eric Andersenaad1a882001-03-16 22:47:14 +000058
Manuel Novoa III cad53642003-03-19 09:13:01 +000059#ifdef L_xstrdup
60extern char * bb_xstrdup (const char *s) {
Eric Andersenaad1a882001-03-16 22:47:14 +000061 char *t;
62
63 if (s == NULL)
64 return NULL;
65
66 t = strdup (s);
67
68 if (t == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000069 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersenaad1a882001-03-16 22:47:14 +000070
71 return t;
72}
73#endif
74
Manuel Novoa III cad53642003-03-19 09:13:01 +000075#ifdef L_xstrndup
76extern char * bb_xstrndup (const char *s, int n) {
Eric Andersenaad1a882001-03-16 22:47:14 +000077 char *t;
78
79 if (s == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000080 bb_error_msg_and_die("bb_xstrndup bug");
Eric Andersenaad1a882001-03-16 22:47:14 +000081
82 t = xmalloc(++n);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000083
Eric Andersenaad1a882001-03-16 22:47:14 +000084 return safe_strncpy(t,s,n);
85}
Manuel Novoa III cad53642003-03-19 09:13:01 +000086#endif
Eric Andersenaad1a882001-03-16 22:47:14 +000087
Manuel Novoa III cad53642003-03-19 09:13:01 +000088#ifdef L_xfopen
89FILE *bb_xfopen(const char *path, const char *mode)
Eric Andersenaad1a882001-03-16 22:47:14 +000090{
91 FILE *fp;
92 if ((fp = fopen(path, mode)) == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000093 bb_perror_msg_and_die("%s", path);
Eric Andersenaad1a882001-03-16 22:47:14 +000094 return fp;
95}
Manuel Novoa III cad53642003-03-19 09:13:01 +000096#endif
Eric Andersenaad1a882001-03-16 22:47:14 +000097
Manuel Novoa III cad53642003-03-19 09:13:01 +000098#ifdef L_xopen
99extern int bb_xopen(const char *pathname, int flags)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000100{
101 int ret;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000102
Glenn L McGrath237ae422002-11-03 14:05:15 +0000103 ret = open(pathname, flags, 0777);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000104 if (ret == -1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000105 bb_perror_msg_and_die("%s", pathname);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000106 }
107 return ret;
108}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000109#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000110
Manuel Novoa III cad53642003-03-19 09:13:01 +0000111#ifdef L_xread
112extern ssize_t bb_xread(int fd, void *buf, size_t count)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000113{
114 ssize_t size;
115
116 size = read(fd, buf, count);
117 if (size == -1) {
"Vladimir N. Oleynik"4eb2fd62005-11-25 11:36:36 +0000118 bb_perror_msg_and_die(bb_msg_read_error);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000119 }
120 return(size);
121}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000122#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000123
Manuel Novoa III cad53642003-03-19 09:13:01 +0000124#ifdef L_xread_all
125extern void bb_xread_all(int fd, void *buf, size_t count)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000126{
127 ssize_t size;
128
Manuel Novoa III cad53642003-03-19 09:13:01 +0000129 while (count) {
130 if ((size = bb_xread(fd, buf, count)) == 0) { /* EOF */
131 bb_error_msg_and_die("Short read");
132 }
133 count -= size;
Manuel Novoa III 948d4902004-03-08 05:44:30 +0000134 buf = ((char *) buf) + size;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000135 }
136 return;
137}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000138#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000139
Manuel Novoa III cad53642003-03-19 09:13:01 +0000140#ifdef L_xread_char
141extern unsigned char bb_xread_char(int fd)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000142{
143 char tmp;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000144
Manuel Novoa III cad53642003-03-19 09:13:01 +0000145 bb_xread_all(fd, &tmp, 1);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000146
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000147 return(tmp);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000148}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000149#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000150
Manuel Novoa III cad53642003-03-19 09:13:01 +0000151#ifdef L_xferror
152extern void bb_xferror(FILE *fp, const char *fn)
153{
154 if (ferror(fp)) {
155 bb_error_msg_and_die("%s", fn);
156 }
157}
158#endif
159
160#ifdef L_xferror_stdout
161extern void bb_xferror_stdout(void)
162{
163 bb_xferror(stdout, bb_msg_standard_output);
164}
165#endif
166
167#ifdef L_xfflush_stdout
168extern void bb_xfflush_stdout(void)
169{
170 if (fflush(stdout)) {
171 bb_perror_msg_and_die(bb_msg_standard_output);
172 }
173}
174#endif
175
176#ifdef L_strlen
Manuel Novoa III cad53642003-03-19 09:13:01 +0000177size_t bb_strlen(const char *string)
Eric Andersendb7d5fc2002-04-13 13:02:03 +0000178{
179 return(strlen(string));
180}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000181#endif
Eric Andersendb7d5fc2002-04-13 13:02:03 +0000182
Eric Andersenaad1a882001-03-16 22:47:14 +0000183/* END CODE */
184/*
185Local Variables:
186c-file-style: "linux"
187c-basic-offset: 4
188tab-width: 4
189End:
190*/