forked from yazgiraldoa/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.c
56 lines (49 loc) · 1.06 KB
/
prompt.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
#include "shell.h"
/**
* prompt_no_interactive - Function gets a string from command line.
* @name_file: name of the executable file.
* Return: char pointer that contains the string.
*/
char *prompt_no_interactive(char *name_file)
{
char *line = NULL;
int input = 0;
size_t line_size = 0;
input = getline(&line, &line_size, stdin);
if (input == -1)
{
if (errno == EINVAL || errno == ENOMEM)
{
perror(name_file);
}
free(line);
return (NULL);
}
return (line);
}
/**
* prompt_interactive - Function prints a prompt and
* gets a string from command line.
* @name_file: name of the executable file.
* Return: char pointer that contains the string.
*/
char *prompt_interactive(char *name_file)
{
char *prompt = "cuchufli% ", *line = NULL;
int input = 0;
size_t line_size = 0;
write(STDOUT_FILENO, prompt, _strlen(prompt));
input = getline(&line, &line_size, stdin);
if (input == -1)
{
if (errno == EINVAL || errno == ENOMEM)
{
perror(name_file);
}
write(1, "\n", 1);
free(line);
line_size = 0;
return (NULL);
}
return (line);
}