You are not logged in.

#1 2009-10-12 22:59:53

DrMikeDuke
Member
From: Australia
Registered: 2008-05-12
Posts: 79
Website

[C] piping user input into system() call (or similar)

Hey guys,

I'm working on creating a C program I think will be really neat to use with linux and scp but I have run in to a roadblock.
I need to pass a statement to the system from my C program but with user defined variables, allow me to explain with an example:

#include <stdio.h>
 
int main()
{
      gets (port);
      gets (cypher);

      system("scp", "-P" port, "-c" cypher);
 return(0);
}

Now obviously (and sadly!) this doesn't work. Can anyone help me achieve this goal?


Regards,

Mike.

Last edited by DrMikeDuke (2009-10-12 23:01:18)


Workstation: Core i7 2600k | Asus PBZ68-V/GEN3 | 8GB DDR3-2000 | Gainward GLH GTX570 | Velociraptor 300 | AntecSS 850W | Essence ST | Corsair 800D | Win7x64 Pro/Arch LinuxX64
Server: 2x Six-Core AMD Opteron Processor 8439 SE, 64GB DDR2 ECC, Tyan S2932-SI, Areca ARC-1230 Raid, PCP&C 1Kw PSU, LSi FusionMPT Ultra320 SCSI/Tandberg LTO4 Autoloader
Laptop: Alienware M14xR3

Offline

#2 2009-10-13 04:18:24

sr
Member
Registered: 2009-10-12
Posts: 51

Re: [C] piping user input into system() call (or similar)

Maybe try a fork, followed by one of the exec*() functions? fork-exec

Offline

#3 2009-10-13 04:54:38

HashBox
Member
Registered: 2009-01-22
Posts: 271

Re: [C] piping user input into system() call (or similar)

Fork and exec is probably the more common way of doing it, but from your example you probably want something like this (can't guarantee this will work but it should help):

#include <stdio.h>

int main()
{
    char temp[256], port[32], cypher[32];

    gets(port);
    gets(cypher);

    // This line uses formatted printing to insert variables into a string
    sprintf(temp, "scp -P %i -c %s", atoi(port), cypher); // %i = int   %s = string/char array

    system(temp);

    return 0;
}

the "atoi" part isn't actually necessary at all but is something to take a note of for future usage. It converts a string into an integer.

Last edited by HashBox (2009-10-13 04:56:41)

Offline

#4 2009-10-13 09:04:34

DrMikeDuke
Member
From: Australia
Registered: 2008-05-12
Posts: 79
Website

Re: [C] piping user input into system() call (or similar)

cool, I'll give these suggestions a spin now I have some time!
Will post back!


Workstation: Core i7 2600k | Asus PBZ68-V/GEN3 | 8GB DDR3-2000 | Gainward GLH GTX570 | Velociraptor 300 | AntecSS 850W | Essence ST | Corsair 800D | Win7x64 Pro/Arch LinuxX64
Server: 2x Six-Core AMD Opteron Processor 8439 SE, 64GB DDR2 ECC, Tyan S2932-SI, Areca ARC-1230 Raid, PCP&C 1Kw PSU, LSi FusionMPT Ultra320 SCSI/Tandberg LTO4 Autoloader
Laptop: Alienware M14xR3

Offline

#5 2009-10-13 12:40:48

DrMikeDuke
Member
From: Australia
Registered: 2008-05-12
Posts: 79
Website

Re: [C] piping user input into system() call (or similar)

going great guys! Thanks for all the advice so far. one question remains however:

i have to use the ":" symbol (i.e. for "user@host:filename) but I cant seem to escape (\) or just plain use ":" inside sprintf() without it stopping at that point. Any ideas why? google isnt turning up anything useful!

Regards,

Mike.


Workstation: Core i7 2600k | Asus PBZ68-V/GEN3 | 8GB DDR3-2000 | Gainward GLH GTX570 | Velociraptor 300 | AntecSS 850W | Essence ST | Corsair 800D | Win7x64 Pro/Arch LinuxX64
Server: 2x Six-Core AMD Opteron Processor 8439 SE, 64GB DDR2 ECC, Tyan S2932-SI, Areca ARC-1230 Raid, PCP&C 1Kw PSU, LSi FusionMPT Ultra320 SCSI/Tandberg LTO4 Autoloader
Laptop: Alienware M14xR3

Offline

#6 2009-10-13 13:01:19

benob
Member
Registered: 2008-11-11
Posts: 187

Re: [C] piping user input into system() call (or similar)

":" should not be a problem as it does not have a signification to sprintf. Did you use snprintf which cuts the string after n chars? Did you try asprintf() which performs the allocation and prevents memory leaks?

Offline

#7 2009-10-13 13:04:54

Lich
Member
Registered: 2009-09-13
Posts: 437

Re: [C] piping user input into system() call (or similar)

DrMikeDuke wrote:

going great guys! Thanks for all the advice so far. one question remains however:

i have to use the ":" symbol (i.e. for "user@host:filename) but I cant seem to escape (\) or just plain use ":" inside sprintf() without it stopping at that point. Any ideas why? google isnt turning up anything useful!

Regards,

Mike.

You don't need to escape it as it's not a "reserved" variable, it should work as a normal character. Paste your code, your problem is somewhere else.
edit: well, benob got here first, but yeah

Last edited by Lich (2009-10-13 13:05:16)


Archlinux | ratpoison + evilwm | urxvtc | tmux

Offline

#8 2009-10-13 13:11:02

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: [C] piping user input into system() call (or similar)

If you need to escape a character for the shell, as opposed to for C, you need to put the escape characters in the C string.  You could do that by wrapping it in single quotes or escaping the escape character (double backslash in your C code).

Just a question:  why are you doing this in C?  It seems to me that you would be better served by a scripting language like bash or Perl.

Offline

#9 2009-10-15 12:23:22

DrMikeDuke
Member
From: Australia
Registered: 2008-05-12
Posts: 79
Website

Re: [C] piping user input into system() call (or similar)

thats true but I wanted this because I set out to do a project in C.
Anyhow. Its nearing completion and not toooo bad at about 350+ lines of code not too big either.
Will release it here soonish! big_smile


Workstation: Core i7 2600k | Asus PBZ68-V/GEN3 | 8GB DDR3-2000 | Gainward GLH GTX570 | Velociraptor 300 | AntecSS 850W | Essence ST | Corsair 800D | Win7x64 Pro/Arch LinuxX64
Server: 2x Six-Core AMD Opteron Processor 8439 SE, 64GB DDR2 ECC, Tyan S2932-SI, Areca ARC-1230 Raid, PCP&C 1Kw PSU, LSi FusionMPT Ultra320 SCSI/Tandberg LTO4 Autoloader
Laptop: Alienware M14xR3

Offline

Board footer

Powered by FluxBB