blob: 9ee4fcd65a19a4c615cbded3a71793ff65c2ac87 [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
Rob Landleydfba7412006-03-06 20:47:33 +000029void *xmalloc(size_t size)
Eric Andersenaad1a882001-03-16 22:47:14 +000030{
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
Rob Landleydfba7412006-03-06 20:47:33 +000039void *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
Rob Landleydfba7412006-03-06 20:47:33 +000049void *xcalloc(size_t nmemb, size_t size)
Eric Andersenaad1a882001-03-16 22:47:14 +000050{
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
Rob Landley31642d72006-03-14 21:45:38 +000060char * bb_xstrdup (const char *s)
61{
Eric Andersenaad1a882001-03-16 22:47:14 +000062 char *t;
63
64 if (s == NULL)
65 return NULL;
66
67 t = strdup (s);
68
69 if (t == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000070 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersenaad1a882001-03-16 22:47:14 +000071
72 return t;
73}
74#endif
75
Manuel Novoa III cad53642003-03-19 09:13:01 +000076#ifdef L_xstrndup
Rob Landley31642d72006-03-14 21:45:38 +000077char * bb_xstrndup (const char *s, int n)
78{
Eric Andersenaad1a882001-03-16 22:47:14 +000079 char *t;
80
81 if (s == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000082 bb_error_msg_and_die("bb_xstrndup bug");
Eric Andersenaad1a882001-03-16 22:47:14 +000083
84 t = xmalloc(++n);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000085
Eric Andersenaad1a882001-03-16 22:47:14 +000086 return safe_strncpy(t,s,n);
87}
Manuel Novoa III cad53642003-03-19 09:13:01 +000088#endif
Eric Andersenaad1a882001-03-16 22:47:14 +000089
Manuel Novoa III cad53642003-03-19 09:13:01 +000090#ifdef L_xfopen
91FILE *bb_xfopen(const char *path, const char *mode)
Eric Andersenaad1a882001-03-16 22:47:14 +000092{
93 FILE *fp;
94 if ((fp = fopen(path, mode)) == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000095 bb_perror_msg_and_die("%s", path);
Eric Andersenaad1a882001-03-16 22:47:14 +000096 return fp;
97}
Manuel Novoa III cad53642003-03-19 09:13:01 +000098#endif
Eric Andersenaad1a882001-03-16 22:47:14 +000099
Manuel Novoa III cad53642003-03-19 09:13:01 +0000100#ifdef L_xopen
Rob Landleydfba7412006-03-06 20:47:33 +0000101int bb_xopen(const char *pathname, int flags)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000102{
103 int ret;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000104
Glenn L McGrath237ae422002-11-03 14:05:15 +0000105 ret = open(pathname, flags, 0777);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000106 if (ret == -1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000107 bb_perror_msg_and_die("%s", pathname);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000108 }
109 return ret;
110}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000111#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000112
Manuel Novoa III cad53642003-03-19 09:13:01 +0000113#ifdef L_xread
Rob Landleydfba7412006-03-06 20:47:33 +0000114ssize_t bb_xread(int fd, void *buf, size_t count)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000115{
116 ssize_t size;
117
118 size = read(fd, buf, count);
119 if (size == -1) {
"Vladimir N. Oleynik"4eb2fd62005-11-25 11:36:36 +0000120 bb_perror_msg_and_die(bb_msg_read_error);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000121 }
122 return(size);
123}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000124#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000125
Manuel Novoa III cad53642003-03-19 09:13:01 +0000126#ifdef L_xread_all
Rob Landleydfba7412006-03-06 20:47:33 +0000127void bb_xread_all(int fd, void *buf, size_t count)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000128{
129 ssize_t size;
130
Manuel Novoa III cad53642003-03-19 09:13:01 +0000131 while (count) {
132 if ((size = bb_xread(fd, buf, count)) == 0) { /* EOF */
133 bb_error_msg_and_die("Short read");
134 }
135 count -= size;
Manuel Novoa III 948d4902004-03-08 05:44:30 +0000136 buf = ((char *) buf) + size;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000137 }
138 return;
139}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000140#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000141
Manuel Novoa III cad53642003-03-19 09:13:01 +0000142#ifdef L_xread_char
Rob Landleydfba7412006-03-06 20:47:33 +0000143unsigned char bb_xread_char(int fd)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000144{
145 char tmp;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000146
Manuel Novoa III cad53642003-03-19 09:13:01 +0000147 bb_xread_all(fd, &tmp, 1);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000148
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000149 return(tmp);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000150}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000151#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000152
Manuel Novoa III cad53642003-03-19 09:13:01 +0000153#ifdef L_xferror
Rob Landleydfba7412006-03-06 20:47:33 +0000154void bb_xferror(FILE *fp, const char *fn)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000155{
156 if (ferror(fp)) {
157 bb_error_msg_and_die("%s", fn);
158 }
159}
160#endif
161
162#ifdef L_xferror_stdout
Rob Landleydfba7412006-03-06 20:47:33 +0000163void bb_xferror_stdout(void)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000164{
165 bb_xferror(stdout, bb_msg_standard_output);
166}
167#endif
168
169#ifdef L_xfflush_stdout
Rob Landleydfba7412006-03-06 20:47:33 +0000170void bb_xfflush_stdout(void)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000171{
172 if (fflush(stdout)) {
173 bb_perror_msg_and_die(bb_msg_standard_output);
174 }
175}
176#endif
177
178#ifdef L_strlen
Manuel Novoa III cad53642003-03-19 09:13:01 +0000179size_t bb_strlen(const char *string)
Eric Andersendb7d5fc2002-04-13 13:02:03 +0000180{
181 return(strlen(string));
182}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000183#endif
Eric Andersendb7d5fc2002-04-13 13:02:03 +0000184
Eric Andersenaad1a882001-03-16 22:47:14 +0000185/* END CODE */
186/*
187Local Variables:
188c-file-style: "linux"
189c-basic-offset: 4
190tab-width: 4
191End:
192*/