LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-08-2008, 07:44 AM   #1
cleopard
Member
 
Registered: May 2006
Posts: 39

Rep: Reputation: 15
getting result from 'translate' call inside C


I have a Linux C program that, among other things, takes a string, gets rid of the invalid characters, then uses that string as a filename. I came up with a shell command using 'tr' to do what I want, but how can I call that within a C program and get a result in return? If 'exec' or similar calls are used, they don't return if they are successful.

Thank you.
 
Old 08-08-2008, 08:15 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
What you do is check for a non-zero value (the exec functions return -1 if unsuccessful) and deal with that. Something like
Code:
do something
if (exec (arguments)) {
    (void) fprintf (stderr, "exec failed\n");
    exit (EXIT_FAILURE);
}
do something else
 
Old 08-08-2008, 08:39 AM   #3
cleopard
Member
 
Registered: May 2006
Posts: 39

Original Poster
Rep: Reputation: 15
But if I 'exec'ed a 'tr' command on a string, I still wouldn't be able to get the resulting string; I'd just be finding out if the command was successful or not.
 
Old 08-08-2008, 01:22 PM   #4
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Why not include a redirect into a file in /tmp then open that and see what you got? That ought to be quick and easy.

On another hand, rather than using one of the exec functions, why not parse the string and get rid of the blanks, backslants, slashes, carets and all the other junk folks seem to feel it necessary to type in things? That's a pretty straight-forward proposition and you can actually control what happens as you go. Something like set a pointer to the beginning of the string and character-by-character see what's there and either change it to what you want or just ignore it might just be the berries.
 
Old 08-08-2008, 01:49 PM   #5
smoked kipper
Member
 
Registered: May 2008
Location: UK
Distribution: Slackware,Slamd64
Posts: 81

Rep: Reputation: 15
If you need to capture the output of a command, that's what pipes are for. man 2 pipe.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to call a function inside switch case sarathius Programming 4 01-28-2008 05:36 AM
to translate or not to translate HTML rblampain General 2 07-05-2007 09:04 AM
Cannot use copy_to_user inside the open() sys call routine archieval Programming 4 05-26-2007 09:09 AM
How to acess Variable defined in perl script inside an awk call sumin Programming 3 04-26-2007 05:19 AM
problem on dynamic function call inside a class! antony_csf Programming 0 06-29-2004 10:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration