Skip to content

Commit

Permalink
Fixes for portability to Linux.
Browse files Browse the repository at this point in the history
  • Loading branch information
compuphase committed May 30, 2019
1 parent 8760c55 commit 6de7e49
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 70 deletions.
14 changes: 7 additions & 7 deletions cppsetup.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ build_keyword_list (const char* keys, const char* values)
const char *ptmp;
int len;
/* alloc new member */
pnew = malloc(sizeof(*pnew));
pnew = (keyword_type*)malloc(sizeof(*pnew));
if (!pnew)
{
fprintf(stderr, "out of memory in my_eval_variable\n");
Expand All @@ -232,7 +232,7 @@ build_keyword_list (const char* keys, const char* values)
len = 0;
while (*ptmp && (*ptmp != ','))
ptmp++, len++;
pnew->name = malloc(len+1);
pnew->name = (char*)malloc(len+1);
if (!pnew->name)
memoryerr();
assert(pnew->name);
Expand All @@ -247,7 +247,7 @@ build_keyword_list (const char* keys, const char* values)
len = 0;
while (*ptmp && (*ptmp != ',') && (*ptmp != ')'))
ptmp++, len++;
pnew->value = malloc(len+1);
pnew->value = (char*)malloc(len+1);
if (!pnew->value)
memoryerr();
assert(pnew->value);
Expand Down Expand Up @@ -318,7 +318,7 @@ my_eval_variable (IfParser *ip, const char *var, int len, const char *args)
int newline_len = 0, newline_offset = 0;

newline_len = 64; /* start with some buffer, might increase later */
newline = malloc(newline_len);
newline = (char*)malloc(newline_len);
if (!newline)
{
fprintf(stderr, "out of memory in my_eval_variable\n");
Expand Down Expand Up @@ -350,7 +350,7 @@ my_eval_variable (IfParser *ip, const char *var, int len, const char *args)
if (newline_offset + 2 >= newline_len)
{
newline_len *= 2;
newline = realloc(newline, newline_len);
newline = (char*)realloc(newline, newline_len);
if (!newline)
{
fprintf(stderr, "out of memory in my_eval_variable\n");
Expand All @@ -370,7 +370,7 @@ my_eval_variable (IfParser *ip, const char *var, int len, const char *args)
if (newline_offset + 2 >= newline_len)
{
newline_len *= 2;
newline = realloc(newline, newline_len);
newline = (char*)realloc(newline, newline_len);
if (!newline)
{
fprintf(stderr, "out of memory in my_eval_variable\n");
Expand All @@ -389,7 +389,7 @@ my_eval_variable (IfParser *ip, const char *var, int len, const char *args)
if (newline_offset + 2 >= newline_len)
{
newline_len *= 2;
newline = realloc(newline, newline_len);
newline = (char*)realloc(newline, newline_len);
if (!newline)
{
fprintf(stderr, "out of memory in my_eval_variable\n");
Expand Down
9 changes: 6 additions & 3 deletions def.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ in this Software without prior written authorization from The Open Group.
typedef unsigned char boolean;

struct symtab {
char *s_name;
char *s_value;
char *s_args;
const char *s_name;
const char *s_value;
const char *s_args;
};

/* possible i_flag */
Expand Down Expand Up @@ -183,3 +183,6 @@ void fatalerr(const char*, ...);
void warning(const char*, ...);
void warning1(const char*, ...);
void showusage(void);

/* defined in main.c */
extern const char * const directives[];
2 changes: 1 addition & 1 deletion include.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ inc_path(const char *file, char *include, int type)
* should be at the end.
*/
if ((type == INCLUDE) || (type == INCLUDEDOT))
includedirsnext = includedirs;
includedirsnext = (const char**)includedirs;
pp = includedirsnext;

for (; *pp; pp++) {
Expand Down
91 changes: 48 additions & 43 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,42 @@ in this Software without prior written authorization from The Open Group.
#endif /* hpux */

#ifdef X_POSIX_C_SOURCE
#define _POSIX_C_SOURCE X_POSIX_C_SOURCE
#include <signal.h>
#undef _POSIX_C_SOURCE
# define _POSIX_C_SOURCE X_POSIX_C_SOURCE
# include <signal.h>
# undef _POSIX_C_SOURCE
#else
#if defined(X_NOT_POSIX) || defined(_POSIX_SOURCE)
#include <signal.h>
#else
#define _POSIX_SOURCE
#include <signal.h>
#undef _POSIX_SOURCE
# if defined(X_NOT_POSIX) || defined(_POSIX_SOURCE)
# include <signal.h>
# else
# define _POSIX_SOURCE
# include <signal.h>
# undef _POSIX_SOURCE
# endif
#endif

#if (defined _WIN32 || defined __WIN32__) && !defined WIN32
# define WIN32
#endif

#if defined _MSC_VER
#include <io.h>
#define strcasecmp(s1, s2) _stricmp((s1), (s2))
#if defined WIN32
# include <io.h>
# define strcasecmp(s1, s2) _stricmp((s1), (s2))
# define open(name, method) _open((name), (method))
# define close(handle) _close(handle)
# define read(handle, buffer, size) _read((handle), (buffer), (size))
# define unlink(name) _unlink(name)
#endif

#include <stdarg.h>

#ifdef MINIX
#define USE_CHMOD 1
# define USE_CHMOD 1
#endif

#ifdef DEBUG
int _debugmask;
#endif

#if (defined _WIN32 || defined __WIN32__) && !defined WIN32
#define WIN32
#endif

/* #define DEBUG_DUMP */
#ifdef DEBUG_DUMP
#define DBG_PRINT(file, fmt, args) fprintf(file, fmt, args)
Expand Down Expand Up @@ -126,9 +130,9 @@ const char **includedirsnext = includedirs;
char *notdotdot[MAXDIRS];
static int cmdinc_count = 0;
static char *cmdinc_list[2 * MAXINCFILES];
char *objprefix = "";
char *objsuffix = OBJSUFFIX;
static char *startat = "# GENERATED DEPENDENCIES. DO NOT DELETE.";
const char *objprefix = "";
const char *objsuffix = OBJSUFFIX;
static const char *startat = "# GENERATED DEPENDENCIES. DO NOT DELETE.";
int width = 78;
static boolean make_backup = TRUE;
static boolean include_cfile = FALSE;
Expand All @@ -151,7 +155,7 @@ static
#else
void
#endif
catch (int sig)
_catch (int sig)
{
fflush (stdout);
fatalerr ("got signal %d\n", sig);
Expand Down Expand Up @@ -202,14 +206,14 @@ int main (int argc, char *argv[])
char quotechar = '\0';

nargc = 1;
if ((afd = _open (argv[1] + 1, O_RDONLY)) < 0)
if ((afd = open (argv[1] + 1, O_RDONLY)) < 0)
fatalerr ("cannot open \"%s\"\n", argv[1] + 1);
fstat (afd, &ast);
args = (char *) malloc (ast.st_size + 1);
if ((ast.st_size = _read (afd, args, ast.st_size)) < 0)
if ((ast.st_size = read (afd, args, ast.st_size)) < 0)
fatalerr ("failed to read %s\n", argv[1] + 1);
args[ast.st_size] = '\0';
_close (afd);
close (afd);
for (p = args; *p; p++) {
if (quotechar) {
if (quotechar == '\\' || (*p == quotechar && p[-1] != '\\'))
Expand Down Expand Up @@ -291,9 +295,9 @@ int main (int argc, char *argv[])
/* Undef's override all -D's so save them up */
numundefs++;
if (numundefs == 1)
undeflist = malloc (sizeof (char *));
undeflist = (char**)malloc (sizeof (char *));
else
undeflist = realloc (undeflist, numundefs * sizeof (char *));
undeflist = (char**)realloc (undeflist, numundefs * sizeof (char *));
i = 2;
if (argv[0][i] == '\0') {
argv++;
Expand Down Expand Up @@ -417,8 +421,9 @@ int main (int argc, char *argv[])
fatalerr ("Too many -include flags.\n");
argc--;
argv++;
buf = malloc (strlen (DASH_INC_PRE) +
strlen (argv[0]) + strlen (DASH_INC_POST) + 1);
buf = (char*)malloc (strlen (DASH_INC_PRE) +
strlen (argv[0]) +
strlen (DASH_INC_POST) + 1);
if (!buf)
fatalerr ("out of memory at " "-include string\n");
cmdinc_list[2 * cmdinc_count + 0] = argv[0];
Expand Down Expand Up @@ -492,7 +497,7 @@ int main (int argc, char *argv[])
#endif
}

redirect (startat, makefile, (accumulate ? filelist : NULL));
redirect (startat, makefile, (accumulate ? (const char**)filelist : NULL));

/*
* catch signals.
Expand All @@ -501,22 +506,22 @@ int main (int argc, char *argv[])

/* should really reset SIGINT to SIG_IGN if it was. */
#ifdef SIGHUP
signal (SIGHUP, catch);
signal (SIGHUP, _catch);
#endif
signal (SIGINT, catch);
signal (SIGINT, _catch);
#ifdef SIGQUIT
signal (SIGQUIT, catch);
signal (SIGQUIT, _catch);
#endif
signal (SIGILL, catch);
signal (SIGILL, _catch);
#ifdef SIGBUS
signal (SIGBUS, catch);
signal (SIGBUS, _catch);
#endif
signal (SIGSEGV, catch);
signal (SIGSEGV, _catch);
#ifdef SIGSYS
signal (SIGSYS, catch);
signal (SIGSYS, _catch);
#endif
#else
sig_act.sa_handler = catch;
sig_act.sa_handler = _catch;
#if defined(_POSIX_SOURCE) || !defined(X_NOT_POSIX)
sigemptyset (&sig_act.sa_mask);
sigaddset (&sig_act.sa_mask, SIGINT);
Expand Down Expand Up @@ -610,7 +615,7 @@ struct filepointer *getfile (const char *file)
memoryerr();
assert(content);
content->f_name = file;
if ((fd = _open (file, O_RDONLY)) < 0) {
if ((fd = open (file, O_RDONLY)) < 0) {
warning ("cannot open \"%s\"\n", file);
content->f_p = content->f_base = content->f_end = (char *) malloc (1);
*content->f_p = '\0';
Expand All @@ -621,12 +626,12 @@ struct filepointer *getfile (const char *file)
if (content->f_base == NULL)
memoryerr ();
assert(content->f_base);
if ((st.st_size = _read (fd, content->f_base, st.st_size)) < 0)
if ((st.st_size = read (fd, content->f_base, st.st_size)) < 0)
fatalerr ("failed to read %s\n", file);
#ifdef __UNIXOS2__
st.st_size = elim_cr (content->f_base, st.st_size);
#endif
_close (fd);
close (fd);
content->f_len = st.st_size + 1;
content->f_p = content->f_base;
content->f_end = content->f_base + st.st_size;
Expand Down Expand Up @@ -900,7 +905,7 @@ void redirect (const char *line, const char *makefile, const char *filelist[])
if ((fdin = fopen (makefile, OPEN_READ_TEXT)) == NULL)
fatalerr ("cannot open \"%s\"\n", makefile);
sprintf (backup, "%s.bak", makefile);
_unlink (backup);
unlink (backup);
#if defined(WIN32) || defined(__UNIXOS2__) || defined(__CYGWIN__)
assert(fdin);
fclose (fdin);
Expand Down Expand Up @@ -940,7 +945,7 @@ void redirect (const char *line, const char *makefile, const char *filelist[])
* spaces)
*/
size_t length = ptr - buf;
char *target = malloc(length);
char *target = (char*)malloc(length);
size_t i, j;
const char **fp;
if (!target)
Expand Down Expand Up @@ -974,7 +979,7 @@ void redirect (const char *line, const char *makefile, const char *filelist[])
}
fclose(fdin);
if (!make_backup)
_unlink (backup);
unlink (backup);
fflush (fdout);
/* don't close fdout, because it is the re-opened stdout */
#if defined(USGISH) || defined(_SEQUENT_) || defined(USE_CHMOD)
Expand Down
17 changes: 8 additions & 9 deletions parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ in this Software without prior written authorization from The Open Group.

#include "def.h"

extern char *directives[];
extern struct inclist inclist[ MAXFILES ],
*inclistnext,
maininclist;
Expand Down Expand Up @@ -106,7 +105,7 @@ static int
deftype (char *line, struct filepointer *filep,
struct inclist *file_red, struct inclist *file, int parse_it)
{
register char *p;
register char *p;
char *directive, savechar, *q;
register int ret;

Expand All @@ -122,7 +121,7 @@ deftype (char *line, struct filepointer *filep,
p++;
savechar = *p;
*p = '\0';
ret = match(directive, directives);
ret = match(directive, (const char**)directives);
*p = savechar;

/* If we don't recognize this compiler directive or we happen to just
Expand Down Expand Up @@ -207,7 +206,7 @@ deftype (char *line, struct filepointer *filep,
if (!sym)
break;

p = (*sym)->s_value;
p = (char*)(*sym)->s_value;
debug(3,("%s : #includes SYMBOL %s = %s\n",
file->i_incstring,
(*sym) -> s_name,
Expand Down Expand Up @@ -388,13 +387,13 @@ define2(const char *name, const char *args, const char *val, struct inclist *fil
name, (*sp)->s_value, val, file->i_file));

if ( (*sp)->s_args )
free((*sp)->s_args);
free((void*) (*sp)->s_args);
if (args)
(*sp)->s_args = copy(args);
else
(*sp)->s_args = NULL;

free((*sp)->s_value);
free((void*) (*sp)->s_value);
(*sp)->s_value = copy(val);
return;
}
Expand Down Expand Up @@ -512,7 +511,7 @@ static char args[S_ARGS_BUFLEN];
val++;

if (!*val) /* define statements without a value will get a value of 1 */
val = "1";
val = (char*)"1";

if (args && (strlen(args)>0))
define2(def, args, val, file);
Expand Down Expand Up @@ -602,8 +601,8 @@ merge2defines(struct inclist *file1, struct inclist *file2)

while ((last1 >= first1) && (last2 >= first2))
{
char *s1=file1->i_defs[first1]->s_name;
char *s2=file2->i_defs[first2]->s_name;
const char *s1=file1->i_defs[first1]->s_name;
const char *s2=file2->i_defs[first2]->s_name;

assert(i_defs);
if (strcmp(s1,s2) < 0)
Expand Down
4 changes: 2 additions & 2 deletions pr.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ char *
targetname(const char *base)
{
int prefixlen;
char *target = malloc(BUFSIZ);
char *target = (char*)malloc(BUFSIZ);
if (!target)
memoryerr();

Expand All @@ -88,7 +88,7 @@ targetname(const char *base)
}
strxcat(target, objsuffix, BUFSIZ, 0);

target = realloc(target, strlen(target) + 1);
target = (char*)realloc(target, strlen(target) + 1);
assert(target != NULL); /* since the size shrinks, realloc always succeeds */
return target;
}
Expand Down
Loading

0 comments on commit 6de7e49

Please sign in to comment.