2

Here is the relevant code:

#define _GNU_SOURCE
#define BUFFER_SIZE 1024

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

int main(void) {
    while (1) {
        char* buffer;
        size_t size = 32;
        size_t line;
        line = getline(&buffer,&size,stdin);
        printf("%s\n",buffer);

        int commandList[line];
        int count = 0;
        while (strsep(buffer," ")) {
            commandList[count] = strsep(buffer," ");
            count++;
        }
    }
}

I am using Code Blocks with minGW and Clang.

I know some of my code does not do what it is supposed to at the moment, but I am pretty sure it should at least compile. I am also getting a warning: "implicit declaration of function 'strsep'".

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
BrennanWal
  • 63
  • 1
  • 5
  • 4
    The `strsep()` function isn't [Standard C](http://port70.net/~nsz/c/c11/n1570.html) so it may not be available in your Windows / MinGW / Clang environment. (MinGW is "Minimal GNU for Windows" — it doesn't include everything.) . You might be able to use `_strsep()` — more likely, you'll need to find its code and implement it yourself, or use a different function. – Jonathan Leffler Oct 05 '19 at 00:25
  • 1
    You are passing a `char *` to `strsep`. The first parameter is supposed to be a `char**`. Try passing in the `&buffer` instead of just `buffer`. If you give up on MinGW, you might want to try cygwin with it's implementation of gcc. I know that it has `strsep`. – bruceg Oct 05 '19 at 00:31

2 Answers2

4

strsep is trivial to implement yourself

#include <string.h>

char *strsep(char **stringp, const char *delim) {
    char *rv = *stringp;
    if (rv) {
        *stringp += strcspn(*stringp, delim);
        if (**stringp)
            *(*stringp)++ = '\0';
        else
            *stringp = 0; }
    return rv;
}
Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
0

The source file should include string.h:

 #include <string.h>

Not sure if that will fix the link error (undefined reference), but it might.

According to my Linux manual, it might need:

 #define _DEFAULT_SOURCE
    (...)
 #include <string.h>

or

 #define _BSD_SOURCE
    (...)
 #include <string.h>

If all else fails, use strtok() instead, also from string.h which is in many ways superior. Strtok() doesn't modify the first argument; works on const strings; and it doesn't overwrite the delimiter.

wallyk
  • 56,922
  • 16
  • 83
  • 148
  • I just added both those #define statements, but unfortunately it didn't work. – BrennanWal Oct 05 '19 at 00:06
  • 1
    It won't fix the link error. In general, header files don't do that. – S.S. Anne Oct 05 '19 at 00:46
  • @JL2210: Of course. However, `strsep` is easily defined in terms of strtok or strstr, or with additional logic, in terms of other `string.h` functions. – wallyk Oct 05 '19 at 02:55
  • *can be* != *is*. – S.S. Anne Oct 05 '19 at 03:09
  • Using `strtok()` is a possibility, but … `strtok()` modifies its first argument just as `strsep()` does. `strtok()` cannot be used in concurrent loops (e.g to parse a big string with one delimiter (e.g. `:` to separate components of a PATH-like environment variable) and then analyze the token found with another (e.g. `/` to separate directory components)). Also, by design, `strtok()` treats multiple adjacent delimiters as a single delimiter, whereas `strsep()` treats adjacent delimiters as surrounding an empty field. Use `strtok_s()` on Windows or `strtok_r()` on POSIX for nested parsing. – Jonathan Leffler Oct 05 '19 at 20:53
  • More emphatically: `strtok()` **does** modify the first argument; `strtok()` **does not** work reliably if the first argument is a `const` string or a string literal; `strtok()` **does** overwrite the delimiter it finds with a null byte. That's a 100% wrong list of reasons for why `strtok()` might be superior, and all of those features are shared by `strsep()`. The key differences are (1) handling of multiple adjacent delimiters, and (2) ability to use independent, concurrent sequences of calls to `strsep()` that is impossible with `strtok()` (but is possible with `strtok_s()` or `strtok_r()`. – Jonathan Leffler Oct 05 '19 at 20:57