2020-02-22 23:23:50 +01:00
|
|
|
/****************************************************************************
|
2020-02-23 23:40:04 +01:00
|
|
|
* tools/zds/zdsar.c
|
2020-02-22 23:23:50 +01:00
|
|
|
*
|
2024-09-10 14:15:58 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2020-02-22 23:23:50 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#ifdef HOST_CYGWIN
|
|
|
|
# include <sys/cygwin.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define MAX_BUFFER (4096)
|
|
|
|
#define MAX_EXPAND (2048)
|
|
|
|
|
|
|
|
/* MAX_PATH might be defined in stdlib.h */
|
|
|
|
|
|
|
|
#if !defined(MAX_PATH)
|
|
|
|
# define MAX_PATH (512)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* NAME_MAX is typically defined in limits.h */
|
|
|
|
|
|
|
|
#if !defined(NAME_MAX)
|
|
|
|
|
|
|
|
/* FILENAME_MAX might be defined in stdio.h */
|
|
|
|
|
|
|
|
# if defined(FILENAME_MAX)
|
|
|
|
# define NAME_MAX FILENAME_MAX
|
|
|
|
# else
|
|
|
|
|
|
|
|
/* MAXNAMELEN might be defined in dirent.h */
|
|
|
|
|
|
|
|
# include <dirent.h>
|
|
|
|
# if defined(MAXNAMLEN)
|
|
|
|
# define NAME_MAX MAXNAMLEN
|
|
|
|
# else
|
|
|
|
|
|
|
|
/* Lets not let a silly think like this stop us... just make something up */
|
|
|
|
|
|
|
|
# define NAME_MAX 256
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
/* Maximum objects per librarian call.
|
|
|
|
*
|
|
|
|
* REVISIT: The librarian is supposed to handle multiple object insertions
|
2020-02-24 20:54:37 +01:00
|
|
|
* per call, but my experience was that it was unreliable in that case. That
|
|
|
|
* may have improved, however, and perhaps we can increase MAX_OPBJEXT.. TRY
|
|
|
|
* IT!
|
2020-02-23 22:13:24 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define MAX_OBJECTS 1 /* 64 */
|
|
|
|
|
|
|
|
/* Name of the host. The ZDS-II toolchain runs only on Windows. Therefore,
|
|
|
|
* the only options are (1) Windows native, or (2) Cygwin or environments
|
|
|
|
* that derive for Cygwin (like MSYS2).
|
|
|
|
*/
|
2020-02-22 23:23:50 +01:00
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
#define WINSEPARATOR '\\'
|
2020-02-22 23:23:50 +01:00
|
|
|
|
|
|
|
#if defined(HOST_NATIVE)
|
|
|
|
# define SEPARATOR '\\'
|
|
|
|
# define HOSTNAME "Native" /* Windows native */
|
|
|
|
#elif defined(HOST_CYGWIN)
|
|
|
|
# define SEPARATOR '/'
|
2020-02-23 09:50:23 +01:00
|
|
|
# define HOSTNAME "Cygwin" /* Cygwin or MSYS under Windows */
|
2020-02-22 23:23:50 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
static char *g_current_wd = NULL; /* Current working directory */
|
|
|
|
static char *g_ar = NULL; /* Full path to the librarian program */
|
|
|
|
static char *g_arflags = NULL; /* Flags to use with the librarian program */
|
|
|
|
static char *g_libpath = NULL; /* Path to the library */
|
|
|
|
static char *g_libname = NULL; /* Library file name*/
|
|
|
|
static char *g_objects = NULL; /* List of object files */
|
|
|
|
static int g_debug = 0; /* Debug output enabled if >0 */
|
|
|
|
|
|
|
|
static char g_command[MAX_BUFFER]; /* Full librarian command */
|
|
|
|
static char g_initial_wd[MAX_PATH]; /* Initial working directory */
|
2020-02-24 22:24:35 +01:00
|
|
|
static char g_path[MAX_PATH]; /* Temporary for path generation */
|
|
|
|
static char g_objpath[MAX_PATH]; /* Holds the relative path to the objects */
|
2020-02-22 23:23:50 +01:00
|
|
|
#ifdef HOST_CYGWIN
|
2020-02-24 18:25:11 +01:00
|
|
|
static char g_expand[MAX_EXPAND]; /* Temporary for quoted path */
|
2020-02-23 23:40:04 +01:00
|
|
|
static char g_dequoted[MAX_PATH]; /* Temporary for de-quoted path */
|
|
|
|
static char g_hostpath[MAX_PATH]; /* Temporary for host path conversions */
|
2020-02-22 23:23:50 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* MinGW does not seem to provide strtok_r */
|
|
|
|
|
|
|
|
#ifndef HAVE_STRTOK_R
|
2020-02-23 22:13:24 +01:00
|
|
|
static char *my_strtok_r(char *str, const char *delim, char **saveptr)
|
2020-02-22 23:23:50 +01:00
|
|
|
{
|
|
|
|
char *pbegin;
|
|
|
|
char *pend = NULL;
|
|
|
|
|
|
|
|
/* Decide if we are starting a new string or continuing from
|
|
|
|
* the point we left off.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (str)
|
|
|
|
{
|
|
|
|
pbegin = str;
|
|
|
|
}
|
|
|
|
else if (saveptr && *saveptr)
|
|
|
|
{
|
|
|
|
pbegin = *saveptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the beginning of the next token */
|
|
|
|
|
|
|
|
for (;
|
|
|
|
*pbegin && strchr(delim, *pbegin) != NULL;
|
|
|
|
pbegin++);
|
|
|
|
|
|
|
|
/* If we are at the end of the string with nothing
|
|
|
|
* but delimiters found, then return NULL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!*pbegin)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the end of the token */
|
|
|
|
|
|
|
|
for (pend = pbegin + 1;
|
|
|
|
*pend && strchr(delim, *pend) == NULL;
|
|
|
|
pend++);
|
|
|
|
|
|
|
|
/* pend either points to the end of the string or to
|
|
|
|
* the first delimiter after the string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (*pend)
|
|
|
|
{
|
|
|
|
/* Turn the delimiter into a null terminator */
|
|
|
|
|
|
|
|
*pend++ = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the pointer where we left off and return the
|
|
|
|
* beginning of the token.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (saveptr)
|
|
|
|
{
|
|
|
|
*saveptr = pend;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pbegin;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef strtok_r
|
2020-02-23 22:13:24 +01:00
|
|
|
# define strtok_r my_strtok_r
|
2020-02-22 23:23:50 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static void append(char **base, char *str)
|
|
|
|
{
|
|
|
|
char *oldbase;
|
|
|
|
char *newbase;
|
|
|
|
int alloclen;
|
|
|
|
|
|
|
|
oldbase = *base;
|
|
|
|
if (oldbase == NULL)
|
|
|
|
{
|
|
|
|
newbase = strdup(str);
|
|
|
|
if (!newbase)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Failed to strdup %s\n", str);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
alloclen = strlen(oldbase) + strlen(str) + sizeof((char) ' ') +
|
|
|
|
sizeof((char) '\0');
|
|
|
|
newbase = (char *)malloc(alloclen);
|
|
|
|
if (!newbase)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Failed to allocate %d bytes\n", alloclen);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(newbase, alloclen, "%s %s", oldbase, str);
|
|
|
|
free(oldbase);
|
2020-02-23 22:13:24 +01:00
|
|
|
}
|
2020-02-22 23:23:50 +01:00
|
|
|
|
|
|
|
*base = newbase;
|
|
|
|
}
|
|
|
|
|
2020-02-24 18:25:11 +01:00
|
|
|
static const char *quote_backslash(const char *argument)
|
2020-02-22 23:23:50 +01:00
|
|
|
{
|
|
|
|
#ifdef HOST_CYGWIN
|
|
|
|
const char *src;
|
|
|
|
char *dest;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
src = argument;
|
|
|
|
dest = g_expand;
|
|
|
|
len = 0;
|
|
|
|
|
|
|
|
while (*src && len < MAX_EXPAND)
|
|
|
|
{
|
|
|
|
if (*src == '\\')
|
|
|
|
{
|
|
|
|
/* Copy backslash */
|
|
|
|
|
|
|
|
*dest++ = *src++;
|
|
|
|
if (++len >= MAX_EXPAND)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-02-24 18:25:11 +01:00
|
|
|
/* Already quoted? */
|
2020-02-22 23:23:50 +01:00
|
|
|
|
|
|
|
if (*src == '\\')
|
|
|
|
{
|
|
|
|
/* Yes... just copy all consecutive backslashes */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
*dest++ = *src++;
|
|
|
|
if (++len >= MAX_EXPAND)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (*src == '\\');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No.. expend */
|
|
|
|
|
|
|
|
*dest++ = '\\';
|
|
|
|
if (++len >= MAX_EXPAND)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*dest++ = *src++;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*src)
|
|
|
|
{
|
2020-02-23 22:13:24 +01:00
|
|
|
fprintf(stderr, "ERROR: Truncated during expansion string "
|
|
|
|
"is too long [%lu/%u]\n",
|
2020-02-22 23:23:50 +01:00
|
|
|
(unsigned long)strlen(argument), MAX_EXPAND);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
*dest = '\0';
|
|
|
|
return g_expand;
|
|
|
|
#else
|
|
|
|
return argument;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove backslash quoting from a path */
|
|
|
|
|
|
|
|
#ifdef HOST_CYGWIN
|
|
|
|
static bool dequote_path(const char *winpath)
|
|
|
|
{
|
|
|
|
char *dest = g_dequoted;
|
|
|
|
const char *src = winpath;
|
|
|
|
int len = 0;
|
|
|
|
bool quoted = false;
|
|
|
|
|
|
|
|
while (*src && len < MAX_PATH)
|
|
|
|
{
|
2020-02-23 22:13:24 +01:00
|
|
|
if (src[0] != '\\' ||
|
|
|
|
(src[1] != ' ' && src[1] != '(' && src[1] != ')'))
|
2020-02-22 23:23:50 +01:00
|
|
|
{
|
|
|
|
*dest++ = *src;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
quoted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*src || len >= MAX_PATH)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "# ERROR: Path truncated\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
*dest = '\0';
|
|
|
|
return quoted;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* If using Cygwin with a Window's Toolchain, then we have to convert the
|
2020-02-23 23:40:04 +01:00
|
|
|
* POSIX path to a Windows or POSIX path.
|
2020-02-22 23:23:50 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HOST_CYGWIN
|
2020-02-23 22:13:24 +01:00
|
|
|
static const char *convert_path(const char *path, cygwin_conv_path_t what)
|
|
|
|
{
|
2020-02-22 23:23:50 +01:00
|
|
|
const char *retptr;
|
|
|
|
ssize_t size;
|
|
|
|
ssize_t ret;
|
|
|
|
bool quoted;
|
|
|
|
|
|
|
|
quoted = dequote_path(path);
|
|
|
|
if (quoted)
|
|
|
|
{
|
2020-02-23 23:40:04 +01:00
|
|
|
retptr = g_hostpath;
|
2020-02-22 23:23:50 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-23 23:40:04 +01:00
|
|
|
retptr = &g_hostpath[1];
|
2020-02-22 23:23:50 +01:00
|
|
|
}
|
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
size = cygwin_conv_path(what | CCP_RELATIVE, g_dequoted, NULL, 0);
|
2020-02-22 23:23:50 +01:00
|
|
|
if (size > (MAX_PATH - 3))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "# ERROR: POSIX path too long: %lu\n",
|
|
|
|
(unsigned long)size);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
ret = cygwin_conv_path(what | CCP_RELATIVE, g_dequoted,
|
2020-02-23 23:40:04 +01:00
|
|
|
&g_hostpath[1], MAX_PATH - 3);
|
2020-02-22 23:23:50 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "# ERROR: cygwin_conv_path '%s' failed: %s\n",
|
|
|
|
g_dequoted, strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (quoted)
|
|
|
|
{
|
|
|
|
size++;
|
2020-02-23 23:40:04 +01:00
|
|
|
g_hostpath[0] = '"';
|
|
|
|
g_hostpath[size] = '"';
|
2020-02-22 23:23:50 +01:00
|
|
|
}
|
|
|
|
|
2020-02-23 23:40:04 +01:00
|
|
|
g_hostpath[size + 1] = '\0';
|
2020-02-22 23:23:50 +01:00
|
|
|
return retptr;
|
2020-02-23 22:13:24 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const char *convert_path_windows(const char *path)
|
|
|
|
{
|
|
|
|
#ifdef HOST_CYGWIN
|
|
|
|
return convert_path(path, CCP_POSIX_TO_WIN_A);
|
|
|
|
#else
|
2020-02-24 22:24:35 +01:00
|
|
|
strcpy(g_path, path);
|
|
|
|
return g_path;
|
2020-02-23 22:13:24 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *convert_path_posix(const char *path)
|
|
|
|
{
|
|
|
|
#ifdef HOST_CYGWIN
|
|
|
|
return convert_path(path, CCP_WIN_A_TO_POSIX);
|
2020-02-22 23:23:50 +01:00
|
|
|
#else
|
2020-02-24 22:24:35 +01:00
|
|
|
strcpy(g_path, path);
|
|
|
|
return g_path;
|
2020-02-22 23:23:50 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void show_usage(const char *progname, const char *msg, int exitcode)
|
|
|
|
{
|
|
|
|
if (msg)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
fprintf(stderr, "%s:\n", msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "\n");
|
2020-02-23 22:13:24 +01:00
|
|
|
fprintf(stderr, "%s [OPTIONS] --ar \"<AR>\" --library \"<LIBRARY>\" "
|
|
|
|
"obj [obj [obj...]]\n",
|
2020-02-22 23:23:50 +01:00
|
|
|
progname);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
fprintf(stderr, "Where:\n");
|
|
|
|
fprintf(stderr, " --ar <AR>\n");
|
2020-02-23 22:13:24 +01:00
|
|
|
fprintf(stderr, " A command line string that defines how to execute the "
|
|
|
|
"ZDS-II librarian\n");
|
2020-02-22 23:23:50 +01:00
|
|
|
fprintf(stderr, " --library \"<LIBRARY>\"\n");
|
2020-02-23 22:13:24 +01:00
|
|
|
fprintf(stderr, " The library into which the object files will be "
|
|
|
|
"inserted\n");
|
2020-02-22 23:23:50 +01:00
|
|
|
fprintf(stderr, " obj\n");
|
2020-02-23 22:13:24 +01:00
|
|
|
fprintf(stderr, " One or more object files that will be inserted into "
|
|
|
|
"the archive. Each expected\n");
|
|
|
|
fprintf(stderr, " to reside in the current directory unless --obj-path "
|
|
|
|
"is provided on the command line\n");
|
2020-02-22 23:23:50 +01:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
fprintf(stderr, "And [OPTIONS] include:\n");
|
|
|
|
fprintf(stderr, " --ar_flags \"<ARFLAGS>\"\n");
|
|
|
|
fprintf(stderr, " Optional librarian flags\n");
|
|
|
|
fprintf(stderr, " --obj-path <path>\n");
|
2020-02-23 22:13:24 +01:00
|
|
|
fprintf(stderr, " Do not look in the current directory for the object "
|
2020-02-24 22:24:35 +01:00
|
|
|
"files. Instead, look in <path> for\n");
|
|
|
|
fprintf(stderr, " the object files. --obj-path may be used only once "
|
2020-02-23 22:13:24 +01:00
|
|
|
"on the command line\n");
|
2020-02-22 23:23:50 +01:00
|
|
|
fprintf(stderr, " --debug\n");
|
|
|
|
fprintf(stderr, " Enable %s debug output\n", progname);
|
|
|
|
fprintf(stderr, " --help\n");
|
|
|
|
fprintf(stderr, " Shows this message and exits\n");
|
|
|
|
exit(exitcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_args(int argc, char **argv)
|
|
|
|
{
|
2020-02-24 22:24:35 +01:00
|
|
|
const char *tmp = NULL;
|
|
|
|
char *library = NULL;
|
|
|
|
char *objpath = NULL;
|
|
|
|
int pathlen;
|
2020-02-22 23:23:50 +01:00
|
|
|
int argidx;
|
|
|
|
|
2020-02-23 23:40:04 +01:00
|
|
|
/* Parse arguments */
|
2020-02-22 23:23:50 +01:00
|
|
|
|
|
|
|
for (argidx = 1; argidx < argc; argidx++)
|
|
|
|
{
|
|
|
|
if (strcmp(argv[argidx], "--ar") == 0)
|
|
|
|
{
|
|
|
|
argidx++;
|
|
|
|
if (argidx >= argc)
|
|
|
|
{
|
2020-02-23 22:13:24 +01:00
|
|
|
show_usage(argv[0], "ERROR: Missing argument to --ar",
|
|
|
|
EXIT_FAILURE);
|
2020-02-22 23:23:50 +01:00
|
|
|
}
|
|
|
|
else if (g_ar != NULL)
|
|
|
|
{
|
2020-02-23 22:13:24 +01:00
|
|
|
show_usage(argv[0], "ERROR: Multiple --ar arguments",
|
|
|
|
EXIT_FAILURE);
|
2020-02-22 23:23:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
g_ar = argv[argidx];
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[argidx], "--ar_flags") == 0)
|
|
|
|
{
|
|
|
|
argidx++;
|
|
|
|
if (argidx >= argc)
|
|
|
|
{
|
2020-02-23 22:13:24 +01:00
|
|
|
show_usage(argv[0], "ERROR: Missing argument to --ar_flags",
|
|
|
|
EXIT_FAILURE);
|
2020-02-22 23:23:50 +01:00
|
|
|
}
|
|
|
|
else if (g_arflags != NULL)
|
|
|
|
{
|
2020-02-23 22:13:24 +01:00
|
|
|
show_usage(argv[0], "ERROR: Multiple --ar_flags arguments",
|
|
|
|
EXIT_FAILURE);
|
2020-02-22 23:23:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
g_arflags = argv[argidx];
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[argidx], "--library") == 0)
|
|
|
|
{
|
2020-02-23 00:53:28 +01:00
|
|
|
const char *tmp_path;
|
|
|
|
|
2020-02-22 23:23:50 +01:00
|
|
|
argidx++;
|
|
|
|
if (argidx >= argc)
|
|
|
|
{
|
2020-02-23 22:13:24 +01:00
|
|
|
show_usage(argv[0], "ERROR: Missing argument to --library",
|
|
|
|
EXIT_FAILURE);
|
2020-02-22 23:23:50 +01:00
|
|
|
}
|
|
|
|
else if (library != NULL)
|
|
|
|
{
|
2020-02-23 22:13:24 +01:00
|
|
|
show_usage(argv[0], "ERROR: Multiple --library arguments",
|
|
|
|
EXIT_FAILURE);
|
2020-02-22 23:23:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the library path a POSIX. NOTE this is a no-op in Windows
|
|
|
|
* native mode.
|
|
|
|
*/
|
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
tmp_path = convert_path_posix(argv[argidx]);
|
2020-02-23 00:53:28 +01:00
|
|
|
library = strdup(tmp_path);
|
|
|
|
if (library == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: strdup() failed\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2020-02-22 23:23:50 +01:00
|
|
|
}
|
|
|
|
else if (strcmp(argv[argidx], "--obj-path") == 0)
|
|
|
|
{
|
|
|
|
argidx++;
|
|
|
|
if (argidx >= argc)
|
|
|
|
{
|
2020-02-23 22:13:24 +01:00
|
|
|
show_usage(argv[0], "ERROR: Missing argument to --obj-path",
|
|
|
|
EXIT_FAILURE);
|
2020-02-22 23:23:50 +01:00
|
|
|
}
|
|
|
|
else if (objpath != NULL)
|
|
|
|
{
|
2020-02-23 22:13:24 +01:00
|
|
|
show_usage(argv[0], "ERROR: Multiple --obj-path arguments",
|
|
|
|
EXIT_FAILURE);
|
2020-02-22 23:23:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
objpath = argv[argidx];
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[argidx], "--debug") == 0)
|
|
|
|
{
|
|
|
|
g_debug++;
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[argidx], "--help") == 0)
|
|
|
|
{
|
|
|
|
show_usage(argv[0], NULL, EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
else if (strncmp(argv[argidx], "--", 2) == 0)
|
|
|
|
{
|
|
|
|
show_usage(argv[0], "ERROR: Unrecognized option", EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Accumulate object files */
|
|
|
|
|
|
|
|
for (; argidx < argc; argidx++)
|
|
|
|
{
|
|
|
|
append(&g_objects, argv[argidx]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_debug)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Selections:\n");
|
2020-02-23 22:13:24 +01:00
|
|
|
fprintf(stderr, " CWD : [%s]\n",
|
|
|
|
g_initial_wd);
|
|
|
|
fprintf(stderr, " Host Environ : [%s]\n",
|
|
|
|
HOSTNAME);
|
|
|
|
fprintf(stderr, " AR : [%s]\n",
|
|
|
|
g_ar ? g_ar : "(None)");
|
|
|
|
fprintf(stderr, " AR Flags : [%s]\n",
|
|
|
|
g_arflags ? g_arflags : "(None)");
|
|
|
|
fprintf(stderr, " Library : [%s]\n",
|
|
|
|
library ? library : "(None)");
|
|
|
|
fprintf(stderr, " Object Path : [%s]\n",
|
|
|
|
objpath ? objpath : "(None");
|
|
|
|
fprintf(stderr, " Object Files : [%s]\n\n",
|
|
|
|
g_objects ? g_objects : "(None)");
|
2020-02-22 23:23:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for required parameters */
|
|
|
|
|
|
|
|
if (g_ar == NULL)
|
|
|
|
{
|
2020-02-23 22:13:24 +01:00
|
|
|
show_usage(argv[0], "ERROR: No librarian specified",
|
|
|
|
EXIT_FAILURE);
|
2020-02-22 23:23:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (library == NULL)
|
|
|
|
{
|
2020-02-23 22:13:24 +01:00
|
|
|
show_usage(argv[0], "ERROR: No library specified",
|
|
|
|
EXIT_FAILURE);
|
2020-02-22 23:23:50 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Separate the library file name from the path. The ZDS-II librarian
|
|
|
|
* expects the library to be in the current working directory.
|
|
|
|
*/
|
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
g_libname = basename(library); /* Must come first */
|
2020-02-22 23:23:50 +01:00
|
|
|
g_libpath = dirname(library);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_objects == NULL)
|
|
|
|
{
|
|
|
|
/* Don't report an error -- this happens normally in some configurations */
|
|
|
|
|
|
|
|
printf("No object files specified\n");
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_objpath[0] = '\0';
|
|
|
|
if (objpath != NULL)
|
|
|
|
{
|
|
|
|
/* If the object path relative to the current working directory? */
|
|
|
|
|
|
|
|
/* It is a relative path if the path does not begin with the path
|
|
|
|
* segment separator or if in the Windows native case, it begins
|
|
|
|
* with a volume specified like C:.
|
|
|
|
*/
|
|
|
|
|
|
|
|
pathlen = 0;
|
|
|
|
|
|
|
|
#ifdef HOST_NATIVE
|
|
|
|
if (objpath[0] != SEPARATOR ||
|
|
|
|
(isalpha(objpath[0]) && objpath[1] != ':'))
|
|
|
|
#else
|
|
|
|
if (objpath[0] != SEPARATOR)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Add the default working directory to the path */
|
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
/* Copy the initial working directory */
|
2020-02-22 23:23:50 +01:00
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
pathlen = strlen(g_initial_wd);
|
2020-02-22 23:23:50 +01:00
|
|
|
if (pathlen >= MAX_PATH)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Working directory path is "
|
|
|
|
"too long [%d/%d]: %s\n",
|
2020-02-23 22:13:24 +01:00
|
|
|
pathlen, MAX_PATH, g_initial_wd);
|
2020-02-22 23:23:50 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
strcpy(g_path, g_initial_wd);
|
2020-02-22 23:23:50 +01:00
|
|
|
|
|
|
|
/* Append a separator is one is not already present */
|
|
|
|
|
2020-02-24 22:24:35 +01:00
|
|
|
if (g_path[pathlen - 1] != SEPARATOR)
|
2020-02-22 23:23:50 +01:00
|
|
|
{
|
2020-02-23 22:13:24 +01:00
|
|
|
int newlen = pathlen + 1;
|
|
|
|
if (newlen >= MAX_PATH)
|
2020-02-22 23:23:50 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Object path is too long "
|
|
|
|
"with separator[%d/%d]: %s\n",
|
2020-02-23 22:13:24 +01:00
|
|
|
newlen, MAX_PATH, g_initial_wd);
|
2020-02-22 23:23:50 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2020-02-24 22:24:35 +01:00
|
|
|
g_path[pathlen] = SEPARATOR;
|
2020-02-22 23:23:50 +01:00
|
|
|
g_path[pathlen + 1] = '\0';
|
2020-02-23 22:13:24 +01:00
|
|
|
pathlen = newlen;
|
2020-02-22 23:23:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the object file path after the current working directory */
|
|
|
|
|
|
|
|
pathlen += strlen(objpath);
|
|
|
|
if (pathlen >= MAX_PATH)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Path+objpath is too long [%d/%d]\n",
|
|
|
|
pathlen, MAX_PATH);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
strcat(g_path, objpath);
|
2020-02-23 22:13:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The object was in the current working directory. If a library path
|
|
|
|
* is NOT the current working directory, then the library path will now
|
|
|
|
* be the current working directory and the path to the objects will be
|
|
|
|
* the working directory when the program was started.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (g_libpath != NULL && strcmp(g_libpath, ".") != 0)
|
|
|
|
{
|
2020-02-24 22:24:35 +01:00
|
|
|
strcpy(g_path, g_initial_wd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the absolute objection file path to the native host path form
|
|
|
|
* NOTE that convert_path_posix() is a no-op in Windows native mode.
|
|
|
|
*/
|
|
|
|
|
|
|
|
tmp = convert_path_posix(g_path);
|
|
|
|
strcpy(g_path, tmp);
|
|
|
|
|
|
|
|
/* Check for a relative path. We will CD to g_libpath because the
|
|
|
|
* library must be in the current working directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
pathlen = strlen(g_libpath);
|
|
|
|
if (strncmp(g_path, g_libpath, pathlen) == 0)
|
|
|
|
{
|
|
|
|
const char *relpath = &g_path[pathlen];
|
|
|
|
|
|
|
|
/* Skip over leading path segment delimiters.. that should be as
|
|
|
|
* least one.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (*relpath == SEPARATOR)
|
|
|
|
{
|
|
|
|
relpath++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the relative object file path to the Windows path form
|
|
|
|
* for the ZDS-II tool tool. NOTE that convert_path_windows() is
|
|
|
|
* a no-op in Windows native mode.
|
|
|
|
*/
|
|
|
|
|
|
|
|
tmp = convert_path_windows(relpath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Convert the absolute object file path to the Windows path form
|
|
|
|
* for the ZDS-II tool tool. NOTE that convert_path_windows() is
|
|
|
|
* a no-op in Windows native mode.
|
|
|
|
*/
|
|
|
|
|
|
|
|
tmp = convert_path_windows(g_path);
|
2020-02-22 23:23:50 +01:00
|
|
|
}
|
|
|
|
|
2020-02-24 22:24:35 +01:00
|
|
|
/* And save the path in as a native Windows path */
|
|
|
|
|
|
|
|
strcpy(g_objpath, tmp);
|
|
|
|
|
|
|
|
/* Dump some intermediate results */
|
|
|
|
|
2020-02-22 23:23:50 +01:00
|
|
|
if (g_debug)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Derived:\n");
|
2020-02-23 22:13:24 +01:00
|
|
|
fprintf(stderr, " Object Path : [%s]\n",
|
|
|
|
g_objpath[0] != '\0' ? g_objpath : "(None");
|
|
|
|
fprintf(stderr, " Library Path : [%s]\n",
|
|
|
|
g_libpath ? g_libpath : "(None)");
|
|
|
|
fprintf(stderr, " Library Name : [%s]\n\n",
|
|
|
|
g_libname ? g_libname : "(None)");
|
2020-02-22 23:23:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_archive(void)
|
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
char *alloc;
|
|
|
|
char *objects;
|
|
|
|
char *object;
|
|
|
|
char *lasts;
|
|
|
|
int cmdlen;
|
|
|
|
int pathlen;
|
|
|
|
int objlen;
|
|
|
|
int totallen;
|
|
|
|
int nobjects;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Make a copy of g_objects. We need to do this because at least the version
|
|
|
|
* of strtok_r above does modify it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
alloc = strdup(g_objects);
|
|
|
|
if (alloc == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Failed to strdup object list\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
objects = alloc;
|
|
|
|
|
|
|
|
/* We may have to loop since we limit the number of objects in each call
|
|
|
|
* to the librarian.
|
|
|
|
*/
|
|
|
|
|
2020-02-23 23:40:04 +01:00
|
|
|
lasts = NULL;
|
2020-02-22 23:23:50 +01:00
|
|
|
for (; ; )
|
|
|
|
{
|
|
|
|
/* Copy the librarian into the command buffer */
|
|
|
|
|
|
|
|
cmdlen = strlen(g_ar);
|
|
|
|
if (cmdlen >= MAX_BUFFER)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Librarian string is too long [%d/%d]: %s\n",
|
|
|
|
cmdlen, MAX_BUFFER, g_ar);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(g_command, g_ar);
|
|
|
|
|
|
|
|
/* Add a space */
|
|
|
|
|
|
|
|
g_command[cmdlen] = ' ';
|
|
|
|
cmdlen++;
|
|
|
|
g_command[cmdlen] = '\0';
|
|
|
|
|
|
|
|
/* Copy the librarian flags into the command buffer */
|
|
|
|
|
|
|
|
if (g_arflags != NULL)
|
|
|
|
{
|
2020-02-24 18:25:11 +01:00
|
|
|
const char *quoted;
|
2020-02-22 23:23:50 +01:00
|
|
|
|
2020-02-24 18:25:11 +01:00
|
|
|
quoted = quote_backslash(g_arflags);
|
|
|
|
cmdlen += strlen(quoted);
|
2020-02-22 23:23:50 +01:00
|
|
|
|
|
|
|
if (cmdlen >= MAX_BUFFER)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: CFLAG string is too long [%d/%d]: %s\n",
|
|
|
|
cmdlen, MAX_BUFFER, g_arflags);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2020-02-24 18:25:11 +01:00
|
|
|
strcat(g_command, quoted);
|
2020-02-22 23:23:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add each object file. This loop will continue until each path has been
|
|
|
|
* tried (failure) or until stat() finds the object file
|
|
|
|
*/
|
|
|
|
|
|
|
|
nobjects = 0;
|
|
|
|
while ((object = strtok_r(objects, " ", &lasts)) != NULL)
|
|
|
|
{
|
2020-02-24 18:25:11 +01:00
|
|
|
const char *quoted;
|
2020-02-22 23:23:50 +01:00
|
|
|
const char *converted;
|
|
|
|
|
|
|
|
/* Set objects to NULL. This will force strtok_r to move from the
|
|
|
|
* the first object in the list.
|
|
|
|
*/
|
|
|
|
|
|
|
|
objects = NULL;
|
|
|
|
|
|
|
|
/* Add a space */
|
|
|
|
|
|
|
|
g_command[cmdlen] = ' ';
|
|
|
|
cmdlen++;
|
|
|
|
g_command[cmdlen] = '\0';
|
|
|
|
|
|
|
|
/* Create a full path to the object file */
|
|
|
|
|
|
|
|
g_path[0] = '\0';
|
|
|
|
pathlen = 0;
|
|
|
|
|
|
|
|
/* Add the path to buffer path buffer first */
|
|
|
|
|
|
|
|
if (g_objpath[0] != '\0')
|
|
|
|
{
|
|
|
|
/* Copy the obj_path */
|
|
|
|
|
|
|
|
pathlen = strlen(g_objpath);
|
|
|
|
if (pathlen >= MAX_PATH)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Path is too long [%d/%d]: %s\n",
|
|
|
|
pathlen, MAX_PATH, g_objpath);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(g_path, g_objpath);
|
|
|
|
|
|
|
|
/* Append a separator is one is not already present */
|
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
if (g_path[pathlen - 1] != WINSEPARATOR)
|
2020-02-22 23:23:50 +01:00
|
|
|
{
|
2020-02-23 22:13:24 +01:00
|
|
|
int newlen = pathlen + 1;
|
|
|
|
if (newlen >= MAX_PATH)
|
2020-02-22 23:23:50 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Path is too long with "
|
|
|
|
"separator[%d/%d]: %s\n",
|
2020-02-23 22:13:24 +01:00
|
|
|
newlen, MAX_PATH, g_path);
|
2020-02-22 23:23:50 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
g_path[pathlen] = WINSEPARATOR;
|
2020-02-22 23:23:50 +01:00
|
|
|
g_path[pathlen + 1] = '\0';
|
2020-02-23 22:13:24 +01:00
|
|
|
pathlen = newlen;
|
2020-02-22 23:23:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the object file name after the path */
|
|
|
|
|
|
|
|
objlen = strlen(object);
|
|
|
|
pathlen += objlen;
|
|
|
|
if (pathlen >= MAX_PATH)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Path+objfile is too long [%d/%d]\n",
|
|
|
|
pathlen, MAX_PATH);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
strcat(g_path, object);
|
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
/* Check that a object file actually exists at this path. NOTE
|
|
|
|
* that convert_path_posix() is a no-op in Windows native mode.
|
|
|
|
*/
|
2020-02-22 23:23:50 +01:00
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
converted = convert_path_posix(g_path);
|
2020-02-22 23:23:50 +01:00
|
|
|
ret = stat(converted, &buf);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-02-23 22:13:24 +01:00
|
|
|
fprintf(stderr, "WARNING: Stat of object %s failed: %s\n",
|
|
|
|
g_path, strerror(errno));
|
2020-02-22 23:23:50 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!S_ISREG(buf.st_mode))
|
|
|
|
{
|
2020-02-23 22:13:24 +01:00
|
|
|
fprintf(stderr, "ERROR: Object %s exists but is not a regular "
|
|
|
|
"file\n",
|
2020-02-22 23:23:50 +01:00
|
|
|
g_path);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Expand the path */
|
|
|
|
|
|
|
|
/* Copy the librarian argument of form like:
|
|
|
|
*
|
2020-02-23 22:13:24 +01:00
|
|
|
* <libname>=-+<objpath>
|
2020-02-22 23:23:50 +01:00
|
|
|
*/
|
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
pathlen = 4; /* For =-+ and terminator */
|
2020-02-22 23:23:50 +01:00
|
|
|
|
2020-02-24 18:25:11 +01:00
|
|
|
quoted = quote_backslash(g_path);
|
|
|
|
pathlen += strlen(quoted);
|
2020-02-22 23:23:50 +01:00
|
|
|
|
|
|
|
/* Get the full length */
|
|
|
|
|
|
|
|
pathlen += strlen(g_libname);
|
|
|
|
totallen = cmdlen + pathlen;
|
|
|
|
|
|
|
|
if (totallen >= MAX_BUFFER)
|
|
|
|
{
|
2020-02-23 22:13:24 +01:00
|
|
|
fprintf(stderr, "ERROR: object argument is too long [%d/%d]: "
|
|
|
|
"%s=-+%s\n",
|
2020-02-24 18:25:11 +01:00
|
|
|
totallen, MAX_BUFFER, g_libname, quoted);
|
2020-02-22 23:23:50 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Append the next librarian command */
|
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
pathlen = snprintf(&g_command[cmdlen], MAX_BUFFER - cmdlen, "%s=-+%s",
|
2020-02-24 18:25:11 +01:00
|
|
|
g_libname, quoted);
|
2020-02-22 23:23:50 +01:00
|
|
|
cmdlen += pathlen;
|
|
|
|
|
|
|
|
/* Terminate early if we have a LOT files in the command line */
|
|
|
|
|
|
|
|
if (++nobjects >= MAX_OBJECTS)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
/* Handling the final command which may have not objects to insert */
|
2020-02-22 23:23:50 +01:00
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
if (nobjects > 0)
|
2020-02-22 23:23:50 +01:00
|
|
|
{
|
2020-02-23 22:13:24 +01:00
|
|
|
/* Okay.. we have everything. Add the object files to the library.
|
|
|
|
* On a failure to start the compiler, system() will return -1;
|
|
|
|
* Otherwise, the returned value from the compiler is in
|
|
|
|
* WEXITSTATUS(ret).
|
|
|
|
*/
|
2020-02-22 23:23:50 +01:00
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
if (g_debug)
|
2020-02-22 23:23:50 +01:00
|
|
|
{
|
2020-02-23 22:13:24 +01:00
|
|
|
fprintf(stderr, "Executing: %s\n", g_command);
|
2020-02-22 23:23:50 +01:00
|
|
|
}
|
2020-02-23 22:13:24 +01:00
|
|
|
|
|
|
|
ret = system(g_command);
|
|
|
|
#ifdef WEXITSTATUS
|
|
|
|
if (ret < 0 || WEXITSTATUS(ret) != 0)
|
2020-02-22 23:23:50 +01:00
|
|
|
{
|
2020-02-23 22:13:24 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: system failed: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: %s failed: %d\n", g_ar,
|
|
|
|
WEXITSTATUS(ret));
|
|
|
|
}
|
2020-02-22 23:23:50 +01:00
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
fprintf(stderr, " command: %s\n", g_command);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2020-02-22 23:23:50 +01:00
|
|
|
#else
|
2020-02-23 22:13:24 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: system failed: %s\n", strerror(errno));
|
|
|
|
fprintf(stderr, " command: %s\n", g_command);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2020-02-22 23:23:50 +01:00
|
|
|
#endif
|
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
/* We don't really know that the command succeeded... Let's
|
|
|
|
* assume that it did
|
|
|
|
*/
|
|
|
|
}
|
2020-02-22 23:23:50 +01:00
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
/* Check if we have more objects to process */
|
2020-02-22 23:23:50 +01:00
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
if (object == NULL)
|
2020-02-22 23:23:50 +01:00
|
|
|
{
|
|
|
|
/* No, we are finished */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(alloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int main(int argc, char **argv, char **envp)
|
|
|
|
{
|
|
|
|
char *wd;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get the current working directory */
|
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
wd = getcwd(g_initial_wd, MAX_PATH);
|
2020-02-22 23:23:50 +01:00
|
|
|
if (wd == NULL)
|
|
|
|
{
|
2020-02-23 22:13:24 +01:00
|
|
|
fprintf(stderr, "ERROR: getcwd failed: %s\n", strerror(errno));
|
2020-02-22 23:23:50 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
g_current_wd = g_initial_wd;
|
2020-02-22 23:23:50 +01:00
|
|
|
|
|
|
|
/* Parse command line parameters */
|
|
|
|
|
|
|
|
parse_args(argc, argv);
|
|
|
|
|
|
|
|
/* Change to the directory containing the library */
|
|
|
|
|
2020-02-23 22:13:24 +01:00
|
|
|
if (g_libpath != NULL && strcmp(g_libpath, ".") != 0)
|
2020-02-22 23:23:50 +01:00
|
|
|
{
|
|
|
|
ret = chdir(g_libpath);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-02-23 22:13:24 +01:00
|
|
|
fprintf(stderr, "ERROR: getcwd failed: %s\n", strerror(errno));
|
2020-02-22 23:23:50 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2020-02-23 22:13:24 +01:00
|
|
|
|
|
|
|
g_current_wd = g_libpath;
|
2020-02-22 23:23:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then generate dependencies for each path on the command line. */
|
|
|
|
|
|
|
|
do_archive();
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|