Returning Strings from C program to Unix shell script

Hi,
Iam calling a C program from a Unix shell script. The (C) program reads encrypted username/password from a text file , decrypts and returns the decrypted string.
Is there any way i can return the decrypted string to Unix shell program.
My shell script uses the output of the program to login to oracle DB.

C programs main() function returns int. How is it possible to return a String from a C program?

Please help me.

Thanks in advance.

Rao.

The return value of the main() function is the exit status for the program, it should normally be 0 for correct completion or something differnt for error or special conditions.

Your shell script should not use this value as the output of the program, but rather to be sure that the program has completed successfully. Instead you should write the decrypted password to stdout and read that value from your script.

somewhere in your C code you would do:

printf("%s", decrypted_password);

make sure that this is the only output from the program.

in your script you would then have something like :
The script snippet here assumes you are using ksh/bash


...
clear_text_password=$(my_C_program)
if [[ $? -eq 0 ]] ; then
     #The password was returned correctly
     #Do whatever you like here
     echo "The decrypted value is: ${clear_text_password}
else
     echo "Password decryption failed"
     exit 1
fi

Thank you very much reborg.
I am going to follow whatever approach you suggested. I have a small concern here. In future, if some one comes and adds one more printf to the program (for debugging purpose), the script won't function properly right??

Is it possible to write the output values to a common memory which can be used by Unix shell as well as C program. Once the C program writes the output to that common memory , can Unix pick the values?
Since the program deals with passwords I can't write the output to a temp text file so that shell script reads the file and delete it.

How about writing the values to environment variables from C and accessing from Unix?

Iam new to Unix and C, please correct me if my questions are dumb.

Thanks
Rao.

That's not really the way things are done in Unix.

There is an old tenet in Unix programming which says, if you have nothing to say don't say anything, in other words if there is no reason for output from your program don't have any. So taking that into account you should not have any debugging output.

That said you could make sure you have the correct information by making sure the outut of the "correct" printf has a well defined format, by prefixing it with something, like this.


printf("DECRYPTED_DATA %s", decrypted_password);

he the script would for example become

clear_text_password=$(my_C_program | awk '/DECRYPTED_DATA/{print $2}' )
if [[ $? -eq 0 ]] ; then
     #The password was returned correctly
     #Do whatever you like here
     echo "The decrypted value is: ${clear_text_password}
else
     echo "Password decryption failed"
     exit 1
fi

Debug output should go to stderr, not stdout.

Very true, I did provide the alternative because I think the concern was that additional output might be added to the program by someone lazy or not sufficiently informed as to know that.

satguyz, you should read up on the use of stdout and stderr for both your application and your shell script.

Thanks for the clarification.

Rao.

In place of
clear_text_password=$(my_C_program)

clear_text_password=`my_C_program`
can be used

Hey, you had the idea yourself, sending the password string to environment. Go aheas and do a
strcpy(string, env_pass_var);
strcat(string, "=");
strcat(string, c_pass_var);
putenv(string)

access this in shell script by $value_of_env_pass_var

Will Not Work���.

The C program inherits a copy of the environment variables of the shell, and the changes don't go back, they stay in the C program. The changes would be inherited by other programs the C program lanuches, but not the original shell.

Corona is correct - the C code runs in a child process. Any environment changes it makes are lost when it exits.

Whar I'm not so sure about is:

The status that will be in $? is the status from the last process in the pipe -- awk,
not my_C_program.

yep, I got carried away by the idea of putting the variable in the environment. Thanks Corona, Thanks Jim for the eye opener :slight_smile: