Thread: running shell functions based on user input.

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    163

    running shell functions based on user input.

    I want to use the system() function to change directories, and run a few programs based on user input.

    Code so far:
    Code:
    #include <stdio.h>
    
    
    int main()
    {
    	int MAX_PATH = 100;
    	int i;
    	char path[MAX_PATH];
    	char cd_cmd[MAX_PATH + 3];
    	printf("Please enter sox src directory: ");
    	fgets(path,MAX_PATH,stdin);
    	for(i = 0; i < MAX_PATH; i++)
    	{
        	if(path[i] == '\n')
        	{
        	    path[i] = '\0';
        	    break;
        	}
    	}
    	cd_cmd[0] = 'c';
    	cd_cmd[1] = 'd';
    	cd_cmd[2] = ' ';
    	for(i = 0; i < MAX_PATH; i++)
    	{
    		cd_cmd[i+3] = path[i];
    	}
    	printf("cd_cmd is now %s \n", cd_cmd);
    	system("ls -l");
    	system(cd_cmd);
    	system("ls -l");
    		 
     	return(0);
    }
    I also tried omitting the whole cd_cmd array and doing this:
    Code:
    system("cd ", path);
    that compiled, but didn't change the directory like I wanted it to.

    So how do I get a path from a user and change the current directory to that?

    Thanks!

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    chdir()

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    Quote Originally Posted by brewbuck View Post
    chdir()

    Thank you very much. That solved it!


    edit: My question still stands, though, as I now need to do other commands that involve some user input. example: ./sox chimes.wav -r 8000 -c 1 -u -1 cchimes.wav where chimes.wav and cchimes.wav are user defined.
    Last edited by System_159; 10-11-2007 at 07:04 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    36
    u can also use snprintf() function to accept command/data from the user and place that in the buffer and pass the same to the system()...this will solve the issue...otherwise u can also use exec() functions, eg. execvp() and pass the array of command and their options to get executed

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But the exec*() family never returns. It replaces your process with the new one.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    36
    ya thats correct...the child process gets executed and the control never returns to the parent process...so depending on ur requirement u can use execvp()...if u want control back to the parent process, use snprintf() to copy the exact command in the buffer and pass that to the system()

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Or you could, on POSIX, use one of the spawn*() functions, which have the same interface as exec*(), but spawn a new process.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    But if you prefix your exec() call with a fork() call, you have all the control you could ever want.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    36
    can u plz let me know how exactly to implement it, i mean prefixing exec() before fork()...if u give a code example, that wud be gr8

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It would be great if you could drop the kiddie speak and search the board for many previous examples.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    36
    ya sure...thanx

  13. #13
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    ok, thanks for the help guys. Here's what I have that works now:

    Code:
    snprintf(cmd, MAX_PATH+(MAX_FILE*2)+25, "&#37;ssox %s -r 8000 -c 1 -u -1", path, file);
        snprintf(cmd, MAX_PATH+(MAX_FILE*2)+25, "%s temp%s", cmd, outfile);
    
        system(cmd);

    For those that are wondering, it accepts a .wav file, and converts it to 8 bit samples at 8kHz, single channel and saves in new location.

    edit: Moved to general c programming, as the next part has nothing specific to Linux
    Last edited by System_159; 10-12-2007 at 02:10 PM.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Be careful to sanitize "path" if it comes from user input. As it is, it looks like a good place for command injection. (Of course, that only applies if this is somehow externally reachable. But it is something to keep in mind.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    Quote Originally Posted by CornedBee View Post
    Be careful to sanitize "path" if it comes from user input. As it is, it looks like a good place for command injection. (Of course, that only applies if this is somehow externally reachable. But it is something to keep in mind.)
    I have no idea what that means

    My very first experience with terminal was only a couple of weeks ago, and as I've got nobody around to help me out, and I'm to busy during the day to put a huge amount of effort into it I'm moving pretty slowly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-03-2008, 09:07 PM
  2. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  3. vectors and user input
    By Chaplin27 in forum C++ Programming
    Replies: 6
    Last Post: 01-17-2005, 10:23 AM
  4. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM
  5. Action Based On User Input
    By Stealth in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2001, 05:38 AM