Skip to content
Snippets Groups Projects
Commit 36f35685 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

generalizations of switching programs

parent bc79f4c9
No related branches found
No related tags found
No related merge requests found
//============================================================================
// Name : aconvert.cpp
// Author : Martin Zak
//============================================================================
#include <iostream>
#include <string>
#include <cstdlib>
/*
* aconvert
*
* Usage: aconvert [-V] [-t outputtype] [output-options] input
*
* Authors: Martin Zak, Jan Travnicek
*
*/
#include <stdio.h>
#include <unistd.h>
#include <cstdio>
#include <string.h>
#include <err.h>
#include <stdlib.h>
#include <sys/stat.h>
 
#define PROGNAME "aconvert"
#define VERSION "0.0.1"
#define USAGE "Usage: aconvert -t outputtype input";
#define PROGNAME "./aconvert.%s"
#define VERSION "0.0.1"
#define USAGE "aconvert [-V] [-t outputtype] [output-options] input"
 
using namespace std;
int main(int argc, char *argv[]) {
if (argc == 2 && (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version"))) {
char* program_name = argv[0];
char* p = NULL;
if ((p = strrchr(program_name, '/')) != NULL)
program_name = p + 1;
 
int main(int argc, char** argv) {
if(argc == 2 && string(argv[1])=="-v") {
cout << argv[0] << " version " << VERSION << "\n";
cout << USAGE;
cout << endl;
return EXIT_SUCCESS;
printf("%s (%s)\n", program_name, VERSION);
exit(EXIT_SUCCESS);
}
 
string outputType = "";
char* difftype = NULL;
int i, verbose = 0;
 
int i=getopt(argc, argv, "t:");
while(i!=-1) {
switch(i){
while ((i = getopt(argc, argv, "Vt:")) != -1)
switch (i) {
case 'V':
verbose = 1;
break;
case 't':
outputType = string(optarg);
difftype = optarg;
break;
default:
optind--;
break;
goto more;
}
i=getopt(argc, argv, "t:");
}
more:
 
if(optind == argc || outputType == "") {
cerr << USAGE;
cerr << endl;
if (optind == argc || difftype == NULL) {
fprintf(stderr, "Usage: %s\n", USAGE);
return EXIT_FAILURE;
} else {
string programName = "/usr/bin/" + string(PROGNAME) + "." + string(outputType);
execv(programName.c_str(), argv + optind - 1);
perror(programName.c_str());
}
if (verbose)
argv[--optind] = (char*) "-V";
char* program_name = (char*) malloc(sizeof(PROGNAME) - 3 + strlen(difftype) + 1);
sprintf(program_name, PROGNAME, difftype);
argv[--optind] = program_name + 2;
struct stat buffer;
if(stat (program_name, &buffer)) {
program_name += 2;
}
 
execvp(program_name, argv + optind);
perror(program_name);
return EXIT_FAILURE;
}
......@@ -14,7 +14,7 @@
#include <stdlib.h>
#include <sys/stat.h>
 
#define PROGNAME "%s/adiff.%s"
#define PROGNAME "./adiff.%s"
#define VERSION "0.0.1"
#define USAGE "adiff [-V] -t difftype [diff-options] input1 input2"
 
......@@ -51,22 +51,18 @@ more:
return EXIT_FAILURE;
}
char* pwd = get_current_dir_name();
int pwdlen = strlen(pwd) + 1;
if (verbose)
argv[--optind] = (char*) "-V";
char* program_name = (char*) malloc(pwdlen + sizeof(PROGNAME) - 3 + strlen(difftype) + 1);
sprintf(program_name, PROGNAME, pwd, difftype);
char* program_name = (char*) malloc(sizeof(PROGNAME) - 3 + strlen(difftype) + 1);
sprintf(program_name, PROGNAME, difftype);
argv[--optind] = program_name + 2;
struct stat buffer;
if(stat (program_name, &buffer) == 1) {
program_name += pwdlen;
pwdlen = 0;
if(stat (program_name, &buffer)) {
program_name += 2;
}
if (verbose)
argv[--optind] = (char*) "-V";
argv[--optind] = program_name + pwdlen;
 
execvp(program_name, argv + optind);
perror(program_name);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment