/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   exit.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: sede-san <sede-san@student.42madrid.com    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2025/10/30 01:20:48 by sede-san          #+#    #+#             */
/*   Updated: 2026/02/08 21:16:18 by sede-san         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "builtins.h"
#include <stdint.h>

uint8_t	builtin_exit(
	t_command cmd,
	t_minishell *msh
)
{
	uint8_t	exit_status;
	
	ft_eputendl("exit");
	if (cmd.argc == 1)
		exit_status = msh->exit_status;
	else if (!ft_strisnum(cmd.argv[1]))
	{
		ft_eprintf(
			"minishell: exit: %s: numeric argument required\n",
			cmd.argv[1]);
		return (2);
	}
	else if (cmd.argc > 2)
	{
		ft_eputendl("exit: too many arguments");
		return (2);
	}
	else
		exit_status = (uint8_t)ft_atol(cmd.argv[1]);

	printf("builtin_exit: exit_status=%d\n", exit_status); // Debug print

	msh->exit = 1;
	msh->exit_status = exit_status;
	return (exit_status);
}
