Newer
Older
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
/*
** builtin_exit.c for 42sh
**
** Made by Seblu
** Login <seblu@epita.fr>
**
** Started on Sun Nov 12 16:48:50 2006 Seblu
** Last update Sun Nov 12 16:49:42 2006 Seblu
*/
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include "builtin.h"
#include "../main/42sh.h"
/*!
** Terminate program
**
** @param argv vector to argument
** @param shopt option structure of shell
**
** @return
*/
int builtin_exit(char *argv[])
{
long i;
char *endptr;
assert(argv && argv[0]);
if (!argv[1])
{
/* if (isatty(fileno(stdin))) */
/* printf("exit\n"); */
i = sh->last_status;
sh_destroy(sh);
exit(i);
}
if (argv[2])
{
fprintf(stderr, "42sh : exit : too many arguments\n");
fflush(stderr);
return 1;
}
i = strtol(argv[1], &endptr, 10);
/* if (isatty(fileno(stdin))) */
/* printf("exit\n"); */
if (*endptr)
fprintf(stderr, "42sh: exit: %s: numeric argument required\n", argv[1]);
sh_destroy(sh);
exit((!*endptr) ? i : 255);
return 1;
}