unsigned int status_num;
status_file = xfopen(filename, "r");
- while ((control_buffer = fgets_str(status_file, "\n\n")) != NULL) {
+ while ((control_buffer = xmalloc_fgets_str(status_file, "\n\n")) != NULL) {
const unsigned int package_num = fill_package_struct(control_buffer);
if (package_num != -1) {
status_node = xmalloc(sizeof(status_node_t));
int i = 0;
/* Update previously known packages */
- while ((control_buffer = fgets_str(old_status_file, "\n\n")) != NULL) {
+ while ((control_buffer = xmalloc_fgets_str(old_status_file, "\n\n")) != NULL) {
if ((tmp_string = strstr(control_buffer, "Package:")) == NULL) {
continue;
}
}
do {
- f = bb_wfopen_input(*argv);
+ f = fopen_or_warn_stdin(*argv);
if (f) {
off_t r = bb_copyfd_eof(fileno(f), STDOUT_FILENO);
- bb_fclose_nonstdin(f);
+ fclose_if_not_stdin(f);
if (r >= 0) {
continue;
}
int inp_stdin = (argc == optind) ? 1 : 0;
do {
- fp = bb_wfopen_input((inp_stdin) ? bb_msg_standard_input : *++argv);
+ fp = fopen_or_warn_stdin((inp_stdin) ? bb_msg_standard_input : *++argv);
crc = 0;
length = 0;
{
FILE *fp;
- if ((fp = bb_wfopen_input(filename)) != NULL) {
+ fp = fopen_or_warn_stdin(filename);
+ if (fp)
return fp;
- }
-
exit(xfunc_error_retval); /* We already output an error message. */
}
FILE *file;
for (; optind < argc; optind++) {
- file = bb_wfopen(argv[optind], "r");
+ file = fopen_or_warn(argv[optind], "r");
if (file) {
cut_file(file);
fclose(file);
}
do {
- FILE *istream = bb_wfopen_input(*argv);
+ FILE *istream = fopen_or_warn_stdin(*argv);
int c;
int column = 0; /* Screen column where next char will go. */
int offset_out = 0; /* Index in `line_out' for next char. */
fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
}
- if (ferror(istream) || bb_fclose_nonstdin(istream)) {
+ if (ferror(istream) || fclose_if_not_stdin(istream)) {
bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */
errs |= EXIT_FAILURE;
}
#endif
do {
- fp = bb_wfopen_input(*argv);
+ fp = fopen_or_warn_stdin(*argv);
if (fp) {
if (fp == stdin) {
*argv = (char *) bb_msg_standard_input;
}
putchar(c);
}
- if (bb_fclose_nonstdin(fp)) {
+ if (fclose_if_not_stdin(fp)) {
bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */
retval = EXIT_FAILURE;
}
bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
count_failed, count_total);
}
- if (bb_fclose_nonstdin(pre_computed_stream) == EOF) {
+ if (fclose_if_not_stdin(pre_computed_stream) == EOF) {
bb_perror_msg_and_die("cannot close file %s", file_ptr);
}
} else {
fp = stdin;
have_read_stdin++;
} else {
- fp = bb_wfopen(file, "r");
+ fp = fopen_or_warn(file, "r");
if (fp == NULL)
goto out;
}
if (ferror(fp)) {
bb_perror_msg(file);
- bb_fclose_nonstdin(fp);
+ fclose_if_not_stdin(fp);
goto out;
}
- if (bb_fclose_nonstdin(fp) == EOF) {
+ if (fclose_if_not_stdin(fp) == EOF) {
bb_perror_msg(file);
goto out;
}
goto GOT_NEW_FILE;
do {
- if ((*p = bb_wfopen(*argv, mode)) == NULL) {
+ if ((*p = fopen_or_warn(*argv, mode)) == NULL) {
retval = EXIT_FAILURE;
continue;
}
}
free(line);
ret = decode_fn_ptr(src_stream, dst_stream);
- bb_fclose_nonstdin(src_stream);
+ fclose_if_not_stdin(src_stream);
return(ret);
}
bb_error_msg_and_die("No `begin' line");
while ((arg = *argv++) != 0) {
++num_files;
- fp = bb_wfopen_input(arg);
+ fp = fopen_or_warn_stdin(arg);
if (!fp) {
status = EXIT_FAILURE;
continue;
}
totals[WC_LENGTH] -= counts[WC_LENGTH];
- bb_fclose_nonstdin(fp);
+ fclose_if_not_stdin(fp);
OUTPUT:
/* coreutils wc tries hard to print pretty columns
process_files();
continue;
}
- file = bb_wfopen(argv[i], "r");
+ file = fopen_or_warn(argv[i], "r");
if (!file) {
status = EXIT_FAILURE;
continue;
}
}
matched += grep_file(file);
- bb_fclose_nonstdin(file);
+ fclose_if_not_stdin(file);
grep_done:
if (matched < 0) {
/* we found a match but were told to be quiet, stop here and
extern long *find_pid_by_name( const char* pidName);
extern long *pidlist_reverse(long *pidList);
extern char *find_block_device(char *path);
-extern char *xmalloc_fgets(FILE *file);
-/* Chops off '\n' from the end, unlike fgets: */
-extern char *xmalloc_getline(FILE *file);
-extern char *bb_get_chunk_from_file(FILE *file, int *end);
extern off_t bb_copyfd_size(int fd1, int fd2, off_t size);
extern off_t bb_copyfd_eof(int fd1, int fd2);
extern char bb_process_escape_sequence(const char **ptr);
extern char *bb_get_last_path_component(char *path);
-extern FILE *bb_wfopen(const char *path, const char *mode);
-extern FILE *bb_wfopen_input(const char *filename);
-extern FILE *xfopen(const char *path, const char *mode);
-extern int bb_fclose_nonstdin(FILE *f);
+/* Prints to stdout closes entire FILE. Exits on error: */
+extern void xprint_and_close_file(FILE *file);
+extern char *xmalloc_fgets(FILE *file);
+/* /* Read up to (and including) TERMINATING_STRING: */
+extern char *xmalloc_fgets_str(FILE *file, const char *terminating_string);
+/* Chops off '\n' from the end, unlike fgets: */
+extern char *xmalloc_getline(FILE *file);
+extern char *bb_get_chunk_from_file(FILE *file, int *end);
+extern void die_if_ferror(FILE *file, const char *msg);
+extern void die_if_ferror_stdout(void);
+extern void xfflush_stdout(void);
extern void fflush_stdout_and_exit(int retval) ATTRIBUTE_NORETURN;
+extern int fclose_if_not_stdin(FILE *file);
+extern FILE *xfopen(const char *filename, const char *mode);
+/* Prints warning to stderr and returns NULL on failure: */
+extern FILE *fopen_or_warn(const char *filename, const char *mode);
+/* "Opens" stdin if filename is special, else just opens file: */
+extern FILE *fopen_or_warn_stdin(const char *filename);
extern void xstat(char *filename, struct stat *buf);
extern int xsocket(int domain, int type, int protocol);
extern uint32_t option_mask32;
extern uint32_t getopt32(int argc, char **argv, const char *applet_opts, ...);
-extern void die_if_ferror(FILE *fp, const char *fn);
-extern void die_if_ferror_stdout(void);
-extern void xfflush_stdout(void);
-
extern void bb_warn_ignoring_args(int n);
extern void chomp(char *s);
char *concat_subpath_file(const char *path, const char *filename);
char *last_char_is(const char *s, int c);
-char *fgets_str(FILE *file, const char *terminating_string);
-
int execable_file(const char *name);
char *find_execable(const char *filename);
int exists_execable(const char *filename);
void reset_ino_dev_hashtable(void);
char *xasprintf(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
-void xprint_and_close_file(FILE *file);
#define FAIL_DELAY 3
extern void bb_do_delay(int seconds);
compare_string_array.o concat_path_file.o copy_file.o copyfd.o \
crc32.o create_icmp_socket.o create_icmp6_socket.o \
device_open.o dump.o error_msg.o error_msg_and_die.o \
- find_pid_by_name.o find_root_device.o fgets_str.o \
+ find_pid_by_name.o find_root_device.o xmalloc_fgets_str.o \
full_write.o get_last_path_component.o get_line_from_file.o \
herror_msg.o herror_msg_and_die.o \
human_readable.o inet_common.o inode_hash.o isdirectory.o \
#include <stdio.h>
#include <libbb.h>
-int bb_fclose_nonstdin(FILE *f)
+int fclose_if_not_stdin(FILE *f)
{
if (f != stdin) {
return fclose(f);
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
#include "libbb.h"
/* Read up to (and including) TERMINATING_STRING from FILE and return it.
* Return NULL on EOF. */
-char *fgets_str(FILE *file, const char *terminating_string)
+char *xmalloc_fgets_str(FILE *file, const char *terminating_string)
{
char *linebuf = NULL;
const int term_length = strlen(terminating_string);
/* grow the line buffer as necessary */
while (idx > linebufsz - 2) {
- linebuf = xrealloc(linebuf, linebufsz += 1000);
+ linebufsz += 200;
+ linebuf = xrealloc(linebuf, linebufsz);
}
linebuf[idx] = ch;
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*/
-#include <stdio.h>
-#include <errno.h>
#include "libbb.h"
-FILE *bb_wfopen(const char *path, const char *mode)
+FILE *fopen_or_warn(const char *path, const char *mode)
{
- FILE *fp;
- if ((fp = fopen(path, mode)) == NULL) {
+ FILE *fp = fopen(path, mode);
+ if (!fp) {
bb_perror_msg("%s", path);
errno = 0;
}
* Note: We also consider "" to main stdin (for 'cmp' at least).
*/
-#include <stdio.h>
-#include <sys/stat.h>
-#include <libbb.h>
+#include "libbb.h"
-FILE *bb_wfopen_input(const char *filename)
+FILE *fopen_or_warn_stdin(const char *filename)
{
FILE *fp = stdin;
- if ((filename != bb_msg_standard_input)
- && filename[0] && ((filename[0] != '-') || filename[1])
+ if (filename != bb_msg_standard_input
+ && filename[0]
+ && (filename[0] != '-' || filename[1])
) {
- fp = bb_wfopen(filename, "r");
+ fp = fopen_or_warn(filename, "r");
}
return fp;
// close that file.
void xprint_and_close_file(FILE *file)
{
+ fflush(stdout);
// copyfd outputs error messages for us.
if (bb_copyfd_eof(fileno(file), 1) == -1)
exit(xfunc_error_retval);
struct stat statbuf;
- if ((passwd = bb_wfopen(filename, "r"))) {
+ if ((passwd = fopen_or_warn(filename, "r"))) {
// Remove pointless const.
xstat((char *)filename, &statbuf);
buffer = (char *) xmalloc(statbuf.st_size * sizeof(char));
b = boundary(buffer, login);
if (b.stop != 0) {
/* write the file w/o the user */
- if ((passwd = bb_wfopen(filename, "w"))) {
+ if ((passwd = fopen_or_warn(filename, "w"))) {
fwrite(buffer, (b.start - 1), sizeof(char), passwd);
fwrite(&buffer[b.stop], (statbuf.st_size - b.stop), sizeof(char), passwd);
fclose(passwd);
if (argc == 1) bb_show_usage();
while (*++argv) {
- if ((f = bb_wfopen(*argv, "r")) != NULL) {
+ if ((f = fopen_or_warn(*argv, "r")) != NULL) {
int r, fd=fileno(f);
r = readahead(fd, 0, fdlength(fd));
}
do {
- file = bb_wfopen(*argv, "r");
+ file = fopen_or_warn(*argv, "r");
if (file) {
PIPE:
count = 0;
}
count++;
} while (c != EOF);
- bb_fclose_nonstdin(file);
+ fclose_if_not_stdin(file);
} else {
status = EXIT_FAILURE;
}
FILE *input;
int status;
- input = bb_wfopen(child->argv[1], "r");
+ input = fopen_or_warn(child->argv[1], "r");
if (!input) {
return EXIT_FAILURE;
}
llist_add_to(&close_me_list, (void *)(long)fileno(prof_input));
/* Now run the file */
busy_loop(prof_input);
- bb_fclose_nonstdin(prof_input);
+ fclose_if_not_stdin(prof_input);
llist_pop(&close_me_list);
}
}
char line[100], ptname[100], devname[120], *s;
int ma, mi, sz;
- procpt = bb_wfopen("/proc/partitions", "r");
+ procpt = fopen_or_warn("/proc/partitions", "r");
while (fgets(line, sizeof(line), procpt)) {
if (sscanf(line, " %d %d %d %[^\n ]",
if (argc == 0) {
file = stdin;
} else
- file = bb_wfopen(*argv, "r");
+ file = fopen_or_warn(*argv, "r");
if(file==0)
goto loop;