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
#define _MYSHELLIN_LOOP
void remove_new_line(char *line);
char *get_working_directory();
void print_input_line();
void loop();
#endif

View File

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