#import <stdio.h>
#import <string.h>
#import <stdlib.h>
#import <errno.h>
#import <sys/param.h>
#ifndef WIN32
#import <unistd.h>
#endif
#define PROGRAM_NAME "ofileListTool"
static void usage(const char * program)
{
fprintf(stderr,"Usage: %s file1.o file2.o ... list1.ofileList list2.ofileList ... [-o ofileList] [-removePrefix <prefix>] [-inDirectory <ofileDir>]\n",program);
exit(1);
}
int main(int argc, const char *argv[])
{
const char * ofiles[argc];
const char * listFiles[argc];
const char * prefixes[argc];
const char * str;
char ofileDirBuf[MAXPATHLEN];
int prefixLen[argc], ofileDirLen = 0;
int i, j, numOfiles = 0, numLists = 0, numPrefixes = 0;
FILE * outs = stdout;
if (argc <= 1)
usage(PROGRAM_NAME);
for (i = 1 ; i < argc; i++) {
if (argv[i][0] == '-') {
if (!strncmp(argv[i],"-o", 2)) {
if (++i < argc) {
#ifndef WIN32
unlink(argv[i]);
#endif
outs = fopen(argv[i], "w");
if (!outs) {
fprintf(stderr,"ofileListTool: Couldn't open output file %s (%s)",
argv[i],strerror(errno));
exit(1);
}
}
} else if (!strncmp(argv[i],"-removePrefix", 2)) {
if (++i < argc) {
int len = prefixLen[numPrefixes] = strlen(argv[i]);
prefixes[numPrefixes++] = argv[i];
if (len > 2 && !strncmp(argv[i], "./", 2)) {
prefixes[numPrefixes] = &argv[i][2];
prefixLen[numPrefixes++] = len - 2;
}
}
} else if (!strncmp(argv[i],"-inDirectory", 2)) {
if (++i < argc) {
sprintf(ofileDirBuf, "%s/", argv[i]);
ofileDirLen = strlen(argv[i])+1;
}
} else {
usage(PROGRAM_NAME);
}
} else {
const char * extension = strrchr(argv[i],'.');
int good = 0;
if (extension && !strcmp(extension,".o")) {
ofiles[numOfiles++] = argv[i];
good = 1;
} else if (extension && !strcmp(extension, ".ofileList")) {
listFiles[numLists++] = argv[i];
good = 1;
}
if (!good)
fprintf(stderr,"%s: %s is not a .o or .ofileList. Ignoring....\n",
PROGRAM_NAME, argv[i]);
}
}
for (i = 0; i < numOfiles; i++) {
if (ofileDirLen) {
strcpy((char *)(ofileDirBuf+ofileDirLen), ofiles[i]);
str = ofileDirBuf;
} else {
str = ofiles[i];
}
for (j = 0; j < numPrefixes; j++)
while (!strncmp(str, prefixes[j], prefixLen[j]))
str = (const char*) (str + prefixLen[j]);
fprintf(outs, "%s\n", str);
}
for (i = 0; i < numLists; i++) {
FILE * ins = fopen(listFiles[i], "r");
if (!ins) {
fprintf(stderr,"%s: %s cannot be read. Ignoring....\n",
PROGRAM_NAME, listFiles[i]);
} else {
char buf[MAXPATHLEN+1];
while (fgets(buf, MAXPATHLEN, ins)) {
str = buf;
for (j = 0; j < numPrefixes; j++)
while (!strncmp(str, prefixes[j], prefixLen[j]))
str = (const char*) (buf + prefixLen[j]);
if (str)
fputs(str, outs);
}
}
}
fclose(outs);
exit(0);
}