Thread: Execvp help

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    4

    Again

    Ok I changed it a bit, but now I'm having trouble getting the file name into the system call... here's what I did:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main (int argc, char *argv[])
    {
    	int pid, fd;
    	char command;
    	
    	/*attempt to get the file name stored in *argv to append to the sort command*/
    	command = strcat("Sort ",argv[1]);
    
    	/*create new process or quit */
    	if ( (pid = fork() ) == -1 ) {
    		perror("fork"); 
    		exit(1);
    	}
    	/*child does the work */
    	if (pid == 0) {
    		close(1);
    		fd = creat("userlist", 0644);
    		system(command);
    		perror("System");
    		exit(1);
    	}
    	/*parent waits then reports */
    	if(pid != 0) {
    		wait(NULL);
    		printf("Done running sort.  Results in userlist\n");
    	}
    
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by hambergler View Post
    Ok I changed it a bit, but now I'm having trouble getting the file name into the system call... here's what I did:

    [code]

    char command;

    /*attempt to get the file name stored in *argv to append to the sort command*/
    command = strcat("Sort ",argv[1]);
    "char command" is one character. Use "char command[1024]" or something.

    You don't have to use fork() with system. You need it with exec() because exec causes the current process to be replaced (notice, no code after an exec() call gets executed). This is not an issue with system().

    So if this is for an assignment that's supposed to teach you about fork and exec, then choose a different task (if not, just use system with no fork).

    WRT argv[1], the argv array is broken on whitespace unless you use quotes. So eg, if your executable is called a.out:

    ./a.out do > this
    argv[1] will be "do", argv[2] will be ">", argv[3] will be "this".
    ./a.out "do > this"
    argv[1] will be "do > this".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    4
    This is for a project. I'm learning to close a "line" like stdout or stdin and open another file to connect to the closed line. Thus the fork and the previous execlp. I'm just having trouble with the syntax of the exec family.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by hambergler View Post
    This is for a project. I'm learning to close a "line" like stdout or stdin and open another file to connect to the closed line. Thus the fork and the previous execlp. I'm just having trouble with the syntax of the exec family.
    Okay -- well, you have hit a limitation of exec/execlp. Someone may come along with evidence to the contrary, but I am pretty sure this kind of command:

    sort somefile > someotherfile

    or anything else involving > redirection is not possible. You can't even use pipes (|) with exec. You can really only issue one single command, and provide values for the arguments it accepts. "> somewhere" and "| whatever" are not arguments to the command. They're part of the shell's own I/O functionality. You may be able to pull it off by opening a file and using dup() and pipe() -- but honestly, I would not bother. This is not what is expected of you. You just need to issue a simple command with optional arguments.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do i make an array of pointers for execvp
    By Blasz in forum C Programming
    Replies: 4
    Last Post: 05-18-2010, 10:42 PM
  2. Using execvp
    By kotoko in forum C Programming
    Replies: 21
    Last Post: 01-06-2009, 02:05 PM
  3. Storing the result of execvp to a char array in C
    By kponenation in forum C Programming
    Replies: 1
    Last Post: 12-14-2005, 11:43 PM
  4. Replies: 1
    Last Post: 10-27-2005, 10:24 AM
  5. getting input from keyboard, passing to execvp
    By jumpyg in forum C++ Programming
    Replies: 4
    Last Post: 11-02-2003, 08:49 PM