(perf): conditions inutiles
This commit is contained in:
parent
178dd8bffc
commit
e68598e39a
@ -13,48 +13,35 @@ static char* get_prompt(lst** env)
|
|||||||
int size;
|
int size;
|
||||||
char cwd[PATH_MAX];
|
char cwd[PATH_MAX];
|
||||||
if (getcwd(cwd, sizeof(cwd)) == NULL)
|
if (getcwd(cwd, sizeof(cwd)) == NULL)
|
||||||
{
|
|
||||||
dprintf(2, "chemin inconnu");
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
char hostname_buff[BUFSIZ];
|
char hostname_buff[BUFSIZ];
|
||||||
int file_host = open("/etc/hostname", O_RDONLY);
|
int file_host = open("/etc/hostname", O_RDONLY);
|
||||||
if (file_host == -1)
|
if (file_host == -1)
|
||||||
strcpy(hostname_buff, "");
|
strcpy(hostname_buff, "");
|
||||||
read(file_host, hostname_buff, BUFSIZ);
|
read(file_host, hostname_buff, BUFSIZ);
|
||||||
close(file_host);
|
close(file_host);
|
||||||
char* contains = strchr(hostname_buff, '\n');
|
char* first_line_return = strchr(hostname_buff, '\n');
|
||||||
if (contains)
|
if (first_line_return)
|
||||||
{
|
{
|
||||||
*contains = '\0';
|
*first_line_return = '\0';
|
||||||
}
|
}
|
||||||
char* user = get_env_variable(env, "USER");
|
char* user = get_env_variable(env, "USER");
|
||||||
char* home = get_env_variable(env, "HOME");
|
char* home = get_env_variable(env, "HOME");
|
||||||
if (user == NULL)
|
if (strncmp(home, cwd, strlen(home)) == 0)
|
||||||
user = "";
|
|
||||||
else if (strncmp(home, cwd, strlen(home)) == 0)
|
|
||||||
{
|
{
|
||||||
strcpy(cwd, "~");
|
strcpy(cwd, "~");
|
||||||
strcat(cwd, cwd + strlen(home));
|
strcat(cwd, cwd + strlen(home));
|
||||||
}
|
}
|
||||||
|
size = strlen(user) + strlen(hostname_buff) + strlen(cwd) + 9;
|
||||||
|
out = malloc(size*sizeof(char));
|
||||||
|
if(out == NULL)
|
||||||
|
return NULL;
|
||||||
|
sprintf(out, "[%s@%s] > %s ",user, hostname_buff, cwd);
|
||||||
if (strcmp(user, "root") == 0)
|
if (strcmp(user, "root") == 0)
|
||||||
{
|
strcat(out, "# ");
|
||||||
size = strlen(user) + strlen(hostname_buff) + strlen(cwd) + 9;
|
|
||||||
out = malloc(size*sizeof(char));
|
|
||||||
if(out == NULL)
|
|
||||||
return NULL;
|
|
||||||
sprintf(out, "[%s@%s] > %s # ",user, hostname_buff, cwd);
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
strcat(out, "$ ");
|
||||||
size = strlen(user) + strlen(hostname_buff) + strlen(cwd) + 9;
|
return out;
|
||||||
out = malloc(size*sizeof(char));
|
|
||||||
if(out == NULL)
|
|
||||||
return NULL;
|
|
||||||
sprintf(out, "[%s@%s] > %s $ ",user, hostname_buff, cwd);
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *get_user_input(lst** env)
|
char *get_user_input(lst** env)
|
||||||
|
Loading…
Reference in New Issue
Block a user