Copy string from argv to char array in C

I have the following code that will copy the argument string into a char array.

char *str = malloc(strlen(argv[1]) + 1); strcpy(str, argv[1]); printf("%s\n", str); 

Why, when I pass the following argument:

 $6$4MfvmFOaDUaa5bfr$cvtrefr 

I get:

 MfvmFOaDUaa5bfr 

Instead of a whole line. Somewhere I'm losing the first number. I tried various methods and each of them works the same or does not work.

My key only gets salt (in this case) 4MfvmFOaDUaa5bfr or $6$4MfvmFOaDUaa5bfr without the third character $. I am also trying to get a method for copying a string when I meet the third $, and then stop copying.

+7
c arrays linux copy
source share
1 answer

Because in the line $6$4MfvmFOaDUaa5bfr$cvtrefr , $6 , $4 and $cvtrefr expanded by the shell for positional arguments and variables, and they are all empty.

Pass an argument with single quotes:

 ./a.out '$6$4MfvmFOaDUaa5bfr$cvtrefr' 

which will prevent shell expansion.

+10
source share

All Articles