-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_1.c
58 lines (53 loc) · 2.14 KB
/
init_1.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init_1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ngasco <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/14 11:22:29 by ngasco #+# #+# */
/* Updated: 2022/03/14 11:22:30 by ngasco ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
/* Initializing structure containing information on prompt */
void ft_init_prompt_data(t_cdata *t_cdata)
{
char *username;
char *hostname;
t_cdata->t_pdata = (t_pdata *)malloc(sizeof(t_pdata));
memset(t_cdata->t_pdata, 0, sizeof(t_pdata));
if (!getenv("USER"))
t_cdata->t_pdata->username = ft_strdup("username");
else
t_cdata->t_pdata->username = ft_strdup(getenv("USER"));
if (!getenv("HOSTNAME"))
t_cdata->t_pdata->hostname = ft_strdup("minishell");
else
t_cdata->t_pdata->hostname = ft_strdup(getenv("HOSTNAME"));
username = t_cdata->t_pdata->username;
hostname = t_cdata->t_pdata->hostname;
t_cdata->t_pdata->prompt_text = ft_create_prompt_text(username, hostname);
t_cdata->t_pdata->prompt_nl_text = ft_strdup("> ");
}
/* The text shown when prompting user for input, e.g. username@hostname */
char *ft_create_prompt_text(char *username, char *hostname)
{
int i;
int len1;
int len2;
char *result;
i = -1;
len1 = ft_strlen(username);
len2 = ft_strlen(hostname);
result = (char *)malloc(sizeof(char) * (len1 + len2 + 4));
while (++i < len1)
result[i] = username[i];
result[i] = '@';
while (++i < (len1 + len2 + 1))
result[i] = hostname[i - len1 - 1];
result[i] = ':';
result[i + 1] = ' ';
result[i + 2] = '\0';
return (result);
}