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