Exec functions

This is a discussion on Exec functions within the C Programming forums, part of the General Programming Boards category; Hi, i just want to know what can i do for open a directory using exec functions. Example, i want ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    34

    Exec functions

    Hi, i just want to know what can i do for open a directory using exec functions.
    Example, i want to open the Documents/Downloads folder

    so im on linux he is on "cd /home/Documents/Downloads"
    Is there any way by using exec functions to do that? To open that directory?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes.

    More information please, would you like to open the folder in their file manager? Or get the contents etc?

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    34
    so, on the shell linux if i do "cd /home/Documents/Downloads" i go to Downloads Folder, and shows something like this on the shell

    user@userlocalhost:~home/documents/downloads:

    i just want to do a exec that do the same that this command "cd /home/Documents/Downloads"

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Why not just use a system call instead?

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    34
    How i do that with System call? There's no chance with exec functions?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, the exec family expects a file to execute, not a system command; also exec doesn't come back, so if there's more things you want done, well, that's too bad.

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Code:
    #include <unistd.h>
    #include <stdio.h>
    #include <errno.h>
    #include <stdlib.h>
    
    int main(void)
    {
    
      char new_dir[] = "/home/tmp/";
    
      if (chdir(new_dir) != 0) {
        perror("Call to chdir failed! ");
        exit(EXIT_FAILURE); 
      }
    
     printf("chdir to %s successful!\n", new_dir);
    
     return 0;
    }
    kermit@fastbox ~/cprogs/board $ gcc -Wall -o my_cd my_cd.c
    kermit@fastbox ~/cprogs/board $ ./my_cd
    Call to chdir failed! : No such file
    Change one line to
    Code:
     char new_dir[] = "/home/kermit/tmp/";
    
    kermit@fastbox ~/cprogs/board $ gcc -Wall -o my_cd my_cd.c
    kermit@fastbox ~/cprogs/board $ ./my_cd
    chdir to /home/kermit/tmp/ successful!
    kermit@fastbox ~/cprogs/board $
    There you go. There is an awful lot more you can (and probably should) do with that to make it more robust, but that is the basics of the system call. You can get all of that from the man page for chdir.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    34
    thank you for that information but what i really want is that after doing that chdir the shell is poiting to that directory like:

    kermit@fastbox ~/cprogs/board $ gcc -Wall -o my_cd my_cd.c
    kermit@fastbox ~/cprogs/board $ ./my_cd
    chdir to /home/kermit/tmp/ successful!

    kermit@fastbox ~/home/kermit/tmp $


    its that what im thinking about...

  9. #9
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Is there some reason why you cannot just do this with a shell script? As zacs7 already noted, perhaps you could give a little more context as to what it is you are trying to achieve, and how you want to achieve it.

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    34
    I want to change the directory wiith the objective to get the names of the files that are inside that directory.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That still sounds exactly like a shell script.

    Even if for some unearthly reason, it isn't a shell script, then there's no reason why you wouldn't use system commands.

  12. #12
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    So then after you have changed into that directory, you want your program to read the names of the files to be used by the program then? If so, you don't even need to do a change of directory, but do some system calls to read a given directory's contents...

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That still sounds exactly like a shell script.

    Even if for some unearthly reason, it isn't a shell script, then there's no reason why you wouldn't use system commands.

  14. #14
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    I agree; some input from the OP to provide some context, in order to determine what is necessary/helpful would be useful. As it is, we are kind of guessing here. There seems to be a bit of that gong around lately....

  15. #15
    Registered User
    Join Date
    Mar 2008
    Posts
    34
    so using system calls i can read my files? can you give an example how to use please?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  2. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  3. exec functions under unix
    By v3dant in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2004, 01:08 PM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

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