Merge branch 'master' of https://secure.softndesign.org/git/compress
[compress.git] / compress.c
index d27f8e5740982070b6383f5cc10cbef86ad52fd5..d8842b472c3c56df515105f6070e398e9aac1690 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 */
 
@@ -31,8 +31,10 @@ char *progname = NULL;
 
 int usage (int ret)
 {
-    int fd = ret ? _fderr : _fdout;
+    int fd = ret ? stdfderr : stdfdout;
     fdprintf (fd, "usage: %s\n", progname);
+    fdprintf (fd, " -c : mode compress\n");
+    fdprintf (fd, " -d : mode decompress\n");
     fdprintf (fd, " -h : help message\n");
     fdprintf (fd, " -i <file>: input file\n");
     fdprintf (fd, " -o <file>: output file\n");
@@ -94,7 +96,7 @@ void print_occ_table (int *table)
 
 leaf_t **init_forest (int *table)
 {
-    static leaf_t *leafs[NB_BYTES] = {0};
+    static leaf_t *leafs[NB_BYTES + 1] = {0};
     int nb_leafs = 0;
     int i, l;
 
@@ -271,7 +273,7 @@ byte_t *encode_header_table (code_t *codes, int *occ)
     VERBOSE (DEBUG, PRINTOUT ("rem: %d\n", size % 256));
 
     /* header */
-    codcpy ((char *)header, sizeof (buffer), (mode == 1) ? "MZ1   " : "MZ2   ");
+    codcpy ((char *)header, sizeof (buffer), (mode == 1) ? "M1Z   " : "M2Z   ");
     header += 6;
 
     /* size */
@@ -481,8 +483,8 @@ code_t *read_header (char *filename) {
     /* read magic number */
     nb = read (fid, buffer, 6);
     VERBOSE (DEBUG, PRINTOUT ("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;
+    if ((nb == 6) && (buffer[0] == 'M') && (buffer[2] == 'Z')) {
+        mode = (buffer[1] == '1') ? 1 : (buffer[1] == '2') ? 2 : 0;
         size = (buffer[3] << 8) + buffer[4];
         VERBOSE (DEBUG, PRINTOUT ("mode, size: %d %d\n", mode, size));
         if (size > NB_BYTES * (NB_BYTES - 1) / 2 / 8 + NB_BYTES) {
@@ -713,7 +715,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':
@@ -760,10 +762,14 @@ int main (int argc, char *argv[])
 // test: compress.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
 // test: compress.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
 // test: compress.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
+// test: compress.exe -v 2>&1 | grep -q 'missing verbose level'
+// test: compress.exe -c -i compress.c 2>&1 | grep -q 'missing file'
+// test: compress.exe -c -v 4 -i compress.c -o compress.mz | grep -q "Occurence table"
 // test: compress.exe -c -i compress.c -o compress.mz
-// test: ls -sS1 compress.c compress.mz | tail -1 | grep compress.mz
+// test: ls -sS1 compress.c compress.mz | tail -1 | grep -q compress.mz
 // test: compress.exe -d -i compress.mz -o tmp.c
-// test: cmp compress.c tmp.c
-// test: rm compress.mz tmp.c
+// test: cmp compress.c tmp.c; x=$?; rm compress.mz tmp.c; test x$x = x0
+// test: compress.exe -c -i test/compress.c -o compress.mz 2>&1 | grep -q "can't open file"
+// test: compress.exe -c -i compress.c -o test/compress.mz 2>&1 | grep -q "can't open file"
 
 /* vim: set ts=4 sw=4 et: */