Reputation: 13
This is my perl program
print "Enter your username \n";
$user;
chomp($user);
if ($user =~ /[^a-zA-Z0-9]+/) {
print "Not Matched";
} else {
print "Matched";
}
print "Enter your password \n";
$pwd;
chomp($pwd);
if ($pwd =~ /[^a-zA-Z0-9@#$%^&*]+/) {
print "Not Matched";
} else {
print "Matched";
}
And this is my 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 I run my C program it asks for username and password but after that it is showing bus error:10. All I want to do is to give input to my perl program from C program. What I'm doing wrong ?
Upvotes: 1
Views: 417
Reputation: 30577
I hope you are doing this as some kind of learning exercise rather than a serious attempt to validate a password...
Your call to sprintf is incorrect. sprintf requires as it's first argument a pointer to the output string.
Also your code does not check for any buffer overflow so if someone types input > 19 chars it will crash. To avoid that put %19s in the scanf format specifier.
Also you should quote the arguments to the perl script, otherwise a space in the input could cause a crash or worse. To be safe you'd have to check the input for any quote chars before sending it to system.
Anyway to fix the 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 does not look right either. Putting
$user;
on a line by itself will not do anything. If the user and password are supposed to come in from command line arguments then you should use
$user=shift @ARGV;
And why print prompts in both C and Perl program?
All in all, you would be better off recoding the password check in c.
Upvotes: 3