/* 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>
#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 */
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");
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};
/* open file */
fid = open (filename, O_RDONLY|O_RAW);
if (fid == -1) {
- VERBOSE (ERROR, PRINTERR ("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));
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;
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"));
/* 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;
}
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++;
}
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 */
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;
if (fout == -1) {
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));
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++;
}
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 */
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':
// 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: 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 "can't open file"
+// test: compress.exe -c -i compress.c -o test/compress.mz 2>&1 | grep "can't open file"
/* vim: set ts=4 sw=4 et: */