add: get_user_input
This commit is contained in:
		
							parent
							
								
									38c27fd583
								
							
						
					
					
						commit
						cc8cb37697
					
				
							
								
								
									
										2
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								Makefile
									
									
									
									
									
								
							| @ -7,7 +7,7 @@ OBJ_DIR = obj | ||||
| LIB_DIR = lib | ||||
| SOURCES = $(shell find $(SRC_DIR) -type f -name '*.c') | ||||
| OBJECTS = $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SOURCES)) | ||||
| LIBRARIES = lib/bozolib/bozolib.a | ||||
| LIBRARIES = lib/bozolib/bozolib.a -lreadline | ||||
| DEPS = $(OBJECTS:.o=.d) | ||||
| 
 | ||||
| all: $(NAME) $(LIBRARIES) | ||||
|  | ||||
							
								
								
									
										54
									
								
								src/input/input.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								src/input/input.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,54 @@ | ||||
| #include <stdio.h> | ||||
| #include <readline/readline.h> | ||||
| #include <readline/history.h> | ||||
| #include <limits.h> | ||||
| #include <unistd.h> | ||||
| #include "../../lib/bozolib/bozolib.h" | ||||
| #include "../env/env.h" | ||||
| 
 | ||||
| static char* get_prompt(lst** env) | ||||
| { | ||||
| 	char* out; | ||||
| 	int size; | ||||
| 	char cwd[PATH_MAX]; | ||||
| 	if (getcwd(cwd, sizeof(cwd)) == NULL) | ||||
| 	{ | ||||
| 		dprintf(2, "chemin inconnu"); | ||||
| 		return NULL; | ||||
| 	}  | ||||
| 	char* computer = get_env_variable(env, "COMPUTER"); | ||||
| 	char* user = get_env_variable(env, "USER"); | ||||
| 	char* home = get_env_variable(env, "HOME"); | ||||
| 	if (computer == NULL) | ||||
| 		computer = ""; | ||||
| 	if (user == NULL) | ||||
| 		user = ""; | ||||
| 	else if (strncmp(home, cwd, strlen(home)) == 0) | ||||
| 	{ | ||||
| 		strcpy(cwd, "~"); | ||||
| 		strcat(cwd, cwd + strlen(home)); | ||||
| 	} | ||||
| 	size = snprintf(NULL, 0, "%s@%s>%s $ ",user, computer, cwd) + 10; | ||||
| 	out = malloc(size*sizeof(char)); | ||||
| 	if(out == NULL) | ||||
| 		return NULL; | ||||
| 	sprintf(out, "%s@%s > %s $ ",user, computer, cwd); | ||||
| 	return out; | ||||
| } | ||||
| 
 | ||||
| char *get_user_input(lst** env) | ||||
| { | ||||
| 	char *prompt; | ||||
| 	char *input; | ||||
| 	 | ||||
| 	prompt = get_prompt(env); | ||||
| 	if (prompt == NULL) | ||||
| 		return NULL; | ||||
| 	input = readline(prompt); | ||||
| 	if (input != NULL && str_contain_only(input, "\t ") == 0) | ||||
| 		add_history(input); | ||||
| 	free(prompt); | ||||
| 	if (input == NULL) | ||||
| 		printf("exit"); | ||||
| 	return (input); | ||||
| } | ||||
| @ -1,4 +1,5 @@ | ||||
| #pragma once | ||||
| #include "../../lib/bozolib/bozolib.h" | ||||
| char* get_prompt(lst** env); | ||||
| 
 | ||||
| #include "../../lib/bozolib/bozolib.h" | ||||
| 
 | ||||
| char *get_user_input(lst** env); | ||||
| @ -1,38 +0,0 @@ | ||||
| #include <stdio.h> | ||||
| #include <limits.h> | ||||
| #include <unistd.h> | ||||
| #include "../../lib/bozolib/bozolib.h" | ||||
| #include "../env/env.h" | ||||
| 
 | ||||
| char* get_prompt(lst** env) | ||||
| { | ||||
|   char* out; | ||||
|   int size; | ||||
|   char cwd[PATH_MAX]; | ||||
|   if (getcwd(cwd, sizeof(cwd)) == NULL) | ||||
|   { | ||||
|     dprintf(2, "chemin inconnu"); | ||||
|     return NULL; | ||||
|   }  | ||||
|   char* computer = get_env_variable(env, "COMPUTER"); | ||||
|   char* user = get_env_variable(env, "USER"); | ||||
|   char* home = get_env_variable(env, "HOME"); | ||||
|   if (computer == NULL) | ||||
|     computer = ""; | ||||
|   if (user == NULL) | ||||
|     user = ""; | ||||
|   else if (strncmp(home, cwd, strlen(home)) == 0) | ||||
|   { | ||||
|     strcpy(cwd, "~"); | ||||
|     strcat(cwd, cwd + strlen(home)); | ||||
|   } | ||||
|   size = snprintf(NULL, 0, "%s@%s>%s $",user, computer, cwd); | ||||
|   out = malloc(size*sizeof(char)); | ||||
|   if(out == NULL) | ||||
|   { | ||||
|     return NULL; | ||||
|   } | ||||
|   sprintf(out, "%s@%s > %s $",user, computer, cwd); | ||||
|   | ||||
|   return out; | ||||
| } | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 starnakin
						starnakin