2 * Copyright (c) 1999 Global Technology Associates, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
19 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
20 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
21 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
23 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD: src/usr.sbin/kgzip/kgzip.c,v 1.3.2.3 2001/07/19 04:42:38 kris Exp $
27 * $DragonFly: src/usr.sbin/kgzip/kgzip.c,v 1.3 2004/08/19 21:36:46 joerg Exp $
30 #include <sys/types.h>
40 #define FN_SRC 0 /* Filename: source */
41 #define FN_OBJ 1 /* Filename: relocatable */
42 #define FN_KGZ 2 /* Filename: executable */
43 #define FN_CNT 3 /* Number of filenames */
45 #define SFX_OBJ ".o" /* Filename suffix: relocatable */
46 #define SFX_KGZ ".kgz" /* Filename suffix: executable */
47 #define SFX_MAX 5 /* Size of larger filename suffix */
49 const char *loader
= "/usr/lib/kgzldr.o"; /* Default loader */
50 int format
; /* Output format */
52 char *tname
; /* Name of temporary file */
54 static void cleanup(void);
55 static void mk_fn(int, const char *, const char *, const char *[]);
56 static void usage(void);
62 main(int argc
, char *argv
[])
64 static const char *fn
[FN_CNT
];
70 tmpdir
= getenv("TMPDIR");
71 if (asprintf(&tname
, "%s/kgzXXXXXXXXXX",
72 tmpdir
== NULL
? _PATH_TMP
: tmpdir
) == -1)
73 errx(1, "Out of memory");
76 while ((c
= getopt(argc
, argv
, "cvf:l:o:")) != -1)
85 if (!strcmp(optarg
, "aout"))
87 else if (!strcmp(optarg
, "elf"))
90 errx(1, "%s: Unknown format", optarg
);
106 mk_fn(cflag
, *argv
, output
, fn
);
107 memset(&kh
, 0, sizeof(kh
));
111 kgzcmp(&kh
, fn
[FN_SRC
], fn
[FN_OBJ
]);
114 kgzld(&kh
, fn
[FN_OBJ
], fn
[FN_KGZ
]);
116 printf("dload=%#x dsize=%#x isize=%#x entry=%#x nsize=%#x\n",
117 kh
.dload
, kh
.dsize
, kh
.isize
, kh
.entry
, kh
.nsize
);
122 * Clean up after processing.
132 * Make the required filenames.
135 mk_fn(int cflag
, const char *f1
, const char *f2
, const char *fn
[])
144 n
= sizeof(SFX_OBJ
) - 1;
145 if ((size_t)(s
- f1
) > n
&& !memcmp(s
- n
, SFX_OBJ
, n
)) {
150 if (i
== FN_OBJ
&& !cflag
) {
151 if ((fd
= mkstemp(tname
)) == -1)
158 p
= (p
= strrchr(f1
, '/')) ? p
+ 1 : f1
;
160 sfx
= malloc(n
+ SFX_MAX
);
164 strcpy(sfx
+ n
, i
== FN_OBJ
? SFX_OBJ
: SFX_KGZ
);
170 * Display usage information.
176 "usage: kgzip [-cv] [-f format] [-l file] [-o filename] file\n");