Module Name:src
Committed By: christos
Date: Thu Jan 12 01:58:03 UTC 2017
Modified Files:
src/distrib/utils/zcat: Makefile zcat.c
Added Files:
src/distrib/utils/zcat: misc.c
Log Message:
This is an example how to make a small program using libc. Original size
300K; final size 75K.
To generate a diff of this commit:
cvs rdiff -u -r1.16 -r1.17 src/distrib/utils/zcat/Makefile
cvs rdiff -u -r0 -r1.1 src/distrib/utils/zcat/misc.c
cvs rdiff -u -r1.4 -r1.5 src/distrib/utils/zcat/zcat.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/distrib/utils/zcat/Makefile
diff -u src/distrib/utils/zcat/Makefile:1.16 src/distrib/utils/zcat/Makefile:1.17
--- src/distrib/utils/zcat/Makefile:1.16 Tue Jan 10 23:04:12 2017
+++ src/distrib/utils/zcat/Makefile Wed Jan 11 20:58:03 2017
@@ -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
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
-DPADD+= ${LIBZ}
-LDADD+= -lz
-.include
+# 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
-.PATH: ${SRCDIR}
+.include
test: zcat
echo 'hello, hello!' | gzip | ./zcat
Index: src/distrib/utils/zcat/zcat.c
diff -u src/distrib/utils/zcat/zcat.c:1.4 src/distrib/utils/zcat/zcat.c:1.5
--- src/distrib/utils/zcat/zcat.c:1.4 Thu May 19 18:23:12 2011
+++ src/distrib/utils/zcat/zcat.c Wed Jan 11 20:58:03 2017
@@ -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
+#include
+#include
#include
#include
+#include
#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, ...)
{
-
- fprintf(stderr, "%s: %s\n", prog, msg);
- exit(EXIT_SUCCESS);
+ char buf[1024];
+ va_list ap;
+ int l;
+
+ 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 @@ void gz_uncompress(gzFile in, FILE *out)
for (;;) {
len = gzread(in, buf, sizeof(buf));
if (len < 0)
- error (gzerror(in, ));
+ error ("%s", gzerror(in, ));
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 @@ void gz_uncompress(gzFile in, FILE *out)
* Usage: zcat [files...]
*/
-int main(int argc, char *argv[])
+int
+main(int argc, char *argv[])
{
gzFile zfp;
@@ -82,10 +95,10 @@ int main(int argc, char *argv[])
}
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 @@ int main(int argc, char *argv[])
/* 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);
}
-