running shell functions based on user input.

This is a discussion on running shell functions based on user input. within the Linux Programming forums, part of the Platform Specific Boards category; I want to use the system() function to change directories, and run a few programs based on user input. Code ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,159
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,892
    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,892
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,892
    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, 02:53 PM
  3. vectors and user input
    By Chaplin27 in forum C++ Programming
    Replies: 6
    Last Post: 01-17-2005, 09:23 AM
  4. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 09:53 AM
  5. Action Based On User Input
    By Stealth in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2001, 05:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21