I am building my own shell and I was wondering: how can I know if a commande is on the directory "/bin" and its subdirectories. I would like to implement a function char* path_to_command(char* commandname) which return the path of a command given in parameters.
I've been looking for functions which determine if a specific file is in a directory (like fopen, or access). But those functions just tell us if the file is in the directory only. I was thinking about a recursive function with fopen, where I would have to use strcat a lots of time. I believe that there is a better solution.
English is not my native language, I'm very sorry if there are mistakes. Thank you.
Don't write the recursive function by yourself, prefer use of the standard nftw:
NAME
nftw - walk a file tree
SYNOPSIS
#include <ftw.h> int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags);
DESCRIPTION
The nftw() function shall recursively descend the directory hierarchy rooted in path.
But, for a standard shell there is no need to search recursively (worst, that would be very weird for any user), you just need to search in directories mentioned in PATH
environment variable.