From 36f356857760cb63b1c527b5c179dba8082bc0f5 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Wed, 29 Jan 2014 11:24:09 +0100
Subject: [PATCH] generalizations of switching programs

---
 aconvert/src/aconvert.cpp | 90 +++++++++++++++++++++++----------------
 adiff/src/adiff.cpp       | 22 ++++------
 2 files changed, 62 insertions(+), 50 deletions(-)

diff --git a/aconvert/src/aconvert.cpp b/aconvert/src/aconvert.cpp
index c6500e7395..501cbdd480 100644
--- a/aconvert/src/aconvert.cpp
+++ b/aconvert/src/aconvert.cpp
@@ -1,54 +1,70 @@
-//============================================================================
-// 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;
 }
diff --git a/adiff/src/adiff.cpp b/adiff/src/adiff.cpp
index 6fb58b6361..31614290c0 100644
--- a/adiff/src/adiff.cpp
+++ b/adiff/src/adiff.cpp
@@ -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);
-- 
GitLab