blob: 9b9081e2635c6b4690371856b36caee3670c8811 [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>
Rob Landley552b56d2006-05-04 21:22:27 +000017#include "busybox.h"
Eric Andersenaad1a882001-03-16 22:47:14 +000018
19#ifndef DMALLOC
Manuel Novoa III cad53642003-03-19 09:13:01 +000020#ifdef L_xmalloc
Rob Landleydfba7412006-03-06 20:47:33 +000021void *xmalloc(size_t size)
Eric Andersenaad1a882001-03-16 22:47:14 +000022{
23 void *ptr = malloc(size);
Matt Kraaia99b1942002-02-26 15:28:22 +000024 if (ptr == NULL && size != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000025 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersenaad1a882001-03-16 22:47:14 +000026 return ptr;
27}
Manuel Novoa III cad53642003-03-19 09:13:01 +000028#endif
Eric Andersenaad1a882001-03-16 22:47:14 +000029
Manuel Novoa III cad53642003-03-19 09:13:01 +000030#ifdef L_xrealloc
Rob Landleydfba7412006-03-06 20:47:33 +000031void *xrealloc(void *ptr, size_t size)
Eric Andersenaad1a882001-03-16 22:47:14 +000032{
Matt Kraaia99b1942002-02-26 15:28:22 +000033 ptr = realloc(ptr, size);
34 if (ptr == NULL && size != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000035 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersenaad1a882001-03-16 22:47:14 +000036 return ptr;
37}
Manuel Novoa III cad53642003-03-19 09:13:01 +000038#endif
Eric Andersenaad1a882001-03-16 22:47:14 +000039
Manuel Novoa III cad53642003-03-19 09:13:01 +000040#ifdef L_xcalloc
Rob Landleydfba7412006-03-06 20:47:33 +000041void *xcalloc(size_t nmemb, size_t size)
Eric Andersenaad1a882001-03-16 22:47:14 +000042{
43 void *ptr = calloc(nmemb, size);
Matt Kraaia99b1942002-02-26 15:28:22 +000044 if (ptr == NULL && nmemb != 0 && size != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000045 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersenaad1a882001-03-16 22:47:14 +000046 return ptr;
47}
Manuel Novoa III cad53642003-03-19 09:13:01 +000048#endif
Eric Andersen9f894f42003-07-05 22:15:43 +000049#endif /* DMALLOC */
Eric Andersenaad1a882001-03-16 22:47:14 +000050
Manuel Novoa III cad53642003-03-19 09:13:01 +000051#ifdef L_xstrdup
Rob Landley31642d72006-03-14 21:45:38 +000052char * bb_xstrdup (const char *s)
53{
Eric Andersenaad1a882001-03-16 22:47:14 +000054 char *t;
55
56 if (s == NULL)
57 return NULL;
58
59 t = strdup (s);
60
61 if (t == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000062 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersenaad1a882001-03-16 22:47:14 +000063
64 return t;
65}
66#endif
67
Manuel Novoa III cad53642003-03-19 09:13:01 +000068#ifdef L_xstrndup
Rob Landley31642d72006-03-14 21:45:38 +000069char * bb_xstrndup (const char *s, int n)
70{
Eric Andersenaad1a882001-03-16 22:47:14 +000071 char *t;
72
73 if (s == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 bb_error_msg_and_die("bb_xstrndup bug");
Eric Andersenaad1a882001-03-16 22:47:14 +000075
76 t = xmalloc(++n);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000077
Eric Andersenaad1a882001-03-16 22:47:14 +000078 return safe_strncpy(t,s,n);
79}
Manuel Novoa III cad53642003-03-19 09:13:01 +000080#endif
Eric Andersenaad1a882001-03-16 22:47:14 +000081
Manuel Novoa III cad53642003-03-19 09:13:01 +000082#ifdef L_xfopen
83FILE *bb_xfopen(const char *path, const char *mode)
Eric Andersenaad1a882001-03-16 22:47:14 +000084{
85 FILE *fp;
86 if ((fp = fopen(path, mode)) == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000087 bb_perror_msg_and_die("%s", path);
Eric Andersenaad1a882001-03-16 22:47:14 +000088 return fp;
89}
Manuel Novoa III cad53642003-03-19 09:13:01 +000090#endif
Eric Andersenaad1a882001-03-16 22:47:14 +000091
Manuel Novoa III cad53642003-03-19 09:13:01 +000092#ifdef L_xopen
Rob Landleydfba7412006-03-06 20:47:33 +000093int bb_xopen(const char *pathname, int flags)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000094{
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +000095 return bb_xopen3(pathname, flags, 0777);
96}
97#endif
98
99#ifdef L_xopen3
100int bb_xopen3(const char *pathname, int flags, int mode)
101{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000102 int ret;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000103
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +0000104 ret = open(pathname, flags, mode);
105 if (ret < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000106 bb_perror_msg_and_die("%s", pathname);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000107 }
108 return ret;
109}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000110#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000111
Manuel Novoa III cad53642003-03-19 09:13:01 +0000112#ifdef L_xread
Rob Landleydfba7412006-03-06 20:47:33 +0000113ssize_t bb_xread(int fd, void *buf, size_t count)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000114{
115 ssize_t size;
116
117 size = read(fd, buf, count);
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +0000118 if (size < 0) {
"Vladimir N. Oleynik"4eb2fd62005-11-25 11:36:36 +0000119 bb_perror_msg_and_die(bb_msg_read_error);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000120 }
121 return(size);
122}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000123#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000124
Manuel Novoa III cad53642003-03-19 09:13:01 +0000125#ifdef L_xread_all
Rob Landleydfba7412006-03-06 20:47:33 +0000126void bb_xread_all(int fd, void *buf, size_t count)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000127{
128 ssize_t size;
129
Manuel Novoa III cad53642003-03-19 09:13:01 +0000130 while (count) {
131 if ((size = bb_xread(fd, buf, count)) == 0) { /* EOF */
132 bb_error_msg_and_die("Short read");
133 }
134 count -= size;
Manuel Novoa III 948d4902004-03-08 05:44:30 +0000135 buf = ((char *) buf) + size;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000136 }
137 return;
138}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000139#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000140
Manuel Novoa III cad53642003-03-19 09:13:01 +0000141#ifdef L_xread_char
Rob Landleydfba7412006-03-06 20:47:33 +0000142unsigned char bb_xread_char(int fd)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000143{
144 char tmp;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000145
Manuel Novoa III cad53642003-03-19 09:13:01 +0000146 bb_xread_all(fd, &tmp, 1);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000147
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000148 return(tmp);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000149}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000150#endif
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000151
Manuel Novoa III cad53642003-03-19 09:13:01 +0000152#ifdef L_xferror
Rob Landleydfba7412006-03-06 20:47:33 +0000153void bb_xferror(FILE *fp, const char *fn)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000154{
155 if (ferror(fp)) {
156 bb_error_msg_and_die("%s", fn);
157 }
158}
159#endif
160
161#ifdef L_xferror_stdout
Rob Landleydfba7412006-03-06 20:47:33 +0000162void bb_xferror_stdout(void)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000163{
164 bb_xferror(stdout, bb_msg_standard_output);
165}
166#endif
167
168#ifdef L_xfflush_stdout
Rob Landleydfba7412006-03-06 20:47:33 +0000169void bb_xfflush_stdout(void)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000170{
171 if (fflush(stdout)) {
172 bb_perror_msg_and_die(bb_msg_standard_output);
173 }
174}
175#endif
176
Eric Andersenaad1a882001-03-16 22:47:14 +0000177/* END CODE */
178/*
179Local Variables:
180c-file-style: "linux"
181c-basic-offset: 4
182tab-width: 4
183End:
184*/