aboutsummaryrefslogtreecommitdiffstats
path: root/guts/execle.c
blob: 13fbfb95f8ab8fc85325e1a98234d702e2ba0c30 (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
/* 
 * Copyright (c) 2010 Wind River Systems; see
 * guts/COPYRIGHT for information.
 *
 * static int
 * wrap_execle(const char *path, const char *arg, va_list ap) {
 *	int rc = -1;
 */

  size_t i = 0;
  size_t alloc_size = 256;
  const char **argv = malloc(sizeof (const char *) * alloc_size);
  char *const *envp;
  if (!argv) {
	pseudo_debug(1, "execle failed: couldn't allocate memory for %lu arguments\n",
		(unsigned long) alloc_size);
	errno = ENOMEM;
	return -1;
  }

  argv[i++] = arg;

  while (argv[i-1]) {
	argv[i++] = va_arg (ap, const char *);
	if ( i > alloc_size - 1 ) {
		alloc_size = alloc_size + 256;
		argv = realloc(argv, sizeof (const char *) * alloc_size);
		if (!argv) {
			pseudo_debug(1, "execle failed: couldn't allocate memory for %lu arguments\n",
				(unsigned long) alloc_size);
			errno = ENOMEM;
			return -1;
		}
	}
  }
  envp = va_arg (ap, char *const *);

  rc = wrap_execve (path, (char *const *) argv, envp);

  free (argv);

/*	return rc;
 * }
 */