-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt.c
78 lines (73 loc) · 1.38 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "header.h"
// prints prompt
void printprompt()
{
char hostname[HOST_NAME_MAXX+1];
char login[LOGIN_NAME_MAXX];
// null-terminated regardless
hostname[HOST_NAME_MAXX] = 0;
login[LOGIN_NAME_MAXX-1] = 0;
gethostname( hostname, HOST_NAME_MAXX );
getlogin_r( login, LOGIN_NAME_MAXX );
// <username>@<hostname>:)
fprintf( stdout, "%s@%s:", login, hostname);
int i,l1,l2,length;
char array[1000]="";
char tilda[2]={'~','\0'};
if(getcwd(array, sizeof(array))!=NULL)
{
length=strcmp(home,array);
if(length==0)
printf("~>");
else if(length>0)
printf("%s>",array);
else
{
l1=strlen(home);
l2=strlen(array);
printf("~");
for(i=l1;i<l2;i++)
printf("%c",array[i]);
printf(">");
}
}
fflush( stdout );
}
//get input from the user
char *get_input(void)
{
int c,position;
int pos=0;
int max_input;
max_input=MAXX_INPUT;
char *buffer=malloc(sizeof(char)*MAXX_INPUT);
if(!buffer)
{
fprintf(stderr, "myshell: allocation error\n");
exit(EXIT_FAILURE);
}
while(1)
{
// if(backexit!=1)
c=getchar();
if (c==EOF|| c=='\n')
{
buffer[pos]='\0';
return buffer;
}
else
buffer[pos]=c;
pos++;
if(pos>=max_input)
{
max_input=max_input*2;
// reallocating the buffer for more input
buffer=realloc(buffer, max_input);
if(!buffer)
{
fprintf(stderr, "myshell: allocation error\n");
exit(EXIT_FAILURE);
}
}
}
}