remove stdout/stderr calls
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Wed, 7 Dec 2022 09:07:03 +0000 (10:07 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Wed, 7 Dec 2022 09:07:03 +0000 (10:07 +0100)
code.c
code.h
compress.c
debug.h
fprintf.c
fprintf.h

diff --git a/code.c b/code.c
index 70b170ec1857b88391215f1095769c6342651536..67220e05b02dd97a390f25a7d49409da3872d840 100644 (file)
--- a/code.c
+++ b/code.c
@@ -1,5 +1,5 @@
 #include <stddef.h>
-#include <stdio.h>
+#include <unistd.h>
 
 #include "debug.h"
 #include "fprintf.h"
@@ -8,7 +8,7 @@
 
 /* concatenate code */
 
-int codcat (char *dst, size_t n, char *src)
+int codcat (char *dst, int n, char *src)
 {
     while ((*dst != 0) && (n > 0)) {
         n--;
@@ -36,9 +36,9 @@ int codcmp (char *cod1, char *cod2)
 
 /* copy code */
 
-int codcpy (char *dst, size_t n, char *src)
+int codcpy (char *dst, int n, char *src)
 {
-    size_t i;
+    int i;
 
     for (i = 0; i < n; i++) {
         dst[i] = src[i];
@@ -46,7 +46,7 @@ int codcpy (char *dst, size_t n, char *src)
             return i;
         }
     }
-    VERBOSE (ERROR, myfprintf (stdout, "Buffer too short\n"));
+    VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "Buffer too short\n"));
 
     return -1;
 }
diff --git a/code.h b/code.h
index ce9820b246ea7d2af8104a7f977eba16f2f4b8fd..684745bde13928e300572a57cc1154fc14ba2f08 100644 (file)
--- a/code.h
+++ b/code.h
@@ -1,8 +1,6 @@
 #ifndef __CODE_H__
 #define __CODE_H__
 
-#include <stddef.h>
-
 #define NB_BYTES 256
 
 /* byte type */
@@ -15,9 +13,9 @@ typedef struct {
     char code[NB_BYTES - 1 + 1];
 } code_t;
 
-int codcat (char *dst, size_t n, char *src);
+int codcat (char *dst, int n, char *src);
 int codcmp (char *cod1, char *cod2);
-int codcpy (char *dst, size_t n, char *src);
+int codcpy (char *dst, int n, char *src);
 int codlen (char *code);
 
 #endif /* __CODE_H__ */
index bda2587b8d7dc01cefaba382aa9f764f631297a6..539429c1927908162a3e084e6958296def57dd7a 100644 (file)
@@ -24,12 +24,12 @@ char *progname = NULL;
 
 void usage (int ret)
 {
-    FILE *fd = ret ? stderr : stdout;
-    myfprintf (fd, "usage: %s\n", progname);
-    myfprintf (fd, " -h : help message\n");
-    myfprintf (fd, " -i <file>: input file\n");
-    myfprintf (fd, " -o <file>: output file\n");
-    myfprintf (fd, " -v : verbose level (%d)\n", verbose);
+    int fd = ret ? STDERR_FILENO : STDOUT_FILENO;
+    fdprintf (fd, "usage: %s\n", progname);
+    fdprintf (fd, " -h : help message\n");
+    fdprintf (fd, " -i <file>: input file\n");
+    fdprintf (fd, " -o <file>: output file\n");
+    fdprintf (fd, " -v : verbose level (%d)\n", verbose);
 
     exit (ret);
 }
@@ -50,28 +50,28 @@ int *create_table (char *filename)
     int *table = NULL;
     int fid = 0;
 
-    VERBOSE (DEBUG, PRINTF ("start creating occurence table\n"));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start creating occurence table\n"));
 
     /* memory allocation */
     table = (int *) calloc (NB_BYTES, sizeof (int));
     if (table == NULL) {
-        VERBOSE (ERROR, myfprintf (stdout, "can't allocate memory\n"));
+        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't allocate memory\n"));
         return NULL;
     }
-    VERBOSE (INFO, myfprintf (stdout, "memory allocated\n"));
+    VERBOSE (INFO, fdprintf (STDOUT_FILENO, "memory allocated\n"));
 
     /* open file */
     fid = open (filename, O_RDONLY|O_RAW);
     if (fid == -1) {
-        VERBOSE (ERROR, myfprintf (stdout, "can't open file '%s'\n", filename));
+        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't open file '%s'\n", filename));
         free (table);
         return NULL;
     }
-    VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", filename));
+    VERBOSE (INFO, fdprintf (STDOUT_FILENO, "file '%s' opened\n", filename));
 
     /* read file */
     while ((nbread = read (fid, buffer, BUFFER_SIZE)) > 0) {
-        VERBOSE (DEBUG, PRINTF ("nbread: %d\n", nbread));
+        VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "nbread: %d\n", nbread));
         while (nbread--) {
             table[(int)buffer[nbread]]++;
         }
@@ -80,7 +80,7 @@ int *create_table (char *filename)
     /* close file */
     close (fid);
 
-    VERBOSE (DEBUG, PRINTF ("end creating occurence table\n"));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end creating occurence table\n"));
 
     return table;
 }
@@ -91,10 +91,10 @@ void print_occ_table (int *table)
 {
     int i;
 
-    myfprintf (stdout, "Occurence table\n");
+    fdprintf (STDOUT_FILENO, "Occurence table\n");
     for (i = 0; i < NB_BYTES; i++) {
         if (table[i]) {
-            myfprintf (stdout, "0x%02x '%c': %d\n", i, ((i < 32) || (i > 127)) ? '.' : i, table[i]);
+            fdprintf (STDOUT_FILENO, "0x%02x '%c': %d\n", i, ((i < 32) || (i > 127)) ? '.' : i, table[i]);
         }
     }
 }
@@ -117,7 +117,7 @@ leaf_t **init_forest (int *table)
     int nb_leafs = 0;
     int i, l;
 
-    VERBOSE (DEBUG, PRINTF ("start initiliazing forest\n"));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start initiliazing forest\n"));
 
     /* count number of leafs */
     for (i = 0; i < NB_BYTES; i++) {
@@ -129,7 +129,7 @@ leaf_t **init_forest (int *table)
     /* allocate memory */
     leafs = (leaf_t **) calloc (nb_leafs + 1, sizeof (leaf_t *));
     if (leafs == NULL) {
-        VERBOSE (ERROR, myfprintf (stdout, "can't allocate memory\n"));
+        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't allocate memory\n"));
         return NULL;
     }
 
@@ -138,7 +138,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, myfprintf (stdout, "can't allocate memory\n"));
+                VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't allocate memory\n"));
                 return NULL;
             }
             leafs[l]->occ = table[i];
@@ -147,7 +147,7 @@ leaf_t **init_forest (int *table)
         }
     }
 
-    VERBOSE (DEBUG, PRINTF ("end initiliazing forest\n"));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end initiliazing forest\n"));
 
     return leafs;
 }
@@ -162,7 +162,7 @@ leaf_t *create_tree (leaf_t **leafs)
     int ante;
     int i, j;
 
-    VERBOSE (DEBUG, PRINTF ("start creating tree\n"));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start creating tree\n"));
 
     /* count number of leafs */
     while (leafs[nb_leafs] != NULL) {
@@ -196,12 +196,12 @@ leaf_t *create_tree (leaf_t **leafs)
 
         /* create branch */
         if ((last == -1) || (ante == -1)) {
-            VERBOSE (ERROR, myfprintf (stdout, "error during tree building\n"));
+            VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "error during tree building\n"));
             return NULL;
         }
         branch = (leaf_t *) calloc (1, sizeof (leaf_t));
         if (branch == NULL) {
-            VERBOSE (ERROR, myfprintf (stdout, "can't allocate memory\n"));
+            VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't allocate memory\n"));
             return NULL;
         }
         branch->left = leafs[last];
@@ -211,7 +211,7 @@ leaf_t *create_tree (leaf_t **leafs)
         leafs[ante] = NULL;
     }
 
-    VERBOSE (DEBUG, PRINTF ("end creating tree\n"));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end creating tree\n"));
 
     return (last != -1) ? leafs[last] : NULL;
 }
@@ -252,18 +252,18 @@ code_t *create_code (leaf_t *root)
     code_t *table = NULL;
     code_t code = {0};
 
-    VERBOSE (DEBUG, PRINTF ("start creating code table\n"));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start creating code table\n"));
 
     /* allocate table */
     table = (code_t *) calloc (NB_BYTES, sizeof (code_t));
     if (table == NULL) {
-        VERBOSE (ERROR, myfprintf (stdout, "can't allocate memory\n"));
+        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't allocate memory\n"));
         return NULL;
     }
 
     explore_tree (table, root, (char *)&code, 0);
 
-    VERBOSE (DEBUG, PRINTF ("end creating code table\n"));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end creating code table\n"));
 
     return table;
 }
@@ -275,13 +275,13 @@ void print_code_table (code_t *codes)
     char *code;
     int i;
 
-    myfprintf (stdout, "Code table\n");
+    fdprintf (STDOUT_FILENO, "Code table\n");
     for (i = 0; i < NB_BYTES; i++) {
         code = (char *)(codes + i);
         if (codlen (code) == 0) {
             continue;
         }
-        myfprintf (stdout, "0x%02x '%c': %s\n", i, ((i < 32) || (i > 127)) ? '.' : i, code);
+        fdprintf (STDOUT_FILENO, "0x%02x '%c': %s\n", i, ((i < 32) || (i > 127)) ? '.' : i, code);
     }
 }
 
@@ -297,7 +297,7 @@ byte_t *encode_header_table (code_t *codes, int *occ)
     int nb = 0;
     int size = 0;
 
-    VERBOSE (DEBUG, PRINTF ("start encoding header and code table\n"));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start encoding header and code table\n"));
 
     /* mode 1 or 2 */
     for (i = 0; i < NB_BYTES; i++) {
@@ -308,10 +308,10 @@ byte_t *encode_header_table (code_t *codes, int *occ)
         }
     }
     mode = (NB_BYTES < 2 * nb + 1) ? 1 : 2;
-    VERBOSE (DEBUG, PRINTF ("nb chars: %d\n", nb));
-    VERBOSE (DEBUG, PRINTF ("mode: %d\n", mode));
-    VERBOSE (DEBUG, PRINTF ("size: %d\n", size));
-    VERBOSE (DEBUG, PRINTF ("rem: %d\n", size % 256));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "nb chars: %d\n", nb));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "mode: %d\n", mode));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "size: %d\n", size));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "rem: %d\n", size % 256));
 
     /* header */
     codcpy ((char *)header, sizeof (buffer), (mode == 1) ? "MZ1   " : "MZ2   ");
@@ -369,7 +369,7 @@ byte_t *encode_header_table (code_t *codes, int *occ)
 
     /* length */
     length = (int)(header - buffer - 6);
-    VERBOSE (DEBUG, PRINTF ("lengh: %d %02x %02x\n", length, length >> 8, length & 0xff));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "lengh: %d %02x %02x\n", length, length >> 8, length & 0xff));
     buffer[3] = (byte_t)(length >> 8);
     buffer[4] = (byte_t)(length & 0xff);
     buffer[5] = (byte_t)(size % 256);
@@ -378,7 +378,7 @@ byte_t *encode_header_table (code_t *codes, int *occ)
     header = (byte_t *) calloc (length + 6, 1);
     blkcpy (header, buffer, length + 6);
 
-    VERBOSE (DEBUG, PRINTF ("end encoding header and code table\n"));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end encoding header and code table\n"));
 
     return header;
 }
@@ -390,11 +390,11 @@ void print_header (byte_t *header)
     int length, i;
 
     length = (header[3] << 8) + header[4];
-    VERBOSE (DEBUG, PRINTF ("lengh: %d\n", length));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "lengh: %d\n", length));
     for (i = 0; i < length + 6; i++) {
-        myfprintf (stdout, "%02x", header[i]);
+        fdprintf (STDOUT_FILENO, "%02x", header[i]);
     }
-    myfprintf (stdout, "\n");
+    fdprintf (STDOUT_FILENO, "\n");
 }
 
 /* write crompressed file */
@@ -409,34 +409,34 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header)
     int i, j, nbread;
     byte_t *pt;
 
-    VERBOSE (DEBUG, PRINTF ("start writting compressed file\n"));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start writting compressed file\n"));
 
     /* open input file */
     fin = open (input, O_RDONLY|O_RAW);
     if (fin == -1) {
-        VERBOSE (ERROR, myfprintf (stdout, "can't open file '%s' for reading\n", input));
+        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't open file '%s' for reading\n", input));
         return 1;
     }
-    VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", input));
+    VERBOSE (INFO, fdprintf (STDOUT_FILENO, "file '%s' opened\n", input));
 
     /* open output file */
     fout = open (output, O_WRONLY|O_CREAT|O_RAW, 0700);
     if (fout == -1) {
-        VERBOSE (ERROR, myfprintf (stdout, "can't open file '%s' for writing\n", output));
+        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't open file '%s' for writing\n", output));
            close (fin);
         return 1;
     }
-    VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", output));
+    VERBOSE (INFO, fdprintf (STDOUT_FILENO, "file '%s' opened\n", output));
 
     /* write header */
     length = (header[3] << 8) + header[4];
-    VERBOSE (DEBUG, PRINTF ("lengh: %d\n", length));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "lengh: %d\n", length));
     write (fout, header, length + 6);
 
     /* write file */
     pt = bufout;
     while ((nbread = read (fin, bufin, BUFFER_SIZE)) > 0) {
-        VERBOSE (DEBUG, PRINTF ("nbread: %d\n", nbread));
+        VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "nbread: %d\n", nbread));
         for (i = 0; i < nbread; i++) {
             codcat (bits, sizeof (code_t), (char *)(codes + bufin[i]));
             while (codlen (bits) > (8 - 1)) {
@@ -456,7 +456,7 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header)
             }
         }
     }
-    VERBOSE (DEBUG, PRINTF ("lastest bits : %d\n", codlen (bits)));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "lastest bits : %d\n", codlen (bits)));
     if (codlen (bits) > 0) {
         for (j = 0; j < (int)codlen (bits); j++) {
             *pt <<= 1;
@@ -470,7 +470,7 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header)
         pt++;
     }
     if (pt != bufout) {
-        VERBOSE (DEBUG, PRINTF ("last partial buffer written: %u\n", pt - bufout));
+        VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "last partial buffer written: %u\n", pt - bufout));
         write (fout, bufout, pt - bufout);
     }
     
@@ -478,7 +478,7 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header)
     close (fin);
     close (fout);
 
-    VERBOSE (DEBUG, PRINTF ("end writting compressed file\n"));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end writting compressed file\n"));
 
     return 0;
 }
@@ -495,28 +495,28 @@ code_t *read_header (char *filename) {
     int mode = 0;
     int i, j, l, nb, size;
 
-    VERBOSE (DEBUG, PRINTF ("start reading header\n"));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start reading header\n"));
 
     /* open file */
     fid = open (filename, O_RDONLY|O_RAW);
     if (fid == -1) {
-        VERBOSE (ERROR, myfprintf (stdout, "can't open file '%s'\n", filename));
+        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't open file '%s'\n", filename));
         return NULL;
     }
-    VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", filename));
+    VERBOSE (INFO, fdprintf (STDOUT_FILENO, "file '%s' opened\n", filename));
 
     /* read magic number */
     nb = read (fid, buffer, 6);
-    VERBOSE (DEBUG, PRINTF ("nb, buffer: %d 0x%02x 0x%02x\n", nb, buffer[0], buffer[1]));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "nb, buffer: %d 0x%02x 0x%02x\n", nb, buffer[0], buffer[1]));
     if ((nb == 6) && (buffer[0] == 'M') && (buffer[1] == 'Z')) {
         mode = (buffer[2] == '1') ? 1 : (buffer[2] == '2') ? 2 : 0;
         size = (buffer[3] << 8) + buffer[4];
-        VERBOSE (DEBUG, PRINTF ("mode, size: %d %d\n", mode, size));
+        VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "mode, size: %d %d\n", mode, size));
         if (size > NB_BYTES * (NB_BYTES - 1) / 2 / 8 + NB_BYTES) {
             mode = 0;
         } else {
             nb = read (fid, buffer, size);
-            VERBOSE (DEBUG, PRINTF ("nb read: %d/%d\n", nb, size));
+            VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "nb read: %d/%d\n", nb, size));
             if (nb != size) {
                 mode = 0;
             }
@@ -524,7 +524,7 @@ code_t *read_header (char *filename) {
     }
     close (fid);
     if (mode == 0) {
-        VERBOSE (ERROR, myfprintf (stdout, "incorrect file\n"));
+        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "incorrect file\n"));
         return NULL;
     }
 
@@ -538,14 +538,14 @@ code_t *read_header (char *filename) {
         break;
     case 2:
         nb = *(codes++) + 1;
-        VERBOSE (DEBUG, PRINTF ("nb codes: %d\n", nb));
+        VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "nb codes: %d\n", nb));
         for (i = 0; i < nb; i++) {
             j = *(codes++);
             lengths[j] = *(codes++);
         }
         break;
     }
-    VERBOSE (DEBUG, for (i = 0; i < NB_BYTES; i++) if (lengths[i]) PRINTF ("%d: %d\n", i, lengths[i]));
+    VERBOSE (DEBUG, for (i = 0; i < NB_BYTES; i++) if (lengths[i]) fdprintf (STDOUT_FILENO, "%d: %d\n", i, lengths[i]));
 
     /* check lengths */
     for (i = 0, l = 0; i < NB_BYTES; i++) {
@@ -553,14 +553,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, myfprintf (stdout, "incorrect code table length: %d %d %d\n", size, nb, l));
+        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "incorrect code table length: %d %d %d\n", size, nb, l));
         return NULL;
     }
 
     /* allocate table */
     table = (code_t *) calloc (NB_BYTES, sizeof (code_t));
     if (table == NULL) {
-        VERBOSE (ERROR, myfprintf (stdout, "can't allocate memory\n"));
+        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't allocate memory\n"));
         return NULL;
     }
 
@@ -582,7 +582,7 @@ code_t *read_header (char *filename) {
         }
     }
 
-    VERBOSE (DEBUG, PRINTF ("end reading header\n"));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end reading header\n"));
 
     return table;
 }
@@ -601,30 +601,30 @@ int write_decompress (char *output, char *input, code_t *codes)
     int l = 0;
     byte_t *pt;
 
-    VERBOSE (DEBUG, PRINTF ("start writing decompressed file\n"));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start writing decompressed file\n"));
 
     /* open file for reading */
     fin = open (input, O_RDONLY|O_RAW);
     if (fin == -1) {
-        VERBOSE (ERROR, myfprintf (stdout, "can't open file '%s' for reading\n", input));
+        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't open file '%s' for reading\n", input));
         return 1;
     }
-    VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", input));
+    VERBOSE (INFO, fdprintf (STDOUT_FILENO, "file '%s' opened\n", input));
 
     /* read magic number */
     nb = read (fin, bufhea, 6);
     if (nb != 6) {
-        VERBOSE (ERROR, myfprintf (stdout, "can't read file\n"));
+        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't read file\n"));
         close (fin);
         return 1;
     }
     size = (bufhea[3] << 8) + bufhea[4];
-    VERBOSE (DEBUG, myfprintf (stdout, "table size: %d\n", size));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "table size: %d\n", size));
     rem = bufhea[5];
-    VERBOSE (DEBUG, myfprintf (stdout, "remainder: %d\n", rem));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "remainder: %d\n", rem));
     nb = read (fin, bufhea, size);
     if (nb != size) {
-        VERBOSE (ERROR, myfprintf (stdout, "can't read file\n"));
+        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't read file\n"));
         close (fin);
         return 1;
     }
@@ -632,33 +632,33 @@ int write_decompress (char *output, char *input, code_t *codes)
     /* open file for writing */
     fout = open (output, O_WRONLY|O_CREAT|O_RAW, 0700);
     if (fout == -1) {
-        VERBOSE (ERROR, myfprintf (stdout, "can't open file '%s' for writing\n", output));
+        VERBOSE (ERROR, fdprintf (STDOUT_FILENO, "can't open file '%s' for writing\n", output));
         close (fin);
        return 2;
     }
-    VERBOSE (INFO, myfprintf (stdout, "file '%s' opened\n", output));
+    VERBOSE (INFO, fdprintf (STDOUT_FILENO, "file '%s' opened\n", output));
 
     /* write file */
     pt = bufout;
     while ((nb = read (fin, bufin, BUFFER_SIZE)) > 0) {
-        VERBOSE (DEBUG, PRINTF ("nbread: %d\n", nb));
+        VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "nbread: %d\n", nb));
         for (i = 0; i < nb; i++) {
             for (j = 0; j < 8; j++) {
                 codcat (bits, sizeof (bits), ((bufin[i] & 0x80) == 0) ? "0" : "1");
                 bufin[i] <<= 1;
                 l++;
-                VERBOSE (DEBUG, PRINTF ("bits: %d - %s\n", codlen (bits), bits));
+                VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "bits: %d - %s\n", codlen (bits), bits));
 
                 /* look for correct code */
                 is_found = 0;
                 for (k = 0; (k < NB_BYTES) && (!is_found); k++) {
                     if (codcmp ((char *)(codes + k), bits) == 0) {
                         is_found = 1;
-                        VERBOSE (DEBUG, PRINTF ("found: %d\n", k));
+                        VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "found: %d\n", k));
                         *pt= k;
                         bits[0] = 0;
                         if (pt - bufout == BUFFER_SIZE - 1) {
-                            VERBOSE (DEBUG, PRINTF ("nb buffer out: %u\n", (pt - bufout)));
+                            VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "nb buffer out: %u\n", (pt - bufout)));
                             write (fout, bufout, BUFFER_SIZE);
                             pt = bufout;
                         } else {
@@ -667,14 +667,14 @@ int write_decompress (char *output, char *input, code_t *codes)
                     }
                 }
                 if ((i == nb - 1) && (l % 256 == rem) && (nb != BUFFER_SIZE)) {
-                    VERBOSE (DEBUG, PRINTF ("break\n"));
+                    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "break\n"));
                     break;
                 }
             }
         }
     }
     if (pt != bufout) {
-        VERBOSE (DEBUG, PRINTF ("nb buffer out: %u\n", (pt - bufout)));
+        VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "nb buffer out: %u\n", (pt - bufout)));
         write (fout, bufout, pt - bufout);
     }
 
@@ -682,7 +682,7 @@ int write_decompress (char *output, char *input, code_t *codes)
     close (fin);
     close (fout);
 
-    VERBOSE (DEBUG, PRINTF ("end writing decompressed file\n"));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end writing decompressed file\n"));
 
     return 0;
 }
@@ -705,15 +705,15 @@ int main (int argc, char *argv[])
 
     int c;
     char * arg;
-    VERBOSE (DEBUG, PRINTF ("start processing arguments\n"));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "start processing arguments\n"));
     while (argc-- > 1) {
         arg = *(++argv);
         if (arg[0] != '-') {
-            myfprintf (stderr, "%s: invalid option -- %s\n", progname, arg);
+            fdprintf (STDERR_FILENO, "%s: invalid option -- %s\n", progname, arg);
             usage (1);
         }
         c = arg[1];
-        VERBOSE (DEBUG, PRINTF ("option: %c\n", c));
+        VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "option: %c\n", c));
         switch (c) {
         case 'c':
             mode = COMPRESS;
@@ -723,20 +723,20 @@ int main (int argc, char *argv[])
             break;
         case 'i':
             input = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL;
-            VERBOSE (DEBUG, PRINTF ("input: %s\n", input));
+            VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "input: %s\n", input));
             break;
         case 'o':
             output = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL;
-            VERBOSE (DEBUG, PRINTF ("output: %s\n", output));
+            VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "output: %s\n", output));
             break;
         case 'v':
             arg = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL;
             if (arg == NULL) {
-                myfprintf (stderr, "%s: missing verbose level\n", progname);
+                fdprintf (STDERR_FILENO, "%s: missing verbose level\n", progname);
                 usage (1);
             }
             verbose = myatoi (arg);
-            VERBOSE (INFO, myfprintf (stdout, "verbose: %d\n", verbose));
+            VERBOSE (INFO, fdprintf (STDOUT_FILENO, "verbose: %d\n", verbose));
             break;
         case 'h':
         default:
@@ -744,10 +744,10 @@ int main (int argc, char *argv[])
         }
     }
     if ((input == NULL) || (output == NULL)) {
-        myfprintf (stderr, "%s: missing file\n", progname);
+        fdprintf (STDERR_FILENO, "%s: missing file\n", progname);
         usage (1);
     }
-    VERBOSE (DEBUG, PRINTF ("end processing arguments\n"));
+    VERBOSE (DEBUG, fdprintf (STDOUT_FILENO, "end processing arguments\n"));
 
     switch (mode) {
     case COMPRESS:
@@ -776,7 +776,6 @@ int main (int argc, char *argv[])
     }
 
     /* clean everything */
-    fflush (stdout);
     if (header) free (header);
     if (codes) free (codes);
     if (root) free_tree (root);
diff --git a/debug.h b/debug.h
index b05fd061db96aabd995d005f83fcc14f0d9fb38a..62fc38d71e5c713b10324456ed7493adc7c87226 100644 (file)
--- a/debug.h
+++ b/debug.h
@@ -1,9 +1,6 @@
 #ifndef __DEBUG_H__
 #define __DEBUG_H__
 
-#include <stdio.h>
-#include "fprintf.h"
-
 /* constants */
 
 #define COMPRESS 1
@@ -22,7 +19,6 @@
 #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 { myfprintf (stdout, args); fflush (stdout); } while (0)
 
 /* gobal variables */
 
index 6d60b82daa63e15fe64b665675674c1c888967f5..7b3b097b753cdb4828d73676ff5105cda5b7d8f6 100644 (file)
--- a/fprintf.c
+++ b/fprintf.c
@@ -11,9 +11,9 @@
   - initial version
 */
 
+#include <fcntl.h>
 #include <stdarg.h>
-#include <stddef.h>
-#include <stdio.h>
+#include <unistd.h>
 
 inline unsigned int nextpow (unsigned int x, int base) {
     unsigned int n = 0;
@@ -26,7 +26,7 @@ inline unsigned int nextpow (unsigned int x, int base) {
 
 /* simple fprintf function */
 
-size_t myfprintf (FILE *fid, const char *fmt, ...)
+int fdprintf (int fd, const char *fmt, ...)
 {
     char buffer[1024 + 1] = { 0 };
     char *str = buffer;
@@ -45,7 +45,7 @@ size_t myfprintf (FILE *fid, const char *fmt, ...)
         } else {
             int t = 0;
                char w = '0';
-            size_t i, sz = 0;
+            int i, sz = 0;
             void *p = NULL;
 
             /* stamp */
@@ -118,9 +118,9 @@ size_t myfprintf (FILE *fid, const char *fmt, ...)
     va_end (ap);
 
     /* output string */
-    size_t n = str - buffer;
-    if (n < sizeof (buffer) - 1) {
-        fwrite (buffer, n, 1, fid);
+    int n = str - buffer;
+    if (n < (int)sizeof (buffer) - 1) {
+        write (fd, buffer, n);
         return n;
     }
     return 0;
index 1820f1b9ecfc6d291e94c504fe5bc643f3c9cac3..8686b43507ab5cab6abb40115c6e913787fa0445 100644 (file)
--- a/fprintf.h
+++ b/fprintf.h
@@ -1,10 +1,7 @@
 #ifndef __FPRINTF_H__
 #define __FPRINTF_H__
 
-#include <stddef.h>
-#include <stdio.h>
-
-size_t myfprintf (FILE *fid, const char *fmt, ...);
+int fdprintf (int fd, const char *fmt, ...);
 
 #endif /* __FPRINTF_H__ */