0

Just to clarify, I'm a complete novice in C programming. I have a tokenize function and it isn't behaving like what I expect it to. I'm trying to read from the FIFO or named pipe that is passed by the client side, and this is the server side. The client side reads a file and pass it to the FIFO. The problem is that tokenize doesn't return a format where execvp can process it, as running gdb tells me that it failed at calling the execute function in main(). (append function appends a char into the string)

sam
  • 33
  • 6
  • This is questionable: `line = append(line,'\0');` What does `append()` do? And what memory does `char **tokens` actually point to? – Andrew Henle Oct 17 '17 at 11:15
  • What is `append`? It looks like you try to add a NUL character to a string, but a string is _already_ terminated by a NUL char otherwise it wouldn't be a string.... And you neve initialize `tokens`. – Jabberwocky Oct 17 '17 at 11:16
  • You are using `strtok()` correctly. There are however other errors in your code, for example, `tokens` is not initialized. – Ctx Oct 17 '17 at 11:17
  • i did initialise tokens – sam Oct 17 '17 at 11:18
  • @sam _where_ did you initialize `tokens`? Three people sayou didn't... so... – Jabberwocky Oct 17 '17 at 11:18
  • @sam [edit] your question and put the code of `append` _there_. Don't put code in comments, it's unreadable. – Jabberwocky Oct 17 '17 at 11:20
  • at the start of the method – sam Oct 17 '17 at 11:21
  • `string = allocator((strlen(input) + 2), char);` doesn't compile. – Jabberwocky Oct 17 '17 at 11:22
  • 1
    ... and sorry, I don't see any initialisation of `tokens` (with an `s` at the end). – Jabberwocky Oct 17 '17 at 11:24
  • can i know what do you mean by that? sorry i'm new to this. but i did print string in the gdb during execution of main() in strcpy(string2,string), it says "ls" with null terminated – sam Oct 17 '17 at 11:25
  • 3rd line in main() char buffer[1024], *string, **tokens; – sam Oct 17 '17 at 11:25
  • @sam A variable declared inside a function is not assigned a default value. You need to allocate a large enough chunk of memory to it. – Klas Lindbäck Oct 17 '17 at 11:26
  • 3rd line in tokenize() char **tokens, *line2, *token, *delimiter; – sam Oct 17 '17 at 11:26
  • @sam that's where you _declare_ it. Declaration and initialisation are two very different things. The content of `tokens` (and of any other local variable) is undetermined unless you assign something to it. – Jabberwocky Oct 17 '17 at 11:27
  • So what do i need to initialise it with? – sam Oct 17 '17 at 11:36
  • `buffer[size_buffer-1] = "\0";` -->> `buffer[size_buffer] = 0;` (still a lurking buffer overflow) But *please* don't try to combine the `strxxx()` functions for unterminated buffers, like the result from `read()` . (and, IMHO, strtok() is crippled by design anyway) – joop Oct 17 '17 at 12:21

1 Answers1

1

One bug is that tokens is neither initialized nor allocated any memory.

Here is an example on how to initialize and allocate memory for tokens:

char **tokenize(char *line){
    line = append(line,'\0');
    int i = 0, tlen = 0;
    char **tokens = NULL, *line2, *token, *delimiter;
    delimiter = " \t";
    token = strtok(line,delimiter);
    while (token != NULL) {
        if (i == tlen) {
            // Allocate more space
            tlen += 10;
            tokens = realloc(tokens, tlen * sizeof *tokens);
            if (tokens == NULL) {
                exit(1);
            }
        }
        tokens[i] = token;
        token = strtok(NULL, delimiter);
        i += 1;
    }
    tokens[i] = NULL;
    return tokens;
}

This code will allocate memory for 10 tokens at a time. If the memory allocation fails, it will end the program with a non-zero return value to indicate failure.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • @sam You probably have several other bugs in your code. Finding them and fixing them is good practice for you in your quest to learn C. – Klas Lindbäck Oct 17 '17 at 14:20
  • @sam If you do it for tests: Probably. If you do it for exercises that you turn in: Maybe. Ask your professor/teacher about the school policy for using internet sources and outside help when completing tasks. If you do it for excercises that you don't need to turn in: No. – Klas Lindbäck Oct 17 '17 at 15:16