From 84706bd2d581e6b868cb35522f517355c7e5e6f1 Mon Sep 17 00:00:00 2001 From: cuz Date: Sun, 11 Dec 2005 12:40:51 +0000 Subject: [PATCH] Fixed portability problems with va_copy. In three places, calls to fstat had to be replaced by calls to stat, because fileno is no longer available when forcing the compiler into pure c89 (or c99) mode. git-svn-id: svn://svn.cc65.org/cc65/trunk@3683 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/ar65/make/gcc.mak | 4 +-- src/ar65/objfile.c | 13 +++++++--- src/ca65/make/gcc.mak | 4 +-- src/ca65/scanner.c | 49 ++++++++++++++++++++---------------- src/cc65/input.c | 11 ++++++-- src/cc65/make/gcc.mak | 2 +- src/cc65/preproc.c | 9 ++++--- src/cl65/make/gcc.mak | 4 +-- src/co65/make/gcc.mak | 4 +-- src/common/make/gcc.mak | 4 +-- src/common/va_copy.h | 13 ++++++++-- src/da65/make/gcc.mak | 4 +-- src/grc/grc.c | 2 -- src/grc/grc.h | 2 +- src/grc/make/gcc.mak | 4 +-- src/ld65/make/gcc.mak | 4 +-- src/make/gcc.mak | 2 +- src/od65/make/gcc.mak | 4 +-- src/sim65/chips/make/gcc.mak | 4 +-- src/sim65/make/gcc.mak | 4 +-- 20 files changed, 88 insertions(+), 59 deletions(-) diff --git a/src/ar65/make/gcc.mak b/src/ar65/make/gcc.mak index e4addbcb7..48238700f 100644 --- a/src/ar65/make/gcc.mak +++ b/src/ar65/make/gcc.mak @@ -5,7 +5,7 @@ # Library dir COMMON = ../common -CFLAGS = -g -O2 -Wall -W -I$(COMMON) +CFLAGS = -g -O2 -Wall -W -std=c89 -I$(COMMON) CC = gcc EBIND = emxbind LDFLAGS = @@ -56,6 +56,6 @@ zap: clean .PHONY: depend dep depend dep: $(OBJS:.o=.c) @echo "Creating dependency information" - $(CC) -I$(COMMON) -MM $^ > .depend + $(CC) $(CFLAGS) -MM $^ > .depend diff --git a/src/ar65/objfile.c b/src/ar65/objfile.c index 1c18fddd5..11d4de8f7 100644 --- a/src/ar65/objfile.c +++ b/src/ar65/objfile.c @@ -142,7 +142,7 @@ void ObjWriteHeader (FILE* Obj, ObjHeader* H) Write32 (Obj, H->StrPoolOffs); Write32 (Obj, H->StrPoolSize); Write32 (Obj, H->AssertOffs); - Write32 (Obj, H->AssertSize); + Write32 (Obj, H->AssertSize); Write32 (Obj, H->ScopeOffs); Write32 (Obj, H->ScopeSize); } @@ -164,8 +164,15 @@ void ObjAdd (const char* Name) Error ("Could not open `%s': %s", Name, strerror (errno)); } - /* Get the modification time of the object file */ - if (fstat (fileno (Obj), &StatBuf) != 0) { + /* Get the modification time of the object file. There a race condition + * here, since we cannot use fileno() (non standard identifier in standard + * header file), and therefore not fstat. When using stat with the + * file name, there's a risk that the file was deleted and recreated + * while it was open. Since mtime and size are only used to check + * if a file has changed in the debugger, we will ignore this problem + * here. + */ + if (stat (Name, &StatBuf) != 0) { Error ("Cannot stat object file `%s': %s", Name, strerror (errno)); } diff --git a/src/ca65/make/gcc.mak b/src/ca65/make/gcc.mak index 3845422e0..b6b75d54d 100644 --- a/src/ca65/make/gcc.mak +++ b/src/ca65/make/gcc.mak @@ -5,7 +5,7 @@ # Library dir COMMON = ../common -CFLAGS = -g -O2 -Wall -W -I$(COMMON) +CFLAGS = -g -O2 -Wall -W -std=c89 -I$(COMMON) CC = gcc EBIND = emxbind LDFLAGS = @@ -98,7 +98,7 @@ zap: clean .PHONY: depend dep depend dep: $(OBJS:.o=.c) @echo "Creating dependency information" - $(CC) -I$(COMMON) -MM -MG $^ > .depend + $(CC) $(CFLAGS) -MM -MG $^ > .depend # ----------------------------------------------------------------------------- # Rules to make config includes diff --git a/src/ca65/scanner.c b/src/ca65/scanner.c index cc202fe6c..7504ae2ea 100644 --- a/src/ca65/scanner.c +++ b/src/ca65/scanner.c @@ -307,15 +307,12 @@ int IsIdStart (int C) void NewInputFile (const char* Name) /* Open a new input file */ { - InputFile* I; - FILE* F; + char* PathName = 0; /* First try to open the file */ - F = fopen (Name, "r"); + FILE* F = fopen (Name, "r"); if (F == 0) { - char* PathName; - /* Error (fatal error if this is the main file) */ if (ICount == 0) { Fatal ("Cannot open input file `%s': %s", Name, strerror (errno)); @@ -330,19 +327,26 @@ void NewInputFile (const char* Name) Error ("Cannot open include file `%s': %s", Name, strerror (errno)); } - /* Free the allocated memory */ - xfree (PathName); - + /* Use the path name from now on */ + Name = PathName; } /* check again if we do now have an open file */ if (F != 0) { unsigned FileIdx; - - /* Stat the file and remember the values */ + InputFile* IF; + + /* Stat the file and remember the values. There a race condition here, + * since we cannot use fileno() (non standard identifier in standard + * header file), and therefore not fstat. When using stat with the + * file name, there's a risk that the file was deleted and recreated + * while it was open. Since mtime and size are only used to check + * if a file has changed in the debugger, we will ignore this problem + * here. + */ struct stat Buf; - if (fstat (fileno (F), &Buf) != 0) { + if (stat (Name, &Buf) != 0) { Fatal ("Cannot stat input file `%s': %s", Name, strerror (errno)); } @@ -350,18 +354,18 @@ void NewInputFile (const char* Name) FileIdx = AddFile (Name, Buf.st_size, Buf.st_mtime); /* Create a new state variable and initialize it */ - I = xmalloc (sizeof (*I)); - I->F = F; - I->Pos.Line = 0; - I->Pos.Col = 0; - I->Pos.Name = FileIdx; - I->Tok = Tok; - I->C = C; - I->Line[0] = '\0'; + IF = xmalloc (sizeof (*IF)); + IF->F = F; + IF->Pos.Line = 0; + IF->Pos.Col = 0; + IF->Pos.Name = FileIdx; + IF->Tok = Tok; + IF->C = C; + IF->Line[0] = '\0'; /* Use the new file */ - I->Next = IFile; - IFile = I; + IF->Next = IFile; + IFile = IF; ++ICount; /* Read the first character from the new file */ @@ -373,6 +377,9 @@ void NewInputFile (const char* Name) Tok = TOK_SEP; } + + /* Free an allocated name buffer */ + xfree (PathName); } diff --git a/src/cc65/input.c b/src/cc65/input.c index d093f4103..7a18be365 100644 --- a/src/cc65/input.c +++ b/src/cc65/input.c @@ -143,9 +143,16 @@ static AFile* NewAFile (IFile* IF, FILE* F) */ if (IF->Usage++ == 0) { - /* Get file size and modification time */ + /* Get file size and modification time. There a race condition here, + * since we cannot use fileno() (non standard identifier in standard + * header file), and therefore not fstat. When using stat with the + * file name, there's a risk that the file was deleted and recreated + * while it was open. Since mtime and size are only used to check + * if a file has changed in the debugger, we will ignore this problem + * here. + */ struct stat Buf; - if (fstat (fileno (F), &Buf) != 0) { + if (stat (IF->Name, &Buf) != 0) { /* Error */ Fatal ("Cannot stat `%s': %s", IF->Name, strerror (errno)); } diff --git a/src/cc65/make/gcc.mak b/src/cc65/make/gcc.mak index dc2aa0df1..030189fd7 100644 --- a/src/cc65/make/gcc.mak +++ b/src/cc65/make/gcc.mak @@ -17,7 +17,7 @@ COMMON = ../common CC65_INC = \"/usr/lib/cc65/include/\" # -CFLAGS = -O2 -g -Wall -W -I$(COMMON) -DCC65_INC=$(CC65_INC) +CFLAGS = -O2 -g -Wall -W -std=c89 -I$(COMMON) -DCC65_INC=$(CC65_INC) CC=gcc EBIND=emxbind LDFLAGS=-lm diff --git a/src/cc65/preproc.c b/src/cc65/preproc.c index 4f7e77fce..f04a9642a 100644 --- a/src/cc65/preproc.c +++ b/src/cc65/preproc.c @@ -672,8 +672,9 @@ static void MacroCall (StrBuf* Target, Macro* M) static void ExpandMacro (StrBuf* Target, Macro* M) /* Expand a macro into Target */ { + /* ### printf ("Expanding %s(%u)\n", M->Name, ++V); */ + /* Check if this is a function like macro */ - //printf ("Expanding %s(%u)\n", M->Name, ++V); if (M->ArgCount >= 0) { int Whitespace = IsSpace (CurC); @@ -710,7 +711,7 @@ static void ExpandMacro (StrBuf* Target, Macro* M) DoneMacroExp (&E); } - //printf ("Done with %s(%u)\n", M->Name, V--); + /* ### printf ("Done with %s(%u)\n", M->Name, V--); */ } @@ -770,7 +771,7 @@ static void DefineMacro (void) NextChar (); NextChar (); - /* Remember that the macro is variadic and use __VA_ARGS__ as + /* Remember that the macro is variadic and use __VA_ARGS__ as * the argument name. */ AddMacroArg (M, "__VA_ARGS__"); @@ -827,7 +828,7 @@ static void DefineMacro (void) SB_Drop (&M->Replacement, 1); } - //printf ("%s: <%.*s>\n", M->Name, SB_GetLen (&M->Replacement), SB_GetConstBuf (&M->Replacement)); + /* ### printf ("%s: <%.*s>\n", M->Name, SB_GetLen (&M->Replacement), SB_GetConstBuf (&M->Replacement)); */ /* If we have an existing macro, check if the redefinition is identical. * Print a diagnostic if not. diff --git a/src/cl65/make/gcc.mak b/src/cl65/make/gcc.mak index 80757991b..a7c02ae59 100644 --- a/src/cl65/make/gcc.mak +++ b/src/cl65/make/gcc.mak @@ -13,7 +13,7 @@ endif CC=gcc -CFLAGS = -O2 -g -Wall -W -I$(COMMON) -D$(SPAWN) +CFLAGS = -O2 -g -Wall -W -std=c89 -I$(COMMON) -D$(SPAWN) EBIND = emxbind LDFLAGS= @@ -53,7 +53,7 @@ zap: clean .PHONY: depend dep depend dep: $(OBJS:.o=.c) @echo "Creating dependency information" - $(CC) -I$(COMMON) -D$(SPAWN) -MM $^ > .depend + $(CC) $(CFLAGS) -D$(SPAWN) -MM $^ > .depend diff --git a/src/co65/make/gcc.mak b/src/co65/make/gcc.mak index 7a56468dd..1fa07598c 100644 --- a/src/co65/make/gcc.mak +++ b/src/co65/make/gcc.mak @@ -5,7 +5,7 @@ # Library dir COMMON = ../common -CFLAGS = -g -O2 -Wall -W -I$(COMMON) +CFLAGS = -g -O2 -Wall -W -std=c89 -I$(COMMON) CC = gcc EBIND = emxbind LDFLAGS = @@ -49,6 +49,6 @@ zap: clean .PHONY: depend dep depend dep: $(OBJS:.o=.c) @echo "Creating dependency information" - $(CC) -I$(COMMON) -MM $^ > .depend + $(CC) $(CFLAGS) -MM $^ > .depend diff --git a/src/common/make/gcc.mak b/src/common/make/gcc.mak index 073532ce9..56e90448a 100644 --- a/src/common/make/gcc.mak +++ b/src/common/make/gcc.mak @@ -2,7 +2,7 @@ # gcc Makefile for the binutils common stuff # -CFLAGS = -g -O2 -Wall -W +CFLAGS = -g -O2 -Wall -W -std=c89 CC = gcc LDFLAGS = LIB = common.a @@ -72,6 +72,6 @@ zap: clean .PHONY: depend dep depend dep: $(OBJS:.o=.c) @echo "Creating dependency information" - $(CC) -MM $^ > .depend + $(CC) $(CFLAGS) -MM $^ > .depend diff --git a/src/common/va_copy.h b/src/common/va_copy.h index 18c8a75b7..4164dd319 100644 --- a/src/common/va_copy.h +++ b/src/common/va_copy.h @@ -38,18 +38,27 @@ +/* No action if we're using a C99 compiler */ +#if (__STDC_VERSION__ < 199901) + + + /* The watcom compiler doesn't have va_copy and a problematic va_list definition */ #if defined(__WATCOMC__) #define va_copy(dest,src) memcpy((dest), (src), sizeof (va_list)) #endif -/* GNU C before version 3 has its own name */ -#if defined(__GNUC__) && (__GNUC__ == 2) +/* GNU C has a builtin function */ +#if defined(__GNUC__) #define va_copy(dest,src) __va_copy(dest, src) #endif +#endif /* #if (__STDC_VERSION__ < 199901) */ + + + /* End of va_copy.h */ #endif diff --git a/src/da65/make/gcc.mak b/src/da65/make/gcc.mak index d4ac9d77b..fe3db010c 100644 --- a/src/da65/make/gcc.mak +++ b/src/da65/make/gcc.mak @@ -5,7 +5,7 @@ # Library dir COMMON = ../common -CFLAGS = -g -O2 -Wall -W -I$(COMMON) +CFLAGS = -g -O2 -Wall -W -std=c89 -I$(COMMON) CC=gcc EBIND=emxbind LDFLAGS= @@ -60,6 +60,6 @@ zap: clean .PHONY: depend dep depend dep: $(OBJS:.o=.c) @echo "Creating dependency information" - $(CC) -I$(COMMON) -MM $^ > .depend + $(CC) $(CFLAGS) -MM $^ > .depend diff --git a/src/grc/grc.c b/src/grc/grc.c index fa884f411..fb3ad2cc6 100644 --- a/src/grc/grc.c +++ b/src/grc/grc.c @@ -28,7 +28,6 @@ #include "grc.h" /* common stuff */ -//#include "cmdline.h" #include "fname.h" #include "abend.h" #include "chartype.h" @@ -261,7 +260,6 @@ int a; } } -//char *bintos(unsigned char a, char *out) { char *bintos(unsigned char a, char out[7]) { int i=0; for (;i<8;i++) { diff --git a/src/grc/grc.h b/src/grc/grc.h index d2a96362b..9825fe931 100644 --- a/src/grc/grc.h +++ b/src/grc/grc.h @@ -66,7 +66,7 @@ const unsigned char icon1[] = { 128, 0, 1, 128, 0, 1, 128, 0, 1, 128, 0, 1, 128, 0, 1, 128, 0, 1, 128, 0, 1, 128, 0, 1, 128, 0, 1, 255, 255, 255 }; -char *ProgName; // for AbEnd, later remove and use common/cmdline.h +char *ProgName; /* for AbEnd, later remove and use common/cmdline.h */ char *outputCName=NULL, *outputSName=NULL, *outputVName=NULL; FILE *outputCFile, *outputSFile, *outputVFile; diff --git a/src/grc/make/gcc.mak b/src/grc/make/gcc.mak index 592c1fee4..d86b3055b 100644 --- a/src/grc/make/gcc.mak +++ b/src/grc/make/gcc.mak @@ -4,7 +4,7 @@ COMMON = ../common -CFLAGS = -g -O2 -Wall -W -I$(COMMON) +CFLAGS = -g -O2 -Wall -W -std=c89 -I$(COMMON) CC = gcc LDFLAGS = EBIND = emxbind @@ -42,6 +42,6 @@ zap: clean .PHONY: depend dep depend dep: $(OBJS:.o=.c) @echo "Creating dependency information" - $(CC) -I$(COMMON) -MM $^ > .depend + $(CC) $(CFLAGS) -MM $^ > .depend diff --git a/src/ld65/make/gcc.mak b/src/ld65/make/gcc.mak index c8112c61e..94ff4a67c 100644 --- a/src/ld65/make/gcc.mak +++ b/src/ld65/make/gcc.mak @@ -10,7 +10,7 @@ COMMON = ../common CC65_LIB = \"/usr/lib/cc65/lib/\" # -CFLAGS = -g -O2 -Wall -W -I$(COMMON) -DCC65_LIB=$(CC65_LIB) +CFLAGS = -g -O2 -Wall -W -std=c89 -I$(COMMON) -DCC65_LIB=$(CC65_LIB) CC=gcc EBIND=emxbind LDFLAGS= @@ -113,7 +113,7 @@ zap: clean .PHONY: depend dep depend dep: $(OBJS:.o=.c) @echo "Creating dependency information" - $(CC) -I$(COMMON) -MM -MG $^ > .depend + $(CC) $(CFLAGS) -MM -MG $^ > .depend # ----------------------------------------------------------------------------- # Rules to make config includes diff --git a/src/make/gcc.mak b/src/make/gcc.mak index 4e3bcdcc6..747b1400c 100644 --- a/src/make/gcc.mak +++ b/src/make/gcc.mak @@ -2,7 +2,7 @@ # gcc Makefile for the program sources # -CFLAGS = -g -O2 -Wall +CFLAGS = -g -O2 -Wall -std=c89 CC = gcc LDFLAGS = diff --git a/src/od65/make/gcc.mak b/src/od65/make/gcc.mak index 97659c6f5..1c05b8193 100644 --- a/src/od65/make/gcc.mak +++ b/src/od65/make/gcc.mak @@ -6,7 +6,7 @@ # Library dir COMMON = ../common -CFLAGS = -O2 -g -Wall -W -I$(COMMON) +CFLAGS = -O2 -g -Wall -W -std=c89 -I$(COMMON) CC=gcc EBIND=emxbind LDFLAGS= @@ -48,7 +48,7 @@ zap: clean .PHONY: depend dep depend dep: $(OBJS:.o=.c) @echo "Creating dependency information" - $(CC) -I$(COMMON) -MM $^ > .depend + $(CC) $(CFLAGS) -MM $^ > .depend diff --git a/src/sim65/chips/make/gcc.mak b/src/sim65/chips/make/gcc.mak index bb923e929..4626b8ce8 100644 --- a/src/sim65/chips/make/gcc.mak +++ b/src/sim65/chips/make/gcc.mak @@ -6,7 +6,7 @@ COMMON = ../../common SIM65 = .. -CFLAGS = -g -O2 -Wall -W -I$(COMMON) -I$(SIM65) -fpic +CFLAGS = -g -O2 -Wall -W -std=c89 -I$(COMMON) -I$(SIM65) -fpic CC = gcc EBIND = emxbind LDFLAGS = @@ -56,6 +56,6 @@ zap: clean .PHONY: depend dep depend dep: $(CHIPS:.so=.c) @echo "Creating dependency information" - $(CC) -I$(COMMON) -I$(SIM65) -MM $^ > .depend + $(CC) $(CFLAGS) -MM $^ > .depend diff --git a/src/sim65/make/gcc.mak b/src/sim65/make/gcc.mak index e2c1e25b0..7d7cf173f 100644 --- a/src/sim65/make/gcc.mak +++ b/src/sim65/make/gcc.mak @@ -5,7 +5,7 @@ # Library dir COMMON = ../common -CFLAGS = -g -O2 -Wall -W -I$(COMMON) +CFLAGS = -g -O2 -Wall -W -std=c89 -I$(COMMON) CC = gcc EBIND = emxbind LDFLAGS = @@ -63,6 +63,6 @@ zap: clean .PHONY: depend dep depend dep: $(OBJS:.o=.c) @echo "Creating dependency information" - $(CC) -I$(COMMON) -MM $^ > .depend + $(CC) $(CFLAGS) -MM $^ > .depend -- 2.39.5