Initial import from FreeBSD RELENG_4:
[games.git] / usr.sbin / kgzip / kgzip.c
1 /*
2  * Copyright (c) 1999 Global Technology Associates, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
25  */
26
27 #ifndef lint
28 static const char rcsid[] =
29   "$FreeBSD: src/usr.sbin/kgzip/kgzip.c,v 1.3.2.3 2001/07/19 04:42:38 kris Exp $";
30 #endif /* not lint */
31
32 #include <sys/types.h>
33 #include <err.h>
34 #include <paths.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39
40 #include "kgzip.h"
41
42 #define FN_SRC          0       /* Filename: source */
43 #define FN_OBJ          1       /* Filename: relocatable */
44 #define FN_KGZ          2       /* Filename: executable */
45 #define FN_CNT          3       /* Number of filenames */
46
47 #define SFX_OBJ         ".o"    /* Filename suffix: relocatable */
48 #define SFX_KGZ         ".kgz"  /* Filename suffix: executable */
49 #define SFX_MAX         5       /* Size of larger filename suffix */
50
51 const char *loader = "/usr/lib/kgzldr.o";  /* Default loader */
52 int format;                     /* Output format */
53
54 char *tname;                    /* Name of temporary file */
55
56 static void cleanup(void);
57 static void mk_fn(int, const char *, const char *, char *[]);
58 static void usage(void);
59
60 /*
61  * Compress a kernel.
62  */
63 int
64 main(int argc, char *argv[])
65 {
66     static char *fn[FN_CNT];
67     struct kgz_hdr kh;
68     const char *output;
69     char *tmpdir;
70     int cflag, vflag, c;
71
72     tmpdir = getenv("TMPDIR");
73     if (asprintf(&tname, "%s/kgzXXXXXXXXXX",
74                  tmpdir == NULL ? _PATH_TMP : tmpdir) == -1)
75         errx(1, "Out of memory");
76     output = NULL;
77     cflag = vflag = 0;
78     while ((c = getopt(argc, argv, "cvf:l:o:")) != -1)
79         switch (c) {
80         case 'c':
81             cflag = 1;
82             break;
83         case 'v':
84             vflag = 1;
85             break;
86         case 'f':
87             if (!strcmp(optarg, "aout"))
88                 format = F_AOUT;
89             else if (!strcmp(optarg, "elf"))
90                 format = F_ELF;
91             else
92                 errx(1, "%s: Unknown format", optarg);
93             break;
94         case 'l':
95             loader = optarg;
96             break;
97         case 'o':
98             output = optarg;
99             break;
100         default:
101             usage();
102         }
103     argc -= optind;
104     argv += optind;
105     if (argc != 1)
106         usage();
107     atexit(cleanup);
108     mk_fn(cflag, *argv, output, fn);
109     memset(&kh, 0, sizeof(kh));
110     if (fn[FN_SRC]) {
111         if (!format)
112             format = F_ELF;
113         kgzcmp(&kh, fn[FN_SRC], fn[FN_OBJ]);
114     }
115     if (!cflag)
116         kgzld(&kh, fn[FN_OBJ], fn[FN_KGZ]);
117     if (vflag)
118         printf("dload=%#x dsize=%#x isize=%#x entry=%#x nsize=%#x\n",
119                kh.dload, kh.dsize, kh.isize, kh.entry, kh.nsize);
120     return 0;
121 }
122
123 /*
124  * Clean up after processing.
125  */
126 static void
127 cleanup(void)
128 {
129     if (tname)
130         unlink(tname);
131 }
132
133 /*
134  * Make the required filenames.
135  */
136 static void
137 mk_fn(int cflag, const char *f1, const char *f2, char *fn[])
138 {
139     const char *p, *s;
140     size_t n;
141     int i, fd;
142
143     i = 0;
144     s = strrchr(f1, 0);
145     n = sizeof(SFX_OBJ) - 1;
146     if ((size_t)(s - f1) > n && !memcmp(s - n, SFX_OBJ, n)) {
147         s -= n;
148         i++;
149     }
150     fn[i++] = (char *)f1;
151     if (i == FN_OBJ && !cflag) {
152         if ((fd = mkstemp(tname)) == -1)
153             err(1, NULL);
154         close(fd);
155         fn[i++] = (char *)tname;
156     }
157     if (!(fn[i] = (char *)f2)) {
158         p = (p = strrchr(f1, '/')) ? p + 1 : f1;
159         n = (size_t)(s - p);
160         if (!(fn[i] = malloc(n + SFX_MAX)))
161             err(1, NULL);
162         memcpy(fn[i], p, n);
163         strcpy(fn[i] + n, i == FN_OBJ ? SFX_OBJ : SFX_KGZ);
164     }
165 }
166
167 /*
168  * Display usage information.
169  */
170 static void
171 usage(void)
172 {
173     fprintf(stderr,
174       "usage: kgzip [-cv] [-f format] [-l file] [-o filename] file\n");
175     exit(1);
176 }