Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying string from argv to char array in C

Tags:

arrays

c

linux

copy

I have following code which copy argument string to char array.

char *str = malloc(strlen(argv[1]) + 1);
strcpy(str, argv[1]);

printf("%s\n", str);

Why when I pass following argument:

$6$4MfvmFOaDUaa5bfr$cvtrefr

I get:

MfvmFOaDUaa5bfr

Instead of whole string. Somewhere I lose first number. I tried various of method and every one works the same or doesn't work either.

My key is get only salt(in this case) 4MfvmFOaDUaa5bfr or $6$4MfvmFOaDUaa5bfr without third $ character. I try to also get method to copy string while I meet the third $ and then stop copying.

like image 243
adm Avatar asked May 18 '15 17:05

adm


1 Answers

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

Pass the argument with single quotes:

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

which will prevent the shell expansion.

like image 159
P.P Avatar answered Nov 17 '22 09:11

P.P