2c6dd1039939a38674cc02eafe11ac4574e9a536
[rrq/maintain_lilo.git] / src / temp.c
1 /* temp.c  -  Temporary file registry */
2 /* 
3 Copyright 1992-1998 Werner Almesberger.
4 Copyright 1999-2006 John Coffman.
5 All rights reserved.
6
7 Licensed under the terms contained in the file 'COPYING' in the 
8 source directory.
9
10 */
11
12
13 #define _GNU_SOURCE
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <errno.h>
18
19 #include "lilo.h"
20 #include "common.h"
21 #ifndef LILO_BINARY
22 #include "temp.h"
23 #include "loader.i"
24
25
26 typedef struct _temp {
27     char *name;
28     struct _temp *next;
29 } TEMP;
30
31
32 static TEMP *list = NULL;
33
34
35 void temp_register(char *name)
36 {
37     TEMP *new;
38
39     new = alloc_t(TEMP);
40     new->name = stralloc(name);
41     new->next = list;
42     list = new;
43 }
44
45
46 void temp_unregister(char *name)
47 {
48     TEMP **walk,*this;
49
50     for (walk = &list; *walk; walk = &(*walk)->next)
51         if (!strcmp(name,(*walk)->name)) {
52             this = *walk;
53             *walk = this->next;
54             free(this->name);
55             free(this);
56             return;
57         }
58     die("Internal error: temp_unregister %s",name);
59 }
60
61
62 void temp_remove(void)
63 {
64     TEMP *next;
65
66     while (list) {
67         next = list->next;
68         if (remove(list->name) < 0)
69             warn("(temp) %s: %s",list->name,strerror(errno));
70         else if (verbose>=2) printf("Removed temporary file %s\n",list->name);
71         free(list->name);
72         free(list);
73         list = next;
74     }
75 }
76
77
78 #else
79 #include <sys/types.h>
80 #include <sys/stat.h>
81 #include <fcntl.h>
82 #include <unistd.h>
83
84 #ifdef LCF_BUILTIN
85 void process(char *file, char *name)
86 {
87     struct stat buf;
88     int fd;
89     int nchar, nrd, i;
90 #define NBUF 16
91     unsigned char data[NBUF];
92     
93     if ((fd = open(file, O_RDONLY)) < 0) exit(1);
94     if (fstat(fd, &buf)) exit(1);
95     
96     nchar = buf.st_size;
97     printf("struct { int size; unsigned char data[%d]; } %s = { %d, {",
98         nchar, name, nchar);
99     while (nchar>0) {
100         nrd = (nchar>NBUF ? NBUF : nchar);
101         if (read(fd, data, nrd) != nrd) exit(1);
102         for (i=0; i<nrd; i++) {
103             printf("%c%3d", i?',':'\n', (int)data[i]);
104         }
105         nchar -= nrd;
106         if (nchar>0) printf(",");
107     }
108     printf("}};\n");
109     close(fd);
110     return;
111 }
112 #endif
113
114 int main(void)
115 {
116     printf("/* begin loader ***/\n");
117 #ifdef LCF_BUILTIN
118     process("first.b", "First");
119     process("second.b", "Second");
120     process("third.b", "Third");
121     process("bitmap.b", "Bitmap");
122     process("mbr.b", "Mbr");
123     process("mbr2.b", "Mbr2");
124     process("chain.b", "Chain");
125 #ifndef LCF_SOLO_CHAIN
126     process("os2_d.b", "Os2_d");
127 #endif
128 #endif
129     printf("/*** end loader ***/\n");
130     return 0;
131 }
132
133 #endif