aboutsummaryrefslogtreecommitdiff
path: root/src/helper_functions.c
blob: cd3584867d8720998099ed54585cfd7c950d927e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#ifndef shell_helper_functions
#define shell_helper_functions

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

void *malloce(size_t size){
    void* mal=malloc(size);
    if(!mal){
        fprintf(stderr, "lsh: allocation error\n");
        exit(EXIT_FAILURE);
    }
    return mal;
}

void *realloce(void* mal,size_t size){
    mal=realloc(mal,size);
    if(!mal){
        fprintf(stderr, "lsh: allocation error\n");
        exit(EXIT_FAILURE);
    }
    return mal;
}

char *pop(char *token, int ch_i){
    if(token[ch_i]=='\0'){
        return token;
    }

    char *right=(char*)malloce(sizeof(char)*strlen(token)+1);

    strcpy(right,&token[ch_i+1]);
    token[ch_i]='\0';
    strcat(token,right);

    free(right);
    return token;
}


// You must free the result if result is non-NULL.
char *str_replace(char *orig, char *rep, char *with) {
    char *result; // the return string
    char *ins;    // the next insert point
    char *tmp;    // varies
    int len_rep;  // length of rep (the string to remove)
    int len_with; // length of with (the string to replace rep with)
    int len_front; // distance between rep and end of last rep
    int count;    // number of replacements

    // sanity checks and initialization
    if (!orig || !rep)
        return NULL;
    len_rep = strlen(rep);
    if (len_rep == 0)
        return NULL; // empty rep causes infinite loop during count
    if (!with)
        with = "";
    len_with = strlen(with);

    // count the number of replacements needed
    ins = orig;
    for (count = 0; (tmp = strstr(ins, rep)); ++count) {
        ins = tmp + len_rep;
    }

    tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1);

    if (!result)
        return NULL;

    // first time through the loop, all the variable are set correctly
    // from here on,
    //    tmp points to the end of the result string
    //    ins points to the next occurrence of rep in orig
    //    orig points to the remainder of orig after "end of rep"
    while (count--) {
        ins = strstr(orig, rep);
        len_front = ins - orig;
        tmp = strncpy(tmp, orig, len_front) + len_front;
        tmp = strcpy(tmp, with) + len_with;
        orig += len_front + len_rep; // move to next "end of rep"
    }
    strcpy(tmp, orig);
    return result;
}

char *basename(char *path_in) 
{
    char *token = NULL;
    char *lastToken = NULL;
    char *basePath = NULL;

    basePath = (char *)malloce(sizeof(char) * (strlen(path_in) + 1));
    strcpy(basePath, path_in);

    token = strtok(basePath, "/");
    while (token != NULL) {
        lastToken = token;
        token = strtok(NULL, "/");
    }

    if (lastToken == NULL){
        free(basePath);
        return NULL;
    }

    char *ret_str = (char*)malloce(sizeof(char)*strlen(lastToken)+1);
    strcpy(ret_str,lastToken);
    free(basePath);
    return ret_str;
}

#endif