0

I am confronted with the following code:

int get_config(const char *key, char *value) {

FILE *fp = NULL;
char s[100];
char *ret1 = NULL;
char *ret2 = NULL;

fp = fopen(CONFIG_FILE_PATH, "r");

if (fp == NULL) {
    perror(CONFIG_FILE_PATH);
    return FALSE;
}

while (fgets(s, 100, fp) != NULL) {
    //printf("line=%s", s);
    ret1 = strstr(s, key);
    if (ret1 != NULL) {

        ret1 = strstr(s, "=");
        if (ret1 != NULL) {
            ret1++;
            ret2 = strstr(s, "\n");

            strncpy(value, ret1, ret2 - ret1);
            //printf("ret1=%p ret2=%p\n", ret1,ret2);
            value[ret2 - ret1] = 0x0;

            printf("config key=%s value=%s\n", key, value);

            fclose(fp);
            fp = NULL;
            ret1 = NULL;
            ret2 = NULL;

            return TRUE;
        }
    }
}

I struggle to understand what ret1++ does. If I understood the strstr() correctly, after ret1 = strstr(s, "="); ret1 will contain all everything following the = sign within s.

Lets assume this not a number, but a word "value", resulting in ret1 = value What does this mean for ret1++? Maybe my assumption of the CONFIG_FILE_PATH is wrong, and a number always follows the equal sign.

Sidenote: I just wished, I knew what CONFIG_FILE_PATH looks like. But as #define CONFIG_FILE_PATH "/etc/config/duvs.conf" But this path is probably located on the device, this program is written for... :/

user3554329
  • 111
  • 2
  • 11
  • What are you talking about? `ret1` points either to the C-string following `=` (possibly the empty string) or equals to `NULL`. – Jean-Baptiste Yunès Aug 09 '17 at 07:48
  • Mybe I am confused due to this description here: https://www.tutorialspoint.com/c_standard_library/c_function_strstr.htm In the example provided below, `ret` equals `Point` which led me to assume, that in my case `ret1` also equals to a string. Thus making me not understand, how `ret1++` makes any sense. :) – user3554329 Aug 09 '17 at 07:52

2 Answers2

0

ret1 is a pointer.

I supposer your variable s is "dir=/my/path".

The value points to the character. In your example, strstr(s, "="); returns NULL if the "=" has not been found or its pointer inside the string. Meaning ret1 contains a string with the rest of the text including the "=" (=/my/path in my example). By incrementing of 1 character (ret++), the pointer points now to the same string one character after: /my/path.

This is the basics of C code where pointers are used because strings are not really part of the language but just memory data.

William R
  • 37
  • 12
  • Thank you for your explanation. I am really inexperienced when it comes to pointers. Thus didn`t understand the idea of `ret1++` = pointing to the next character in the string. – user3554329 Aug 09 '17 at 08:03
0

After ret1 = strstr(s, "="); ret1 will contain all everything following the = sign within s.

You are right. Let's assume s is the string enable=false\n. After ret1 is incremented with ret1++, it points to the character f. After the statement ret2 = strstr(s, "\n");, the pointer ret2 points to the character \n.

ret2 - ret1 equals 5 which is the length of the substring false. For pointer arithmetic, this article may help you.

After strncpy(value, ret1, ret2 - ret1);, the content of value is the string false.

EDIT: The configuration file specified with CONFIG_FILE_PATH may look like this:

timeout=30
enable=false
priority=5
log=/var/log/mylogfile
...
haolee
  • 892
  • 9
  • 19
  • Thank you very much for this visual example. One of my problems was, not being able to imagine how s (and therefore CONFIG_FILE_PATH) looks like, as I am really inexperienced programmer. – user3554329 Aug 09 '17 at 08:01
  • @user3554329 I think it is a key-value based configuration file. Its content may like this: `key=value`. This is an example: https://en.wikipedia.org/wiki/Configuration_file#MS-DOS – haolee Aug 09 '17 at 08:10