refactor the console line code
This commit is contained in:
parent
990a1d2b3e
commit
a7cf487631
2
Makefile
2
Makefile
@ -2,7 +2,7 @@ CC=clang
|
||||
CFLAGS=-Wall -Isrc/include -DDEBUG -g -std=c17
|
||||
LDFLAGS=
|
||||
FILENAME=myshellin
|
||||
SRC=src/myshellin.c src/loop.c src/user.c
|
||||
SRC=src/myshellin.c src/loop.c src/console_line.c
|
||||
OBJ=$(SRC:.c=.o)
|
||||
|
||||
all: myshellin
|
||||
|
@ -13,8 +13,12 @@
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#define _GNU_SOURCE
|
||||
#include <limits.h>
|
||||
#include <pwd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
char *get_user() {
|
||||
@ -22,3 +26,23 @@ char *get_user() {
|
||||
pass = getpwuid(getuid());
|
||||
return pass->pw_name;
|
||||
}
|
||||
|
||||
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();
|
||||
char *cwd = get_working_directory();
|
||||
printf("%s:%s $ ", name, cwd);
|
||||
free(name);
|
||||
free(cwd);
|
||||
}
|
@ -13,7 +13,9 @@
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef _MYSHELLIN_USER
|
||||
#define _MYSHELLIN_USER
|
||||
#ifndef _MYSHELLIN_CONSOLE_LINE
|
||||
#define _MYSHELLIN_CONSOLE_LINE
|
||||
char *get_user();
|
||||
char *get_working_directory();
|
||||
void print_input_line();
|
||||
#endif
|
@ -16,7 +16,5 @@
|
||||
#ifndef _MYSHELLIN_LOOP
|
||||
#define _MYSHELLIN_LOOP
|
||||
void remove_new_line(char *line);
|
||||
char *get_working_directory();
|
||||
void print_input_line();
|
||||
void loop();
|
||||
#endif
|
||||
|
34
src/loop.c
34
src/loop.c
@ -14,39 +14,15 @@
|
||||
*/
|
||||
|
||||
#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"
|
||||
#include "console_line.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();
|
||||
char *cwd = get_working_directory();
|
||||
printf("%s:%s $ ", name, cwd);
|
||||
free(name);
|
||||
free(cwd);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
size_t buffer_size = 0;
|
||||
char *line = NULL;
|
||||
@ -57,7 +33,9 @@ void loop() {
|
||||
if (feof(stdin)) {
|
||||
// the stdin was closed, this usually happens for CTRL-D
|
||||
printf("\n");
|
||||
free(line);
|
||||
if (line != NULL) {
|
||||
free(line);
|
||||
}
|
||||
break;
|
||||
}
|
||||
else {
|
||||
@ -72,7 +50,9 @@ void loop() {
|
||||
remove_new_line(line);
|
||||
|
||||
if (strcmp(line, "quit") == 0) {
|
||||
free(line);
|
||||
if (line != NULL) {
|
||||
free(line);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user