remove most external function calls (except io)
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Thu, 1 Dec 2022 15:28:54 +0000 (16:28 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Thu, 1 Dec 2022 15:28:54 +0000 (16:28 +0100)
code.c
code.h
compress.c
debug.h

diff --git a/code.c b/code.c
index 17ef4d0ef9b43f07d40d6d19bc6ae4ceba758a86..d8ef5da72d2de038631a0e4c68c035f51511ebe3 100644 (file)
--- a/code.c
+++ b/code.c
@@ -1,5 +1,3 @@
-#include <stddef.h>
-#include <stdio.h>
 #include "debug.h"
 
 #include "code.h"
diff --git a/code.h b/code.h
index 5ffcf48ddb18da9ee3b7bb9ef6ebd96766316cd9..977efc3e88687cbad2f8d86efb4f9055f6691dfc 100644 (file)
--- a/code.h
+++ b/code.h
@@ -1,6 +1,8 @@
 #ifndef __CODE_H__
 #define __CODE_H__
 
+#include <stddef.h>
+
 #define NB_BYTES 256
 
 /* byte type */
index 65d12fe7b7b248f621324e91355575334946b306..8cfce314cc13aba7e4e47c94da1c49ee52c7fa97 100644 (file)
@@ -2,12 +2,8 @@
 /* cflags: */
 /* linker: code.o debug.o */
 
-#include <assert.h>
-#include <getopt.h>
 #include <malloc.h>
 #include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
 #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 01cc89076f827b095eb5d6e8707f5e0f55461a01..01963d2a33e5ec8c9d54779f64bffe47762fcaa2 100644 (file)
--- a/debug.h
+++ b/debug.h
@@ -1,6 +1,8 @@
 #ifndef __DEBUG_H__
 #define __DEBUG_H__
 
+#include <stdio.h>
+
 /* constants */
 
 #define COMPRESS 1