LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-14-2005, 10:51 AM   #1
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Rep: Reputation: 30
system programing in unix


i'm learning system programing for fun in unix. I'm trying to implment the redirection (>) arrow, like in unix the command ls -l > foo.txt writings the ouptut to the file names foo.txt

Code:
         first fork to make child process...
         wait of child process to end
     in child:
        go through the arguments untill get to >(arrow)
        /* get the filename */
        freopen(args[i+1], "w", stout);
        /* the command line args */
         execvp(*args, args);
         fclose(stout);
when i run it, it just hangs. I read the man pages, and they say check the return values for errors. But the only error it gives me is 10, and i looked for what this number means, but no luck. If some could point me in the right direction and explain on how to go about redirecting a command to file.


thanks,

djgerbavor3

Last edited by djgerbavore; 01-14-2005 at 10:53 AM.
 
Old 01-14-2005, 01:00 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
While it is probably a typo in your post, and not in your actual code, this line:
Code:
freopen(args[i+1], "w", stout);
should read:
Code:
freopen(args[i+1], "w", stdout);
To get a more understandable error-message, you can call the "perror()" function. It will print a error message about the last error that occurred in a library function.

An example for redirecting stdout of the program itself to a file given on its command line including checking for error,a nd printing a informative message with perror():
Code:
#include <stdio.h>

/*
 * This program redirects stdout to a file given as the first command line
 * argument, and then prints a message to stdout so it will appear in the file
 * given on the command line. 
 */

int main(int argc, char **argv)
{

    if (argc != 2) {    /* Check command line syntax. */
        fprintf(stderr, "Usage: %s <file name>\n", argv[0]);
        return 1;   /* exit program with error exit code */
    }

    /* Redirect my own stdout to file given and check for error. */
    if (freopen(argv[1], "w", stdout) == NULL) {
        perror("Redirecting stdout");   /* Will give information about the error */
        return 1;   /* exit program with error exit code */
    }

    /* Printing text to stdout, which will end up in the file */
    printf("Printing this to stdout, but it should appear in %s.\n", argv[1]);
    return 0;
}
 
Old 01-14-2005, 03:36 PM   #3
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Original Poster
Rep: Reputation: 30
thank you, i'm making a little further. however freopen command isn't the one thats hanging up program, the command execvp is the one causing my program to do nothing

for example:

Code:
       
if(freopen(filename, "w", stdout) == NULL) {
      perror("ERROR");
      return 1;
}

execvp(*args, args);
fclose(stdout);
;

i put some printf statements to see what's happening and everything works fine untill the command execvp.

Then, I tried hard coded args array to:
Code:
args[0] = "ls";
args[1] = "-l";
i ran the man excevp page and i blieve the parametes are right, but i'm not sure.

Thanks

djgerbavor3
 
Old 01-14-2005, 05:29 PM   #4
Inunu
LQ Newbie
 
Registered: Jan 2005
Location: NZ
Distribution: Customized MDK 9
Posts: 13

Rep: Reputation: 0
Hi DJ.

The first parameter of execvp() is a constant string, indicating the file to execute. In your code it should be args[0].

execv() and execvp() need to have NULL-terminated array. Are you sure that your args will have NULL at the end? Otherwise it will keep on looking for more arguments endlessly.

Code:
const char* args[4];

args[0]="ls";
args[1]="-l";
/* Without this execvp() will go over args[3], args[4], args[5].... args[9999].... until it sees NULL */
args[2]=NULL; 

execvp(args[0], args);
 
Old 01-15-2005, 01:26 PM   #5
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Original Poster
Rep: Reputation: 30
Inunu - you relied worked!!!! as soon as i put a NULL in my array the command worked.

also *args is the same as args[0], other then that you helped me out alot.


thanks again

djgerbavor3
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Studying system programing skicy Programming 1 11-24-2005 06:53 PM
unix system programming mile1982 Programming 4 09-30-2004 10:06 PM
Steven's Unix network programing-for Linux programmer? beginner16 Programming 5 01-12-2004 10:31 PM
How to copy files from A computer of NT System to The other one of Unix system? whepin Linux - Newbie 5 04-06-2003 10:50 AM
Unix file system Swift&Smart General 4 02-17-2003 10:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:54 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