Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Thu 23 Nov 2017, 09:21
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Need some small C advice
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [6 Posts]  
Author Message
LazY Puppy


Joined: 21 Nov 2014
Posts: 2007
Location: Germany

PostPosted: Sat 09 Jan 2016, 11:21    Post subject:  Need some small C advice  

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

_________________
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) Laughing

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! Wink
Back to top
View user's profile Send private message 
6502coder


Joined: 23 Mar 2009
Posts: 405
Location: Western United States

PostPosted: Sat 09 Jan 2016, 17:18    Post subject:  

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.

Code:
#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 */
 
}
Back to top
View user's profile Send private message 
6502coder


Joined: 23 Mar 2009
Posts: 405
Location: Western United States

PostPosted: Sat 09 Jan 2016, 18:07    Post subject:  

On second thought perhaps an example of how to use
"foobar" in a complete program would help:

Code:
#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] );
}


If the excutable is named "checkStr", then from the command line

Code:
# checkStr  someString


takes the argument "someString" and examines it with foobar(),
doing whatever is appropriate.

To set an environment variable, you could do

Code:
#  RESULTSTR=`checkStr somestring`
Back to top
View user's profile Send private message 
LazY Puppy


Joined: 21 Nov 2014
Posts: 2007
Location: Germany

PostPosted: Sun 10 Jan 2016, 19:49    Post subject:  

Quote:
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.

Yes, I meant the C function of a later compiled binary.

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:
char* argv[]

and (sit)
Code:
char *argv[]


Thanks a lot for the C code examples! Smile

_________________
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) Laughing

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! Wink
Back to top
View user's profile Send private message 
6502coder


Joined: 23 Mar 2009
Posts: 405
Location: Western United States

PostPosted: Mon 11 Jan 2016, 01:11    Post subject:  

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.
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4756
Location: Kingwood, TX

PostPosted: Wed 10 Feb 2016, 08:53    Post subject:  

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.

Code:
for(size_t i=1;i<argc;i++) somefunc(argv[i]);
for(size_t i=0;envp[i]!=NULL;i++) somefunc(envp[i]);


in C you can't do a switch case on strings,... only integer types (characters count as integers though)
Code:

if (argv[i][0]=='-'){
  switch (argv[i][1]){
  case 'h' :
  case '?' :
    display_help();
    break;
  case 'x': do_x();
  default : die("unsupported switch");
  }
}


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)

_________________
Check out my github repositories. I may eventually get around to updating my blogspot.
Back to top
View user's profile Send private message Visit poster's website 
Display posts from previous:   Sort by:   
Page 1 of 1 [6 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0418s ][ Queries: 11 (0.0046s) ][ GZIP on ]