Use Painless Security signing key
[freeradius.git] / scripts / jlibtool.c
index e948b93..c04390d 100644 (file)
@@ -79,7 +79,7 @@
 #  define LD_LIBRARY_PATH_LOCAL                "DYLD_FALLBACK_LIBRARY_PATH"
 #endif
 
-#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
+#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || (defined(__sun) && defined(__GNUC__))
 #  define SHELL_CMD                    "/bin/sh"
 #  define DYNAMIC_LIB_EXT              "so"
 #  define MODULE_LIB_EXT               "so"
 #  define SHARED_OPTS                  "-shared"
 #  define MODULE_OPTS                  "-shared"
 #  define LINKER_FLAG_PREFIX           "-Wl,"
+#if !defined(__sun)
 #  define DYNAMIC_LINK_OPTS            LINKER_FLAG_PREFIX "-export-dynamic"
+#else
+#  define DYNAMIC_LINK_OPTS            ""
+#endif
 #  define ADD_MINUS_L
 #  define LD_RUN_PATH                  "LD_RUN_PATH"
 #  define LD_LIBRARY_PATH              "LD_LIBRARY_PATH"
 #  define LD_LIBRARY_PATH_LOCAL                LD_LIBRARY_PATH
 #endif
 
-#if defined(sun)
+#if defined(__sun) && !defined(__GNUC__)
 #  define SHELL_CMD                    "/bin/sh"
 #  define DYNAMIC_LIB_EXT              "so"
 #  define MODULE_LIB_EXT               "so"
 #  define LIBRARIAN                    "ar"
 #  define LIBRARIAN_OPTS               "cr"
 #  define RANLIB                       "ranlib"
-#  define PIC_FLAG                     "-KPIC"
+#  define PIC_FLAG                     "-fPIC"
 #  define RPATH                                "-R"
 #  define SHARED_OPTS                  "-G"
 #  define MODULE_OPTS                  "-G"
@@ -396,9 +400,9 @@ static int snprintf(char *str, size_t n, char const *fmt, ...)
        va_list ap;
        int res;
 
-       va_start( ap, fmt );
-       res = vsnprintf( str, n, fmt, ap );
-       va_end( ap );
+       va_start(ap, fmt);
+       res = vsnprintf(str, n, fmt, ap);
+       va_end(ap);
        return res;
 }
 #endif
@@ -430,7 +434,7 @@ static void init_count_chars(count_chars *cc)
        cc->num = 0;
 }
 
-static count_chars *alloc_countchars()
+static count_chars *alloc_countchars(void)
 {
        count_chars *out;
        out = lt_malloc(sizeof(count_chars));
@@ -457,9 +461,12 @@ static void push_count_chars(count_chars *cc, char const *newval)
        cc->vals[cc->num++] = newval;
 }
 
-static void pop_count_chars(count_chars *cc)
+static char const *pop_count_chars(count_chars *cc)
 {
-       cc->num--;
+       if (!cc->num) {
+               return NULL;
+       }
+       return cc->vals[--cc->num];
 }
 
 static void insert_count_chars(count_chars *cc, char const *newval, int position)
@@ -484,7 +491,7 @@ static void append_count_chars(count_chars *cc, count_chars *cctoadd)
        }
 }
 
-static char const *flatten_count_chars(count_chars *cc, int space)
+static char const *flatten_count_chars(count_chars *cc, char delim)
 {
        int i, size;
        char *newval;
@@ -493,20 +500,22 @@ static char const *flatten_count_chars(count_chars *cc, int space)
        for (i = 0; i < cc->num; i++) {
                if (cc->vals[i]) {
                        size += strlen(cc->vals[i]) + 1;
-                       if (space) {
-                         size++;
+                       if (delim) {
+                               size++;
                        }
                }
        }
 
        newval = (char*)lt_malloc(size + 1);
-       newval[0] = 0;
+       newval[0] = '\0';
 
        for (i = 0; i < cc->num; i++) {
                if (cc->vals[i]) {
                        strcat(newval, cc->vals[i]);
-                       if (space) {
-                               strcat(newval, " ");
+                       if (delim) {
+                               size_t len = strlen(newval);
+                               newval[len] = delim;
+                               newval[len + 1] = '\0';
                        }
                }
        }
@@ -575,12 +584,26 @@ static int external_spawn(command_t *cmd, char const *file, char const **argv)
                        return execvp(argv[0], (char**)argv);
                }
                else {
-                       int statuscode;
-                       waitpid(pid, &statuscode, 0);
-                       if (WIFEXITED(statuscode)) {
-                               return WEXITSTATUS(statuscode);
+                       int status;
+                       waitpid(pid, &status, 0);
+
+                       /*
+                        *      Exited via exit(status)
+                        */
+                       if (WIFEXITED(status)) {
+                               return WEXITSTATUS(status);
                        }
-                       return 0;
+
+#ifdef WTERMSIG
+                       if (WIFSIGNALED(status)) {
+                               return WTERMSIG(status);
+                       }
+#endif
+
+                       /*
+                        *      Some other failure.
+                        */
+                       return 1;
                }
        }
 #endif
@@ -605,7 +628,7 @@ static int run_command(command_t *cmd, count_chars *cc)
 
        append_count_chars(&tmpcc, cc);
 
-       raw = flatten_count_chars(&tmpcc, 1);
+       raw = flatten_count_chars(&tmpcc, ' ');
        command = shell_esc(raw);
 
        memcpy(&tmp, &raw, sizeof(tmp));
@@ -696,13 +719,15 @@ static int parse_long_opt(char const *arg, command_t *cmd)
        if (equal_pos) {
                strncpy(var, arg, equal_pos - arg);
                var[equal_pos - arg] = 0;
-       if (strlen(equal_pos + 1) >= sizeof(var)) return 0;
+               if (strlen(equal_pos + 1) >= sizeof(var)) {
+                       return 0;
+               }
                strcpy(value, equal_pos + 1);
        } else {
                strncpy(var, arg, sizeof(var) - 1);
                var[sizeof(var) - 1] = '\0';
 
-       value[0] = '\0';
+               value[0] = '\0';
        }
 
        if (strcmp(var, "silent") == 0) {
@@ -1102,7 +1127,7 @@ static char const *check_object_exists(command_t *cmd, char const *arg, int argl
  * 1 - .libs suffix
  */
 static char *check_library_exists(command_t *cmd, char const *arg, int pathlen,
-                                 int libdircheck, enum lib_type*libtype)
+                                 int libdircheck, enum lib_type *libtype)
 {
        char *newarg, *ext;
        int pass, rv, newpathlen;
@@ -1203,7 +1228,7 @@ static char * load_install_path(char const *arg)
        return path;
 }
 
-static char * load_noinstall_path(char const *arg, int pathlen)
+static char *load_noinstall_path(char const *arg, int pathlen)
 {
        char *newarg, *expanded_path;
        int newpathlen;
@@ -1660,7 +1685,7 @@ static int parse_output_file_name(char const *arg, command_t *cmd)
        char const *name;
        char const *ext;
        char *newarg = NULL;
-       int pathlen;
+       size_t pathlen;
 
        cmd->fake_output_name = arg;
 
@@ -2207,21 +2232,25 @@ static int run_mode(command_t *cmd)
 
                strcpy(libpath, cmd->arglist->vals[0]);
                add_dotlibs(libpath);
-       l = strrchr(libpath, '/');
-       if (!l) l = strrchr(libpath, '\\');
-       if (l) {
-               *l = '\0';
-               l = libpath;
-       } else {
-               l = ".libs/";
-       }
+               l = strrchr(libpath, '/');
+               if (!l) l = strrchr(libpath, '\\');
+               if (l) {
+                       *l = '\0';
+                       l = libpath;
+               } else {
+                       l = ".libs/";
+               }
 
-       l = "./build/lib/.libs";
-       setenv(LD_LIBRARY_PATH_LOCAL, l, 1);
-       rv = run_command(cmd, cmd->arglist);
+               l = "./build/lib/.libs";
+               setenv(LD_LIBRARY_PATH_LOCAL, l, 1);
+#ifdef __APPLE__
+               setenv("DYLD_FALLBACK_LIBRARY_PATH", l, 1);
+#endif
+               setenv("FR_LIBRARY_PATH", "./build/lib/local/.libs", 1);
+               rv = run_command(cmd, cmd->arglist);
                if (rv) goto finish;
        }
-         break;
+               break;
 
        default:
                break;
@@ -2407,6 +2436,11 @@ static void parse_args(int argc, char *argv[], command_t *cmd)
                arg = argv[a];
                arg_used = 1;
 
+               if (cmd->mode == MODE_EXECUTE) {
+                       push_count_chars(cmd->arglist, arg);
+                       continue;
+               }
+
                if (arg[0] == '-') {
                        /*
                         *      Double dashed (long) single dash (short)
@@ -2418,6 +2452,11 @@ static void parse_args(int argc, char *argv[], command_t *cmd)
                        if (arg_used) continue;
 
                        /*
+                        *      Ignore all options after the '--execute'
+                        */
+                       if (cmd->mode == MODE_EXECUTE) continue;
+
+                       /*
                         *      We haven't done anything with it yet, but
                         *      there are still some arg/value pairs.
                         *