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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
** shell_init.c for 42sh in /home/seblu/devel/c/42sh
**
** Made by Seblu
** Login <seblu@epita.fr>
**
** Started on Sun Jul 16 20:11:09 2006 Seblu
** Last update Sun Jul 16 20:14:11 2006 Seblu
*/
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>
#include "42sh.h"
#include "../var/var.h"
#include "../opt/opt.h"
#include "../history/history.h"
static int pwd_isgood();
static void set_default_option(struct s_opt *);
/*!
** Create 42SH structure
**
** @return the new 42sh structure
*/
struct s_shell *sh_init(void)
{
struct s_42sh *new;
char *tmp;
secmalloc(new, sizeof (struct s_42sh));
new->ast = NULL;
new->vars = var_create();
new->funcs = NULL;
new->history = NULL;
secmalloc(new->opt, sizeof (struct s_opt));
set_default_option(new->opt);
if (!pwd_isgood())
{
var_setenv("PWD", (tmp = var_getcwd()), 1);
free(tmp);
}
var_unsetenv("OLDPWD");
new->dot = 0;
new->last_status = 0;
return new;
}
/*!
** Default sh option
**
*/
static void set_default_option(struct s_opt *opt)
{
opt_unset("xpg_echo", opt);
opt_unset("dotglob", opt);
opt_unset("extglob", opt);
opt_unset("nocaseglob", opt);
opt_unset("expand_aliases", opt);
opt_unset("bonus", opt);
opt_unset("readline", opt);
}
/*!
** Check if pwd, if set on the current directory
**
** @return bool satus
*/
static int pwd_isgood()
{
char *pwd;
struct stat s_pwd;
ino_t ino_pwd;
pwd = getenv("PWD");
if (stat(pwd, &s_pwd) == -1)
return 0;
ino_pwd = s_pwd.st_ino;
pwd = var_getcwd();
if (stat(pwd, &s_pwd) == -1)
{
perror("42sh");
exit(1);
}
free(pwd);
return ino_pwd == s_pwd.st_ino;
}