]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/kgzip/kgzip.c
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.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$";
30 #endif /* not lint */
31
32 #include <sys/types.h>
33 #include <err.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38
39 #include "kgzip.h"
40
41 #define FN_SRC          0       /* Filename: source */
42 #define FN_OBJ          1       /* Filename: relocatable */
43 #define FN_KGZ          2       /* Filename: executable */
44 #define FN_CNT          3       /* Number of filenames */
45
46 #define SFX_OBJ         ".o"    /* Filename suffix: relocatable */
47 #define SFX_KGZ         ".kgz"  /* Filename suffix: executable */
48 #define SFX_MAX         5       /* Size of larger filename suffix */
49
50 const char *loader = "/usr/lib/kgzldr.o";  /* Default loader */
51
52 char *tname;                    /* Name of temporary file */
53
54 static void cleanup(void);
55 static void mk_fn(int, const char *, const char *, char *[]);
56 static void usage(void);
57
58 /*
59  * Compress a kernel.
60  */
61 int
62 main(int argc, char *argv[])
63 {
64     static char *fn[FN_CNT];
65     struct kgz_hdr kh;
66     const char *output;
67     int cflag, vflag, c;
68
69     if (getenv("TMPDIR") == NULL)
70         tname = strdup("/tmp/kgzXXXXXXXXXX");
71     else
72         if (asprintf(&tname, "%s/kgzXXXXXXXXXX", getenv("TMPDIR")) == -1)
73             errx(1, "Out of memory");
74
75     output = NULL;
76     cflag = vflag = 0;
77     while ((c = getopt(argc, argv, "cvl:o:")) != -1)
78         switch (c) {
79         case 'c':
80             cflag = 1;
81             break;
82         case 'v':
83             vflag = 1;
84             break;
85         case 'l':
86             loader = optarg;
87             break;
88         case 'o':
89             output = optarg;
90             break;
91         default:
92             usage();
93         }
94     argc -= optind;
95     argv += optind;
96     if (argc != 1)
97         usage();
98     atexit(cleanup);
99     mk_fn(cflag, *argv, output, fn);
100     memset(&kh, 0, sizeof(kh));
101     if (fn[FN_SRC])
102         kgzcmp(&kh, fn[FN_SRC], fn[FN_OBJ]);
103     if (!cflag)
104         kgzld(&kh, fn[FN_OBJ], fn[FN_KGZ]);
105     if (vflag)
106         printf("dload=%#x dsize=%#x isize=%#x entry=%#x nsize=%#x\n",
107                kh.dload, kh.dsize, kh.isize, kh.entry, kh.nsize);
108     return 0;
109 }
110
111 /*
112  * Clean up after processing.
113  */
114 static void
115 cleanup(void)
116 {
117     if (tname)
118         unlink(tname);
119 }
120
121 /*
122  * Make the required filenames.
123  */
124 static void
125 mk_fn(int cflag, const char *f1, const char *f2, char *fn[])
126 {
127     const char *p, *s;
128     size_t n;
129     int i, fd;
130
131     i = 0;
132     s = strrchr(f1, 0);
133     n = sizeof(SFX_OBJ) - 1;
134     if ((size_t)(s - f1) > n && !memcmp(s - n, SFX_OBJ, n)) {
135         s -= n;
136         i++;
137     }
138     fn[i++] = (char *)f1;
139     if (i == FN_OBJ && !cflag) {
140         if ((fd = mkstemp(tname)) == -1)
141             err(1, NULL);
142         close(fd);
143         fn[i++] = (char *)tname;
144     }
145     if (!(fn[i] = (char *)f2)) {
146         p = (p = strrchr(f1, '/')) ? p + 1 : f1;
147         n = (size_t)(s - p);
148         if (!(fn[i] = malloc(n + SFX_MAX)))
149             err(1, NULL);
150         memcpy(fn[i], p, n);
151         strcpy(fn[i] + n, i == FN_OBJ ? SFX_OBJ : SFX_KGZ);
152     }
153 }
154
155 /*
156  * Display usage information.
157  */
158 static void
159 usage(void)
160 {
161     fprintf(stderr,
162             "usage: kgzip [-cv] [-l file] [-o filename] file\n");
163     exit(1);
164 }