You are not logged in.
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
Maybe try a fork, followed by one of the exec*() functions? fork-exec
Offline
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
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
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
":" 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
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
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
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!
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