summaryrefslogtreecommitdiff
path: root/wrapper/outer.c
blob: 4bc2d2f88050511ee1df1a191ddf993fd03bd05c (plain)
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
/* RVS outer.c - A wrapper for $(bindir) to call the main RVS program
 * Copyright (C) 2015  Luke Shumaker
 *
 * This file is part of rvs.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <errno.h>   /* for errno */
#include <error.h>   /* for error(3) */
#include <libintl.h> /* for dgettext(3) */
#include <locale.h>  /* for bindtextdomain(3) and textdomain(3) */
#include <stdio.h>   /* for asprintf(3) */
#include <stdlib.h>  /* for getenv(3), calloc(3) */
#include <string.h>  /* for mempcy(3) */
#include <unistd.h>  /* for execv(3) */

#include "config.h"
#define _ gettext

#define EXIT_FAILURE_OOM 126
#define EXIT_FAILURE_EXEC 127

int
main(int argc, char *argv[]) {
	bindtextdomain(pkgtextdomain, localedir);
	textdomain(pkgtextdomain);

	unsetenv("ENV");
	unsetenv("BASH_ENV");

	const char *varname = PACKAGE_UPPER "_EXEC_PATH";

	char *exec_path = getenv(varname);
	if (!exec_path)
		exec_path = pkglibexecdir;

	char *exec_file = NULL;
	if (asprintf(&exec_file, "%s/" PACKAGE, exec_path) < 0)
		error(EXIT_FAILURE_OOM, errno,
		      _("Could not allocate memory for string"));

	char **args = calloc(argc+2, sizeof(char*));
	if (!args)
		error(EXIT_FAILURE_OOM, errno,
		      _("Could not allocate cleared memory"));
	args[0] = exec_file;
	memcpy(&args[1], argv, sizeof(char*) * argc);
	
	execv(exec_file, args);
	error(EXIT_FAILURE_EXEC, errno, _("Could not execute: %s"), exec_file);
	return EXIT_FAILURE_EXEC;
}