Hi.
I'm in the need for some advice in C.
Problem:
- I want to call a C binary sending a string (--command or file name) to it
- depending on what kind of string was sent I need to make decisions in a function (like case ..esac in bash)
- after doing the actions a result (also a string) shall be returned from the C binary to the terminal output or into a variable inside the calling-script
Any advice to this or a C program-code that actually does something equal?
Thanks
Need some small C advice
- LazY Puppy
- Posts: 1934
- Joined: Fri 21 Nov 2014, 18:14
- Location: Germany
Need some small C advice
RSH
"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:
No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:
"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:
No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:
When you say "C binary" I assume you really mean a C function.
There's no such thing as a "C binary" -- unless you mean a compiled binary
executable whose source code happened to be in C.
Here's a function that takes an input string,
does various things depending on what the string is,
and prints the result to standard output.
There's no such thing as a "C binary" -- unless you mean a compiled binary
executable whose source code happened to be in C.
Here's a function that takes an input string,
does various things depending on what the string is,
and prints the result to standard output.
Code: Select all
#include <string.h>
void foobar( char* inputString )
{
char* resultString;
char tmpString[32];
if (!strcmp( inputString, "tinker"))
{
resultString = "it's tinker";
}
else if (!strcmp( inputString, "evers"))
{
resultString = "it's evers";
}
else if (!strcmp(inputString, "chance"))
{
/* just to show another way of returning a string result */
strcpy( tmpString, "it's chance");
resultString = tmpString;
}
else
{
resultString = ""; /* return null string if input not recognized */
}
printf( "%s", resultString ); /* print the result to stdout */
}
On second thought perhaps an example of how to use
"foobar" in a complete program would help:
If the excutable is named "checkStr", then from the command line
takes the argument "someString" and examines it with foobar(),
doing whatever is appropriate.
To set an environment variable, you could do
"foobar" in a complete program would help:
Code: Select all
#include <stdio.h>
foobar from previous post goes here....
int main( int argc, char* argv[] )
{
/* argv[1] is the first argument passed to the program */
foobar( argv[1] );
}
Code: Select all
# checkStr someString
doing whatever is appropriate.
To set an environment variable, you could do
Code: Select all
# RESULTSTR=`checkStr somestring`
- LazY Puppy
- Posts: 1934
- Joined: Fri 21 Nov 2014, 18:14
- Location: Germany
Yes, I meant the C function of a later compiled binary.When you say "C binary" I assume you really mean a C function.
There's no such thing as a "C binary" -- unless you mean a compiled binary
executable whose source code happened to be in C.
After doing my post I had a look into source code of sit, which has some similar code, but I was not able to modify this as I did not know about the difference of
Code: Select all
char* argv[]
Code: Select all
char *argv[]
RSH
"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:
No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:
"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:
No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:
There's no difference between
char* argv[]
and
char *argv[]
You could also use
char **argv
or
char** argv
I consider it a matter of style and taste, although I accept that there are times
when it makes sense to have rules for the sake of consistency.
However, it does seem to me more logical to say
char* somePtr
than
char *somePtr
because the former follows the common variable declaration paradigm of
TYPE VARIABLENAME
in that "char*" to me is a type: "char pointer" or "pointer to char"
Logically the asterisk (meaning "pointer") is part of the type, not part of the variable name, so it should be spelled that way. But that's just my opinion. Anway, the compiler is agnostic.
char* argv[]
and
char *argv[]
You could also use
char **argv
or
char** argv
I consider it a matter of style and taste, although I accept that there are times
when it makes sense to have rules for the sake of consistency.
However, it does seem to me more logical to say
char* somePtr
than
char *somePtr
because the former follows the common variable declaration paradigm of
TYPE VARIABLENAME
in that "char*" to me is a type: "char pointer" or "pointer to char"
Logically the asterisk (meaning "pointer") is part of the type, not part of the variable name, so it should be spelled that way. But that's just my opinion. Anway, the compiler is agnostic.
- technosaurus
- Posts: 4853
- Joined: Mon 19 May 2008, 01:24
- Location: Blue Springs, MO
- Contact:
char *argv[] is a null terminated array of pointers to char with the program name as argv[0] followed by the arguments argv[1]...argv[argc-1] followed by argv[argc]==NULL (argc tells the size of that array)
Then there is *envp[] immidiately after the last element of argv, but there is no envc to tell you how many environment variables there are, you just have to iterate over them till you get to NULL.
in C you can't do a switch case on strings,... only integer types (characters count as integers though)
to compare strings you need to use strcmp() or one of its relatives and there is no builtin switch, so it becomes a series of if elses or a function that does it more efficiently (I use a binary search tree for constant, sorted strings)
Then there is *envp[] immidiately after the last element of argv, but there is no envc to tell you how many environment variables there are, you just have to iterate over them till you get to NULL.
Code: Select all
for(size_t i=1;i<argc;i++) somefunc(argv[i]);
for(size_t i=0;envp[i]!=NULL;i++) somefunc(envp[i]);
Code: Select all
if (argv[i][0]=='-'){
switch (argv[i][1]){
case 'h' :
case '?' :
display_help();
break;
case 'x': do_x();
default : die("unsupported switch");
}
}
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].