C question

Tom Watts wattst-dxuVLtCph9gsA/PxXw9srA at public.gmane.org
Thu Jul 20 17:52:41 UTC 2006



Madison Kelly wrote:
> Hi all,
> 
>   As something of a follow-up to my last question, I need some C help (a 
> language I am quite 'n00by' in :p ).
> 
>   What I want to do is, I think, simple. Take a set of command line 
> options and use them when calling a perl script. From the ANSI C book I 
> have gotten to the point where I can print the commands to STDOUT but I 
> can't figure out how to put them into a variable. I know 'char' is for 
> one byte, but don't know what type to use for a full string or how to 
> concatenate the switches into the string.
> 
>   Here's what I've got so far (probably broken from playing with it):
> 
> #include <stdio.h>
> 
> #define REAL_PATH 
> "/home/digimer/projects/mizu-bu/releases/mizu-bu/cgi-bin/exec-priv.pl"
> main(int argc, char *argv[])
> {
>         setuid(geteuid());
>         setgid(getegid());
>     var say;
>     say="Hello";
> 
>     int i;
>     for (i=1; i<argc; i++ )
>     {
>         printf("%s%s", argv[i], (i < argc-1) ? " " : "");
>     }
>     printf("\n");
>     printf("%s", say);
> /*        execv(REAL_PATH, av);*/
> }
> 
>   Thanks for any help!!
> 
> Madison

Ah, the beauty that is string manipulation in C.  In C a string is an 
array of chars with the string variable being a pointer to the first 
char in the array.  So, you can either allocate an array at runtime, or 
use malloc to create a custom size.  However, there are functions to 
make this easier.  Of particular interest to you would probably be 
strcat() which concatenates two strings.  A quick Google came up with 
this which looks like a good start 
http://www.cs.cf.ac.uk/Dave/C/node19.html.

Finally, it is sooo important to always keep pointers in the back of 
your mind; it'll save you a lot of headaches.  There's a lot to say 
about strings in C, but I guess I'll leave it at that and you can ask as 
you have problems.

-Tom
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list