1
0
mirror of https://gitlab.com/klmp200/fish.git synced 2024-11-14 04:43:20 +00:00
fish/fish_shell/fish_settings.c
2017-06-12 13:30:20 +02:00

125 lines
2.3 KiB
C

#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);
}
}
void printPS(char* PS, Settings* s){
int buf= FISH_BUFFER_SIZE;
int i = 0;
int slashed = 0;
char* path = NULL;
while (PS[i] != '\0'){
if(slashed){
switch (PS[i])
{
case 'u':
printf("%s",s->passwd->pw_name);
break;
case 'p':
path = (char*) malloc(sizeof(char)*buf);
while(getcwd(path,buf) == NULL){
buf+=FISH_BUFFER_SIZE;
free(path);
path = (char*) malloc(sizeof(char)*buf);
}
printf("%s",path);
free(path);
path = NULL;
break;
default:printf("%c",PS[i]);
}
slashed = 0;
}
else if(PS[i] == '\\'){
slashed = 1;
}else{
printf("%c",PS[i]);
}
++i;
}
}
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;
}