0

I have written a c program which is passed *input as parameter and should act as a shell. Thus, I need to split input into an array of strings which I am aiming to do in char * tokens. However, e.g. token+2 does not seem to refer to the whole String which I wanted to save at the second position in the array. Could you help me how I would need to change it? Thank you!

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


int parse(char *input)
{   
    char * tokens = (char *) malloc(1024);
    //Zaehler, an welcher aktuellen Stelle im Array tokens das Token ist
    int zaehlertokens=0;
    //zaehler der Stelle im Array input:
    int zaehler=0;
    //Zaehler, welche Stelle im Array input die erste nach einem Leerzeichen war:
    int lastspace=0;
    while(input[zaehler]!='\0')
    {
        //Input durchlaufen und bei Leerzeichen in Elemente aufteilen:
        if(input[zaehler]==' ')
        {
            //Alle Buchstaben seit letzem Leerzeichen als neuen Eintrag in tokens speichern:
            strncpy (tokens+zaehlertokens, input+lastspace, zaehler-lastspace-1);

            zaehlertokens++;
            lastspace=zaehler;
            //damit umgehen, dass mehrere Leerzeichen nacheinander kommen:
            while(input[zaehler]==' ')
            {
                zaehler++;
                lastspace++;
                printf("Leerzeichen. Zaehler: %i, Lastspace: %i\n", zaehler, lastspace);
            }
        }
        //Zeiger auf das naechste Element im input Array
        zaehler++;
    }
    //Wenn \0 aufgetreten ist, muss das letzte Elemente noch als token gespeichert werden:
    if(input[zaehler]=='\0')
    {
        //Alle Buchstaben seit letzem Leerzeichen als neuen Eintrag in tokens speichern:
        strncpy (tokens+zaehlertokens, input+lastspace, zaehler-lastspace-1);
        zaehlertokens++;
        lastspace=zaehler;
    }

    int zaehlerAusgabe=0;
    ///Schleife, die die Befehle nacheinander ausgibt
    printf("Vor Ausgabe. Zaehlertokens: %i\n", zaehlertokens);
    while(zaehlerAusgabe<zaehlertokens)
    {
        //row number:token
        printf("%i: %s\n", zaehlerAusgabe, tokens+zaehlerAusgabe, sizeof(zaehlerAusgabe)+1+sizeof(tokens+zaehlerAusgabe));
        fflush(stdout);
        zaehlerAusgabe++;
        //Ueberpruefen, ob exit eingegeben wurde:
        printf("Comparison with exit, %s\n", tokens+zaehlerAusgabe-2 /*-1?*/);
        if(strcmp(tokens+zaehlerAusgabe-1, "exit")==0)
        {
            printf("exit\n");
            exiteingegeben=1;
        }
    }
    return exiteingegeben;
}
1
  • Please try using English identifiers and comments, you will reach wider audience Commented Dec 5, 2018 at 5:57

1 Answer 1

1

For an array of strings you need a 2D array. Initially you can allocate it to a known number of tokens and realloc if required.

char ** tokens = malloc(noTokens * sizeof(char*));

Then you need to allocate memory for each token.

// Replace below line with
// strncpy (tokens+zaehlertokens, input+lastspace, zaehler-lastspace-1);

tokens[zaehlertokens] = malloc(zaehler + 1);
strncpy (tokens[zaehlertokens], input+lastspace, zaehler-lastspace-1);
tokens[zaehlertokens][zaehler] = '\0';  // null termination

If the memory initially allocated is not enough, you can reallocate it

if (zaehlertokens >= noTokens)
{
    noTokens *= 2;
    char ** tokens = realloc(noTokens * sizeof(char*));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.