2025-02-14 16:47:45 +00:00
|
|
|
|
|
|
|
#include "libtcc.h"
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif /* HAVE_CONFIG_H */
|
|
|
|
|
|
|
|
#include <tcc.h>
|
|
|
|
#include <tcc/memory.h>
|
|
|
|
#include <tcc/io.h>
|
|
|
|
#include "utils/string.h"
|
|
|
|
#include "cc/cc.h"
|
|
|
|
|
|
|
|
void
|
|
|
|
tcc_open_bf(TCCState *s1, const char *filename, int initlen)
|
|
|
|
{
|
|
|
|
BufferedFile *bf;
|
|
|
|
int buflen;
|
|
|
|
|
|
|
|
buflen = initlen ? initlen : IO_BUF_SIZE;
|
|
|
|
bf = tcc_mallocz(sizeof(BufferedFile) + buflen);
|
|
|
|
bf->buf_ptr = bf->buffer;
|
|
|
|
bf->buf_end = bf->buffer + initlen;
|
|
|
|
bf->buf_end[0] = CH_EOB; /* put eob symbol */
|
|
|
|
pstrcpy(bf->filename, sizeof(bf->filename), filename);
|
|
|
|
|
|
|
|
bf->true_filename = bf->filename;
|
|
|
|
bf->line_num = 1;
|
|
|
|
bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
|
|
|
|
bf->fd = -1;
|
|
|
|
bf->prev = file;
|
|
|
|
bf->prev_tok_flags = tok_flags;
|
|
|
|
file = bf;
|
|
|
|
tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tcc_close(void)
|
|
|
|
{
|
|
|
|
TCCState *s1 = tcc_state;
|
|
|
|
BufferedFile *bf = file;
|
|
|
|
|
|
|
|
if (bf->fd > 0)
|
|
|
|
{
|
|
|
|
close(bf->fd);
|
|
|
|
total_lines += bf->line_num - 1;
|
|
|
|
}
|
|
|
|
if (bf->true_filename != bf->filename)
|
|
|
|
{
|
|
|
|
tcc_free(bf->true_filename);
|
|
|
|
}
|
|
|
|
file = bf->prev;
|
|
|
|
tok_flags = bf->prev_tok_flags;
|
|
|
|
tcc_free(bf);
|
|
|
|
}
|
2025-02-21 14:10:26 +00:00
|
|
|
|
|
|
|
ssize_t
|
|
|
|
full_read(int fd, void *buf, size_t count)
|
|
|
|
{
|
|
|
|
char *cbuf;
|
|
|
|
size_t rnum;
|
|
|
|
ssize_t num;
|
|
|
|
|
|
|
|
cbuf = buf;
|
|
|
|
rnum = 0;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
num = read(fd, cbuf, count-rnum);
|
|
|
|
if (num < 0)
|
|
|
|
{
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
if (num == 0)
|
|
|
|
{
|
|
|
|
return rnum;
|
|
|
|
}
|
|
|
|
rnum += num;
|
|
|
|
cbuf += num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
load_data(int fd, unsigned long file_offset, unsigned long size)
|
|
|
|
{
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
data = tcc_malloc(size);
|
|
|
|
lseek(fd, file_offset, SEEK_SET);
|
|
|
|
full_read(fd, data, size);
|
|
|
|
return data;
|
|
|
|
}
|