LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 05-14-2008, 02:16 PM   #1
gdsoccer
LQ Newbie
 
Registered: May 2008
Location: Ohio
Distribution: Ubuntu 8.04
Posts: 7

Rep: Reputation: 0
Redirection


Upon posting on my old post this morning I realized I had moved to redirection but was still posting in my piping question. Here is my issue with redirection. I can get it to work and actually write the output of something like ls to a file that I type in. The problem is that once I call ls or whatever command my system hangs up and I have to use CTRL-D to get out. Here is what I have currently


Code:
void shellRedirection(char *buf, int index){
    char command[100];
    char outFile[100];
    int filedes, dupped;
    mode_t mode = S_IWUSR;
    int output = STDOUT_FILENO;
    //commandbreak is a function that splits the entire command into it's seperate command. e.g. splits ls > testfile into ls and testfile
    commandBreak(buf, command, outFile, index);
    printf("First Command = %s -- Second Command %s \n",command, outFile);
    filedes = creat(outFile, mode);    //creates the new file
    dupped = dup2(filedes, output);    //sets stdout to same descriptor
    system(command);                   //calls command on host system
    dup2(1, output);                   //should put output back at 1?
    
}
Like I said this command is called from a main based on the fact it contains the > character. I uses system commands instead of commands on my shell because I would figure the system commands would be more trustworthy. Can anyone see what my issue is?

Thanks a bunch

Last edited by gdsoccer; 05-14-2008 at 04:41 PM.
 
Old 05-14-2008, 05:46 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Well, I can't tell you (as in don't know) what is wrong, BUT I was always under the impression that the point of these exercises is that you're supposed to write the whole thing in C (like the real shell).
Shelling out via system is 'cheating' imho, and definitely not as per the real thing.
Of course, I could be wrong.
 
Old 05-14-2008, 11:53 PM   #3
gdsoccer
LQ Newbie
 
Registered: May 2008
Location: Ohio
Distribution: Ubuntu 8.04
Posts: 7

Original Poster
Rep: Reputation: 0
hehe, yeah eventually the entire shell will be written but as of now the only working functions are the make file system and the ls commands. In the spirit of more thorough testing I decided to write the commands to the system with the intent of later going back and passing it as a command to my personal shell
 
Old 05-15-2008, 12:13 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
What is output supposed to be? If I understand correctly, output is always 1 (the dup2() call never changes the value of the either argument). What you are trying to accomplish may be done by “saving” the file descriptor before duping it. Alternatively, the more commonly-seen approach is simply to fork() prior to the dup2() so the parent retains all its file descriptors, and only the child will be affected by the redirection.
 
  


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
redirection kkpal Programming 4 11-05-2007 10:24 PM
redirection rblampain Linux - Server 0 09-29-2007 12:39 AM
I/O redirection jittinan2 Linux - Newbie 7 08-21-2007 08:01 PM
redirection bhuvana Linux - General 2 07-03-2006 02:36 AM
redirection in C pantera Programming 2 08-11-2004 01:06 PM

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

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