clean compress
[compress.git] / compress.c
index 17385d49a188d30a04c6a2f445216ed6765000b7..4b91bbf02e73dfbd38626dc76889c31f734ba466 100644 (file)
@@ -1,6 +1,6 @@
 /* depend: */
 /* cflags: */
-/* linker: atoi.o code.o debug.o fprintf.o */
+/* linker: atoi.o code.o debug.o fdprintf.o */
 
 #include <fcntl.h>
 #include <unistd.h>
@@ -8,7 +8,7 @@
 #include "atoi.h"
 #include "code.h"
 #include "debug.h"
-#include "fprintf.h"
+#include "fdprintf.h"
 
 /* constants */
 
 #define COMPRESS 1
 #define DECOMPRESS 2
 
+#ifndef O_RAW
+#define O_RAW 0
+#endif /* O_RAW */
+
 /* macros */
 
 /* gobal variables */
@@ -27,8 +31,7 @@ char *progname = NULL;
 
 int usage (int ret)
 {
-    //int fd = ret ? STDERR_FILENO : STDOUT_FILENO;
-    int fd = ret ? _fderr : _fdout;
+    int fd = ret ? stdfderr : stdfdout;
     fdprintf (fd, "usage: %s\n", progname);
     fdprintf (fd, " -h : help message\n");
     fdprintf (fd, " -i <file>: input file\n");
@@ -38,14 +41,8 @@ int usage (int ret)
     return ret;
 }
 
-void blkcpy (void *dst, const void *src, int len)
-{
-    while (len--) {
-        *((char *)dst++) = *((char *)src++);
-    }
-}
-
 /* create occurence table */
+
 int *create_table (char *filename)
 {
     byte_t buffer[BUFFER_SIZE] = {0};
@@ -58,7 +55,7 @@ int *create_table (char *filename)
     /* open file */
     fid = open (filename, O_RDONLY|O_RAW);
     if (fid == -1) {
-        VERBOSE (ERROR, PRINTOUT ("can't open file '%s'\n", filename));
+        VERBOSE (ERROR, PRINTERR ("can't open file '%s'\n", filename));
         return NULL;
     }
     VERBOSE (INFO, PRINTOUT ("file '%s' opened\n", filename));
@@ -115,7 +112,7 @@ leaf_t **init_forest (int *table)
         if (table[i] > 0) {
             leafs[l] = getleaf (1);
             if (leafs[l] == NULL) {
-                VERBOSE (ERROR, PRINTOUT ("can't allocate memory\n"));
+                VERBOSE (ERROR, PRINTERR ("can't allocate memory\n"));
                 return NULL;
             }
             leafs[l]->occ = table[i];
@@ -173,12 +170,12 @@ leaf_t *create_tree (leaf_t **leafs)
 
         /* create branch */
         if ((last == -1) || (ante == -1)) {
-            VERBOSE (ERROR, PRINTOUT ("error during tree building\n"));
+            VERBOSE (ERROR, PRINTERR ("error during tree building\n"));
             return NULL;
         }
         branch = getleaf (1);
         if (branch == NULL) {
-            VERBOSE (ERROR, PRINTOUT ("can't allocate memory\n"));
+            VERBOSE (ERROR, PRINTERR ("can't allocate memory\n"));
             return NULL;
         }
         branch->left = leafs[last];
@@ -363,7 +360,7 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header)
     char bits[(NB_BYTES - 1) + 8 + 1] = {0};
     int fin, fout;
     int length = 0;
-    int i, j, nbread;
+    int i, j, nbread, nbwrite;
     byte_t *pt;
 
     VERBOSE (DEBUG, PRINTOUT ("start writting compressed file\n"));
@@ -371,7 +368,7 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header)
     /* open input file */
     fin = open (input, O_RDONLY|O_RAW);
     if (fin == -1) {
-        VERBOSE (ERROR, PRINTOUT ("can't open file '%s' for reading\n", input));
+        VERBOSE (ERROR, PRINTERR ("can't open file '%s' for reading\n", input));
         return 1;
     }
     VERBOSE (INFO, PRINTOUT ("file '%s' opened\n", input));
@@ -379,7 +376,7 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header)
     /* open output file */
     fout = open (output, O_WRONLY|O_CREAT|O_RAW, 0700);
     if (fout == -1) {
-        VERBOSE (ERROR, PRINTOUT ("can't open file '%s' for writing\n", output));
+        VERBOSE (ERROR, PRINTERR ("can't open file '%s' for writing\n", output));
            close (fin);
         return 1;
     }
@@ -388,7 +385,14 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header)
     /* write header */
     length = (header[3] << 8) + header[4];
     VERBOSE (DEBUG, PRINTOUT ("lengh: %d\n", length));
-    write (fout, header, length + 6);
+    nbwrite = write (fout, header, length + 6);
+    if (nbwrite != length + 6) {
+        VERBOSE (ERROR, PRINTERR ("can't write %d bytes in file '%s'\n", length + 6 - nbwrite, output));
+           close (fout);
+           close (fin);
+        return 1;
+    }
+
 
     /* write file */
     pt = bufout;
@@ -405,7 +409,13 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header)
                 }
                 codcpy (bits, sizeof (code_t), bits + 8);
                 if (pt - bufout == BUFFER_SIZE - 1) {
-                    write (fout, bufout, BUFFER_SIZE);
+                    nbwrite = write (fout, bufout, BUFFER_SIZE);
+                    if (nbwrite != BUFFER_SIZE) {
+                        VERBOSE (ERROR, PRINTERR ("can't write %d bytes in file '%s'\n", BUFFER_SIZE - nbwrite, output));
+                           close (fout);
+                           close (fin);
+                        return 1;
+                    }
                     pt = bufout;
                 } else {
                     pt++;
@@ -428,7 +438,13 @@ int write_compress (char *output, char *input, code_t *codes, byte_t *header)
     }
     if (pt != bufout) {
         VERBOSE (DEBUG, PRINTOUT ("last partial buffer written: %u\n", pt - bufout));
-        write (fout, bufout, pt - bufout);
+        nbwrite = write (fout, bufout, pt - bufout);
+        if (nbwrite != pt - bufout) {
+            VERBOSE (ERROR, PRINTERR ("can't write %d bytes in file '%s'\n", pt - bufout - nbwrite, output));
+               close (fout);
+               close (fin);
+            return 1;
+        }
     }
     
     /* closing */
@@ -457,7 +473,7 @@ code_t *read_header (char *filename) {
     /* open file */
     fid = open (filename, O_RDONLY|O_RAW);
     if (fid == -1) {
-        VERBOSE (ERROR, PRINTOUT ("can't open file '%s'\n", filename));
+        VERBOSE (ERROR, PRINTERR ("can't open file '%s'\n", filename));
         return NULL;
     }
     VERBOSE (INFO, PRINTOUT ("file '%s' opened\n", filename));
@@ -481,7 +497,7 @@ code_t *read_header (char *filename) {
     }
     close (fid);
     if (mode == 0) {
-        VERBOSE (ERROR, PRINTOUT ("incorrect file\n"));
+        VERBOSE (ERROR, PRINTERR ("incorrect file\n"));
         return NULL;
     }
 
@@ -510,7 +526,7 @@ 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, PRINTOUT ("incorrect code table length: %d %d %d\n", size, nb, l));
+        VERBOSE (ERROR, PRINTERR ("incorrect code table length: %d %d %d\n", size, nb, l));
         return NULL;
     }
 
@@ -546,7 +562,7 @@ int write_decompress (char *output, char *input, code_t *codes)
     byte_t bufhea[MAX(NB_BYTES * (NB_BYTES - 1) / 2 / 8 + NB_BYTES + 6, BUFFER_SIZE)] = {0};
     char bits[(NB_BYTES - 1) + 1] = {0};
     int fin, fout;
-    int i, j, k, nb, size, rem;
+    int i, j, k, nb, size, nbwrite, rem;
     int is_found;
     int l = 0;
     byte_t *pt;
@@ -556,7 +572,7 @@ int write_decompress (char *output, char *input, code_t *codes)
     /* open file for reading */
     fin = open (input, O_RDONLY|O_RAW);
     if (fin == -1) {
-        VERBOSE (ERROR, PRINTOUT ("can't open file '%s' for reading\n", input));
+        VERBOSE (ERROR, PRINTERR ("can't open file '%s' for reading\n", input));
         return 1;
     }
     VERBOSE (INFO, PRINTOUT ("file '%s' opened\n", input));
@@ -564,7 +580,7 @@ int write_decompress (char *output, char *input, code_t *codes)
     /* read magic number */
     nb = read (fin, bufhea, 6);
     if (nb != 6) {
-        VERBOSE (ERROR, PRINTOUT ("can't read file\n"));
+        VERBOSE (ERROR, PRINTERR ("can't read file\n"));
         close (fin);
         return 1;
     }
@@ -574,7 +590,7 @@ int write_decompress (char *output, char *input, code_t *codes)
     VERBOSE (DEBUG, PRINTOUT ("remainder: %d\n", rem));
     nb = read (fin, bufhea, size);
     if (nb != size) {
-        VERBOSE (ERROR, PRINTOUT ("can't read file\n"));
+        VERBOSE (ERROR, PRINTERR ("can't read file\n"));
         close (fin);
         return 1;
     }
@@ -582,9 +598,9 @@ 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, PRINTOUT ("can't open file '%s' for writing\n", output));
+        VERBOSE (ERROR, PRINTERR ("can't open file '%s' for writing\n", output));
         close (fin);
-       return 2;
+           return 1;
     }
     VERBOSE (INFO, PRINTOUT ("file '%s' opened\n", output));
 
@@ -609,7 +625,13 @@ int write_decompress (char *output, char *input, code_t *codes)
                         bits[0] = 0;
                         if (pt - bufout == BUFFER_SIZE - 1) {
                             VERBOSE (DEBUG, PRINTOUT ("nb buffer out: %u\n", (pt - bufout)));
-                            write (fout, bufout, BUFFER_SIZE);
+                            nbwrite = write (fout, bufout, BUFFER_SIZE);
+                            if (nbwrite != BUFFER_SIZE) {
+                                VERBOSE (ERROR, PRINTERR ("can't write %d bytes in file '%s'\n'", BUFFER_SIZE - nbwrite, output));
+                                close (fout);
+                                close (fin);
+                                return 1;
+                            }
                             pt = bufout;
                         } else {
                             pt++;
@@ -625,7 +647,13 @@ int write_decompress (char *output, char *input, code_t *codes)
     }
     if (pt != bufout) {
         VERBOSE (DEBUG, PRINTOUT ("nb buffer out: %u\n", (pt - bufout)));
-        write (fout, bufout, pt - bufout);
+        nbwrite = write (fout, bufout, pt - bufout);
+        if (nbwrite != pt - bufout) {
+            VERBOSE (ERROR, PRINTERR ("can't write %d bytes in file '%s'\n'", pt - bufout - nbwrite, output));
+            close (fout);
+            close (fin);
+            return 1;
+        }
     }
 
     /* close files */
@@ -685,7 +713,7 @@ int main (int argc, char *argv[])
                 PRINTERR ("%s: missing verbose level\n", progname);
                 return usage (1);
             }
-            verbose = myatoi (arg);
+            verbose = atoi (arg);
             VERBOSE (INFO, PRINTOUT ("verbose: %d\n", verbose));
             break;
         case 'h':