C - Split string with repeated delimiter char into 2 substrings












0















I'm making a very simple C program that simulates the export command, getting an input with fgets().



Input example:




KEY=VALUE




Has to be converted to:



setenv("KEY", "VALUE", 1);


That's easy to solve with something similar to this code:



key = strtok(aux, "=");
value = strtok(NULL, "=");


The problem comes when the user input a value that start with one or several equals = characters. For example:




KEY===VALUE




This should be converted to:



setenv("KEY", "==VALUE", 1);


But with my current code it is converted to:



setenv("KEY", NULL, 1);


How I can solve this?



Thanks in advice.










share|improve this question

























  • You have to write your own parser.

    – john elemans
    Nov 13 '18 at 19:11






  • 4





    You could just use strchr instead of strtok

    – Andrew Sun
    Nov 13 '18 at 19:11











  • Can't reproduce. coliru.stacked-crooked.com/a/913d3de1fdad058a

    – SergeyA
    Nov 13 '18 at 19:13
















0















I'm making a very simple C program that simulates the export command, getting an input with fgets().



Input example:




KEY=VALUE




Has to be converted to:



setenv("KEY", "VALUE", 1);


That's easy to solve with something similar to this code:



key = strtok(aux, "=");
value = strtok(NULL, "=");


The problem comes when the user input a value that start with one or several equals = characters. For example:




KEY===VALUE




This should be converted to:



setenv("KEY", "==VALUE", 1);


But with my current code it is converted to:



setenv("KEY", NULL, 1);


How I can solve this?



Thanks in advice.










share|improve this question

























  • You have to write your own parser.

    – john elemans
    Nov 13 '18 at 19:11






  • 4





    You could just use strchr instead of strtok

    – Andrew Sun
    Nov 13 '18 at 19:11











  • Can't reproduce. coliru.stacked-crooked.com/a/913d3de1fdad058a

    – SergeyA
    Nov 13 '18 at 19:13














0












0








0








I'm making a very simple C program that simulates the export command, getting an input with fgets().



Input example:




KEY=VALUE




Has to be converted to:



setenv("KEY", "VALUE", 1);


That's easy to solve with something similar to this code:



key = strtok(aux, "=");
value = strtok(NULL, "=");


The problem comes when the user input a value that start with one or several equals = characters. For example:




KEY===VALUE




This should be converted to:



setenv("KEY", "==VALUE", 1);


But with my current code it is converted to:



setenv("KEY", NULL, 1);


How I can solve this?



Thanks in advice.










share|improve this question
















I'm making a very simple C program that simulates the export command, getting an input with fgets().



Input example:




KEY=VALUE




Has to be converted to:



setenv("KEY", "VALUE", 1);


That's easy to solve with something similar to this code:



key = strtok(aux, "=");
value = strtok(NULL, "=");


The problem comes when the user input a value that start with one or several equals = characters. For example:




KEY===VALUE




This should be converted to:



setenv("KEY", "==VALUE", 1);


But with my current code it is converted to:



setenv("KEY", NULL, 1);


How I can solve this?



Thanks in advice.







c strtok






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 19:07







Tony Ceralva

















asked Nov 13 '18 at 19:05









Tony CeralvaTony Ceralva

7471720




7471720













  • You have to write your own parser.

    – john elemans
    Nov 13 '18 at 19:11






  • 4





    You could just use strchr instead of strtok

    – Andrew Sun
    Nov 13 '18 at 19:11











  • Can't reproduce. coliru.stacked-crooked.com/a/913d3de1fdad058a

    – SergeyA
    Nov 13 '18 at 19:13



















  • You have to write your own parser.

    – john elemans
    Nov 13 '18 at 19:11






  • 4





    You could just use strchr instead of strtok

    – Andrew Sun
    Nov 13 '18 at 19:11











  • Can't reproduce. coliru.stacked-crooked.com/a/913d3de1fdad058a

    – SergeyA
    Nov 13 '18 at 19:13

















You have to write your own parser.

– john elemans
Nov 13 '18 at 19:11





You have to write your own parser.

– john elemans
Nov 13 '18 at 19:11




4




4





You could just use strchr instead of strtok

– Andrew Sun
Nov 13 '18 at 19:11





You could just use strchr instead of strtok

– Andrew Sun
Nov 13 '18 at 19:11













Can't reproduce. coliru.stacked-crooked.com/a/913d3de1fdad058a

– SergeyA
Nov 13 '18 at 19:13





Can't reproduce. coliru.stacked-crooked.com/a/913d3de1fdad058a

– SergeyA
Nov 13 '18 at 19:13












2 Answers
2






active

oldest

votes


















3














Your second strtok() should not use = as the delimiter. You would only do that if there were another = that ended the value. But the value ends at the end of the string. Use an empty delimiter for this part.



key = strtok(aux, "=");
value = strtok(NULL, "");





share|improve this answer
























  • Thanks! It works prefectly.

    – Tony Ceralva
    Nov 13 '18 at 19:43



















0














strtok is probably overkill (and non-reentrant) when it's just one token. This will do,



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

int main(int argc, char **argv) {
char *key, *equals, *value;
if(argc != 2 || !(equals = strchr(key = argv[1], '=')))
return fprintf(stderr, "KEY=VALUEn"), EXIT_FAILURE;
value = equals + 1;
*equals = '';
printf("key: <%s>; value: <%s>.n", key, value);
return EXIT_SUCCESS;
}


Although strtok is probably easier to read. One may try strsep, but it is GNU C.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287894%2fc-split-string-with-repeated-delimiter-char-into-2-substrings%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Your second strtok() should not use = as the delimiter. You would only do that if there were another = that ended the value. But the value ends at the end of the string. Use an empty delimiter for this part.



    key = strtok(aux, "=");
    value = strtok(NULL, "");





    share|improve this answer
























    • Thanks! It works prefectly.

      – Tony Ceralva
      Nov 13 '18 at 19:43
















    3














    Your second strtok() should not use = as the delimiter. You would only do that if there were another = that ended the value. But the value ends at the end of the string. Use an empty delimiter for this part.



    key = strtok(aux, "=");
    value = strtok(NULL, "");





    share|improve this answer
























    • Thanks! It works prefectly.

      – Tony Ceralva
      Nov 13 '18 at 19:43














    3












    3








    3







    Your second strtok() should not use = as the delimiter. You would only do that if there were another = that ended the value. But the value ends at the end of the string. Use an empty delimiter for this part.



    key = strtok(aux, "=");
    value = strtok(NULL, "");





    share|improve this answer













    Your second strtok() should not use = as the delimiter. You would only do that if there were another = that ended the value. But the value ends at the end of the string. Use an empty delimiter for this part.



    key = strtok(aux, "=");
    value = strtok(NULL, "");






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 13 '18 at 19:24









    BarmarBarmar

    423k35244346




    423k35244346













    • Thanks! It works prefectly.

      – Tony Ceralva
      Nov 13 '18 at 19:43



















    • Thanks! It works prefectly.

      – Tony Ceralva
      Nov 13 '18 at 19:43

















    Thanks! It works prefectly.

    – Tony Ceralva
    Nov 13 '18 at 19:43





    Thanks! It works prefectly.

    – Tony Ceralva
    Nov 13 '18 at 19:43













    0














    strtok is probably overkill (and non-reentrant) when it's just one token. This will do,



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

    int main(int argc, char **argv) {
    char *key, *equals, *value;
    if(argc != 2 || !(equals = strchr(key = argv[1], '=')))
    return fprintf(stderr, "KEY=VALUEn"), EXIT_FAILURE;
    value = equals + 1;
    *equals = '';
    printf("key: <%s>; value: <%s>.n", key, value);
    return EXIT_SUCCESS;
    }


    Although strtok is probably easier to read. One may try strsep, but it is GNU C.






    share|improve this answer




























      0














      strtok is probably overkill (and non-reentrant) when it's just one token. This will do,



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

      int main(int argc, char **argv) {
      char *key, *equals, *value;
      if(argc != 2 || !(equals = strchr(key = argv[1], '=')))
      return fprintf(stderr, "KEY=VALUEn"), EXIT_FAILURE;
      value = equals + 1;
      *equals = '';
      printf("key: <%s>; value: <%s>.n", key, value);
      return EXIT_SUCCESS;
      }


      Although strtok is probably easier to read. One may try strsep, but it is GNU C.






      share|improve this answer


























        0












        0








        0







        strtok is probably overkill (and non-reentrant) when it's just one token. This will do,



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

        int main(int argc, char **argv) {
        char *key, *equals, *value;
        if(argc != 2 || !(equals = strchr(key = argv[1], '=')))
        return fprintf(stderr, "KEY=VALUEn"), EXIT_FAILURE;
        value = equals + 1;
        *equals = '';
        printf("key: <%s>; value: <%s>.n", key, value);
        return EXIT_SUCCESS;
        }


        Although strtok is probably easier to read. One may try strsep, but it is GNU C.






        share|improve this answer













        strtok is probably overkill (and non-reentrant) when it's just one token. This will do,



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

        int main(int argc, char **argv) {
        char *key, *equals, *value;
        if(argc != 2 || !(equals = strchr(key = argv[1], '=')))
        return fprintf(stderr, "KEY=VALUEn"), EXIT_FAILURE;
        value = equals + 1;
        *equals = '';
        printf("key: <%s>; value: <%s>.n", key, value);
        return EXIT_SUCCESS;
        }


        Although strtok is probably easier to read. One may try strsep, but it is GNU C.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 19:44









        Neil EdelmanNeil Edelman

        42228




        42228






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287894%2fc-split-string-with-repeated-delimiter-char-into-2-substrings%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python