1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
#include <cstdio>
#include <windows.h>
#include <iostream>
#include <sys/stat.h>
using namespace std;
/*** Declare dynamically allocated arrays?? ***/
char *srcFile, *outFile, *execCompile, *execStrip, *execPack;
int x;
// Usage info
void usage(char** argv) {
cout << "Usage: " << argv[0] << " [FILE]\n" << "\nOptions:\n"
<< "-h, --help\tDisplay this message and exit\n" << "\nNotes:\n"
<< "Do not use an extension in [FILE].\n" << "e.g. '" << argv[0]
<< " mysource', NOT '" << argv[0] << " mysource.cpp'" << endl;
}
// check if a file exsists
int file_exist(char *filename) {
struct stat buffer;
return (stat(filename, &buffer) == 0);
}
// generic error message
int error() {
cerr << "Error: Unknown" << endl;
return 1;
}
// generic success message
void done() {
cout << "Done" << endl;
}
int main(int argc, char** argv) {
/*** Initialize the dynamically allocated char arrays?? ***/
srcFile = new char [strlen(argv[1])+6]; // initialize srcFile with the length of argv[1] + ".cpp" + any null terminations
outFile = new char [strlen(argv[1])+6]; // initialize outFile the same way
execCompile = new char [strlen(outFile)+strlen(srcFile)+31]; // init execCompile with length of outFile + srcFile + string that it will be put in it + null terms
execStrip = new char [strlen(outFile)+18]; // init with outFile + string length + nullterms
execPack = new char [strlen(outFile)+37]; // init with outFile + string length + nullterms
sprintf(srcFile, "%s.cpp", argv[1]); // print "argv[1].cpp" to srcFile
sprintf(outFile, "%s.exe", argv[1]); // same thing
if (argc > 1) {
if (strstr(argv[1], "-h")) { // find -h in argv[1]
usage(argv); // print help & usage info
return 0;
}
sprintf(execCompile, "g++ -Wall -Os -o %s %s", outFile, srcFile); // print "g++ -Wall -Os -o outFile.exe inFile.cpp" to execCompile
sprintf(execStrip, "strip %s", outFile); // printf "strip outFile" to execStrip
sprintf(execPack, "upx --best --ultra-brute %s", outFile); // print "upx --best --ultra-brtue outFile.exe" to execPack
if (file_exist(srcFile)) { // check if specified file exsists
cout << "Compiling ... ";
x = system(execCompile); // execute command line to compile srcFile
if (x == 0) done();
else error();
cout << "Stripping ... ";
x = system(execStrip); // execute CL to strip the resulting executable
if (x == 0) done();
else error();
cout << "Packing ... ";
x = system(execPack); // execute CL to pack the resulting exe
if (x == 0) done();
else error();
} else {
cerr << "Error: " << srcFile << ": No such file" << endl; // error if file doesn't exsist
return 1;
}
} else {
cerr << "Error: No file specified" << endl; // error if no filename was given on CL
cout << "Type " << argv[0] << " --help for more info." << endl;
return 1;
}
/*** Clear out the memory allocations?? ***/
delete[] srcFile;
delete[] outFile;
delete[] execCompile;
delete[] execStrip;
delete[] execPack;
return 0;
}
/*
* TODO: Prompt for filename if not specified in CL arg.
* TODO: Support working with multiple files.
* TODO: Use dynamically allocated char arrays
* TODO: Add more options for output control
* TODO: Allow specifying '.cpp' in filename
*/
|