which utility in UNIX

This is a discussion on which utility in UNIX within the C Programming forums, part of the General Programming Boards category; Hey I want to creat a c program that does the task of the UNIX utility 'which'. The which utility, ...

  1. #1
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151

    which utility in UNIX

    Hey I want to creat a c program that does the task of the UNIX utility 'which'.

    The which utility, in the simplest form, takes one program name and tries to locate this program within the PATH environment variable. If it finds the program, the full path to that program is printed on the terminal otherwise a message about not being able to locate the program will be printed.

    I can use the functions getenv() and stat()

    Can you give me a logic how to do this?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You just described how to do it. Look through the PATH variable and look for the name of the program. What's the hard part?

  3. #3
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    Its nice to know your smart. But im a newbie to programming.
    what im asking is how can access PATH? How can I access file names? Im sure I have to use stacks for this. All I need is a boost. Then I can get along.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    getenv() gives you the environment variable you ask for - for example "path".
    To "access" files, you could just try this:
    Code:
    // pseudo-code
      path = getenv("PATH")
      while more entries in path:
        get path element p
        try open for read (p + name)
        if successfully opened, show p + name; done
        else continue
    I'm sure you can use boost or any other number of utilities, but if you are a beginner programmer in C, you shouldn't be using C++ tools like boost.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    I think he meant he needs a boost, as in "a little help", rather than the boost library.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by QuantumPete View Post
    I think he meant he needs a boost, as in "a little help", rather than the boost library.

    QuantumPete
    Typical - yes, I see now how it would be correctly interpreted that way.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    [On a side-note, I don't know if it's only specific to bash, but there's the "type" builtin command who does the same thing as "which" but gives an answer if the command you are looking for is a builtin.]
    I hate real numbers.

  8. #8
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    Code:
    while more entries in path:
    how do i setup the buffer?

    Code:
        get path element p
        try open for read (p + name)
    What is p?

    How about stat()?
    Last edited by Ron; 06-16-2008 at 10:44 AM.

  9. #9
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    PATH in *nix is a colon-delimited environment variable. So parse out each directory in the PATH environment variable, then use stat to determine the presence (or non-presence) of the file.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Ron View Post
    Code:
    while more entries in path:
    how do i setup the buffer?

    Code:
        get path element p
        try open for read (p + name)
    What is p?

    How about stat()?
    p is "[a copy of] a portion of the path" - it can be described in many ways, but char * would probably be a most suitable type to describe something like that.

    Yes, you can (probably SHOULD) use stat.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by Ron View Post
    Its nice to know your smart. But im a newbie to programming.
    what im asking is how can access PATH? How can I access file names? Im sure I have to use stacks for this. All I need is a boost. Then I can get along.
    I think the question, "What's the hard part," was an attempt to elicit information regarding which part of the process is... well, hard. Not a snark.

    You basically listed everything you need. You use getenv() to get the PATH string. You parse through it, separating each component by ':' characters. For each component, you use stat() to get the file mode of the file, as if it existed in that path. If the mode is executable by the current user, then you've found the hit.

  12. #12
    FOSS Enthusiast
    Join Date
    Jun 2008
    Location
    Germany
    Posts
    64
    there is a more convenient way to traverse a directory tree:

    dirent.h

    a sample code to view all files of the directory /usr would be:

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <dirent.h>
    
    int main(void)
    {
        DIR *directory;
        struct dirent *dp;
    
        if((directory = opendir("/usr")) == NULL)
            return(1);
        while((dp = readdir(directory)) != NULL)
            printf("%s\n", dp->d_name);
    
        closedir(directory);
        return(0);
    }
    based on that, you should be able to implement the functionality of "which". I won't tell anymore, since I spoonfed you enough

    oh btw, I don't know if this will work on windows, but it should on all unix-like systems.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mkruk View Post
    there is a more convenient way to traverse a directory tree:

    dirent.h

    a sample code to view all files of the directory /usr would be:

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <dirent.h>
    
    int main(void)
    {
        DIR *directory;
        struct dirent *dp;
    
        if((directory = opendir("/usr")) == NULL)
            return(1);
        while((dp = readdir(directory)) != NULL)
            printf("%s\n", dp->d_name);
    
        closedir(directory);
        return(0);
    }
    based on that, you should be able to implement the functionality of "which". I won't tell anymore, since I spoonfed you enough

    oh btw, I don't know if this will work on windows, but it should on all unix-like systems.

    But the "which" utility will not need to traverse any directory, since it has all the information needed to find (or not find) the executable file: It needs the path environment variable, and it needs the name of the executable. Then it's just a case of splitting the path string into separate paths, and checking if the executable is in that path or not.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    FOSS Enthusiast
    Join Date
    Jun 2008
    Location
    Germany
    Posts
    64
    ah right, sorry I forgot about that

    Imo, the right thing to do is concatenating the result from getenv() and the app-name, passing as the first argument of stat().

    Anyway, Ron, this should be enough hint.. try figure out yourself and tell us if you have further questions

  15. #15
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    This may help a bit, just from googling...

    File Access and Directory System Calls

    http://www.cs.cf.ac.uk/Dave/C/node20...00000000000000

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

Similar Threads

  1. unix fold utility
    By Sgemin_001 in forum C Programming
    Replies: 20
    Last Post: 12-08-2008, 02:21 PM
  2. How to program in unix
    By Cpro in forum Linux Programming
    Replies: 21
    Last Post: 02-12-2008, 09:54 AM
  3. Setting up a Unix box
    By @nthony in forum Tech Board
    Replies: 6
    Last Post: 07-22-2007, 10:22 PM
  4. UNIX (Linux, BSD, etc) Programming :: UNIX
    By kuphryn in forum Linux Programming
    Replies: 6
    Last Post: 04-01-2004, 07:44 PM
  5. About Unix Programming - Making a career desision
    By null in forum C Programming
    Replies: 0
    Last Post: 10-14-2001, 07:37 AM

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