0

I'm trying to parse some string which is to contain some filenames as arguments. My objective is to parse a command and its parameters and store it to an 2d char array.

Example Input:

/* Where 'cmd' is a command, file* are the arguments */
cmd file1 file2 file3     

Here is my code:

int8_t **file_names = NULL;

/* Allocate Memory for the array,  
Max of 5 files of max strlen 64 each */
file_names = malloc(sizeof(char *));
for(i=0; i<5; i++)
*(file_names + i) = malloc(sizeof(char) * 64);

/* Receive command from user */
cmd = malloc(sizeof(char) * 1024);
printf("Please Enter the command:\n");
fgets(cmd, 2048, stdin);
cmd[strlen(cmd) - 1] = '\0';   /* Overwrite Newline by '\0' char */

int j = 0;
/* Tokenise the file names and cpy to 2d array */
ptr = strtok(cmd, " ");        /* Contains command 'cmd' */
printf("First Token  : [%s]\n\n", ptr);

printf("Parsing Filenames now:\n");
while(ptr = (strtok(NULL, " ")))
{
    printf("\tToken  : [%s]\n", ptr));
    memcpy(*(file_names + j), ptr, strlen(ptr));        /* Copy to 2D array */
    printf("\tFile Name : [%s]\n", *(file_names + j));
    j++;
};

The Output is:

$ gcc 2d.c -Wall -Wextra
$ ./a.out 
Please Enter the command
cmd file1 file2 file3
First Token  : [cmd]

Parsing Filenames now
    Token  : [file1]
    File Name : [file1�,    ȡ,  �,  X�, ��, ]
    Token  : [file2]
    File Name : [file2]
    Token  : [file3]
    File Name : [file3]

Issue:

I don't know why first token is appended with junk. While ptr gets correct data. I suspect memcpy, but strlen returns correct 5 bytes for ptr.

strtok(3) also says:

Each call to strtok() returns a pointer to a null-terminated string containing the next token.

So I believe that it should null-terminated and should print file1 only.

8
  • 3
    memcpying strlen doesn't include the terminator. honestly if you don't mind a posix solution you'd likely be better off just using strdup and throwing out that preallocation entirely. Even then you're asking for an overrun of the pointer array if more than five entries are provided.
    – WhozCraig
    Nov 19, 2014 at 6:50
  • That's right, A very careless mistake Face-palm. Thanks anyways! Nov 19, 2014 at 6:53
  • What's the point of allocating a fixed-sized buffer beforehand when you can allocate when you know the sring length? (You also don't enforce your hard-coded limit.)
    – M Oehm
    Nov 19, 2014 at 6:53
  • 2
    this line: file_names = malloc(sizeof(char *)); should probably be: file_names = malloc(sizeof(char *)*5); to allow enough room for 5 pointers Nov 19, 2014 at 7:02
  • 1
    this line: fgets(cmd, 2048, stdin); is allowing twice as many characters as is actually available from the prior malloc of only 1024 characters. So a VERY long cmd would result in a overflow of the buffer pointed to by cmd. Resulting in undefined behaviour and probably a seg fault event Nov 19, 2014 at 7:05

0

Your Answer

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

Browse other questions tagged or ask your own question.