MiniDevil As beautiful as a shell
builtins.h File Reference

Shell builtin commands prototypes. More...

#include "structs.h"
+ Include dependency graph for builtins.h:
+ This graph shows which files directly or indirectly include this file:

Detailed Description

Shell builtin commands prototypes.

Definition in file builtins.h.

Functions

int builtin_echo (char **args)
 Implement the echo command. More...
 
int builtin_pwd (void)
 Implement the pwd command. More...
 
int builtin_cd (char **args, t_env **env)
 Implement the cd command. More...
 
int builtin_exit (char **args, t_shell *shell)
 Implement the exit command. More...
 
int builtin_env (char **args, t_env *env)
 Implement the env command. More...
 
int builtin_export (char **args, t_env **env)
 Implement the export command. More...
 
void print_sorted_export (t_env *env)
 Print all env vars sorted in alphabetical order, in declare -x format. More...
 
void export_assign (t_env **env, char *arg)
 Export a variable with a value (export KEY=value) More...
 
void export_append (t_env **env, char *arg)
 Export a variable with append (export KEY+=value) More...
 
void export_no_value (t_env **env, char *key)
 Export a variable with no value (export KEY) More...
 
int builtin_unset (char **args, t_env **env)
 Implement the unset command. More...
 

Function Documentation

◆ builtin_echo()

int builtin_echo ( char **  args)

Implement the echo command.

This command prints arguments separated by spaces and supports:

  • the -n flag and variants like -nnn
  • multiple consecutive -n flags (consumed before print)
Parameters
argsNULL terminated arg array with arg[0] = "echo"
Returns
always 0

Definition at line 52 of file builtin_echo.c.

References is_n_flag().

Referenced by exec_builtin().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ builtin_pwd()

int builtin_pwd ( void  )

Implement the pwd command.

Prints the current working directory to STDOUT

Returns
0 on success and 1 otherwise

Definition at line 24 of file builtin_pwd.c.

Referenced by exec_builtin().

+ Here is the caller graph for this function:

◆ builtin_cd()

int builtin_cd ( char **  args,
t_env **  env 
)

Implement the cd command.

Changes the working directory and supports:

  • no args (HOME)
  • "-" (OLDPWD)
  • "--" separator
  • Error on too many arguments
Parameters
argsNULL terminated argument array with arg[0] = "cd"
envPointer to env list head
Returns
0 on success, 1 on error

Definition at line 101 of file builtin_cd.c.

References get_cd_path(), and update_pwd_vars().

Referenced by exec_builtin().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ builtin_exit()

int builtin_exit ( char **  args,
t_shell shell 
)

Implement the exit command.

  • Prints "exit" in interactive mode
  • With no args it exits with the current exit_status
  • With 1 numeric arg it exits with that code (casted to unsigned char)
  • With non numeric args it exits with 2
  • Too many args = error but does not exit (return 1)
Parameters
argsNULL terminated arg array with arg[0] = "exit"
shellShell context (running flag set to 0 in order to trigger exit)
Returns
Exit code (0-255) or 1 if too many args (and shell keeps running)

Definition at line 29 of file builtin_exit.c.

References t_shell::exit_status, t_shell::interactive, and t_shell::running.

Referenced by exec_builtin().

+ Here is the caller graph for this function:

◆ builtin_env()

int builtin_env ( char **  args,
t_env env 
)

Implement the env command.

Print all environment variables which have a value in "KEY=VALUE" format, one per line and rejecting extra arguments.

Parameters
argsNULL terminated arguments array with arg[0] = "env"
envEnvironment list to print
Returns
0 on success and 1 if extra arguments were given

Definition at line 26 of file builtin_env.c.

References t_env::key, t_env::next, and t_env::value.

Referenced by exec_builtin().

+ Here is the caller graph for this function:

◆ builtin_export()

int builtin_export ( char **  args,
t_env **  env 
)

Implement the export command.

  • With no arguments it prints all env vars sorted by alphabetical order in "declare -x" format
  • With arguments it exports each 1 (support for =, += and no value formats)
Parameters
argsNULL terminated arg array with arg[0] = "export"
envPointer to env list head (modified on export)
Returns
0 on success or highest error code from the processed args

Definition at line 99 of file builtin_export.c.

References export_one(), and print_sorted_export().

Referenced by exec_builtin().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ print_sorted_export()

void print_sorted_export ( t_env env)

Print all env vars sorted in alphabetical order, in declare -x format.

Used by export with no arguments

Parameters
envHead of the env list

Definition at line 120 of file export_utils.c.

References count_env(), env_to_node_array(), print_one_export(), and sort_env_array().

Referenced by builtin_export().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ export_assign()

void export_assign ( t_env **  env,
char *  arg 
)

Export a variable with a value (export KEY=value)

Split the argument at = to extract key and value then call set_env_value() to create or update the variable

Parameters
envPointer to env list head
argFull argument string containing = somewhere

Definition at line 55 of file export_ops.c.

References set_env_value().

Referenced by export_one().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ export_append()

void export_append ( t_env **  env,
char *  arg 
)

Export a variable with append (export KEY+=value)

Splits at += and builds the appended value using get_append_value() then calls set_env_value() to update the variable

Parameters
envPointer to env list head
argFull arg string containing "+="

Definition at line 104 of file export_ops.c.

References get_append_value(), and set_env_value().

Referenced by export_one().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ export_no_value()

void export_no_value ( t_env **  env,
char *  key 
)

Export a variable with no value (export KEY)

  • If the key already exists, do nothing
  • If it does not, create a new node with value=NULL
Parameters
envPointer to env list head
keyVariable name to export

Definition at line 26 of file export_ops.c.

References add_env_node(), t_env::key, t_env::next, and t_env::value.

Referenced by export_one().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ builtin_unset()

int builtin_unset ( char **  args,
t_env **  env 
)

Implement the unset command.

Remove named variables from environment

  • Invalid identifiers are skipped (silently)
  • Options (starting with -) return error 2
Parameters
argsNULL terminated arg array with arg[0] = "unset"
envPointer to env list head (nodes will be removed)
Returns
0 on success and 2 if an invalid option was given

Definition at line 86 of file builtin_unset.c.

References is_valid_unset_identifier(), and remove_env_node().

Referenced by exec_builtin().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Go to the source code of this file.