get current working directory for the console line

This commit is contained in:
Chris Cromer 2021-06-19 20:52:42 -04:00
parent 21448cee37
commit 7c277d99bd
2 changed files with 20 additions and 3 deletions

View File

@ -16,6 +16,7 @@
#ifndef _MYSHELLIN_LOOP #ifndef _MYSHELLIN_LOOP
#define _MYSHELLIN_LOOP #define _MYSHELLIN_LOOP
void remove_new_line(char *line); void remove_new_line(char *line);
char *get_working_directory();
void print_input_line(); void print_input_line();
void loop(); void loop();
#endif #endif

View File

@ -15,19 +15,36 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#include <errno.h> #include <errno.h>
#include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h>
#include "user.h" #include "user.h"
void remove_new_line(char* line) { void remove_new_line(char* line) {
line[strcspn(line, "\n")] = 0; line[strcspn(line, "\n")] = 0;
} }
char *get_working_directory() {
char *cwd = NULL;
cwd = getcwd(NULL, PATH_MAX);
if (cwd != NULL) {
return cwd;
}
else {
perror("getcwd() error: ");
exit(EXIT_FAILURE);
}
}
void print_input_line() { void print_input_line() {
char *name = get_user(); char *name = get_user();
printf("%s $ ", name); char *cwd = get_working_directory();
printf("%s:%s $ ", name, cwd);
free(name);
free(cwd);
} }
void loop() { void loop() {
@ -44,8 +61,7 @@ void loop() {
break; break;
} }
else { else {
perror("Error: "); perror("getline() error: ");
printf("\n");
if (line != NULL) { if (line != NULL) {
free(line); free(line);
} }