1
0
mirror of https://gitlab.com/klmp200/fish.git synced 2025-07-11 04:09:22 +00:00

Merge branch 'ame' into 'master'

Garbage

See merge request !10
This commit is contained in:
2017-05-29 17:11:24 +00:00
10 changed files with 142 additions and 28 deletions

View File

@ -103,23 +103,6 @@ char *fishReadLine() {
return NULL;
}
Settings *getSettings() {
Settings *s = (Settings*) malloc(sizeof(Settings));
if (s == NULL){
crash();
} else {
s->PS1 = strdup("\n~>");
}
return s;
}
void freeSettings(Settings *settings){
if (settings != NULL){
free(settings->PS1);
free(settings);
}
}
int fishLoad(WordArray *array) {
pid_t pid;

View File

@ -13,15 +13,12 @@
#include "fish_types.h"
#include "fish_commands.h"
#include "fish_utils.h"
#include "fish_settings.h"
/* WordArray functions */
WordList * split(char *string, char *separator); // Tested
/* Settings functions */
Settings * getSettings();
void freeSettings(Settings *settings);
/* General purpose functions */

View File

@ -0,0 +1,83 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#include <string.h>
#include "fish_settings.h"
#include "fish_core.h"
Settings *getSettings() {
Settings *s = (Settings*) malloc(sizeof(Settings));
if (s == NULL){
crash();
}
uid_t uid;
struct passwd *user;
char* filename = NULL;
uid = getuid();
user = getpwuid(uid);
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");
if (s->PS1 == NULL) s->PS1 = strdup("->");
s->PS2 = extractVariable(filename, (char*) "PS2");
if (s->PS2 == NULL) s->PS2 = strdup("->");
free(filename);
return s;
}
void freeSettings(Settings *settings){
if (settings != NULL){
settings->PS1[0] = '\0';
settings->PS2[0] = '\0';
free(settings->PS1);
free(settings->PS2);
free(settings);
}
}
char* extractVariable(char* filename, char* var){
FILE *file = fopen ( filename, "r" );
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++;
}
tmp[i-var_size-1]='\0';
}
}
fclose(file);
return tmp;
}
else {
perror ( filename ); /* why didn't the file open? */
}
return tmp;
}

View File

@ -0,0 +1,14 @@
#ifndef FISH_FISH_SETTINGS_H
#define FISH_FISH_SETTINGS_H
#include "fish_core.h"
#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

View File

@ -5,6 +5,10 @@
#ifndef FISH_FISH_TYPES_H
#define FISH_FISH_TYPES_H
#include <sys/types.h>
#include <unistd.h>
#include <pwd.h>
#define EXIT_SIGNAL -100
#define ERROR_STRING "\n"
@ -39,6 +43,8 @@ typedef struct {
typedef struct {
char *PS1;
char *PS2;
struct passwd* passwd;
} Settings;