Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/distrib/utils/zcat This is an example how to make a small pr...



details:   https://anonhg.NetBSD.org/src/rev/107e9d2ad222
branches:  trunk
changeset: 820599:107e9d2ad222
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Jan 12 01:58:03 2017 +0000

description:
This is an example how to make a small program using libc. Original size
300K; final size 75K.

diffstat:

 distrib/utils/zcat/Makefile |  30 +++++++++++++++++-----------
 distrib/utils/zcat/misc.c   |  38 ++++++++++++++++++++++++++++++++++++
 distrib/utils/zcat/zcat.c   |  47 ++++++++++++++++++++++++++++----------------
 3 files changed, 86 insertions(+), 29 deletions(-)

diffs (208 lines):

diff -r 9f4725c78b12 -r 107e9d2ad222 distrib/utils/zcat/Makefile
--- a/distrib/utils/zcat/Makefile       Thu Jan 12 01:12:08 2017 +0000
+++ b/distrib/utils/zcat/Makefile       Thu Jan 12 01:58:03 2017 +0000
@@ -1,26 +1,32 @@
-# $NetBSD: Makefile,v 1.16 2017/01/11 04:04:12 christos Exp $
+# $NetBSD: Makefile,v 1.17 2017/01/12 01:58:03 christos Exp $
 # Small zcat (i.e. for install media)
 #
-# Note: gzio.c is compiled here so that crunchgen will assume
-# the same symbol space for zcat.c and gzio.c which is required
-# so that the fake deflate functions in zcat.c will satisfy the
-# references to those functions in gzio.c (yes, it's a hack).
+
+NOSSP=yes
+NOMAN=
+.include <bsd.own.mk>
 
 SRCDIR=                ${.CURDIR}/../../../common/dist/zlib
+LIBC=          ${NETBSDSRCDIR}/lib/libc
+
+.PATH:         ${SRCDIR} ${LIBC}/stdlib
 
 WARNS?=                4
 PROG=          zcat
-NOMAN=         # defined
 
-SRCS=          zcat.c gzread.c gzclose.c gzlib.c
+# Just what we need from libz
+SRCS=          zcat.c gzread.c gzclose.c gzlib.c inflate.c 
+SRCS+=         adler32.c crc32.c zutil.c inffast.c inftrees.c
+CPPFLAGS+=     -I${SRCDIR} -DNO_GZCOMPRESS
 
-CPPFLAGS+=     -I${SRCDIR} -DNO_GZCOMPRESS
-DPADD+=                ${LIBZ}
-LDADD+=                -lz
+# This avoids including stdio, threads, locale, etc.
+SRCS+=         misc.c
+SRCS+=         malloc.c        # small
+CPPFLAGS+=     -I${LIBC}/include
+CPPFLAGS+=     -Dsnprintf=snprintf_ss -Dsprintf=sprintf_ss
+CPPFLAGS+=     -Dstrerror=strerror_ss
 
 .include <bsd.prog.mk>
 
-.PATH:         ${SRCDIR}
-
 test: zcat
        echo 'hello, hello!' | gzip | ./zcat
diff -r 9f4725c78b12 -r 107e9d2ad222 distrib/utils/zcat/misc.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/distrib/utils/zcat/misc.c Thu Jan 12 01:58:03 2017 +0000
@@ -0,0 +1,38 @@
+#include <assert.h>
+#include <signal.h>
+
+/* Avoid stdio */
+__dead void __assert(const char *a, int b, const char *c) {
+       kill(0, SIGQUIT);
+}
+__dead void __assert13(const char *a, int b, const char *c, const char *d) {
+       kill(0, SIGQUIT);
+}
+void __diagassert(const char *a, int b, const char *x) {
+       kill(0, SIGQUIT);
+}
+void __diagassert13(const char * a, int b, const char *c, const char *d) {
+       kill(0, SIGQUIT);
+}
+
+/* Avoid mutexes environment rbree, thread stuff */
+void _libc_init(void);
+void _libc_init(void) {
+}
+
+/* Avoid finalizers, etc. */
+int atexit(void (*)(void));
+
+int atexit(void (*p)(void)) {
+       return 0;
+}
+
+void __cxa_finalize(void *);
+void __cxa_finalize(void *dso) { }
+
+int __cxa_atexit(void (*func)(void *), void *arg, void *dso);
+int
+__cxa_atexit(void (*func)(void *), void *arg, void *dso)
+{
+       return 0;
+}
diff -r 9f4725c78b12 -r 107e9d2ad222 distrib/utils/zcat/zcat.c
--- a/distrib/utils/zcat/zcat.c Thu Jan 12 01:12:08 2017 +0000
+++ b/distrib/utils/zcat/zcat.c Thu Jan 12 01:58:03 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: zcat.c,v 1.4 2011/05/19 22:23:12 tsutsui Exp $ */
+/*     $NetBSD: zcat.c,v 1.5 2017/01/12 01:58:03 christos Exp $        */
 
 /* mini zcat.c -- a minimal zcat using the zlib compression library
  * Copyright (C) 1995-1996 Jean-loup Gailly.
@@ -13,8 +13,11 @@
  */
 
 #include <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>
 #include <string.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 #include "zlib.h"
 
@@ -22,24 +25,33 @@
 
 char *prog;
 
-void error(const char *msg);
-void gz_uncompress(gzFile in, FILE   *out);
-int  main(int argc, char *argv[]);
+static void error(const char *, ...) __printflike(1, 2);
+static void gz_uncompress(gzFile, int);
 
 /* ===========================================================================
  * Display error message and exit
  */
-void error(const char *msg)
+static void
+error(const char *fmt, ...)
 {
+       char buf[1024];
+       va_list ap;
+       int l;
 
-       fprintf(stderr, "%s: %s\n", prog, msg);
-       exit(EXIT_SUCCESS);
+       l = snprintf_ss(buf, sizeof(buf), "%s: ", prog);
+       write(STDERR_FILENO, buf, l);
+       va_start(ap, fmt);
+       l = vsnprintf_ss(buf, sizeof(buf), fmt, ap);
+       va_end(ap);
+       write(STDERR_FILENO, buf, l);
+       _exit(EXIT_SUCCESS);
 }
 
 /* ===========================================================================
  * Uncompress input to output then close both files.
  */
-void gz_uncompress(gzFile in, FILE *out)
+static void
+gz_uncompress(gzFile in, int out)
 {
        char buf[BUFLEN];
        int len;
@@ -48,15 +60,15 @@
        for (;;) {
                len = gzread(in, buf, sizeof(buf));
                if (len < 0)
-                       error (gzerror(in, &err));
+                       error ("%s", gzerror(in, &err));
                if (len == 0)
                        break;
 
-               if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
+               if ((int)write(out, buf, (size_t)len) != len) {
                        error("failed fwrite");
                }
        }
-       if (fclose(out))
+       if (close(out))
                error("failed fclose");
 
        if (gzclose(in) != Z_OK)
@@ -68,7 +80,8 @@
  * Usage:  zcat [files...]
  */
 
-int main(int argc, char *argv[])
+int
+main(int argc, char *argv[])
 {
        gzFile zfp;
 
@@ -82,10 +95,10 @@
        }
 
        if (argc == 0) {
-               zfp = gzdopen(fileno(stdin), "rb");
+               zfp = gzdopen(STDIN_FILENO, "rb");
                if (zfp == NULL)
                        error("can't gzdopen stdin");
-               gz_uncompress(zfp, stdout);
+               gz_uncompress(zfp, STDOUT_FILENO);
                return 0;
        }
 
@@ -93,10 +106,10 @@
                /* file_uncompress(*argv); */
                zfp = gzopen(*argv, "rb");
                if (zfp == NULL) {
-                       fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv);
-                       exit(EXIT_FAILURE);
+                       error("can't gzopen `%s'", *argv);
+                       _exit(EXIT_FAILURE);
                }
-               gz_uncompress(zfp, stdout);
+               gz_uncompress(zfp, STDOUT_FILENO);
        } while (argv++, --argc);
        return 0; /* to avoid warning */
 }



Home | Main Index | Thread Index | Old Index