MiniDevil As beautiful as a shell
tilde_expand.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* tilde_expand.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: baelgadi <baelgadi@student.42.fr> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2026/02/07 16:48:52 by zotaj-di #+# #+# */
9 /* Updated: 2026/03/04 04:44:03 by baelgadi ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include "token.h"
14 #include "env.h"
15 #include "libft.h"
16 
27 static char *expand_tilde(char *str, t_env *env_list, t_quote_type quote_type)
28 {
29  char *home;
30  char *result;
31 
32  if (quote_type != QUOTE_NONE)
33  return (ft_strdup(str));
34  if (!str || str[0] != '~')
35  return (ft_strdup(str));
36  if (str[1] && str[1] != '/')
37  return (ft_strdup(str));
38  home = get_env_value(env_list, "HOME");
39  if (!home)
40  return (ft_strdup(str));
41  if (!str[1])
42  return (ft_strdup(home));
43  result = ft_strjoin(home, str + 1);
44  return (result);
45 }
46 
60 char *expand_full(char *str, t_env *env, t_quote_type qt, int exit_status)
61 {
62  char *tilde_exp;
63  char *result;
64 
65  tilde_exp = expand_tilde(str, env, qt);
66  if (!tilde_exp)
67  return (NULL);
68  result = expand_variables(tilde_exp, env, qt, exit_status);
69  free(tilde_exp);
70  return (result);
71 }
Environment variable management prototypes.
char * get_env_value(t_env *env_list, char *key)
Look up an environment variable's value by its key.
char * expand_variables(char *str, t_env *env_list, t_quote_type quote_type, int exit_status)
Expand all $VARIABLE in a string.
Definition: expander.c:111
t_quote_type
Quote context of a token.
Definition: structs.h:41
@ QUOTE_NONE
Definition: structs.h:42
Environment variable (doubly linked list)
Definition: structs.h:87
char * expand_full(char *str, t_env *env, t_quote_type qt, int exit_status)
Handles variable expansion and tilde expansion.
Definition: tilde_expand.c:60
static char * expand_tilde(char *str, t_env *env_list, t_quote_type quote_type)
Expand leading ~ to $HOME.
Definition: tilde_expand.c:27
Lexer, tokenizer, expander, and quote handling prototypes.