1
0
mirror of https://gitlab.com/klmp200/fish.git synced 2024-11-14 12:53:20 +00:00
fish/fish_shell/fish_utils.c

158 lines
3.1 KiB
C
Raw Normal View History

2017-05-15 12:08:10 +00:00
//
// Created by Antoine Bartuccio on 15/05/2017.
//
#include <stdio.h>
#include <stdlib.h>
2017-05-15 13:02:58 +00:00
#include <string.h>
2017-05-15 13:52:48 +00:00
#include <time.h>
2017-05-15 13:02:58 +00:00
#include "fish_types.h"
2017-05-15 12:08:10 +00:00
void crash(){
2017-05-15 13:52:48 +00:00
char *crashErrors[] = {
"fish: Fucking malloc always returning NULL pointer !",
"fish: Error allocating fucking pointer !",
"fish: C language exploding again",
"fish: It's not you're fault for this time"
};
int picked = 0;
srand((unsigned int) time(NULL));
picked = rand() % (sizeof(crashErrors) / sizeof(char*));
fprintf(stderr, "%s\n", crashErrors[picked]);
2017-05-15 12:08:10 +00:00
exit(EXIT_FAILURE);
}
2017-05-15 13:02:58 +00:00
2017-05-15 13:52:48 +00:00
char *getInsult(){
static int init = 0;
int picked = 0;
char *insults[] = {
"Apprend à écrire crétin !",
"Bolos !",
2017-05-15 17:10:46 +00:00
"Moi aussi je sais écrire de la merde, pourtant je le fait pas !"
2017-05-15 13:52:48 +00:00
};
if (!init){
srand((unsigned int) time(NULL));
init = 1;
}
picked = rand() % (sizeof(insults) / sizeof(char*));
return insults[picked];
}
2017-05-15 13:02:58 +00:00
void freeWordArray(WordArray *array) {
int i;
if (array != NULL) {
for (i = 0; i < array->size; i++) {
free(array->words[i]);
}
free(array->words);
free(array);
}
}
WordList *createWordList() {
WordList *list = (WordList*) malloc(sizeof(WordList));
if (list == NULL) crash();
else {
list->size = 0;
list->first = NULL;
list->last = NULL;
}
return list;
}
void addWordList(WordList *list, char *word) {
WordListElement *new = (WordListElement*) malloc(sizeof(WordListElement));
if (new == NULL) crash();
else {
new->next = NULL;
new->previous = list->last;
if (list->size == 0){
list->first = new;
list->last = new;
} else {
list->last->next = new;
list->last = new;
}
new->word = strdup(word);
list->size++;
}
}
void removeWordList(WordList *list) {
WordListElement *current = list->last;
if (current != NULL){
list->last = current->previous;
if (current->previous != NULL)
current->previous->next = NULL;
if (current->word != NULL)
free(current->word);
free(current);
list->size--;
}
}
void freeWordList(WordList *list) {
while (list->size != 0)
removeWordList(list);
free(list);
}
2017-05-15 17:10:46 +00:00
WordList* concatWordList(WordList* list1, WordList* list2){
if(list1->last != NULL){
list1->last->next = list2->first;
list2->first->previous = list1->last;
list1->last = list2->last;
list1->size = list1->size + list2->size;
free(list2);
return list1;
}
else{
return NULL;
}
}
2017-05-15 13:02:58 +00:00
WordArray *wordListToWordArray(WordList *list) {
WordArray *array = (WordArray*) malloc(sizeof(WordArray));
WordListElement *current = list->first;
int i = 0;
if (array == NULL) crash();
else {
array->size = list->size;
array->words = (char **) malloc(sizeof(char *) * (list->size + 1));
if (array->words == NULL) crash();
while (current != NULL){
array->words[i] = current->word;
current->word = NULL;
current = current->next;
i++;
}
array->words[i] = NULL;
}
freeWordList(list);
return array;
}
WordList *wordArrayToWordList(WordArray *array) {
WordList *list = createWordList();
int i = 0;
for (i=0; i<array->size; i++)
addWordList(list, array->words[i]);
freeWordArray(array);
return list;
}