<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <unistd.h>
#ifdef _WIN32
	#include <windows.h>
#else
	#include <gtk/gtk.h>
#endif
#define NWJSMANAGERPATH "/opt/nwjsmanager/nwjsmanager " //TODO: replace with a good location for nwjsmanager and define different paths for Windows and Linux



//Shows a message. Requires command line arguments because they are required by gtk_init.
void showMsg(int argc, char **argv, char *msg, char *appName){
	char *message = malloc((strlen(msg) - 1 + strlen(appName))*sizeof(char));
	sprintf(message, msg, appName);
	#ifdef _WIN32
		MessageBox(NULL, message, appName, MB_ICONERROR);
	#else
		gtk_init(&argc, &argv);
		GtkWidget *msgBox = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Error while launching application");
		gtk_window_set_title(GTK_WINDOW(msgBox), appName);
		gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(msgBox), "%s", message);
        	gtk_dialog_run(GTK_DIALOG(msgBox));
	#endif
	free(message);
}

//Concatenates two strings.
char *concat(char *str1, char *str2){
	char *result = malloc((strlen(str1) + strlen(str2) + 1)*sizeof(char));
	strcpy(result, str1);
	return strcat(result, str2);
}

//Writes to buf #buflen bytes of the executable path
size_t getBinaryPathBuffer(char *buf, int bufLen){
	#ifdef _WIN32
		return GetModuleFileName(NULL, buf, bufLen);
	#else
		return readlink("/proc/self/exe", buf, bufLen);
	#endif
}

//Returns an absolute path to the directory in which the applauncher's executable is stored.
char *getBinaryPath(){
	int len = 0;
	char *binPath = NULL;
	do{ //The loop is to ensure that the string buffer we're allocating (binPath) is large enough to contain all the path (see http://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html for information about pathname maximum size).
		len += 1024;
		if(binPath)
			free(binPath);
		binPath = malloc(len*sizeof(char));
	}while(getBinaryPathBuffer(binPath, len) >= len);
	return binPath;
}

int main(int argc, char **argv){
	char *binPath = getBinaryPath();
	char *binPath_copy = strcpy(malloc((strlen(binPath) + 1)*sizeof(char)), binPath);
	char *binDir = dirname(binPath);
	char *binName = basename(binPath_copy);
	char *dot = strrchr(binName, '.'); //To remove the trailing .exe on Windows
	if(dot && !strcmp(dot, ".exe"))
		binName[strlen(binName) - 4] = 0;
	printf("[DEBUG] Application directory: %s\n[DEBUG] Application name: %s\n", binDir, binName);
	char *packageJsonPath = concat(binDir, "/package.json");
	printf("[DEBUG] Application's package.json path: %s\n", packageJsonPath);
	if(access(packageJsonPath, F_OK) != 0){
		showMsg(argc, argv, "Couldn't find package.json in %s's directory. Please try reinstalling the application.", binName);
		free(packageJsonPath);
		free(binPath);
		free(binPath_copy);
		return 1;
	}
	free(packageJsonPath);
	char *cmd = concat(NWJSMANAGERPATH, "--version");
	if(system(cmd) != 0){
		showMsg(argc, argv, "Couldn't find nwjsmanager, the tool required to launch %s. Please try reinstalling the application.", binName);
		free(cmd);
		free(binPath);
		free(binPath_copy);
		return 1;
	}
	free(cmd);
	cmd = concat(NWJSMANAGERPATH, binDir);
	free(binPath);
	free(binPath_copy);
	//Command line arguments are passed to nwjsmanager.
	int argIndex;
	for(argIndex = 1; argIndex < argc; argIndex++){
		char *arg = concat(" ", argv[argIndex]);
		char *oldCmd = cmd;
		cmd = concat(oldCmd, arg);
		free(oldCmd);
		free(arg);
	}
	printf("[DEBUG] Launch command: %s\n", cmd);
	int result = system(cmd);
	free(cmd);
	return result;
}<commit_msg>applauncher: Improved search path for nwjsmanager.<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <unistd.h>
#ifdef _WIN32
	#include <windows.h>
#else
	#include <gtk/gtk.h>
#endif

#ifdef _WIN32
	#define NWJSMANAGERPATH "C:\\nwjsmanager.exe"
#else
	#define NWJSMANAGERPATH "/opt/nwjsmanager/nwjsmanager" //TODO: replace with a good location for nwjsmanager and define different paths for Windows and Linux
#endif

//Shows a message. Requires command line arguments because they are required by gtk_init.
void showMsg(int argc, char **argv, char *msg, char *appName){
	char *message = malloc((strlen(msg) - 1 + strlen(appName))*sizeof(char));
	sprintf(message, msg, appName);
	#ifdef _WIN32
		MessageBox(NULL, message, appName, MB_ICONERROR);
	#else
		gtk_init(&argc, &argv);
		GtkWidget *msgBox = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Error while launching application");
		gtk_window_set_title(GTK_WINDOW(msgBox), appName);
		gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(msgBox), "%s", message);
        	gtk_dialog_run(GTK_DIALOG(msgBox));
	#endif
	free(message);
}

//Concatenates two strings.
char *concat(char *str1, char *str2){
	char *result = malloc((strlen(str1) + strlen(str2) + 1)*sizeof(char));
	strcpy(result, str1);
	return strcat(result, str2);
}

//Writes to buf #buflen bytes of the executable path
size_t getBinaryPathBuffer(char *buf, int bufLen){
	#ifdef _WIN32
		return GetModuleFileName(NULL, buf, bufLen);
	#else
		return readlink("/proc/self/exe", buf, bufLen);
	#endif
}

//Returns an absolute path to the directory in which the applauncher's executable is stored.
char *getBinaryPath(){
	int len = 0;
	char *binPath = NULL;
	do{ //The loop is to ensure that the string buffer we're allocating (binPath) is large enough to contain all the path (see http://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html for information about pathname maximum size).
		len += 1024;
		if(binPath)
			free(binPath);
		binPath = malloc(len*sizeof(char));
	}while(getBinaryPathBuffer(binPath, len) >= len);
	return binPath;
}

int main(int argc, char **argv){
	char *binPath = getBinaryPath();
	char *binPath_copy = strcpy(malloc((strlen(binPath) + 1)*sizeof(char)), binPath);
	char *binDir = dirname(binPath);
	char *binName = basename(binPath_copy);
	char *dot = strrchr(binName, '.'); //To remove the trailing .exe on Windows
	if(dot && !strcmp(dot, ".exe"))
		binName[strlen(binName) - 4] = 0;
	printf("[DEBUG] Application directory: %s\n[DEBUG] Application name: %s\n", binDir, binName);
	char *packageJsonPath = concat(binDir, "/package.json");
	printf("[DEBUG] Application's package.json path: %s\n", packageJsonPath);
	if(access(packageJsonPath, F_OK) != 0){
		showMsg(argc, argv, "Couldn't find package.json in %s's directory. Please try reinstalling the application.", binName);
		free(packageJsonPath);
		free(binPath);
		free(binPath_copy);
		return 1;
	}
	free(packageJsonPath);
	char *nwjsmanagerpath = getenv("NWJSMANAGERPATH");
	if(!nwjsmanagerpath)
		nwjsmanagerpath = NWJSMANAGERPATH;
	printf("[DEBUG] Path to nwjsmanager: %s\n", nwjsmanagerpath);
	if(access(nwjsmanagerpath, X_OK) != 0){
		showMsg(argc, argv, "Couldn't find nwjsmanager, the tool required to launch %s.\nIf you have set the NWJSMANAGERPATH environment variable, it is set to a file that doesn't exists; unset it to search for nwjsmanager in the default path. If you haven't set the variable and you just want to run the application, please try reinstalling the application.", binName);
		free(binPath);
		free(binPath_copy);
		return 1;
	}
	free(binPath);
	free(binPath_copy);
	return execv(NWJSMANAGERPATH, argv);
}
{'fraction_non_alphanumeric': 0.13370511425462459, 'fraction_numerical': 0.00911316648531012, 'mean_word_length': 5.433070866141732, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1023518', 'n_tokens_mistral': 2722, 'n_tokens_neox': 2563, 'n_words': 718}