mirror of
				https://gitlab.com/klmp200/fish.git
				synced 2025-11-04 11:13:04 +00:00 
			
		
		
		
	TESTED+0Leak+Sli HAPPY
This commit is contained in:
		@@ -47,7 +47,7 @@ include_directories(
 | 
			
		||||
 | 
			
		||||
set(CMAKE_C_STANDARD 99)
 | 
			
		||||
set(CMAKE_C_FLAGS "-Wall -Werror -pedantic -fpic -Wextra -Wshadow")
 | 
			
		||||
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FALGS} -g")
 | 
			
		||||
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -g")
 | 
			
		||||
 | 
			
		||||
set(SOURCE_FILES fish_shell/main.c fish_shell/fish_types.h fish_shell/fish_core.h fish_shell/fish_core.c fish_shell/fish_commands.c fish_shell/fish_commands.h fish_shell/fish_globbing.c fish_shell/fish_globbing.h fish_shell/fish_utils.c fish_shell/fish_utils.h fish_shell/fish_settings.c fish_shell/fish_settings.h)
 | 
			
		||||
add_executable(fish ${SOURCE_FILES})
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <pwd.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include "fish_settings.h"
 | 
			
		||||
#include "fish_core.h"
 | 
			
		||||
 | 
			
		||||
Settings *getSettings() {
 | 
			
		||||
@@ -14,51 +15,25 @@ Settings *getSettings() {
 | 
			
		||||
 | 
			
		||||
	uid_t uid;
 | 
			
		||||
	struct passwd *user;
 | 
			
		||||
	char* file = (char*)malloc(sizeof(char*)*50);
 | 
			
		||||
	char* filename = NULL;
 | 
			
		||||
	uid = getuid();
 | 
			
		||||
	user = getpwuid(uid);
 | 
			
		||||
 | 
			
		||||
	file = "\0";
 | 
			
		||||
	file = strcat(user->pw_dir, "/.fishrc");
 | 
			
		||||
	s->user1 = user;
 | 
			
		||||
	s->PS1 = extractVariable(file, "PS1");
 | 
			
		||||
	s->PS2 = extractVariable(file,"PS2");
 | 
			
		||||
	filename = (char*) malloc(sizeof(char*)*(strlen(user->pw_dir) + FISH_RC_FILE_SIZE + 1));
 | 
			
		||||
 | 
			
		||||
	if (filename == NULL) crash();
 | 
			
		||||
 | 
			
		||||
	filename[0] = '\0';
 | 
			
		||||
	filename = strcat(filename, user->pw_dir);
 | 
			
		||||
	filename = strcat(filename, (char*) FISH_RC_FILE);
 | 
			
		||||
	s->passwd = user;
 | 
			
		||||
	s->PS1 = extractVariable(filename, (char*) "PS1");
 | 
			
		||||
	s->PS2 = extractVariable(filename, (char*) "PS2");
 | 
			
		||||
 | 
			
		||||
	free(filename);
 | 
			
		||||
	return s;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// char* convertPS(char* s){
 | 
			
		||||
// 	if(s != NULL){
 | 
			
		||||
// 		printf("Quelque chose à convertir\n");
 | 
			
		||||
// 		int i=0;
 | 
			
		||||
// 		char* PS = (char*) malloc(sizeof(char*)* 20);
 | 
			
		||||
// 		PS[0] = '\0';
 | 
			
		||||
// 		while(s[i] != '\0'){
 | 
			
		||||
// 			if(s[i]=='\\'){
 | 
			
		||||
// 			 	i++;
 | 
			
		||||
// 			 	 switch (s[i]){
 | 
			
		||||
// 			 	 case '\\' : PS[i-1] = '\\';
 | 
			
		||||
// 			 	 case 'u' : PS[i-1] = '?';
 | 
			
		||||
// 			 	 default : PS[i] = s[i];
 | 
			
		||||
					
 | 
			
		||||
// 			 	}
 | 
			
		||||
// 			 	i++;
 | 
			
		||||
// 			 }else{
 | 
			
		||||
// 				PS[i] = s[i];
 | 
			
		||||
// 				i++;
 | 
			
		||||
				
 | 
			
		||||
// 			}
 | 
			
		||||
// 		printf("Prompt %i: %s ! %s traiter: %c donne: %c \n",i,PS, s, s[i-1], PS[i-1] );
 | 
			
		||||
			
 | 
			
		||||
// 		}
 | 
			
		||||
// 		return PS;
 | 
			
		||||
// 		PS = NULL;
 | 
			
		||||
// 		free(PS);
 | 
			
		||||
// 	}else{
 | 
			
		||||
// 		printf("Void argument, try again Ame\n");
 | 
			
		||||
// 		return strdup(">");
 | 
			
		||||
// 	}
 | 
			
		||||
// }
 | 
			
		||||
 | 
			
		||||
void freeSettings(Settings *settings){
 | 
			
		||||
	if (settings != NULL){
 | 
			
		||||
		free(settings->PS1);
 | 
			
		||||
@@ -69,43 +44,36 @@ void freeSettings(Settings *settings){
 | 
			
		||||
 | 
			
		||||
char* extractVariable(char* filename, char* var){
 | 
			
		||||
	FILE *file = fopen ( filename, "r" );
 | 
			
		||||
   if ( file != NULL )
 | 
			
		||||
   {
 | 
			
		||||
      char line [ 128 ]; /* or other suitable maximum line size */
 | 
			
		||||
 
 | 
			
		||||
		while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
 | 
			
		||||
		{
 | 
			
		||||
		 	printf("Ligne : %s", line); /* write the line */
 | 
			
		||||
			if (strncmp(line, var, strlen(var)-1) == 0)
 | 
			
		||||
			{
 | 
			
		||||
				int i=0;
 | 
			
		||||
				char* tmp = (char*) malloc(sizeof(char*)* (strlen(line)-strlen(var)));
 | 
			
		||||
				//tmp = "\0";
 | 
			
		||||
				while(var[i] != '\0')
 | 
			
		||||
				{
 | 
			
		||||
	int var_size = strlen(var);
 | 
			
		||||
	char *tmp = NULL;
 | 
			
		||||
	char line[FISH_BUFFER_SIZE];
 | 
			
		||||
	int i = 0;
 | 
			
		||||
 | 
			
		||||
	if ( file != NULL ){
 | 
			
		||||
 | 
			
		||||
		while ( fgets ( line, FISH_BUFFER_SIZE, file ) != NULL ) {
 | 
			
		||||
 | 
			
		||||
			if (!strncmp(line, var, var_size)) {
 | 
			
		||||
 | 
			
		||||
				tmp = (char*) malloc(sizeof(char*)* (strlen(line)-var_size));
 | 
			
		||||
				if (tmp == NULL) crash();
 | 
			
		||||
 | 
			
		||||
				i = var_size + 1;
 | 
			
		||||
				while(line[i] != '\n' && line[i] != '\0'){
 | 
			
		||||
					tmp[i-var_size-1] = line[i];
 | 
			
		||||
					i++;
 | 
			
		||||
				}
 | 
			
		||||
				i= i+1;
 | 
			
		||||
				while(line[i] != '\n' && line[i] != '\0')
 | 
			
		||||
				{
 | 
			
		||||
					printf("ici: %s\n", tmp);
 | 
			
		||||
					tmp[i-strlen(var)-1] = line[i];
 | 
			
		||||
					i++;
 | 
			
		||||
				}
 | 
			
		||||
				tmp[i-strlen(var)-1]='\0';
 | 
			
		||||
				printf("Result: %s\n",tmp);
 | 
			
		||||
				return tmp;
 | 
			
		||||
				free(tmp);
 | 
			
		||||
		    }
 | 
			
		||||
				tmp[i-var_size-1]='\0';
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
      fclose ( file );
 | 
			
		||||
      printf("Variable doesn't exit\n");
 | 
			
		||||
   }
 | 
			
		||||
   else
 | 
			
		||||
   {
 | 
			
		||||
      perror ( filename ); /* why didn't the file open? */
 | 
			
		||||
   }
 | 
			
		||||
   return NULL;
 | 
			
		||||
 | 
			
		||||
		fclose(file);
 | 
			
		||||
		return tmp;
 | 
			
		||||
	}
 | 
			
		||||
	else {
 | 
			
		||||
		perror ( filename ); /* why didn't the file open? */
 | 
			
		||||
	}
 | 
			
		||||
	return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,13 @@
 | 
			
		||||
#ifndef FISH_FISH_SETTINGS_H
 | 
			
		||||
#define FISH_FISH_SETTINGS_H
 | 
			
		||||
 | 
			
		||||
#include "fish_core.h"
 | 
			
		||||
 | 
			
		||||
Settings * getSettings();
 | 
			
		||||
//char* convertPS(char* );
 | 
			
		||||
void freeSettings(Settings *settings);
 | 
			
		||||
char* extractVariable(char* filename, char* var);
 | 
			
		||||
#define FISH_RC_FILE "/.fishrc"
 | 
			
		||||
#define FISH_RC_FILE_SIZE 8
 | 
			
		||||
 | 
			
		||||
Settings * getSettings(); //TESTEDssssss
 | 
			
		||||
void freeSettings(Settings *settings); //TESTED
 | 
			
		||||
char* extractVariable(char* filename, char* var);//TESTED
 | 
			
		||||
 | 
			
		||||
#endif //FISH_FISH_SETTINGS_H
 | 
			
		||||
@@ -42,7 +42,7 @@ typedef struct {
 | 
			
		||||
typedef struct {
 | 
			
		||||
	char *PS1;
 | 
			
		||||
	char *PS2;
 | 
			
		||||
	struct passwd* user1;
 | 
			
		||||
	struct passwd* passwd;
 | 
			
		||||
} Settings;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,28 @@
 | 
			
		||||
#include "gtest/gtest.h"
 | 
			
		||||
#include "../fish_shell/fish_types.h"
 | 
			
		||||
#include "../fish_shell/fish_settings.h"
 | 
			
		||||
#include "../fish_shell/fish_settings.h"
 | 
			
		||||
 | 
			
		||||
TEST(free_settings_Test, freeSettings){
 | 
			
		||||
	Settings *s1 = getSettings();
 | 
			
		||||
	Settings *s2 = getSettings();
 | 
			
		||||
 | 
			
		||||
	freeSettings(s1);
 | 
			
		||||
 | 
			
		||||
	ASSERT_STRNE(s1->PS1, s2->PS1);
 | 
			
		||||
 | 
			
		||||
	freeSettings(s2);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(extract_variable_Test, extractVariable){
 | 
			
		||||
	ASSERT_TRUE(extractVariable((char*) "P4T3", (char*) "PS1") == NULL);
 | 
			
		||||
	ASSERT_STREQ(extractVariable((char*) "../fish_shell_tests/fishrc", (char*) "PS1"), "sli->");
 | 
			
		||||
	ASSERT_TRUE(extractVariable((char*) "../fish_shell_tests/fishrc", (char*) "P1ZZ4") == NULL);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(get_settings_Test, getSettings){
 | 
			
		||||
	Settings* s = getSettings();
 | 
			
		||||
	ASSERT_FALSE(s ==NULL);
 | 
			
		||||
	ASSERT_FALSE(s->passwd == NULL);
 | 
			
		||||
	freeSettings(s);
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user