Move file I/O related functions out of nasmlib.c

nasmlib.[ch] desperately need to be broken up into smaller chunks.

Break file I/O related functions out into file.c, so at least we can
avoid the problem with P_WAIT being defined in <io.h> on Windows but
is also used as a prefix constant in "nasm.h".

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
This commit is contained in:
H. Peter Anvin 2016-03-08 01:22:13 -08:00
parent 1917f45ec2
commit bdae10bee2
10 changed files with 151 additions and 111 deletions

View File

@ -79,7 +79,7 @@ endif
#-- Begin File Lists --#
NASM = nasm.$(O) nasmlib.$(O) ver.$(O) \
raa.$(O) saa.$(O) rbtree.$(O) \
realpath.$(O) \
realpath.$(O) file.$(O) \
float.$(O) insnsa.$(O) insnsb.$(O) \
directiv.$(O) \
assemble.$(O) labels.$(O) hashtbl.$(O) crc64.$(O) parser.$(O) \
@ -333,6 +333,7 @@ eval.$(O): eval.c compiler.h config.h directiv.h eval.h float.h insnsi.h \
labels.h nasm.h nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h
exprlib.$(O): exprlib.c compiler.h config.h directiv.h insnsi.h nasm.h \
nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h
file.$(O): file.c compiler.h config.h nasmlib.h
float.$(O): float.c compiler.h config.h directiv.h float.h insnsi.h nasm.h \
nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h
hashtbl.$(O): hashtbl.c compiler.h config.h directiv.h hashtbl.h insnsi.h \

View File

@ -48,7 +48,7 @@ X = .exe
# Edit in Makefile.in, not here!
NASM = nasm.$(O) nasmlib.$(O) ver.$(O) \
raa.$(O) saa.$(O) rbtree.$(O) \
realpath.$(O) \
realpath.$(O) file.$(O) \
float.$(O) insnsa.$(O) insnsb.$(O) \
directiv.$(O) \
assemble.$(O) labels.$(O) hashtbl.$(O) crc64.$(O) parser.$(O) \
@ -242,6 +242,7 @@ eval.$(O): eval.c compiler.h directiv.h eval.h float.h insnsi.h labels.h \
nasm.h nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h
exprlib.$(O): exprlib.c compiler.h directiv.h insnsi.h nasm.h nasmlib.h \
opflags.h pptok.h preproc.h regs.h tables.h
file.$(O): file.c compiler.h nasmlib.h
float.$(O): float.c compiler.h directiv.h float.h insnsi.h nasm.h nasmlib.h \
opflags.h pptok.h preproc.h regs.h tables.h
hashtbl.$(O): hashtbl.c compiler.h directiv.h hashtbl.h insnsi.h nasm.h \

View File

@ -32,7 +32,7 @@ O = o
# Edit in Makefile.in, not here!
NASM = nasm.o nasmlib.o ver.o \
raa.o saa.o rbtree.o \
realpath.o \
realpath.o file.o \
float.o insnsa.o insnsb.o \
directiv.o \
assemble.o labels.o hashtbl.o crc64.o parser.o \
@ -147,6 +147,7 @@ eval.o: eval.c compiler.h config.h directiv.h eval.h float.h insnsi.h \
labels.h nasm.h nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h
exprlib.o: exprlib.c compiler.h config.h directiv.h insnsi.h nasm.h \
nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h
file.o: file.c compiler.h config.h nasmlib.h
float.o: float.c compiler.h config.h directiv.h float.h insnsi.h nasm.h \
nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h
hashtbl.o: hashtbl.c compiler.h config.h directiv.h hashtbl.h insnsi.h \

View File

@ -48,7 +48,7 @@ X = .exe
# Edit in Makefile.in, not here!
NASM = nasm.$(O) nasmlib.$(O) ver.$(O) &
raa.$(O) saa.$(O) rbtree.$(O) &
realpath.$(O) &
realpath.$(O) file.$(O) &
float.$(O) insnsa.$(O) insnsb.$(O) &
directiv.$(O) &
assemble.$(O) labels.$(O) hashtbl.$(O) crc64.$(O) parser.$(O) &
@ -284,6 +284,7 @@ eval.$(O): eval.c compiler.h config.h directiv.h eval.h float.h insnsi.h &
labels.h nasm.h nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h
exprlib.$(O): exprlib.c compiler.h config.h directiv.h insnsi.h nasm.h &
nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h
file.$(O): file.c compiler.h config.h nasmlib.h
float.$(O): float.c compiler.h config.h directiv.h float.h insnsi.h nasm.h &
nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h
hashtbl.$(O): hashtbl.c compiler.h config.h directiv.h hashtbl.h insnsi.h &

View File

@ -59,7 +59,7 @@ X = .exe
# Edit in Makefile.in, not here!
NASM = nasm.$(O) nasmlib.$(O) ver.$(O) \
raa.$(O) saa.$(O) rbtree.$(O) \
realpath.$(O) \
realpath.$(O) file.$(O) \
float.$(O) insnsa.$(O) insnsb.$(O) \
directiv.$(O) \
assemble.$(O) labels.$(O) hashtbl.$(O) crc64.$(O) parser.$(O) \
@ -253,6 +253,7 @@ eval.$(O): eval.c compiler.h directiv.h eval.h float.h insnsi.h labels.h \
nasm.h nasmlib.h opflags.h pptok.h preproc.h regs.h tables.h
exprlib.$(O): exprlib.c compiler.h directiv.h insnsi.h nasm.h nasmlib.h \
opflags.h pptok.h preproc.h regs.h tables.h
file.$(O): file.c compiler.h nasmlib.h
float.$(O): float.c compiler.h directiv.h float.h insnsi.h nasm.h nasmlib.h \
opflags.h pptok.h preproc.h regs.h tables.h
hashtbl.$(O): hashtbl.c compiler.h directiv.h hashtbl.h insnsi.h nasm.h \

140
file.c Normal file
View File

@ -0,0 +1,140 @@
/* ----------------------------------------------------------------------- *
*
* Copyright 1996-2016 The NASM Authors - All Rights Reserved
* See the file AUTHORS included with the NASM distribution for
* the specific copyright holders.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ----------------------------------------------------------------------- */
#include "compiler.h"
#include "nasmlib.h"
#include <errno.h>
#ifdef HAVE_IO_H
# include <io.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
void nasm_write(const void *ptr, size_t size, FILE *f)
{
size_t n = fwrite(ptr, 1, size, f);
if (n != size || ferror(f) || feof(f))
nasm_fatal(0, "unable to write output: %s", strerror(errno));
}
#ifdef WORDS_LITTLEENDIAN
void fwriteint16_t(uint16_t data, FILE * fp)
{
nasm_write(&data, 2, fp);
}
void fwriteint32_t(uint32_t data, FILE * fp)
{
nasm_write(&data, 4, fp);
}
void fwriteint64_t(uint64_t data, FILE * fp)
{
nasm_write(&data, 8, fp);
}
void fwriteaddr(uint64_t data, int size, FILE * fp)
{
nasm_write(&data, size, fp);
}
#else /* not WORDS_LITTLEENDIAN */
void fwriteint16_t(uint16_t data, FILE * fp)
{
char buffer[2], *p = buffer;
WRITESHORT(p, data);
nasm_write(buffer, 2, fp);
}
void fwriteint32_t(uint32_t data, FILE * fp)
{
char buffer[4], *p = buffer;
WRITELONG(p, data);
nasm_write(buffer, 4, fp);
}
void fwriteint64_t(uint64_t data, FILE * fp)
{
char buffer[8], *p = buffer;
WRITEDLONG(p, data);
nasm_write(buffer, 8, fp);
}
void fwriteaddr(uint64_t data, int size, FILE * fp)
{
char buffer[8], *p = buffer;
WRITEADDR(p, data, size);
nasm_write(buffer, size, fp);
}
#endif
#ifdef HAVE_FILENO /* Useless without fileno() */
# ifdef HAVE__CHSIZE_S
# define nasm_ftruncate(fd,size) _chsize_s(fd,size)
# elif defined(HAVE__CHSIZE)
# define nasm_ftruncate(fd,size) _chsize(fd,size)
# elif defined(HAVE_FTRUNCATE)
# define nasm_ftruncate(fd,size) ftruncate(fd,size)
# endif
#endif
void fwritezero(size_t bytes, FILE *fp)
{
size_t blksize;
#ifdef nasm_ftruncate
if (bytes >= BUFSIZ && !ferror(fp) && !feof(fp)) {
off_t pos = ftello(fp);
if (pos >= 0) {
if (!fflush(fp) &&
!nasm_ftruncate(fileno(fp), pos + bytes) &&
!fseeko(fp, pos+bytes, SEEK_SET))
return;
}
}
#endif
while (bytes) {
blksize = (bytes < ZERO_BUF_SIZE) ? bytes : ZERO_BUF_SIZE;
nasm_write(zero_buffer, blksize, fp);
bytes -= blksize;
}
}

4
nasm.h
View File

@ -479,10 +479,6 @@ enum vex_class {
* prefixes, we must ensure the enumerations for prefixes and
* register names do not overlap.
*/
#ifdef P_WAIT
# undef P_WAIT /* Macro defined on some Windows versions */
#endif
enum prefixes { /* instruction prefixes */
P_none = 0,
PREFIX_ENUM_START = REG_ENUM_LIMIT,

View File

@ -162,13 +162,6 @@ no_return nasm_assert_failed(const char *file, int line, const char *msg)
exit(1);
}
void nasm_write(const void *ptr, size_t size, FILE *f)
{
size_t n = fwrite(ptr, 1, size, f);
if (n != size || ferror(f) || feof(f))
nasm_fatal(0, "unable to write output: %s", strerror(errno));
}
#ifndef nasm_stricmp
int nasm_stricmp(const char *s1, const char *s2)
{
@ -405,94 +398,6 @@ int32_t seg_alloc(void)
return this_seg;
}
#ifdef WORDS_LITTLEENDIAN
void fwriteint16_t(uint16_t data, FILE * fp)
{
nasm_write(&data, 2, fp);
}
void fwriteint32_t(uint32_t data, FILE * fp)
{
nasm_write(&data, 4, fp);
}
void fwriteint64_t(uint64_t data, FILE * fp)
{
nasm_write(&data, 8, fp);
}
void fwriteaddr(uint64_t data, int size, FILE * fp)
{
nasm_write(&data, size, fp);
}
#else /* not WORDS_LITTLEENDIAN */
void fwriteint16_t(uint16_t data, FILE * fp)
{
char buffer[2], *p = buffer;
WRITESHORT(p, data);
nasm_write(buffer, 2, fp);
}
void fwriteint32_t(uint32_t data, FILE * fp)
{
char buffer[4], *p = buffer;
WRITELONG(p, data);
nasm_write(buffer, 4, fp);
}
void fwriteint64_t(uint64_t data, FILE * fp)
{
char buffer[8], *p = buffer;
WRITEDLONG(p, data);
nasm_write(buffer, 8, fp);
}
void fwriteaddr(uint64_t data, int size, FILE * fp)
{
char buffer[8], *p = buffer;
WRITEADDR(p, data, size);
nasm_write(buffer, size, fp);
}
#endif
#ifdef HAVE_FILENO /* Useless without fileno() */
# ifdef HAVE__CHSIZE_S
# define nasm_ftruncate(fd,size) _chsize_s(fd,size)
# elif defined(HAVE__CHSIZE)
# define nasm_ftruncate(fd,size) _chsize(fd,size)
# elif defined(HAVE_FTRUNCATE)
# define nasm_ftruncate(fd,size) ftruncate(fd,size)
# endif
#endif
void fwritezero(size_t bytes, FILE *fp)
{
size_t blksize;
#ifdef nasm_ftruncate
if (bytes >= BUFSIZ && !ferror(fp) && !feof(fp)) {
off_t pos = ftello(fp);
if (pos >= 0) {
if (!fflush(fp) &&
!nasm_ftruncate(fileno(fp), pos + bytes) &&
!fseeko(fp, pos+bytes, SEEK_SET))
return;
}
}
#endif
while (bytes) {
blksize = (bytes < ZERO_BUF_SIZE) ? bytes : ZERO_BUF_SIZE;
nasm_write(zero_buffer, blksize, fp);
bytes -= blksize;
}
}
void standard_extension(char *inname, char *outname, char *extension)
{
char *p, *q;

View File

@ -46,12 +46,6 @@
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#ifdef HAVE_IO_H
# include <io.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
/*
* tolower table -- avoids a function call on some platforms.

View File

@ -32,7 +32,7 @@ MKDIR = mkdir
LDRDFLIBS = rdoff.$(O) symtab.$(O) collectn.$(O) \
rdlib.$(O) segtab.$(O) hash.$(O)
RDXLIBS = rdoff.$(O) rdfload.$(O) symtab.$(O) collectn.$(O) hash.$(O)
NASMLIB = ../nasmlib.$(O)
NASMLIB = ../nasmlib.$(O) ../file.$(O)
# Binary suffixes
O = @OBJEXT@