From d3dbaf9870630d2724b1d22a370184da7954f3a5 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Thu, 1 Dec 2022 16:28:54 +0100 Subject: [PATCH] remove most external function calls (except io) --- code.c | 2 -- code.h | 2 ++ compress.c | 37 ++++++++++++++++++++++++++----------- debug.h | 2 ++ 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/code.c b/code.c index 17ef4d0..d8ef5da 100644 --- a/code.c +++ b/code.c @@ -1,5 +1,3 @@ -#include -#include #include "debug.h" #include "code.h" diff --git a/code.h b/code.h index 5ffcf48..977efc3 100644 --- a/code.h +++ b/code.h @@ -1,6 +1,8 @@ #ifndef __CODE_H__ #define __CODE_H__ +#include + #define NB_BYTES 256 /* byte type */ diff --git a/compress.c b/compress.c index 65d12fe..8cfce31 100644 --- a/compress.c +++ b/compress.c @@ -2,12 +2,8 @@ /* cflags: */ /* linker: code.o debug.o */ -#include -#include #include #include -#include -#include #include "code.h" #include "debug.h" @@ -35,6 +31,13 @@ void usage (int ret) exit (ret); } +void blkcpy (void *dst, const void *src, int len) +{ + while (len--) { + *((char *)dst++) = *((char *)src++); + } +} + /* create occurence table */ int *create_table (char *filename) @@ -371,7 +374,7 @@ byte_t *encode_header_table (code_t *codes, int *occ) /* allocation */ header = (byte_t *) calloc (length + 6, 1); - memcpy (header, buffer, length + 6); + blkcpy (header, buffer, length + 6); VERBOSE (DEBUG, PRINTF ("end encoding header and code table\n")); @@ -699,8 +702,15 @@ int main (int argc, char *argv[]) progname = argv[0]; int c; + char * arg; VERBOSE (DEBUG, PRINTF ("start processing arguments\n")); - while ((c = getopt(argc, argv, "cdhi:o:v:")) != EOF) { + while (argc-- > 1) { + arg = *(++argv); + if (arg[0] != '-') { + fprintf (stderr, "%s: invalid option -- %s\n", progname, arg); + usage (1); + } + c = arg[1]; VERBOSE (DEBUG, PRINTF ("option: %c\n", c)); switch (c) { case 'c': @@ -710,15 +720,20 @@ int main (int argc, char *argv[]) mode = DECOMPRESS; break; case 'i': - input = optarg; + input = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL; VERBOSE (DEBUG, PRINTF ("input: %s\n", input)); break; case 'o': - output = optarg; + output = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL; VERBOSE (DEBUG, PRINTF ("output: %s\n", output)); break; case 'v': - verbose = atoi (optarg); + arg = (arg[2]) ? arg + 2 : (--argc > 0 ) ? *(++argv) : NULL; + if (arg == NULL) { + fprintf (stderr, "%s: missing verbose level\n", progname); + usage (1); + } + verbose = atoi (arg); VERBOSE (INFO, printf ("verbose: %d\n", verbose)); break; case 'h': @@ -726,8 +741,8 @@ int main (int argc, char *argv[]) usage (c != 'h'); } } - if (argc - optind != 0) { - fprintf (stderr, "%s: invalid option -- %s\n", progname, argv[optind]); + if ((input == NULL) || (output == NULL)) { + fprintf (stderr, "%s: missing file\n", progname); usage (1); } VERBOSE (DEBUG, PRINTF ("end processing arguments\n")); diff --git a/debug.h b/debug.h index 01cc890..01963d2 100644 --- a/debug.h +++ b/debug.h @@ -1,6 +1,8 @@ #ifndef __DEBUG_H__ #define __DEBUG_H__ +#include + /* constants */ #define COMPRESS 1 -- 2.30.2