From 5f83300ce6a5da27e68085bddce47b736cfdea79 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Mon, 5 Dec 2022 11:20:59 +0100 Subject: [PATCH] remove calls to standard fprintf function --- code.c | 8 +++-- code.h | 2 +- compress.c | 87 +++++++++++++++++++++++---------------------- debug.c | 2 ++ debug.h | 5 +-- fprintf.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++ fprintf.h | 11 ++++++ skel.c | 2 ++ 8 files changed, 171 insertions(+), 48 deletions(-) create mode 100644 fprintf.c create mode 100644 fprintf.h diff --git a/code.c b/code.c index d8ef5da..6f04c18 100644 --- a/code.c +++ b/code.c @@ -1,4 +1,8 @@ +#include +#include + #include "debug.h" +#include "fprintf.h" #include "code.h" @@ -42,7 +46,7 @@ int codcpy (char *dst, size_t n, char *src) return i; } } - VERBOSE (ERROR, printf ("Buffer too short\n")); + VERBOSE (ERROR, _fprintf (stdout, "Buffer too short\n")); return -1; } @@ -60,4 +64,4 @@ int codlen (char *code) return i; } -// vim: ts=4 sw=4 et +/* vim: set ts=4 sw=4 et */ diff --git a/code.h b/code.h index 977efc3..ce9820b 100644 --- a/code.h +++ b/code.h @@ -22,4 +22,4 @@ int codlen (char *code); #endif /* __CODE_H__ */ -// vim: ts=4 sw=4 et +/* vim: set ts=4 sw=4 et */ diff --git a/compress.c b/compress.c index 8cfce31..91f0d0e 100644 --- a/compress.c +++ b/compress.c @@ -1,11 +1,12 @@ /* depend: */ /* cflags: */ -/* linker: code.o debug.o */ +/* linker: code.o debug.o fprintf.o */ #include #include #include "code.h" #include "debug.h" +#include "fprintf.h" /* constants */ @@ -22,11 +23,11 @@ char *progname = NULL; void usage (int ret) { FILE *fd = ret ? stderr : stdout; - fprintf (fd, "usage: %s\n", progname); - fprintf (fd, " -h : help message\n"); - fprintf (fd, " -i : input file\n"); - fprintf (fd, " -o : output file\n"); - fprintf (fd, " -v : verbose level (%d)\n", verbose); + _fprintf (fd, "usage: %s\n", progname); + _fprintf (fd, " -h : help message\n"); + _fprintf (fd, " -i : input file\n"); + _fprintf (fd, " -o : output file\n"); + _fprintf (fd, " -v : verbose level (%d)\n", verbose); exit (ret); } @@ -52,19 +53,19 @@ int *create_table (char *filename) /* memory allocation */ table = (int *) calloc (NB_BYTES, sizeof (int)); if (table == NULL) { - VERBOSE (ERROR, printf ("can't allocate memory\n")); + VERBOSE (ERROR, _fprintf (stdout, "can't allocate memory\n")); return NULL; } - VERBOSE (INFO, printf ("memory allocated\n")); + VERBOSE (INFO, _fprintf (stdout, "memory allocated\n")); /* open file */ fid = fopen (filename, "rb"); if (fid == NULL) { - VERBOSE (ERROR, printf ("can't open file '%s'\n", filename)); + VERBOSE (ERROR, _fprintf (stdout, "can't open file '%s'\n", filename)); free (table); return NULL; } - VERBOSE (INFO, printf ("file '%s' opened\n", filename)); + VERBOSE (INFO, _fprintf (stdout, "file '%s' opened\n", filename)); /* read file */ while (!feof (fid)) { @@ -89,10 +90,10 @@ void print_occ_table (int *table) { int i; - printf ("Occurence table\n"); + _fprintf (stdout, "Occurence table\n"); for (i = 0; i < NB_BYTES; i++) { if (table[i]) { - printf ("0x%02x '%c': %d\n", i, ((i < 32) || (i > 127)) ? '.' : i, table[i]); + _fprintf (stdout, "0x%02x '%c': %d\n", i, ((i < 32) || (i > 127)) ? '.' : i, table[i]); } } } @@ -127,7 +128,7 @@ leaf_t **init_forest (int *table) /* allocate memory */ leafs = (leaf_t **) calloc (nb_leafs + 1, sizeof (leaf_t *)); if (leafs == NULL) { - VERBOSE (ERROR, printf ("can't allocate memory\n")); + VERBOSE (ERROR, _fprintf (stdout, "can't allocate memory\n")); return NULL; } @@ -136,7 +137,7 @@ leaf_t **init_forest (int *table) if (table[i] > 0) { leafs[l] = (leaf_t *) calloc (1, sizeof (leaf_t)); if (leafs[l] == NULL) { - VERBOSE (ERROR, printf ("can't allocate memory\n")); + VERBOSE (ERROR, _fprintf (stdout, "can't allocate memory\n")); return NULL; } leafs[l]->occ = table[i]; @@ -194,12 +195,12 @@ leaf_t *create_tree (leaf_t **leafs) /* create branch */ if ((last == -1) || (ante == -1)) { - VERBOSE (ERROR, printf ("error during tree building\n")); + VERBOSE (ERROR, _fprintf (stdout, "error during tree building\n")); return NULL; } branch = (leaf_t *) calloc (1, sizeof (leaf_t)); if (branch == NULL) { - VERBOSE (ERROR, printf ("can't allocate memory\n")); + VERBOSE (ERROR, _fprintf (stdout, "can't allocate memory\n")); return NULL; } branch->left = leafs[last]; @@ -255,7 +256,7 @@ code_t *create_code (leaf_t *root) /* allocate table */ table = (code_t *) calloc (NB_BYTES, sizeof (code_t)); if (table == NULL) { - VERBOSE (ERROR, printf ("can't allocate memory\n")); + VERBOSE (ERROR, _fprintf (stdout, "can't allocate memory\n")); return NULL; } @@ -273,13 +274,13 @@ void print_code_table (code_t *codes) char *code; int i; - printf ("Code table\n"); + _fprintf (stdout, "Code table\n"); for (i = 0; i < NB_BYTES; i++) { code = (char *)(codes + i); if (codlen (code) == 0) { continue; } - printf ("0x%02x '%c': %s\n", i, ((i < 32) || (i > 127)) ? '.' : i, code); + _fprintf (stdout, "0x%02x '%c': %s\n", i, ((i < 32) || (i > 127)) ? '.' : i, code); } } @@ -390,9 +391,9 @@ void print_header (byte_t *header) length = (header[3] << 8) + header[4]; VERBOSE (DEBUG, PRINTF ("lengh: %d\n", length)); for (i = 0; i < length + 6; i++) { - printf ("%02x", header[i]); + _fprintf (stdout, "%02x", header[i]); } - printf ("\n"); + _fprintf (stdout, "\n"); } /* write crompressed file */ @@ -412,18 +413,18 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header) /* open input file */ fin = fopen (input, "rb"); if (fin == NULL) { - VERBOSE (ERROR, printf ("can't open file '%s' for reading\n", input)); + VERBOSE (ERROR, _fprintf (stdout, "can't open file '%s' for reading\n", input)); return 1; } - VERBOSE (INFO, printf ("file '%s' opened\n", input)); + VERBOSE (INFO, _fprintf (stdout, "file '%s' opened\n", input)); /* open output file */ fout = fopen (output, "wb"); if (fin == NULL) { - VERBOSE (ERROR, printf ("can't open file '%s' for writing\n", output)); + VERBOSE (ERROR, _fprintf (stdout, "can't open file '%s' for writing\n", output)); return 1; } - VERBOSE (INFO, printf ("file '%s' opened\n", output)); + VERBOSE (INFO, _fprintf (stdout, "file '%s' opened\n", output)); /* write header */ length = (header[3] << 8) + header[4]; @@ -498,10 +499,10 @@ code_t *read_header (char *filename) { /* open file */ fid = fopen (filename, "rb"); if (fid == NULL) { - VERBOSE (ERROR, printf ("can't open file '%s'\n", filename)); + VERBOSE (ERROR, _fprintf (stdout, "can't open file '%s'\n", filename)); return NULL; } - VERBOSE (INFO, printf ("file '%s' opened\n", filename)); + VERBOSE (INFO, _fprintf (stdout, "file '%s' opened\n", filename)); /* read magic number */ nb = fread (buffer, 1, 6, fid); @@ -522,7 +523,7 @@ code_t *read_header (char *filename) { } fclose (fid); if (mode == 0) { - VERBOSE (ERROR, printf ("incorrect file\n")); + VERBOSE (ERROR, _fprintf (stdout, "incorrect file\n")); return NULL; } @@ -551,14 +552,14 @@ code_t *read_header (char *filename) { } if (((mode == 1) && (size - 256 != (l + 7) / 8)) || ((mode == 2) && (size - 2 * nb - 1 != (l + 7) / 8))) { - VERBOSE (ERROR, printf ("incorrect code table length\n")); + VERBOSE (ERROR, _fprintf (stdout, "incorrect code table length\n")); return NULL; } /* allocate table */ table = (code_t *) calloc (NB_BYTES, sizeof (code_t)); if (table == NULL) { - VERBOSE (ERROR, printf ("can't allocate memory\n")); + VERBOSE (ERROR, _fprintf (stdout, "can't allocate memory\n")); return NULL; } @@ -604,25 +605,25 @@ int write_decompress (char *output, char *input, code_t *codes) /* open file for reading */ fin = fopen (input, "rb"); if (fin == NULL) { - VERBOSE (ERROR, printf ("can't open file '%s' for reading\n", input)); + VERBOSE (ERROR, _fprintf (stdout, "can't open file '%s' for reading\n", input)); return 1; } - VERBOSE (INFO, printf ("file '%s' opened\n", input)); + VERBOSE (INFO, _fprintf (stdout, "file '%s' opened\n", input)); /* read magic number */ nb = fread (bufhea, 1, 6, fin); if (nb != 6) { - VERBOSE (ERROR, printf ("can't read file\n")); + VERBOSE (ERROR, _fprintf (stdout, "can't read file\n")); fclose (fin); return 1; } size = (bufhea[3] << 8) + bufhea[4]; - VERBOSE (DEBUG, printf ("table size: %d\n", size)); + VERBOSE (DEBUG, _fprintf (stdout, "table size: %d\n", size)); rem = bufhea[5]; - VERBOSE (DEBUG, printf ("remainder: %d\n", rem)); + VERBOSE (DEBUG, _fprintf (stdout, "remainder: %d\n", rem)); nb = fread (bufhea, 1, size, fin); if (nb != size) { - VERBOSE (ERROR, printf ("can't read file\n")); + VERBOSE (ERROR, _fprintf (stdout, "can't read file\n")); fclose (fin); return 1; } @@ -630,10 +631,10 @@ int write_decompress (char *output, char *input, code_t *codes) /* open file for writing */ fout = fopen (output, "wb"); if (fout == NULL) { - VERBOSE (ERROR, printf ("can't open file '%s' for writing\n", output)); + VERBOSE (ERROR, _fprintf (stdout, "can't open file '%s' for writing\n", output)); return 2; } - VERBOSE (INFO, printf ("file '%s' opened\n", output)); + VERBOSE (INFO, _fprintf (stdout, "file '%s' opened\n", output)); /* write file */ pt = bufout; @@ -707,7 +708,7 @@ int main (int argc, char *argv[]) while (argc-- > 1) { arg = *(++argv); if (arg[0] != '-') { - fprintf (stderr, "%s: invalid option -- %s\n", progname, arg); + _fprintf (stderr, "%s: invalid option -- %s\n", progname, arg); usage (1); } c = arg[1]; @@ -730,11 +731,11 @@ int main (int argc, char *argv[]) case 'v': arg = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL; if (arg == NULL) { - fprintf (stderr, "%s: missing verbose level\n", progname); + _fprintf (stderr, "%s: missing verbose level\n", progname); usage (1); } verbose = atoi (arg); - VERBOSE (INFO, printf ("verbose: %d\n", verbose)); + VERBOSE (INFO, _fprintf (stdout, "verbose: %d\n", verbose)); break; case 'h': default: @@ -742,7 +743,7 @@ int main (int argc, char *argv[]) } } if ((input == NULL) || (output == NULL)) { - fprintf (stderr, "%s: missing file\n", progname); + _fprintf (stderr, "%s: missing file\n", progname); usage (1); } VERBOSE (DEBUG, PRINTF ("end processing arguments\n")); @@ -794,4 +795,4 @@ int main (int argc, char *argv[]) // test: cmp compress.c tmp.c // test: rm compress.mz tmp.c -// vim: ts=4 sw=4 et +/* vim: set ts=4 sw=4 et */ diff --git a/debug.c b/debug.c index b1e3e04..f2ff1ce 100644 --- a/debug.c +++ b/debug.c @@ -1,3 +1,5 @@ #include "debug.h" int verbose = 2; + +/* vim: set ts=4 sw=4 et */ diff --git a/debug.h b/debug.h index 01963d2..64ec161 100644 --- a/debug.h +++ b/debug.h @@ -2,6 +2,7 @@ #define __DEBUG_H__ #include +#include "fprintf.h" /* constants */ @@ -22,7 +23,7 @@ #define MIN(x, y) (((x) < (y)) ? (x) : (y)) #define MAX(x, y) (((x) > (y)) ? (x) : (y)) #define VERBOSE(level, statement...) do { if (level <= verbose) { statement; } } while(0) -#define PRINTF(args...) do { fprintf (stdout, args); fflush (stdout); } while (0) +#define PRINTF(args...) do { _fprintf (stdout, args); fflush (stdout); } while (0) /* gobal variables */ @@ -30,4 +31,4 @@ extern int verbose; #endif /* __DEBUG_H__ */ -// vim: ts=4 sw=4 et +/* vim: set ts=4 sw=4 et */ diff --git a/fprintf.c b/fprintf.c new file mode 100644 index 0000000..47b19c7 --- /dev/null +++ b/fprintf.c @@ -0,0 +1,102 @@ +/* + File name : fprintf.c + Date of creation : 05/12/2022 + Version : 1.0 + Copyright : Soft'n'design + Author : Laurent Mazet + + Description : This file contains embedded printf + + History : + - initial version +*/ + +#include +#include +#include + +inline unsigned int nextpow10 (unsigned int x) { + unsigned int n = 0; + while (x) { + n++; + x = x / 10; + } + return (n == 0) ? 1 : n; +} + +/* simple fprintf function */ + +size_t _fprintf (FILE *fid, const char *fmt, ...) +{ + char buffer[1024 + 1] = { 0 }; + char *str = buffer; + + va_list ap; + va_start (ap, fmt); + while (*fmt) { + char *s; + int d = 0; + unsigned int u; + void *p; + char c = *fmt++; + + /* copy standard char */ + if (c != '%') { + *str++ = c; + } else { + int t = 0; + size_t i; + + /* process format char */ + switch (*fmt++) { + case 'c': /* char */ + c = (char) va_arg (ap, int); + *str++ = c; + break; + case 'd': /* int */ + d = va_arg (ap, int); + if (d < 0) { + *str++ = '-'; + d = -d; + } + t = 1; + /* fall through */ + case 'u': /* unsigned int */ + u = (t) ? (unsigned int)d : va_arg (ap, unsigned int); + for (i = nextpow10 (u), s = str; i > 0; i--, s++) { + str[i - 1] = '0' + (u % 10); + u /= 10; + } + str = s; + break; + case 'p': /* pointer */ + p = va_arg (ap, void *); + *str++ = '0'; + *str++ = 'x'; + for (i = sizeof (void *) * 2; i > 0; i--) { + char x = (char)(((uintptr_t)p >> (i * 4 - 4)) & 0xf); + *str++ = (x > 9) ? 'a' + x - 10 : '0' + x; + } + break; + case 's': /* string */ + s = va_arg (ap, char *); + while (s && *s) + *str++ = *s++; + break; + default: + *str++ = '?'; + } + } + } + va_end (ap); + + /* output string */ + size_t n = str - buffer; + if (n < sizeof (buffer) - 1) { + fwrite (buffer, n, 1, fid); + return n; + } + return 0; +} + +/* vim: set ts=4 sw=4 et */ diff --git a/fprintf.h b/fprintf.h new file mode 100644 index 0000000..053620e --- /dev/null +++ b/fprintf.h @@ -0,0 +1,11 @@ +#ifndef __FPRINTF_H__ +#define __FPRINTF_H__ + +#include +#include + +size_t _fprintf (FILE *fid, const char *fmt, ...); + +#endif /* __FPRINTF_H__ */ + +/* vim: set ts=4 sw=4 et */ diff --git a/skel.c b/skel.c index c517dc5..2219fca 100644 --- a/skel.c +++ b/skel.c @@ -64,3 +64,5 @@ int main (int argc, char *argv[]) // test: skel.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }' // test: skel.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }' // test: skel.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }' + +/* vim: set ts=4 sw=4 et */ -- 2.30.2