loop #2

Merged
cromer merged 11 commits from loop into master 2021-06-19 23:03:40 -04:00
2 changed files with 20 additions and 3 deletions
Showing only changes of commit 7c277d99bd - Show all commits

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);
}