summaryrefslogtreecommitdiff
path: root/lib/libc_r/TEST/test_execve.c
blob: 409b37bfc1b5fc9844a7edf0d5351d97f846fcbc (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
/* ==== test_execve.c ============================================================
 * Copyright (c) 1994 by Chris Provenzano, proven@athena.mit.edu
 *
 * Description : Test execve() and dup2() calls.
 *
 *  1.00 94/04/29 proven
 *      -Started coding this file.
 */

#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <err.h>
#include "test.h"

extern char **environ;
char *argv[] = {
	"/bin/echo",
	"This message should be displayed after the execve system call",
	NULL
};

char * should_succeed = "This line should be displayed\n";
char * should_fail = "Error: This line should NOT be displayed\n";

int
main()
{
	int fd;

	printf("This is the first message\n");
	if (isatty(1)) {
		char *ttynm;

		ttynm = ttyname(1);
		printf("tty is %s\n", ttynm);
		if ((fd = open(ttynm, O_RDWR)) < OK)
			err(1, "%s", ttynm);
	} else
		errx(1, "stdout not a tty");

	printf("This output is necessary to set the stdout fd to NONBLOCKING\n");

	/* do a dup2 */
	dup2(fd, 1);
	write(1, should_succeed, (size_t)strlen(should_succeed));
	_thread_sys_write(1, should_fail, strlen(should_fail));

	if (execve(argv[0], argv, environ) < OK) 
		err(1, "execve");

	PANIC();
}