-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompt.c
More file actions
34 lines (30 loc) · 837 Bytes
/
prompt.c
File metadata and controls
34 lines (30 loc) · 837 Bytes
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
#include "prompt.h"
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
extern char currDir[256];
extern char homeDir[256];
// Assuming 128 to be max length of hostname
char *username, hostname[128];
void getUserHostNames() {
struct passwd *temp = getpwuid(getuid());
username = temp->pw_name;
gethostname(hostname, 128);
}
void storePromptString(char *store) {
getcwd(currDir, 256);
char *toprintDir = (char *)(malloc(256));
char *copy_for_free = toprintDir;
strcpy(toprintDir, currDir);
if (strncmp(currDir, homeDir, strlen(homeDir)) == 0) {
toprintDir += strlen(homeDir) - 1;
toprintDir[0] = '~';
}
if (username == NULL)
getUserHostNames();
sprintf(store, "<%s@%s:%s> ", username, hostname, toprintDir);
free(copy_for_free);
}