1
0
mirror of https://gitlab.com/klmp200/fish.git synced 2024-11-22 08:43:20 +00:00
fish/fish_shell/fish_settings.c

84 lines
1.7 KiB
C
Raw Normal View History

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