-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.c
69 lines (63 loc) · 1.14 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
//libraries
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <signal.h>
#include <dirent.h>
#include <sys/dir.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <locale.h>
#include<sys/utsname.h>
#include "declarations.h"
void prompt()
{
int i;//general use
char dir[1000];//stores getcwd
char pwd[1000];//stores getcwd after removing home
char* user=getenv("USER");//user
char* home=getenv("HOME");//home
//to get linux version
struct utsname buf;
int ret=uname(&buf);
if(ret)
{
printf("Error in the System call\n");
}
char*t1=strtok(buf.version," ");
char*t2=strtok(t1,"-");
t2=strtok(NULL,"-");
//pwd
getcwd(dir,sizeof(dir));
if(strlen(dir)<=strlen(home))
{
strcpy(pwd,dir);
}
else
{
for(i=0;dir[i]==home[i];i++);
int j=i;
while(dir[i]!='\0')
{
pwd[i-j]=dir[i];
i++;
}
pwd[i-j]='\0';
}
//homedir for cd ~
//will be executed only once
if(homeflag==1)
{
strcpy(homedir,dir);
homeflag=0;
}
//print prompt
printf("%s@%s:~%s$ ",user,t2,pwd);
}