transfer values between c and bash - c

Simple question, I hope
I have a c program that does a lot of math. It requires a few input floats and then returns a few floats. I would like this code to be incorporated into a bash script that runs it at the right time and passes it the right value, and then reads the result.
What is the simplest and easiest way to do this? Would passing these values as a command line argument at the calling of the c program work? And then simply store the results as a string in bash to be parsed at my convenience? Please tell me there is an easy way to do that!
Thanks

You can pass command line arguments to your C program, through arguments to main. In the easiest case, your program returns a single number (result) and you can capture that result back in your bash script:
#!/bin/sh
...
RESULT=$(mycprogram arg1 arg2)
...

You need to use
char * getenv (const char *name)
so you'll use something like
char *foo = NULL;
foo = getenv("BAR");
Keep in mind that you'll get a char* back, so if you're hoping for an int you'll need to use atoi() or something like it. Same goes for checking it's not NULL. If you're expecting many variables that you rely on, you could check for everything at the beginning of your program.

Related

sscanf function in C

I'm working in my C program and I have a doubt about how to use the sscanf function.
I have to get from a string like something$HOME/something2 three strings: the first one has to contain "something", the second one "HOME" and the third one "/something2".
But also it has to be able to split $HOME or $HOME/something, or something$HOME...
What I wrote was something like that:
sscanf(str,"%m[^$]$%m[_a-zA-Z0-9]%*m", &ant, &act, &sig);
But for the cases $HOME, $HOME/something, something$HOME... it takes very strange strings. Any ideas of what can I put on my sscanf to get the other values by NULL in case they don't exist?
Sorry, but you should not use sscanf for that. Your input seems to be a shell command line argument and you seem to want implement variable expansion.
For that, you have to write your own parser that would parse the expression, identify and do variable expansion. It's not a job for scanf - it's a job for your custom loop where you inspect each and every character in a string.
There's a utility with like exactly that functionality called envsubst. You could inspect its source gettext/envsubst.c. You could also take a look at wordexp; however, wordexp function is a way broader function.

Parsing command line arguments in c without spaces

I have a program that parses command line arguments using a while loop. Simply, while iterating through the length of argc, if an argument matches a flag than the next argument is taken as a variable. Now in my assignment we are asked to do this in a way that spaces between flags and integer arguments are optional.
For example if i input -k1 it is the same as -k 1 and 1 is the value stored.
I can't find anything that allows this. The only thing I can think is that if argc is an even number it means that there are no pals between a set of argument and i could use scanf("-k%d",key).
Any helpful pointers for me?
At a POSIX-compatible OS you can use a standart API for that: man getopt. It will do all the dirty job to parse the parameters and will provide you a convenient interface to deal with.
Here is a good example for it: http://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html#Example-of-Getopt

Converting String literal to variable name

I have an assignment which tells me i need to accept arguments from the command line.
I know how to accept arguments from the command line, however this is what i need
I am told my arguments are as follows name_of_function name_of_variable argument1, arugment2
is there an easy way to map name_of_function to the name of the function and name_of_variable to the name of the global variable, without going strcmp on each of them ?
There is no tool or library that converts a string to the corresponding variable, function, or anything else in C. When you have e.g. a .NET runtime environment you could use reflection to see, if an object is in your program and to access it.
You will have to use strcmp or similar to interpret the command line arguments and decide how to deal with the commands.

making getopt() process argv[0]

I trying to emulate main() function like behavior for normal functions using string tokenizing and storing tokens in a NULL terminated char* array.
Every thing is fine except getopt(). It won't rearrange argv[0] coz it expects the first argument to be program name. But for my function the argv[0] isn't the program name. I want to make getopt() to also rearrange argv[0](non-option). How do I do that?
getopt(3) uses a global variable optind (option index) to track its progress through argv and initializes it to 1. Try setting optind = 0 before reading the options.
Try using
c = getopt(argc + 1, argv - 1, "xyz")
Edit: Which, as pointed out below, is a hack But I'd be interested to see a machine on which it didn't work.
Though the hack #Tom Tanner suggested worked for certain systems, it didn't compile for the target which I'm supposed to make it work. Another work around I found is to replace the first argument in my argv[] array with a dummy string and use getopt() with it.

Is it possible to print on a file the output of a function?

I'm writing a tool which uses various void functions to analyze some elements that the program receive in input, and print in the stdout (using printf()) the results of the analysis. Obviously that means that i use printf() very much, because there are lots of things that need being printed. I would like that everything is printed on the stdout, is printed also on a log file. But the use of fprintf() for each printf() function makes the program really longer, confused and not well-ordered. Is there a way to save directly the output of a function on a file? So if i have a void function for example analyze() wich contains lots of printf() functions, the output of analyze() will be printed in the stdout (to the shell) and also on a file. I tried to look for it but withouth any results.
Thanks for help guys!
You could write a function that will issue a printf and fprintf and call that function instead.
For instance, here's a skeleton, obviously you'll have to fill in the ...:
void myPrintf(...)
{
printf(...);
fprintf(...);
}
Then in your code, instead of printf, you could call:
myPrintf(...);
You could also do this with a macro.
If you need to turn off logging, you could do so by simply pulling out the fprintf in that function and leave the rest of your code unchanged.
It's generally better to have a specialised logging function, rather than peppering your code with direct printf/fprintf calls. That logging function can write to stdout, a log-file, whatever you want, and can be different in debug/release, etc.

Resources