how to read input to perl program from c program -
this perl program
print "enter username \n"; $user; chomp($user); if ($user =~ /[^a-za-z0-9]+/) { print "not matched"; } else { print "matched"; } print "enter password \n"; $pwd; chomp($pwd); if ($pwd =~ /[^a-za-z0-9@#$%^&*]+/) { print "not matched"; } else { print "matched"; }
and c program
int main() { char user[20],pwd[20], command[500]; printf("enter username: "); scanf("%s",user); printf("enter password: "); scanf("%s",pwd); strcpy(command, "/users/nitinsaxena/desktop/2.pl"); sprintf("command %s %s", user, pwd); system(command); return 0; }
now when run c program asks username , password after showing bus error:10. want give input perl program c program. i'm doing wrong ?
i hope doing kind of learning exercise rather serious attempt validate password...
your call sprintf incorrect. sprintf requires it's first argument pointer output string.
also code not check buffer overflow if types input > 19 chars crash. avoid put %19s in scanf format specifier.
also should quote arguments perl script, otherwise space in input cause crash or worse. safe you'd have check input quote chars before sending system.
anyway fix sprintf call replace this
strcpy(command, "/users/nitinsaxena/desktop/2.pl"); sprintf("command %s %s", user, pwd);
with this
snprintf(command, 500, "/users/nitinsaxena/desktop/2.pl '%s' '%s'", user, pwd);
your perl script not right either. putting
$user;
on line not anything. if user , password supposed come in command line arguments should use
$user=shift @argv;
and why print prompts in both c , perl program?
all in all, better off recoding password check in c.
Comments
Post a Comment