From 3ce8ca13f1fa1b8e694b23a4e287f146e7311a85 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Fri, 10 Aug 2012 12:03:30 +0200 Subject: Rename utils.c to printf-utils.c, remove unused functions --- src/Makefile | 2 +- src/console.c | 12 ++++++--- src/dump.c | 3 ++- src/nolo.c | 5 ++-- src/printf-utils.c | 59 ++++++++++++++++++++++++++++++++++++++++++ src/printf-utils.h | 33 ++++++++++++++++++++++++ src/usb-device.c | 8 +----- src/utils.c | 76 ------------------------------------------------------ 8 files changed, 107 insertions(+), 91 deletions(-) create mode 100644 src/printf-utils.c create mode 100644 src/printf-utils.h delete mode 100644 src/utils.c (limited to 'src') diff --git a/src/Makefile b/src/Makefile index edce894..ed3ef9b 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,6 +1,6 @@ include ../config.mk OBJ=main.o hexdump.o dump.o nolo.o qmode.o -OBJ+=utils.o console.o +OBJ+=printf-utils.o console.o OBJ+=squeue/squeue.o OBJ+=image.o fiasco.o device.o usb-device.o cold-flash.o BIN=0xFFFF diff --git a/src/console.c b/src/console.c index 2a89fc4..be29d94 100644 --- a/src/console.c +++ b/src/console.c @@ -17,11 +17,15 @@ */ #if HAVE_USB -#include "main.h" + #include #include #include #include +#include + +#include "nolo.h" +#include "dump.h" void cmd_exit(char *line) { @@ -72,9 +76,9 @@ void cmd_nanddump(char *line) (char *)&out, (int*)&ignbb, (int *)&ignoob); if (ignoob == -1) { - eprintf("Invalid arguments.\n"); - eprintf("nanddump [dev] [start] [len] [out] [ignore-badblocks] [ignore-oob]\n"); - eprintf(" f.ex: nanddump /dev/mtd0 0x0 0x4000 xloader.bin 1 1\n"); + fprintf(stderr, "Invalid arguments.\n"); + fprintf(stderr, "nanddump [dev] [start] [len] [out] [ignore-badblocks] [ignore-oob]\n"); + fprintf(stderr, " f.ex: nanddump /dev/mtd0 0x0 0x4000 xloader.bin 1 1\n"); } else nanddump(dev, from, length, out, ignbb, ignoob); } diff --git a/src/dump.c b/src/dump.c index ebed3fa..b3270da 100644 --- a/src/dump.c +++ b/src/dump.c @@ -23,6 +23,7 @@ #include "dump.h" #include "hexdump.h" +#include "printf-utils.h" /* * Extracts a piece from an mtd device @@ -389,7 +390,7 @@ int nanddump(char *mtddev, unsigned long start_addr, unsigned long length, char /* Write out page data */ //if (pretty_print) dump_bytes(readbuf, bs); write(ofd, readbuf, bs); - progressbar(ofs, end_addr); + printf_progressbar(ofs, end_addr); // OOB STUFF // if (omitoob) diff --git a/src/nolo.c b/src/nolo.c index e74e2dd..c93139c 100644 --- a/src/nolo.c +++ b/src/nolo.c @@ -25,6 +25,7 @@ #include "nolo.h" #include "image.h" +#include "printf-utils.h" #include "hexdump.h" @@ -182,7 +183,7 @@ void nolo_flash_image(struct image * image) // fclose(fd); return; } - progressbar(off, size); + printf_progressbar(off, size); if (bread<0) { printf("\n"); perror(" -ee- "); @@ -194,7 +195,7 @@ void nolo_flash_image(struct image * image) // fclose(fd); /*/ EOF */ usb_bulk_write(dev, 2, (char *)nolofiller, 0, 1000); - progressbar(1, 1); + printf_progressbar(1, 1); printf("\n"); // index = 4???? diff --git a/src/printf-utils.c b/src/printf-utils.c new file mode 100644 index 0000000..8907dba --- /dev/null +++ b/src/printf-utils.c @@ -0,0 +1,59 @@ +/* + * 0xFFFF - Open Free Fiasco Firmware Flasher + * Copyright (C) 2007 pancake + * Copyright (C) 2012 Pali Rohár + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include + +#include "printf-utils.h" + +int printf_prev = 0; + +void printf_progressbar(unsigned long long part, unsigned long long total) { + + char *columns = getenv("COLUMNS"); + int pc; + int tmp, cols = 80; + + /* percentage calculation */ + pc = (int)(part*100/total); + (pc<0)?pc=0:(pc>100)?pc=100:0; + +#if HAVE_SQUEUE + if (qmode) { + char msg[128]; + sprintf(msg, "%d%%", pc); + squeue_push2(p, "bar", msg, 0); + } else { +#endif + PRINTF_BACK(); + PRINTF_ADD("\x1b[K %3d%% [", pc); + if (columns) + cols = atoi(columns); + if (cols > 115) + cols = 115; + cols-=15; + for(tmp=cols*pc/100;tmp;tmp--) PRINTF_ADD("#"); + for(tmp=cols-(cols*pc/100);tmp;tmp--) PRINTF_ADD("-"); + PRINTF_ADD("]"); + fflush(stdout); +#if HAVE_SQUEUE + } +#endif + +} diff --git a/src/printf-utils.h b/src/printf-utils.h new file mode 100644 index 0000000..5691987 --- /dev/null +++ b/src/printf-utils.h @@ -0,0 +1,33 @@ +/* + * 0xFFFF - Open Free Fiasco Firmware Flasher + * Copyright (C) 2007 pancake + * Copyright (C) 2012 Pali Rohár + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef PRINTF_UTILS_H +#define PRINTF_UTILS_H + +extern int printf_prev; + +#define PRINTF_BACK() do { if ( printf_prev ) { printf("\r%-*s\r", printf_prev, ""); printf_prev = 0; } } while (0) +#define PRINTF_ADD(...) do { printf_prev += printf(__VA_ARGS__); } while (0) +#define PRINTF_LINE(...) do { PRINTF_BACK(); PRINTF_ADD(__VA_ARGS__); fflush(stdout); } while (0) +#define PRINTF_END() do { if ( printf_prev ) { printf("\n"); printf_prev = 0; } } while (0) +#define PRINTF_ERROR(...) do { PRINTF_END(); ERROR_INFO(__VA_ARGS__); } while (0) + +void printf_progressbar(unsigned long long part, unsigned long long total); + +#endif diff --git a/src/usb-device.c b/src/usb-device.c index 400924d..98737dd 100644 --- a/src/usb-device.c +++ b/src/usb-device.c @@ -28,13 +28,7 @@ #include "global.h" #include "device.h" #include "usb-device.h" - -static int prev = 0; -#define PRINTF_BACK() do { if ( prev ) { printf("\r%-*s\r", prev, ""); prev = 0; } } while (0) -#define PRINTF_ADD(format, ...) do { prev += printf(format, ##__VA_ARGS__); } while (0) -#define PRINTF_LINE(format, ...) do { PRINTF_BACK(); PRINTF_ADD(format, ##__VA_ARGS__); fflush(stdout); } while (0) -#define PRINTF_END() do { if ( prev ) { printf("\n"); prev = 0; } } while (0) -#define PRINTF_ERROR(format, ...) do { PRINTF_END(); ERROR_INFO(format, ##__VA_ARGS__); } while (0) +#include "printf-utils.h" static struct usb_flash_device usb_devices[] = { { 0x0421, 0x0105, 2, 1, -1, FLASH_NOLO, { DEVICE_SU_18, DEVICE_RX_44, DEVICE_RX_48, DEVICE_RX_51, 0 } }, diff --git a/src/utils.c b/src/utils.c deleted file mode 100644 index 24872ed..0000000 --- a/src/utils.c +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 0xFFFF - Open Free Fiasco Firmware Flasher - * Copyright (C) 2007 pancake - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "main.h" -#include -#include -#include -#include - -unsigned long get_file_size(const char *file) -{ - FILE *fd = fopen(file, "r"); - unsigned long len = 0; - if (fd == NULL) { - fprintf(stderr, "Cannot open file '%s'\n", file); - exit(1); - } - fseek(fd, 0, SEEK_END); - len = ftell(fd); - fclose(fd); - return len; -} - -void progressbar(unsigned long long part, unsigned long long total) -{ - char *columns = getenv("COLUMNS"); - int pc; - int tmp, cols = 80; - - /* percentage calculation */ - pc = (int)(part*100/total); - (pc<0)?pc=0:(pc>100)?pc=100:0; - -#if HAVE_SQUEUE - if (qmode) { - char msg[128]; - sprintf(msg, "%d%%", pc); - squeue_push2(p, "bar", msg, 0); - } else { -#endif - printf("\x1b[K %3d%% [", pc); - if (columns) - cols = atoi(columns); - cols-=15; - for(tmp=cols*pc/100;tmp;tmp--) printf("#"); - for(tmp=cols-(cols*pc/100);tmp;tmp--) printf("-"); - printf("]\r"); - fflush(stdout); -#if HAVE_SQUEUE - } -#endif -} - -void eprintf(const char *format, ...) -{ - va_list ap; - va_start(ap, format); - vfprintf(stderr, format, ap); - va_end(ap); - //fflush(stderr); // XXX CRASH?!? stdin here?!?!? -} -- cgit v1.2.3