aboutsummaryrefslogtreecommitdiff
path: root/src/builtins_part_one.c
blob: d8bdaa97845c225892c576fbfd3578edf8120cbd (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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   builtins_part_one.c                                :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/08/09 17:01:16 by chuhlig           #+#    #+#             */
/*   Updated: 2025/01/14 16:51:05 by chuhlig          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "env.h"

int	echo(char **av)
{
	int	i;
	int	f;

	i = 1;
	f = 1;
	if (av[1] == NULL || av[1][0] == '\0')
	{
		write(1, "\n", 1);
		return (1);
	}
	if (ft_strncmp(av[1], "-n", 3) == 0)
	{
		i++;
		f = 0;
	}
	while (av[i])
	{
		write(1, av[i], ft_strlen(av[i]));
		i++;
		if (av[i])
			write(1, " ", 1);
	}
	if (f)
		write(1, "\n", 1);
	return (0);
}

void	exit_shell(t_env **env, int exit_status)
{
	free_envlst(env);
	exit(exit_status);
}

int	builtin_exit(char **args, t_env **env)
{
	int	exit_status;

	if (args[1])
		exit_status = ft_atoi(args[1]);
	else
		exit_status = 0;
	exit_shell(env, exit_status);
	return (exit_status);
}

int	unset(char **av, t_env **env)
{
	t_env	*current;
	t_env	*prev;
	int		i;

	i = 0;
	while (av[++i])
	{
		current = *env;
		prev = NULL;
		while (current)
		{
			if (ft_strcmp(current->name, av[i]) == 0)
			{
				if (prev)
					prev->next = current->next;
				else
					*env = current->next;
				free_env_node(current);
				break ;
			}
			prev = current;
			current = current->next;
		}
	}
	return (0);
}

t_env	*check_existing(t_env *env, char *av)
{
	while (env)
	{
		if (ft_strcmp(env->name, av) == 0)
			return (env);
		env = env->next;
	}
	return (NULL);
}

int	export(char **av, t_env **env)
{
	char	*tmp;
	t_env	*current;
	int		i;

	current = NULL;
	i = 0;
	while (av[++i])
	{
		if ((ft_strchr(av[i], '=')))
		{
			tmp = ft_strchr(av[i], '=');
			*tmp = '\0';
			current = check_existing(*env, av[i]);
			if (current)
				free(current->value);
			else
			{
				current = env_new(ft_strdup(av[i]));
				current->next = *env;
				*env = current;
			}
			current->value = ft_strdup(tmp + 1);
		}
	}
	return (0);
}