lib/pam: remove register keyword
[oweals/cde.git] / cde / config / imake / imake.c
index 235d791fc5807fbbefbf5eb87aab0ae20dd8b9f8..3444c045fadc92536c86e285b82a7f919ded604c 100644 (file)
@@ -16,7 +16,7 @@
  * details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with these librararies and programs; if not, write
+ * License along with these libraries and programs; if not, write
  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  * Floor, Boston, MA 02110-1301 USA
  */
@@ -226,9 +226,6 @@ typedef union wait  waitType;
 char *malloc(), *realloc();
 void exit();
 #endif
-#if defined(macII) && !defined(__STDC__)  /* stdlib.h fails to define these */
-char *malloc(), *realloc();
-#endif /* macII */
 #ifdef X_NOT_STDC_ENV
 extern char    *getenv();
 #endif
@@ -256,13 +253,14 @@ extern int        errno;
  * are there any X_NOT_STDC_ENV machines left in the world?
  */
 #include <string.h>
+#include <stdarg.h>
 #include "imakemdep.h"
 
 /*
  * This define of strerror is copied from (and should be identical to)
  * Xos.h, which we don't want to include here for bootstrapping reasons.
  */
-#if defined(X_NOT_STDC_ENV) || (defined(sun) && !defined(SVR4)) || defined(macII)
+#if defined(X_NOT_STDC_ENV) || (defined(sun) && !defined(SVR4))
 # ifndef strerror
 extern char *sys_errlist[];
 extern int sys_nerr;
@@ -275,14 +273,14 @@ extern int sys_nerr;
 #define        FALSE           0
 
 #ifdef FIXUP_CPP_WHITESPACE
-int    InRule = FALSE;
+static int     InRule = FALSE;
 # ifdef INLINE_SYNTAX
-int    InInline = 0;
+static int     InInline = 0;
 # endif
 #endif
 #ifdef MAGIC_MAKE_VARS
-int xvariable = 0;
-int xvariables[10];
+static int xvariable = 0;
+static int xvariables[10];
 #endif
 
 /*
@@ -291,7 +289,8 @@ int xvariables[10];
  * space instead of being deleted.  Blech.
  */
 #ifdef FIXUP_CPP_WHITESPACE
-void KludgeOutputLine(), KludgeResetRule();
+static void KludgeOutputLine(char **pline);
+static void KludgeResetRule(void);
 #else
 # define KludgeOutputLine(arg)
 # define KludgeResetRule()
@@ -313,11 +312,11 @@ typedef   unsigned char   boolean;
 # endif
 #endif
 
-char *cpp = NULL;
+static char *cpp = NULL;
 
-char   *tmpMakefile    = "/tmp/Imf.XXXXXX";
-char   *tmpImakefile    = "/tmp/IIf.XXXXXX";
-char   *make_argv[ ARGUMENTS ] = {
+static char *tmpMakefile = "/tmp/Imf.XXXXXX";
+static char    *tmpImakefile    = "/tmp/IIf.XXXXXX";
+static char    *make_argv[ ARGUMENTS ] = {
 #ifdef WIN32
     "nmake"
 #else
@@ -325,42 +324,72 @@ char      *make_argv[ ARGUMENTS ] = {
 #endif
 };
 
-int    make_argindex;
-int    cpp_argindex;
-char   *Imakefile = NULL;
-char   *Makefile = "Makefile";
-char   *Template = "Imake.tmpl";
-char   *ImakefileC = "Imakefile.c";
-boolean haveImakefileC = FALSE;
-char   *cleanedImakefile = NULL;
-char   *program;
-char   *FindImakefile();
-char   *ReadLine();
-char   *CleanCppInput();
-char   *Strdup();
-char   *Emalloc();
-void   LogFatalI(), LogFatal(), LogMsg();
-
-void   showit();
-void   wrapup();
-void   init();
-void   AddMakeArg();
-void   AddCppArg();
-void   SetOpts();
-void   CheckImakefileC();
-void   cppit();
-void   makeit();
-void   CleanCppOutput();
-boolean        isempty();
-void   writetmpfile();
-
-boolean        verbose = FALSE;
-boolean        show = TRUE;
+static int     make_argindex;
+static int     cpp_argindex;
+static char    *Imakefile = NULL;
+static char    *Makefile = "Makefile";
+static char    *Template = "Imake.tmpl";
+static char    *ImakefileC = "Imakefile.c";
+static boolean haveImakefileC = FALSE;
+static char    *cleanedImakefile = NULL;
+static char    *program;
+static boolean verbose = FALSE;
+static boolean show = TRUE;
+
+static char    *FindImakefile(char *);
+static char    *ReadLine(FILE *, const char *);
+static char    *CleanCppInput(char *);
+static char    *Strdup(const char *);
+static char    *Emalloc(int);
+static void    LogFatal(const char *, ...);
+static void    LogMsg(const char *, ...);
+static void    Log(const char *, va_list);
+
+static void    showit(FILE *);
+static void    wrapup(void);
+static
+#ifdef SIGNALRETURNSINT
+int
+#else
+void
+#endif
+catch(int);
+static void    init(void);
+static void    AddMakeArg(char *);
+static void    AddCppArg(char *);
+static void    SetOpts(int, char **);
+static void    showargs(char **);
+static void    CheckImakefileC(const char *);
+static boolean optional_include(FILE *, const char *, const char *);
+static void    doit(FILE *, const char *, char **);
+#if (defined(DEFAULT_OS_NAME) || defined(DEFAULT_OS_MAJOR_REV) || \
+     defined(DEFAULT_OS_MINOR_REV) || defined(DEFAULT_OS_TEENY_REV))
+static void    parse_utsname(struct utsname *, const char *, char *, const char *);
+#endif
+#if (defined(DEFAULT_OS_MAJOR_REV) || defined(DEFAULT_OS_MINOR_REV) || defined(DEFAULT_OS_TEENY_REV))
+static const char      *trim_version(const char *);
+#endif
+#ifdef linux
+static void    get_distrib(FILE *);
+static void    get_libc_version(FILE *);
+static void    get_ld_version(FILE *);
+#endif
+#if defined(sun) && defined(__SVR4)
+static char     *get_full_path(const char *program);
+static int      get_sun_compiler_version(const char *fspec, const char *product,
+                                        int *cmajor, int *cminor);
+static void    get_sun_compiler_versions(FILE *);
+#endif
+static void    get_gcc_incdir(FILE *);
+static boolean define_os_defaults(FILE *);
+static void    cppit(const char *i, const char *, const char *, FILE *, const char *);
+static void    makeit(void);
+static void    CleanCppOutput(FILE *, const char *);
+static boolean isempty(char *);
+static void    writetmpfile(FILE *, const char *, int, const char *);
 
 int
-main(argc, argv)
-       int     argc;
-       char    **argv;
+main(int argc, char *argv[])
 {
        FILE    *tmpfd;
        char    makeMacro[ BUFSIZ ];
@@ -376,13 +405,14 @@ main(argc, argv)
                tmpMakefile = Makefile;
        else {
                tmpMakefile = Strdup(tmpMakefile);
-               (void) mktemp(tmpMakefile);
+               int ret = mkstemp(tmpMakefile);
+               (void) ret;
        }
        AddMakeArg("-f");
        AddMakeArg( tmpMakefile );
-       sprintf(makeMacro, "MAKE=%s", program);
+       snprintf(makeMacro, BUFSIZ, "MAKE=%s", program);
        AddMakeArg( makeMacro );
-       sprintf(makefileMacro, "MAKEFILE=%s", Imakefile);
+       snprintf(makefileMacro, BUFSIZ, "MAKEFILE=%s", Imakefile);
        AddMakeArg( makefileMacro );
 
        if ((tmpfd = fopen(tmpMakefile, "w+")) == NULL)
@@ -397,12 +427,11 @@ main(argc, argv)
        } else
                makeit();
        wrapup();
-       exit(0);
+       return 0;
 }
 
-void
-showit(fd)
-       FILE    *fd;
+static void
+showit(FILE *fd)
 {
        char    buf[ BUFSIZ ];
        int     red;
@@ -414,8 +443,8 @@ showit(fd)
            LogFatal("Cannot read %s.", tmpMakefile);
 }
 
-void
-wrapup()
+static void
+wrapup(void)
 {
        if (tmpMakefile != Makefile)
                unlink(tmpMakefile);
@@ -425,25 +454,25 @@ wrapup()
                unlink(ImakefileC);
 }
 
+static
 #ifdef SIGNALRETURNSINT
 int
 #else
 void
 #endif
-catch(sig)
-       int     sig;
+catch(int sig)
 {
        errno = 0;
-       LogFatalI("Signal %d.", sig);
+       LogFatal("Signal %d.", sig);
 }
 
 /*
  * Initialize some variables.
  */
-void
-init()
+static void
+init(void)
 {
-       register char   *p;
+       char    *p;
 
        make_argindex=0;
        while (make_argv[ make_argindex ] != NULL)
@@ -457,10 +486,9 @@ init()
         * the default.  Or if cpp is not the default.  Or if the make
         * found by the PATH variable is not the default.
         */
-       if (p = getenv("IMAKEINCLUDE")) {
+       if ((p = getenv("IMAKEINCLUDE"))) {
                if (*p != '-' || *(p+1) != 'I')
-                       LogFatal("Environment var IMAKEINCLUDE %s",
-                               "must begin with -I");
+                       LogFatal("Environment var IMAKEINCLUDE %s must begin with -I");
                AddCppArg(p);
                for (; *p; p++)
                        if (*p == ' ') {
@@ -468,41 +496,37 @@ init()
                                AddCppArg(p);
                        }
        }
-       if (p = getenv("IMAKECPP"))
+       if ((p = getenv("IMAKECPP")))
                cpp = p;
-       if (p = getenv("IMAKEMAKE"))
+       if ((p = getenv("IMAKEMAKE")))
                make_argv[0] = p;
 
        if (signal(SIGINT, SIG_IGN) != SIG_IGN)
                signal(SIGINT, catch);
 }
 
-void
-AddMakeArg(arg)
-       char    *arg;
+static void
+AddMakeArg(char *arg)
 {
        errno = 0;
        if (make_argindex >= ARGUMENTS-1)
-               LogFatal("Out of internal storage.", "");
+               LogFatal("Out of internal storage.");
        make_argv[ make_argindex++ ] = arg;
        make_argv[ make_argindex ] = NULL;
 }
 
-void
-AddCppArg(arg)
-       char    *arg;
+static void
+AddCppArg(char *arg)
 {
        errno = 0;
        if (cpp_argindex >= ARGUMENTS-1)
-               LogFatal("Out of internal storage.", "");
+               LogFatal("Out of internal storage.");
        cpp_argv[ cpp_argindex++ ] = arg;
        cpp_argv[ cpp_argindex ] = NULL;
 }
 
-void
-SetOpts(argc, argv)
-       int     argc;
-       char    **argv;
+static void
+SetOpts(int argc, char **argv)
 {
        errno = 0;
        /*
@@ -523,7 +547,7 @@ SetOpts(argc, argv)
                    else {
                        argc--, argv++;
                        if (! argc)
-                           LogFatal("No description arg after -f flag", "");
+                           LogFatal("No description arg after -f flag");
                        Imakefile = argv[0];
                    }
                } else if (argv[0][1] == 's') {
@@ -533,7 +557,7 @@ SetOpts(argc, argv)
                    else {
                        argc--, argv++;
                        if (!argc)
-                           LogFatal("No description arg after -s flag", "");
+                           LogFatal("No description arg after -s flag");
                        Makefile = ((argv[0][0] == '-') && !argv[0][1]) ?
                            NULL : argv[0];
                    }
@@ -547,7 +571,7 @@ SetOpts(argc, argv)
                    else {
                        argc--, argv++;
                        if (! argc)
-                           LogFatal("No description arg after -T flag", "");
+                           LogFatal("No description arg after -T flag");
                        Template = argv[0];
                    }
                } else if (argv[0][1] == 'C') {
@@ -556,7 +580,7 @@ SetOpts(argc, argv)
                    else {
                        argc--, argv++;
                        if (! argc)
-                           LogFatal("No imakeCfile arg after -C flag", "");
+                           LogFatal("No imakeCfile arg after -C flag");
                        ImakefileC = argv[0];
                    }
                } else if (argv[0][1] == 'v') {
@@ -580,9 +604,8 @@ SetOpts(argc, argv)
        AddCppArg(ImakefileC);
 }
 
-char *
-FindImakefile(Imakefile)
-       char    *Imakefile;
+static char *
+FindImakefile(char *Imakefile)
 {
        if (Imakefile) {
                if (access(Imakefile, R_OK) < 0)
@@ -590,7 +613,7 @@ FindImakefile(Imakefile)
        } else {
                if (access("Imakefile", R_OK) < 0)
                        if (access("imakefile", R_OK) < 0)
-                               LogFatal("No description file.", "");
+                               LogFatal("No description file.");
                        else
                                Imakefile = "imakefile";
                else
@@ -599,49 +622,54 @@ FindImakefile(Imakefile)
        return(Imakefile);
 }
 
-void
-LogFatalI(s, i)
-       char *s;
-       int i;
+static void
+LogFatal(const char *s, ...)
 {
-       /*NOSTRICT*/
-       LogFatal(s, (char *)i);
+    static boolean entered = FALSE;
+    va_list args;
+
+    if (entered)
+        return;
+
+    entered = TRUE;
+
+    va_start(args, s);
+    Log(s, args);
+    va_end(args);
+
+    fprintf(stderr, "Stop.\n");
+
+    wrapup();
+
+    exit(1);
 }
 
-void
-LogFatal(x0,x1)
-       char *x0, *x1;
+static void
+LogMsg(const char *s, ...)
 {
-       static boolean  entered = FALSE;
-
-       if (entered)
-               return;
-       entered = TRUE;
+    va_list args;
 
-       LogMsg(x0, x1);
-       fprintf(stderr, "  Stop.\n");
-       wrapup();
-       exit(1);
+    va_start(args, s);
+    Log(s, args);
+    va_end(args);
 }
 
-void
-LogMsg(x0,x1)
-       char *x0, *x1;
+static void
+Log(const char *s, va_list args)
 {
-       int error_number = errno;
+    int error_number = errno;
 
-       if (error_number) {
-               fprintf(stderr, "%s: ", program);
-               fprintf(stderr, "%s\n", strerror(error_number));
-       }
-       fprintf(stderr, "%s: ", program);
-       fprintf(stderr, x0, x1);
-       fprintf(stderr, "\n");
+    if (error_number) {
+        fprintf(stderr, "%s: ", program);
+        fprintf(stderr, "%s\n", strerror(error_number));
+    }
+    fprintf(stderr, "%s: ", program);
+    vfprintf(stderr, s, args);
+    fprintf(stderr, "\n");
 }
 
-void
-showargs(argv)
-       char    **argv;
+static void
+showargs(char **argv)
 {
        for (; *argv; argv++)
                fprintf(stderr, "%s ", *argv);
@@ -650,9 +678,8 @@ showargs(argv)
 
 #define ImakefileCHeader "/* imake - temporary file */"
 
-void
-CheckImakefileC(masterc)
-       char *masterc;
+static void
+CheckImakefileC(const char *masterc)
 {
        char mkcbuf[1024];
        FILE *inFile;
@@ -678,26 +705,21 @@ CheckImakefileC(masterc)
 #define ImakeTmplSym   "IMAKE_TEMPLATE"
 #define OverrideWarning        "Warning: local file \"%s\" overrides global macros."
 
-boolean
-optional_include(inFile, defsym, fname)
-        FILE   *inFile;
-        char    *defsym;
-        char    *fname;
+static boolean
+optional_include(FILE *inFile, const char *defsym, const char *fname)
 {
        errno = 0;
        if (access(fname, R_OK) == 0) {
-               LogMsg(OverrideWarning, fname);
+               if(errno)
+                       LogMsg(OverrideWarning, fname);
                return (fprintf(inFile, LocalDefineFmt, defsym, fname) < 0 ||
                        fprintf(inFile, IncludeFmt, defsym) < 0);
        }
        return FALSE;
 }
 
-void
-doit(outfd, cmd, argv)
-       FILE    *outfd;
-       char    *cmd;
-       char    **argv;
+static void
+doit(FILE *outfd, const char *cmd, char **argv)
 {
        int     pid;
        waitType        status;
@@ -712,18 +734,18 @@ doit(outfd, cmd, argv)
        if (status < 0)
                LogFatal("Cannot spawn %s.", cmd);
        if (status > 0)
-               LogFatalI("Exit code %d.", status);
+               LogFatal("Exit code %d.", status);
 #else
        pid = fork();
        if (pid < 0)
-               LogFatal("Cannot fork.", "");
+               LogFatal("Cannot fork.");
        if (pid) {      /* parent... simply wait */
                while (wait(&status) > 0) {
                        errno = 0;
                        if (WIFSIGNALED(status))
-                               LogFatalI("Signal %d.", waitSig(status));
+                               LogFatal("Signal %d.", waitSig(status));
                        if (WIFEXITED(status) && waitCode(status))
-                               LogFatalI("Exit code %d.", waitCode(status));
+                               LogFatal("Exit code %d.", waitCode(status));
                }
        }
        else {  /* child... dup and exec cmd */
@@ -738,12 +760,11 @@ doit(outfd, cmd, argv)
 }
 
 #ifndef WIN32
+
+#if (defined(DEFAULT_OS_NAME) || defined(DEFAULT_OS_MAJOR_REV) || \
+     defined(DEFAULT_OS_MINOR_REV) || defined(DEFAULT_OS_TEENY_REV))
 static void
-parse_utsname(name, fmt, result, msg)
-     struct utsname *name;
-     char *fmt;
-     char *result;
-     char *msg;
+parse_utsname(struct utsname *name, const char *fmt, char *result, const char *msg)
 {
   char buf[SYS_NMLN * 5 + 1];
   char *ptr = buf;
@@ -761,35 +782,35 @@ parse_utsname(name, fmt, result, msg)
        case 's':
          if (arg > 0)
            *ptr++ = ' ';
-         strcpy(ptr, name->sysname);
+         strncpy(ptr, name->sysname, SYS_NMLN);
          ptr += strlen(ptr);
          break;
 
        case 'n':
          if (arg > 0)
            *ptr++ = ' ';
-         strcpy(ptr, name->nodename);
+         strncpy(ptr, name->nodename, SYS_NMLN);
          ptr += strlen(ptr);
          break;
 
        case 'r':
          if (arg > 0)
            *ptr++ = ' ';
-         strcpy(ptr, name->release);
+         strncpy(ptr, name->release, SYS_NMLN);
          ptr += strlen(ptr);
          break;
 
        case 'v':
          if (arg > 0)
            *ptr++ = ' ';
-         strcpy(ptr, name->version);
+         strncpy(ptr, name->version, SYS_NMLN);
          ptr += strlen(ptr);
          break;
 
        case 'm':
          if (arg > 0)
            *ptr++ = ' ';
-         strcpy(ptr, name->machine);
+         strncpy(ptr, name->machine, SYS_NMLN);
          ptr += strlen(ptr);
          break;
 
@@ -800,21 +821,23 @@ parse_utsname(name, fmt, result, msg)
 
   /* Just in case... */
   if (strlen(buf) >= sizeof(buf))
-    LogFatal("Buffer overflow parsing uname.", "");
+    LogFatal("Buffer overflow parsing uname.");
 
   /* Parse the buffer.  The sscanf() return value is rarely correct. */
   *result = '\0';
-  (void) sscanf(buf, fmt + arg + 1, result);
+  int ret = sscanf(buf, fmt + arg + 1, result);
+  (void) ret;
 }
+#endif
 
 /* Trim leading 0's and periods from version names.  The 0's cause
    the number to be interpreted as octal numbers.  Some version strings
    have the potential for different numbers of .'s in them.
  */
        
-static char *
-trim_version(p)
-       char *p;
+#if (defined(DEFAULT_OS_MAJOR_REV) || defined(DEFAULT_OS_MINOR_REV) || defined(DEFAULT_OS_TEENY_REV))
+static const char *
+trim_version(const char *p)
 {
 
        if (p != 0 && *p != '\0')
@@ -826,9 +849,11 @@ trim_version(p)
 }
 #endif
 
+#endif
+
 #ifdef linux
-static void get_distrib(inFile)
-  FILE* inFile;
+static void
+get_distrib(FILE *inFile)
 {
   struct stat sb;
 
@@ -863,7 +888,7 @@ static void get_distrib(inFile)
   /* would like to know what version of the distribution it is */
 }
 
-static const char *libc_c=
+static const char libc_c[]=
 "#include <stdio.h>\n"
 "#include <ctype.h>\n"
 "\n"
@@ -928,14 +953,40 @@ static const char *libc_c=
 "}\n"
 ;
 
-static void get_libc_version(inFile)
-  FILE* inFile;
+#define libc32path "/usr/lib/libc.so"
+#define libc64path "/usr/lib64/libc.so"
+
+static void
+get_libc_version(FILE *inFile)
 {
-  static char* libcso = "/usr/lib/libc.so";
+  char* libcso = NULL;
   struct stat sb;
   char buf[PATH_MAX];
   char* ptr;
   int libcmajor, libcminor, libcteeny;
+  struct utsname u;
+
+  /*
+   * If we are on a 64-bit Linux system and we see /usr/lib64/libc.so,
+   * we should use it.  Otherwise go with /usr/lib/libc.so.  It is entirely
+   * possible that someone will be running a 32-bit userland on a 64-bit
+   * system.
+   */
+  if (uname(&u) == -1) {
+    fprintf(stderr, "%s (%d): %s\n", __func__, __LINE__, strerror(errno));
+    abort();
+  }
+
+  if (!strcmp(u.sysname, "Linux") &&
+      (!strcmp(u.machine, "x86_64"))) {
+    if (!lstat (libc64path, &sb) && S_ISREG(sb.st_mode)) {
+      libcso = libc64path;
+    }
+  }
+
+  if (libcso == NULL) {
+    libcso = libc32path;
+  }
 
   if (lstat (libcso, &sb) == 0) {
     if (S_ISLNK (sb.st_mode)) {
@@ -945,23 +996,46 @@ static void get_libc_version(inFile)
         */
       if (readlink (libcso, buf, PATH_MAX) >= 0) {
        for (ptr = buf; *ptr && !isdigit (*ptr); ptr++);
-         (void) sscanf (ptr, "%d.%d.%d", &libcmajor, &libcminor, &libcteeny);
-         fprintf(inFile, "#define DefaultLinuxCLibMajorVersion %d\n", libcmajor);    
-         fprintf(inFile, "#define DefaultLinuxCLibMinorVersion %d\n", libcminor);    
-         fprintf(inFile, "#define DefaultLinuxCLibTeenyVersion %d\n", libcteeny);    
+       int ret = sscanf (ptr, "%d.%d.%d", &libcmajor, &libcminor, &libcteeny);
+       (void) ret;
+       fprintf(inFile, "#define DefaultLinuxCLibMajorVersion %d\n", libcmajor);
+       fprintf(inFile, "#define DefaultLinuxCLibMinorVersion %d\n", libcminor);
+       fprintf(inFile, "#define DefaultLinuxCLibTeenyVersion %d\n", libcteeny);
       }
     } else {
       /* 
        * /usr/lib/libc.so is NOT a symlink -- this is libc 6.x / glibc 2.x
        * now we have to figure this out the hard way.
        */
-      char *aout = tmpnam (NULL);
+      char aout[PATH_MAX];
+      int fd = -1;
       FILE *fp;
       const char *format = "%s -o %s -x c -";
       char *cc;
       int len;
       char *command;
 
+      memset(aout, '\0', PATH_MAX);
+
+      if (!lstat(getenv("TMPDIR"), &sb) && S_ISDIR(sb.st_mode))
+       strncpy(aout, getenv("TMPDIR"), PATH_MAX - 1);
+#ifdef P_tmpdir /* defined by XPG and XOPEN, but don't assume we have it */
+      else if (!lstat(P_tmpdir, &sb) && S_ISDIR(sb.st_mode))
+       strncpy(aout, P_tmpdir, PATH_MAX - 1);
+#endif
+      else if (!lstat("/tmp", &sb) && S_ISDIR(sb.st_mode))
+       strncpy(aout, "/tmp", PATH_MAX - 1);
+      else
+       abort();
+
+      strncat(aout, "/imaketmp.XXXXXX", PATH_MAX - 1);
+
+      if ((fd = mkstemp(aout)) == -1)
+       abort ();
+
+      if (close(fd) == -1)
+       abort ();
+
       cc = getenv ("CC");
       if (cc == NULL)
        cc = "gcc";
@@ -981,7 +1055,7 @@ static void get_libc_version(inFile)
        abort ();
 
       while (fgets (command, len, fp))
-       fprintf (inFile, command);
+       fprintf (inFile, "%s", command);
   
       len = pclose (fp);
       remove (aout);
@@ -991,8 +1065,8 @@ static void get_libc_version(inFile)
   }
 }
 
-static void get_ld_version(inFile)
-  FILE* inFile;
+static void
+get_ld_version(FILE *inFile)
 {
   FILE* ldprog = popen ("ld -v", "r");
   char c;
@@ -1003,7 +1077,8 @@ static void get_ld_version(inFile)
       c = fgetc (ldprog);
     } while (c != EOF && !isdigit (c));
     ungetc (c, ldprog);
-    (void) fscanf (ldprog, "%d.%d", &ldmajor, &ldminor);
+    int ret = fscanf (ldprog, "%d.%d", &ldmajor, &ldminor);
+    (void) ret;
     fprintf(inFile, "#define DefaultLinuxBinUtilsMajorVersion %d\n", 
            ldmajor * 10 + ldminor);    
     pclose (ldprog);
@@ -1016,65 +1091,116 @@ static void get_ld_version(inFile)
 #endif
 
 #if defined(sun) && defined(__SVR4)
-static void get_sun_compiler_versions (inFile)
-  FILE* inFile;
+
+static char *
+get_full_path(const char *program)
+{
+  char *buf;
+  char *cmd;
+  FILE *proc;
+
+  buf = calloc(1, PATH_MAX);
+  asprintf(&cmd, "command -v %s", program);
+
+  if ((proc = popen (cmd, "r")) != NULL)
+    fgets (buf, PATH_MAX, proc);
+
+  pclose (proc);
+
+  return strtok (buf, "\n");    /* strip newline */
+}
+
+static int
+get_sun_compiler_version(const char *fspec, const char *product,
+                         int *cmajor, int *cminor)
 {
   char buf[PATH_MAX];
   char cmd[PATH_MAX];
-  static char* sunpro_cc = "/opt/SUNWspro/bin/cc";
-  static char* sunpro_CC = "/opt/SUNWspro/bin/CC";
-  int cmajor, cminor;
-  char* vptr;
+  char *vptr;
   struct stat sb;
-  FILE* ccproc;
-
-  if (lstat (sunpro_cc, &sb) == 0) {
-    strcpy (cmd, sunpro_cc);
-    strcat (cmd, " -V 2>&1");
-    if ((ccproc = popen (cmd, "r")) != NULL) {
-      if (fgets (buf, PATH_MAX, ccproc) != NULL) {
-       vptr = strrchr (buf, 'C');
-       for (; !isdigit(*vptr); vptr++);
-       (void) sscanf (vptr, "%d.%d", &cmajor, &cminor);
-       fprintf (inFile, 
-                "#define DefaultSunProCCompilerMajorVersion %d\n",
-                cmajor);
-       fprintf (inFile, 
-                "#define DefaultSunProCCompilerMinorVersion %d\n",
-                cminor);
+  FILE *ccproc;
+  int ret;
+  char vendor[4];
+
+  if (lstat (fspec, &sb) != 0)
+    return ENOENT;
+
+  strncpy (cmd, fspec, PATH_MAX);
+  strlcpy (vendor, product, 4);
+
+  if (strcmp (vendor, "Sun") == 0)
+    strncat (cmd, " -V 2>&1", 8);
+  else if (strcmp (vendor, "Gnu") == 0)
+    strncat (cmd, " --version 2>&1", 15);
+  else
+    return EINVAL;
+
+  if ((ccproc = popen (cmd, "r")) != NULL) {
+    if (fgets (buf, PATH_MAX, ccproc) != NULL) {
+      for (vptr = buf; !isdigit(*vptr) && *vptr != NULL; vptr++);
+      if (*vptr == NULL) {
+        pclose (ccproc);
+        return EINVAL;
+      } else {
+        ret = sscanf (vptr, "%d.%d", cmajor, cminor);
       }
-      while (fgets (buf, PATH_MAX, ccproc) != NULL) {};
-      pclose (ccproc);
     }
+    pclose (ccproc);
+  } else {
+    return EIO;
   }
-  if (lstat (sunpro_CC, &sb) == 0) {
-    strcpy (cmd, sunpro_CC);
-    strcat (cmd, " -V 2>&1");
-    if ((ccproc = popen (cmd, "r")) != NULL) {
-      if (fgets (buf, PATH_MAX, ccproc) != NULL) {
-       vptr = strrchr (buf, 'C');
-       for (; !isdigit(*vptr); vptr++);
-       (void) sscanf (vptr, "%d.%d", &cmajor, &cminor);
-       fprintf (inFile, 
-                "#define DefaultSunProCplusplusCompilerMajorVersion %d\n",
-                cmajor);
-       fprintf (inFile, 
-                "#define DefaultSunProCplusplusCompilerMinorVersion %d\n",
-                cminor);
-      }
-      while (fgets (buf, PATH_MAX, ccproc) != NULL) {};
-      pclose (ccproc);
+
+  return 0;
+}
+
+static void
+get_sun_compiler_versions(FILE *inFile)
+{
+  static const char *compilers[][2] =
+    {{"SunProC",         "/opt/solarisstudio/bin/cc"},
+     {"SunProCplusplus", "/opt/solarisstudio/bin/CC"},
+     {"GnuC",            "gcc"},
+     {"GnuCplusplus",    "g++"}};
+  int cmajor, cminor;
+  int i;
+  int ret;
+
+  for (i = 0; i < sizeof compilers / sizeof compilers[0]; i++) {
+    const char *product = compilers[i][0];
+    char *fspec = get_full_path (compilers[i][1]);
+
+    ret = get_sun_compiler_version (fspec, product, &cmajor, &cminor);
+    free (fspec);
+
+    if (ret == 0) {
+      fprintf (inFile,
+               "#define Default%sCompilerMajorVersion %d\n",
+               product,
+               cmajor);
+      fprintf (inFile,
+               "#define Default%sCompilerMinorVersion %d\n",
+               product,
+               cminor);
+    } else if (ret == EINVAL) {
+      printf ("Failed to detect version of compiler: %s\n", product);
+      exit (EINVAL);
     }
   }
+
+  (void) ret;
 }
+
 #endif
 
-static void get_gcc_incdir(inFile)
-  FILE* inFile;
+static void
+get_gcc_incdir(FILE *inFile)
 {
   static char* gcc_path[] = {
 #ifdef linux
     "/usr/bin/cc",     /* for Linux PostIncDir */
+#endif
+#ifdef sun
+    "gcc",
 #endif
     "/usr/local/bin/gcc",
     "/opt/gnu/bin/gcc"
@@ -1086,15 +1212,21 @@ static void get_gcc_incdir(inFile)
   char cmd[PATH_MAX];
   char* ptr;
 
-  buf[0] = '\0';
+  memset(buf, 0, PATH_MAX);
   for (i = 0; i < sizeof gcc_path / sizeof gcc_path[0]; i++) {
+#ifdef sun
+    gcc_path[i] = get_full_path (gcc_path[i]);
+#endif
     if (lstat (gcc_path[i], &sb) == 0) {
-      strcpy (cmd, gcc_path[i]);
-      strcat (cmd, " --print-libgcc-file-name");
+#ifdef sun
+      free (gcc_path[i]);
+#endif
+      strncpy (cmd, gcc_path[i], PATH_MAX - 1 );
+      strncat (cmd, " --print-libgcc-file-name", PATH_MAX - 1);
       if ((gccproc = popen (cmd, "r")) != NULL) {
-       if (fgets (buf, PATH_MAX, gccproc) != NULL) {
+       if (fgets (buf, PATH_MAX - 1, gccproc) != NULL) {
          ptr = strstr (buf, "libgcc.a");
-         if (ptr) strcpy (ptr, "include");
+         if (ptr) strncpy (ptr, "include", 8);
        }
        (void) pclose (gccproc);
        break;
@@ -1105,19 +1237,18 @@ static void get_gcc_incdir(inFile)
     fprintf (inFile, "#define DefaultGccIncludeDir %s\n", buf);
 }
 
-boolean
-define_os_defaults(inFile)
-       FILE    *inFile;
+static boolean
+define_os_defaults(FILE *inFile)
 {
 #ifndef WIN32
 #if (defined(DEFAULT_OS_NAME) || defined(DEFAULT_OS_MAJOR_REV) || \
-     defined(DEFAULT_OS_MINOR_REV) || defined(DEFAUL_OS_TEENY_REV))
+     defined(DEFAULT_OS_MINOR_REV) || defined(DEFAULT_OS_TEENY_REV))
        struct utsname name;
        char buf[SYS_NMLN * 5 + 1];
 
        /* Obtain the system information. */
        if (uname(&name) < 0)
-               LogFatal("Cannot invoke uname", "");
+               LogFatal("Cannot invoke uname");
 
 # ifdef DEFAULT_OS_NAME
        parse_utsname(&name, DEFAULT_OS_NAME, buf, 
@@ -1175,13 +1306,8 @@ define_os_defaults(inFile)
    return FALSE;
 }
 
-void
-cppit(imakefile, template, masterc, outfd, outfname)
-       char    *imakefile;
-       char    *template;
-       char    *masterc;
-       FILE    *outfd;
-       char    *outfname;
+static void
+cppit(const char *imakefile, const char *template, const char *masterc, FILE *outfd, const char *outfname)
 {
        FILE    *inFile;
 
@@ -1198,7 +1324,7 @@ cppit(imakefile, template, masterc, outfd, outfname)
            fprintf(inFile, IncludeFmt, ImakeTmplSym) < 0 ||
            optional_include(inFile, "IMAKE_ADMIN_MACROS", "adminmacros") ||
            optional_include(inFile, "IMAKE_LOCAL_MACROS", "localmacros") ||
-           fflush(inFile) || 
+           fflush(inFile) ||
            fclose(inFile))
                LogFatal("Cannot write to %s.", masterc);
        /*
@@ -1208,15 +1334,14 @@ cppit(imakefile, template, masterc, outfd, outfname)
        CleanCppOutput(outfd, outfname);
 }
 
-void
-makeit()
+static void
+makeit(void)
 {
        doit(NULL, make_argv[0], make_argv);
 }
 
-char *
-CleanCppInput(imakefile)
-       char    *imakefile;
+static char *
+CleanCppInput(char *imakefile)
 {
        FILE    *outFile = NULL;
        FILE    *inFile;
@@ -1271,7 +1396,8 @@ CleanCppInput(imakefile)
                    strcmp(ptoken, "undef")) {
                    if (outFile == NULL) {
                        tmpImakefile = Strdup(tmpImakefile);
-                       (void) mktemp(tmpImakefile);
+                       int ret = mkstemp(tmpImakefile);
+                       (void) ret;
                        outFile = fopen(tmpImakefile, "w");
                        if (outFile == NULL)
                            LogFatal("Cannot open %s for write.",
@@ -1298,15 +1424,13 @@ CleanCppInput(imakefile)
        return(imakefile);
 }
 
-void
-CleanCppOutput(tmpfd, tmpfname)
-       FILE    *tmpfd;
-       char    *tmpfname;
+static void
+CleanCppOutput(FILE *tmpfd, const char *tmpfname)
 {
        char    *input;
        int     blankline = 0;
 
-       while(input = ReadLine(tmpfd, tmpfname)) {
+       while ((input = ReadLine(tmpfd, tmpfname))) {
                if (isempty(input)) {
                        if (blankline++)
                                continue;
@@ -1334,11 +1458,10 @@ CleanCppOutput(tmpfd, tmpfname)
  * space from the end of the line.  Cpp magic cookies are also thrown away.
  * "XCOMM" token is transformed to "#".
  */
-boolean
-isempty(line)
-       register char   *line;
+static boolean
+isempty(char *line)
 {
-       register char   *pend;
+       char    *pend;
 
        /*
         * Check for lines of the form
@@ -1353,10 +1476,10 @@ isempty(line)
                if (*pend == 'l' && pend[1] == 'i' && pend[2] == 'n' &&
                    pend[3] == 'e' && pend[4] == ' ')
                        pend += 5;
-               if (isdigit(*pend)) {
+               if (isdigit((int)*pend)) {
                        do {
                            pend++;
-                       } while (isdigit(*pend));
+                       } while (isdigit((int)*pend));
                        if (*pend == '\n' || *pend == '\0')
                                return(TRUE);
                        if (*pend++ == ' ' && *pend == '"')
@@ -1372,7 +1495,7 @@ isempty(line)
                    (pend[5] == ' ' || pend[5] == '\t' || pend[5] == '\0'))
                {
                    *pend = '#';
-                   strcpy(pend+1, pend+5);
+                   strncpy(pend+1, pend+5, 1);
                }
 #ifdef MAGIC_MAKE_VARS
                if (*pend == 'X' && pend[1] == 'V' && pend[2] == 'A' &&
@@ -1385,7 +1508,7 @@ isempty(line)
                        pend[7] >= '0' && pend[7] <= '9')
                    {
                        i = pend[7] - '0';
-                       sprintf(varbuf, "%0.4d", xvariable);
+                       snprintf(varbuf, 5, "%0.4d", xvariable);
                        strncpy(pend+4, varbuf, 4);
                        xvariables[i] = xvariable;
                        xvariable = (xvariable + 1) % 10000;
@@ -1395,7 +1518,7 @@ isempty(line)
                             pend[7] <= '9')
                    {
                        i = pend[7] - '0';
-                       sprintf(varbuf, "%0.4d", xvariables[i]);
+                       snprintf(varbuf, 5, "%0.4d", xvariables[i]);
                        strncpy(pend+4, varbuf, 4);
                    }
                }
@@ -1408,14 +1531,12 @@ isempty(line)
 }
 
 /*ARGSUSED*/
-char *
-ReadLine(tmpfd, tmpfname)
-       FILE    *tmpfd;
-       char    *tmpfname;
+static char *
+ReadLine(FILE *tmpfd, const char *tmpfname)
 {
        static boolean  initialized = FALSE;
        static char     *buf, *pline, *end;
-       register char   *p1, *p2;
+       char    *p1, *p2;
 
        if (! initialized) {
 #ifdef WIN32
@@ -1444,9 +1565,10 @@ ReadLine(tmpfd, tmpfname)
                        tmpfd = freopen(tmpfname, "w+", fp);
 #endif
                if (! tmpfd)
-                       LogFatal("cannot reopen %s\n", tmpfname);
+                       LogFatal("cannot reopen %s.", tmpfname);
 #else  /* !SYSV */
-               ftruncate(fileno(tmpfd), (off_t) 0);
+               int ret = ftruncate(fileno(tmpfd), (off_t) 0);
+               (void) ret;
 #endif /* !SYSV */
                initialized = TRUE;
            fprintf (tmpfd, "# Makefile generated by imake - do not edit!\n");
@@ -1457,7 +1579,8 @@ ReadLine(tmpfd, tmpfname)
        for (p1 = pline; p1 < end; p1++) {
                if (*p1 == '@' && *(p1+1) == '@'
                    /* ignore ClearCase version-extended pathnames */
-                   && !(p1 != pline && !isspace(*(p1-1)) && *(p1+2) == '/'))
+                   && !(p1 != pline && !isspace((int)*(p1-1))
+                   && *(p1+2) == '/'))
                { /* soft EOL */
                        *p1++ = '\0';
                        p1++; /* skip over second @ */
@@ -1481,32 +1604,26 @@ ReadLine(tmpfd, tmpfname)
        return(p2);
 }
 
-void
-writetmpfile(fd, buf, cnt, fname)
-       FILE    *fd;
-       int     cnt;
-       char    *buf;
-       char    *fname;
+static void
+writetmpfile(FILE *fd, const char *buf, int cnt, const char *fname)
 {
        if (fwrite(buf, sizeof(char), cnt, fd) == -1)
                LogFatal("Cannot write to %s.", fname);
 }
 
-char *
-Emalloc(size)
-       int     size;
+static char *
+Emalloc(int size)
 {
        char    *p;
 
        if ((p = malloc(size)) == NULL)
-               LogFatalI("Cannot allocate %d bytes", size);
+               LogFatal("Cannot allocate %d bytes.", size);
        return(p);
 }
 
 #ifdef FIXUP_CPP_WHITESPACE
-void
-KludgeOutputLine(pline)
-       char    **pline;
+static void
+KludgeOutputLine(char **pline)
 {
        char    *p = *pline;
        char    quotechar = '\0';
@@ -1588,19 +1705,18 @@ breakfor:
        }
 }
 
-void
-KludgeResetRule()
+static void
+KludgeResetRule(void)
 {
        InRule = FALSE;
 }
 #endif /* FIXUP_CPP_WHITESPACE */
 
-char *
-Strdup(cp)
-       register char *cp;
+static char *
+Strdup(const char *cp)
 {
-       register char *new = Emalloc(strlen(cp) + 1);
+       char *new = Emalloc(strlen(cp) + 1);
 
-       strcpy(new, cp);
+       memcpy(new, cp, strlen(cp) + 1);
        return new;
 }