How to check existence of a directory in C - c

I have developed a program that is a simple file manager. For example, it lets a user create a file, delete a file, see the attribute of the files, change directory, and etc based on a CIFS path. However, I have found out a problem when I was implementing the cd command.
I have defined a global variable which will be initialized with a new path when I call FmCommandChangeDirectory. Now I wanted to check the existence of a directory/folder before I change the global working path to the new directory. I have implemented the following function but it has not able to find out a folder exists or not.
bool ChangePathRoot(const char* arg_computer_name)
{
strcpy_s(g_c_RootDrive, "C:\\");
strcpy_s(g_c_CurrentPath, "\\\\");
strcat(g_c_CurrentPath, arg_computer_name);
strcpy_s(g_c_ShareName, "\\C$");
strcat(g_c_CurrentPath, g_c_ShareName);
strcpy_s(g_c_SystemModifiablePath, "\\");
strcat(g_c_CurrentPath, g_c_SystemModifiablePath);
return true;
}
void ChangePathReverseBack(const char* arg_computer_name)
{
char c_PathBuffer[_MAX_PATH];
char c_Drive[_MAX_DRIVE];
char c_DirectoryMain[_MAX_DIR], c_DirectoryTemp[_MAX_DIR];
char c_Filename[_MAX_FNAME];
char c_Extension[_MAX_EXT];
strcpy(c_PathBuffer, g_c_CurrentPath);
_splitpath(c_PathBuffer, c_Drive, c_DirectoryMain, c_Filename, c_Extension);
_splitpath(c_PathBuffer, c_Drive, c_DirectoryTemp, c_Filename, c_Extension);
char* c_MainToken = strtok(c_DirectoryMain, "\\");
int i_StringsCounter = 0;
while (c_MainToken != NULL)
{
c_MainToken = strtok(NULL, "\\");
i_StringsCounter++;
}
char* c_TempToken = strtok(c_DirectoryTemp, "\\");
int i_StringsCounterTemp = i_StringsCounter - 1;
char c_FinalModifiedPath[MAX_PATH];
strcpy(c_FinalModifiedPath, "\\\\");
for (size_t i = 0; i < i_StringsCounterTemp; i++)
{
strcat(c_FinalModifiedPath, c_TempToken);
strcat(c_FinalModifiedPath, "\\");
c_TempToken = strtok(NULL, "\\");
}
char* c_TokenBase = NULL;
char* c_TokenDir = NULL;
c_TokenBase = strtok_s(c_FinalModifiedPath, "\\", &c_TokenDir);
char* c_TokenRoot = NULL;
char* c_TokenDirectory = NULL;
c_TokenRoot = strtok_s(c_TokenDir, "$", &c_TokenDirectory);
ChangePathSecondary(c_TokenRoot, arg_computer_name, c_TokenDirectory);
}
bool ChangePathRelative(const char* arg_directory)
{
strcat(g_c_SystemModifiablePath, arg_directory);
strcat(g_c_SystemModifiablePath, "\\");
strcat(g_c_CurrentPath, arg_directory);
strcat(g_c_CurrentPath, "\\");
return true;
}
void FmCommandChangeDirectory(const char* arg_computer_name)
{
char c_PathBuffer[_MAX_PATH];
char c_Drive[_MAX_DRIVE];
char c_DirectoryMain[_MAX_DIR];
char c_Filename[_MAX_FNAME];
char c_Extension[_MAX_EXT];
scanf_s("%s", c_PathBuffer, MAX_PATH - 1);
_splitpath(c_PathBuffer, c_Drive, c_DirectoryMain, c_Filename, c_Extension);
if (!strcmp(c_PathBuffer, "\\") || !strcmp(c_PathBuffer, "/"))
{
ChangePathRoot(arg_computer_name);
}
else if (!strcmp(c_PathBuffer, ".."))
{
ChangePathReverseBack(arg_computer_name);
}
else
{
ChangePathRelative(c_PathBuffer);
}
}
Now I wanted to implement a function that checks a path (directory) that has exist or not but I don't know how Can I Implement such function to check a directory has been existed or not.

There are two APIs for you to use:
In addition to GetFileAttributes mentioned by #IInspectable, you can also refer PathFileExists.
GetFileAttributes:
Retrieves file system attributes for a specified file or directory.
Code:
DWORD dwAttrib = GetFileAttributes(directorypath);
if(dwAttrib != INVALID_FILE_ATTRIBUTES &&(dwAttrib & FILE_ATTRIBUTE_DIRECTORY))
{
//exist
}
else
{
//not a vaild path
}
PathFileExists:
Determines whether a path to a file system object such as a file or
folder is valid.
Code:
BOOL retval = PathFileExists(directorypath);
if(retval == 1)
{
//file exists
}
else
{
//not a vaild file
}

Related

Fatfs string comparison for fno.fname

I have an issue regarding reading the size of a file on my SD card. The sizes of these files will vary in the application, I therefore, need to get the size of the file. If I run the below code I can see the files in my directory along with their size.
What I need to do is store the size of the DATA.CSV file as a variable.
How do I add a comparision to get the fno.fsize when the listing is "data.csv
This prints out:
00> Listing directory: /00> 0 EVENTLOG.CSV <DIR> SYSTEM~1 183600 DATA.CSV ```
void Get_data_csv_file_size()//of the data csv
{
if(Logging_UART_SD_CARD == true){NRF_LOG_INFO("\r\n Listing directory: /");}
ff_result = f_opendir(&dir, "/");
if (ff_result)
{
if(Logging_UART_SD_CARD == true){NRF_LOG_INFO("Directory listing failed!");}
}
do
{
ff_result = f_readdir(&dir, &fno);
if (ff_result != FR_OK)
{
if(Logging_UART_SD_CARD == true){NRF_LOG_INFO("Directory read failed.");}
}
if (fno.fname[0])
{
if (fno.fattrib & AM_DIR)
{
if(Logging_UART_SD_CARD == true){NRF_LOG_RAW_INFO(" <DIR> %s",(uint32_t)fno.fname);}
}
else
{
if(Logging_UART_SD_CARD == true){NRF_LOG_RAW_INFO("%9lu %s", fno.fsize, (uint32_t)fno.fname);}
if(strcmp((uint32_t)fno.fname, "data.csv")==0)//Convert both to a uint32_t
{
Size_of_file = fno.fsize;//Set the size of the file
//Does not get to here
}
}
}
}
while (fno.fname[0]);
}
Note this is programmed in C using a arm board. What operation do I need to do so I can get the file size?
I want something like:
if(fno.name == "data.csv")
{
Size_of_file = fno.fsize;//Set the size of the file
}
Just in case you determine using an implementation of stricmp() would be useful, here is one that I have used:
//case insensitive string compare
int cb_stricmp(const char *a, const char *b)
{
if(!a) return -1;
if(!b) return -1;
int ch_a = 0;
int ch_b = 0;
while ( ch_a != '\0' &&ch_a == ch_b)
{
ch_a = (unsigned char) *a++;
ch_b = (unsigned char) *b++;
ch_a = tolower(toupper(ch_a));
ch_b = tolower(toupper(ch_b));
}
return ch_a - ch_b;
}
Found a solution using snprintf neeeded to convert fno.fname to a string to compare the result.
char string_test[9] = "DATA.CSV";
char name_test[9]={0};
snprintf(name_test, sizeof(name_test),"%s",(uint32_t)fno.fname);
NRF_LOG_INFO("Result is: %s",name_test);
int result = strcmp(name_test, string_test);
if(result==0)//Convert both to a uint32_t
{
Size_of_file = fno.fsize;//Set the size of the file
NRF_LOG_INFO("Size of file using is: %9lu",Size_of_file);
}

Methods to iterate every file in a directory?

I've been looking around for methods by which a directory can be monitored for file creation/modification etc. however all the previous posts I've found for Windows are C++ specific.
Microsoft does list ReadDirectoryChangesW, but this too is for C++ (I haven't the knowledge to assess whether these are compatible for C). I've only knowledge with inotify for Linux, which is fairly straightforward, and wondered if there are any simple examples of the Windows equivalent? (I do not want to use inotify on Windows despite it technically being achievable).
If you are just looking for methods, maybe this will help a bit:
https://www.geeksforgeeks.org/c-program-list-files-sub-directories-directory/
(just copy-pasted the code in case)
Tested it on linux machine and it seems to work. Not recursive though.
int main(void)
{
struct dirent *de; /* Pointer for directory entry */
/* opendir() returns a pointer of DIR type. */
DIR *dr = opendir(".");
if (dr == NULL) /* opendir returns NULL if couldn't open directory */
{
printf("Could not open current directory" );
return 0;
}
/* Refer http://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html
for readdir() */
while ((de = readdir(dr)) != NULL)
printf("%s\n", de->d_name);
closedir(dr);
return 0;
}
Also, see this question if you need to check if a listed file is a directory:
Checking if a dir. entry returned by readdir is a directory, link or file
This method may not be as portable as it seems, but worth a try.
Cheers!
Using FindFirstFile to hit the first node of certain directory, then to call FindNextFile to iterate files one by one inside one directory layer.
Here is my sample code for your reference, there is a recursive funcion.
#include "stdafx.h"
#include <string>
static void iterate_dir(std::string dir) {
WIN32_FIND_DATA fd;
HANDLE hFind;
std::wstring fn_ws;
std::string fn;
int pos = 0;
int count_bg = 0;
int count_fg = 0;
std::string dir_bkp = dir;
std::string dir_sub;
std::string str_wide_char_for_any = "*.*";
std::string str_folder_node = "..";
if (dir.length() - dir.find_last_of("\\") > 1) //dir ends without "\\"
dir += "\\";
dir += str_wide_char_for_any;
std::wstring dir_wstr = std::wstring(dir.begin(), dir.end());
LPCWSTR dir_wc = dir_wstr.c_str();
hFind = FindFirstFile(dir_wc, &fd);
if (hFind == INVALID_HANDLE_VALUE) {
FindClose(hFind);
return;
}
while(true) {
if (!FindNextFile(hFind, &fd)) {
break;
}
if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
fn_ws = std::wstring(fd.cFileName);
fn = std::string(fn_ws.begin(), fn_ws.end());
if (fn.compare(str_folder_node) == 0) {
continue;
}
else {
if ((pos = dir.rfind(str_wide_char_for_any)) != std::string::npos) {
dir_sub = dir;
dir_sub = dir_sub.replace(dir_sub.begin()+pos, dir_sub.end(), fn.begin(), fn.end());
}
else if (dir.length() - (pos = dir.rfind("\\")) > 1) {
dir_sub = dir;
dir_sub += "\\";
dir_sub += fn;
}
else {
dir_sub = dir;
dir_sub += fn;
}
printf("[%s][directory]:%s\n", __func__, dir.c_str());
iterate_dir(dir_sub);
continue;
}
}
else if (fd.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
fn_ws = std::wstring(fd.cFileName);
fn = std::string(fn_ws.begin(), fn_ws.end());
printf("[%s][file]:%s\n", __func__, fn.c_str());
}
else {
fn_ws = std::wstring(fd.cFileName);
fn = std::string(fn_ws.begin(), fn_ws.end());
printf("[%s][unspecified attribute file]:%s\n", __func__, fn.c_str());
}
}
FindClose(hFind);
return;
}
You can have a main.cpp like:
int main() {
std::string dir_name("C:\\test");
iterate_dir(dir);
return 0;
}

checking directory paths ends with ".", ".."

I have a programming problem that I hope someone out there can help me with. I am trying to learn C programming for a task at work and I have set myself a little project, which consists of reading down a file tree including all the sub directories obtaining information about each file.
The problem I get is that my program dosen't ignore the directory path ends with either /. or /.. and when it prints all the directories, I want to give space in front of the subdirectories for the readablity.
so the error is occured at this part:
int isDir(const char *parent, char *name) {
struct stat st_buf; // file info
char buf[BUF_SIZE];
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
return 0;
}
char *path = malloc(strlen(name) + strlen(parent) + 2);
//sprintf(char *buf, const char *format, [arg1],[arg2],...)
sprintf(path, "%s/%s", parent, name);
stat(path, &st_buf); //
return S_ISDIR(st_buf.st_mode); //directory
}
And this is main and list function:
int list(const char *name) {
DIR *dirp = opendir(name);
struct dirent *dentry;
char buf[BUF_SIZE];
while ((dentry = readdir(dirp)) != NULL) {
char *dir_name = dentry->d_name;
printf(" %s\n", dir_name);
//if it's dir, then go into dir
if (isDir(name, dir_name)) { //name : parent, dir_name : child
chdir(dir_name);
getcwd(buf, BUF_SIZE);
list(buf);
}
}
closedir(dirp);
}
int main()
{
list(".");
return 0;
}
The result is like this:
hm1.c
Data
lab1.txt
result1
lab3.txt
.
..
.
..
result2
lab3.txt
.
..
result3
lab3.txt
.
..
a.c
.
..
a.out
result I want to print
hm1.c
Data
lab1.txt
result1
lab3.txt
result2
lab3.txt
result3
lab3.txt
a.c
a.out
Your isDir is returning true/false where it returns false (or zero) if you have . oe .. and then the true/false value of S_ISDIR in other cases
What you really need is the function to return one of 3 values SKIP, isFILE or isDIR and then write your print logic based on that.
You also need to fix your memory leaks
Also note that chdir(dir_name); changes the actual directory of the process, so once you return fromlist within your loop you will no longer be able to open the files or directories that you are looping over (because you are now in a different directory)
This will fix your problems and print the format you want
enum { doSkip, isFile, isDir } testDir(char *path, char *name)
{
struct stat st_buf;
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
return doSkip;
}
stat(path, &st_buf);
if (S_ISDIR(st_buf.st_mode))
return isDir;
return isFile;
}
void list(const char *path, int indentlevel)
{
DIR *dirp = opendir(path);
struct dirent *dentry;
char buf[10000]; // Lets just make the buffer sufficently big for this example
if (!dirp) {
printf("%*sNo access\n",indentlevel,"");
return;
}
while ((dentry = readdir(dirp)) != NULL) {
sprintf(buf,"%s/%s", path, dentry->d_name);
switch (testDir(buf,dentry->d_name)) {
case doSkip:
/* do nothing */
break;
case isDir:
printf("%*s%s:\n",indentlevel,"",dentry->d_name);
list(buf,indentlevel+4);
break;
case isFile:
printf("%*s%s\n",indentlevel,"",dentry->d_name);
break;
}
}
closedir(dirp);
}
int main()
{
list(".", 0);
return 0;
}
Another way to do it, if you're willing to go to C++, is to use std::experimental::filesystem, also (mostly) known as Boost.Filesystem. With that, you would do something like:
#include <experimental/filesystem> // Could substitute <boost/filesystem.hpp>
#include <boost/range/iterator_range.hpp>
#include <iostream>
using namespace std::experimental;
int main(int argc, char *argv[])
{
const auto path = filesystem::path{ argc > 1 ? argv[1] : "." };
if( filesystem::is_directory(path) )
{
std::cout << path << " is a directory containing:\n";
for( const auto& entry : boost::make_iterator_range( filesystem::recursive_directory_iterator{path}, {} ) )
{
std::cout << entry << "\n";
}
}
}
See it run here. Note that the directory iterators automatically skip . and ...

FreeImage include in c

Is there any way to include http://freeimage.sourceforge.net/index.html in my c test program without first installing the library? It fails to compile because of some memset..
Here is my C code. Is there any way to make it work? Please try compiling it and tell me how to do it if it works?
#define NAZIV_DATOTEKE 50
#include <stdio.h>
#include "FreeImage.h"
void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *message);
FIBITMAP* GenericLoader(const char* lpszPathName, int flag);
int main(){
FreeImage_Initialise();
FIBITMAP *dib, *ptr;
char ulaz_slika[NAZIV_DATOTEKE] = "bmp_24.bmp";
char izlaz_slika[NAZIV_DATOTEKE] = "free.bmp"; //podrazumevana vrednost
dib = GenericLoader(ulaz_slika, 0);
//slika = FreeImage_Load(FIF_BMP, "bmp_24.bmp", BMP_DEFAULT);
FreeImage_SetOutputMessage(FreeImageErrorHandler);
if (dib) {
printf("Ucitan \"%s\".\n", ulaz_slika);
}
FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(ulaz_slika, 0);
if ((fif != FIF_BMP) && (fif != FIF_ICO) && (fif != FIF_JPEG) && (fif != FIF_PNG) && (fif != FIF_TIFF)){
printf("Format slike nije podrzan.\n");
return 1;
}
ptr = FreeImage_ConvertTo24Bits(dib);
FreeImage_SetOutputMessage(FreeImageErrorHandler);
FreeImage_Unload(dib);
FreeImage_SetOutputMessage(FreeImageErrorHandler);
dib = ptr;
if (dib) {
printf("Konvertovan u RGB.\n");
}
const char *slika = (const char*)FreeImage_GetBits(dib);
if (FreeImage_Save(fif, dib, izlaz_slika, BMP_DEFAULT)) {
printf("Snimljen \"%s\".\n", izlaz_slika);
}
if (dib) {
FreeImage_Unload(dib);
}
FreeImage_DeInitialise();
return 0;
}
void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *message){
printf("\n*** ");
if(fif != FIF_UNKNOWN) {
if (FreeImage_GetFormatFromFIF(fif))
printf("%s Format\n", FreeImage_GetFormatFromFIF(fif));
}
printf(message);
printf(" ***\n");
}
FIBITMAP* GenericLoader(const char* lpszPathName, int flag) {
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
// check the file signature and deduce its format
// (the second argument is currently not used by FreeImage)
fif = FreeImage_GetFileType(lpszPathName, 0);
if(fif == FIF_UNKNOWN) {
// no signature ?
// try to guess the file format from the file extension
fif = FreeImage_GetFIFFromFilename(lpszPathName);
}
// check that the plugin has reading capabilities ...
if((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) {
// ok, let's load the file
FIBITMAP *dib = FreeImage_Load(fif, lpszPathName, flag);
// unless a bad file format, we are done !
return dib;
}
return NULL;
}
No you cannot. To compile your source, the linker needs the library.

Search for a file in $PATH on Linux in C

I would like to test whether GNUPlot is installed on the system on which my program is running.
For that, I figured I'll test for the existence of the gnuplot executable in the user's install locations through stat() call.
However, I don't know how to read the $PATH environment variable in C so I can test for the existence of the file in those locations.
Use the getenv() function.
char *paths = getenv("PATH");
To loop through the parts of the column-separated list of paths, use strchr():
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *dup = strdup(getenv("PATH"));
char *s = dup;
char *p = NULL;
do {
p = strchr(s, ':');
if (p != NULL) {
p[0] = 0;
}
printf("Path in $PATH: %s\n", s);
s = p + 1;
} while (p != NULL);
free(dup);
Use getenv() to inspect the value of a particular environment variable.
To read the PATH environment variable, use getenv("PATH").
However, if you just want to run gnuplot if it's available, and perform some fallback action if it's not, then you should just try to run it (e.g. with fork and execvp or posix_spawnp) and handle the failure case.
Let which do the work for you
if (system("which gnuplot"))
/* not installed or not in path or not executable or some other error */
If you need the full path for some reason, run which with popen.
Or run gnuplot with some flag which makes it return immediately with 0 */
if (system("gnuplot --version"))
/* not installed ... */
I had a similar need and resolved it by copying libc execvp code source. I did in the most cross platform I could think of(I have no guatanty and tested just on linux). If it's not such a matter to you and you care about performances, you should use acess or _acess. Note that there is no error check whatsoever and it will just return NULL or a founded openable file in path.
The accepted answer is sometime not acceptable, when you are willing to run the same small binary over and over, redoing the path search every time by calling execvp can be non negligable overhead.
So here is the code and associated tests, you will be mainely interested in the search_in_path_openable_file function.
.h file:
bool is_openable_file(char* path);
/*Return true if path is a readable file. You can call perror if return false to check what happened*/
char* search_in_path_openable_file(char* file_name);
/*Search into PATH env variable a file_name and return the full path of the first that is openable, NULL if not in path*/
char* search_executable(char* file_name);
/*Search file, if not openable and not absolute path(contain /), look for opennable file in the path. If nothing is openable, return NULL. If something is openable, return it as it is (not guaratented to have a full path, but garatanted to be openable)*/
.c file:
#include "file_info.h"
#include <stdio.h>
#include <string.h> //strcpy
/*I wanted to do a really cross platform way. access or _acess may be better*/
bool is_openable_file(char *path) {
FILE *fp = fopen(path, "r");
if (fp) {
// exists
fclose(fp);
return true;
}
return false;
}
bool is_openable_file_until(char *path_begin, size_t until) {
char old = path_begin[until];
path_begin[until] = 0;
bool res = is_openable_file(path_begin);
path_begin[until] = old;
return res;
}
/*You may thinks that libc would have done this function and use it to implement execp function family, but you would be wrong. They just hardcoded the search in every execp function. Unbelievable.
*
* So this function is a modification of their execvp function.
*
* */
char* search_in_path_openable_file(char* file){
char *path = getenv("PATH");
if (path == NULL)
return NULL;
size_t pathlen = strlen(path);
size_t len = strlen(file) + 1;
int total_max_size=pathlen + len;
char* buf=malloc(sizeof(char)*total_max_size);
if (*file == '\0') {
return NULL;
}
char *name, *p;
/* Copy the file name at the top. */
name = memcpy(buf + pathlen + 1, file, len);
/* And add the slash. */
*--name = '/';
p = path;
do {
char *startp;
path = p;
//Let's avoid this GNU extension.
//p = strchrnul (path, ':');
p = strchr(path, ':');
if (!p)
p = strchr(path, '\0');
if (p == path)
/* Two adjacent colons, or a colon at the beginning or the end
of `PATH' means to search the current directory. */
startp = name + 1;
else
startp = memcpy(name - (p - path), path, p - path);
/* Try to execute this name. If it works, execv will not return. */
if (is_openable_file(startp))
return startp;
} while (*p++ != '\0');
/* We tried every element and none of them worked. */
return NULL;
}
char* search_executable(char* file_name){
if (is_openable_file(file_name)){//See realpath manual bug. Watch out
return file_name;
}
if (strchr (file_name, '/') != NULL) //Don't search when it contains a slash.
return NULL;
return search_in_path_openable_file(file_name);
}
tests (As you see I did not test a lot this function, there may exist some problem, use at your risk):
#include "file_info.h"
#include "munit.h"
#include <stdbool.h>
#include <unistd.h>
static void generate_search_executable(char* test_str, char* expected){
char* res= search_executable(test_str);
if (res==NULL)
munit_assert_ptr(expected,==,NULL );
else
munit_assert_string_equal(expected,res);
}
static void generate_openable(char* test_str, bool expected){
bool res= is_openable_file(test_str);
munit_assert_true(expected==res);
}
static void generate_path_search(char* test_str, char* expected_res){
char* res= search_in_path_openable_file(test_str);
if (res==NULL)
munit_assert_ptr(expected_res,==,NULL );
else
munit_assert_string_equal(expected_res,res);
}
//TODO do for other platform, better test would also set path to a custom folder that we control
#define EXISTING_FILE_NOT_IN_PATH "/usr/include/stdlib.h"
#define EXISTING_FILE_IN_PATH "ls"
#define EXISTING_FILE_IN_PATH_FULL "/bin/ls"
#define NOT_EXISTING_FILE "/usrarfzsvdvwxv/ixvxwvnxcvcelgude/ssdvtdbool.h"
int main() {
generate_openable(EXISTING_FILE_IN_PATH, false);
generate_openable(EXISTING_FILE_NOT_IN_PATH, true);
generate_openable(NOT_EXISTING_FILE, false);
generate_path_search(EXISTING_FILE_IN_PATH, EXISTING_FILE_IN_PATH_FULL);
generate_path_search(NOT_EXISTING_FILE, NULL);
generate_path_search(EXISTING_FILE_NOT_IN_PATH, NULL);
generate_search_executable(EXISTING_FILE_IN_PATH, EXISTING_FILE_IN_PATH_FULL);
generate_search_executable(NOT_EXISTING_FILE, NULL);
generate_search_executable(EXISTING_FILE_NOT_IN_PATH, EXISTING_FILE_NOT_IN_PATH);
generate_search_executable("", NULL );
//test current folder existence(maybe it just depend on path containing .,I am not sure, in that case we should remove thoses tests
generate_search_executable("file_info_test", "file_info_test" );
}
To build on one of the previous answers, you can use getenv to get the contents of PATH and then iterate over its components. Instead of using strchr you can use strsep:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
bool exists(const char fname[])
{
return access(fname, F_OK | X_OK) != -1;
}
bool find_in_path(const char name[], char *fullpath, size_t sz) {
char *paths = strdup(getenv("PATH"));
char *tmp = paths; // to use in free
const char *item;
bool found = false;
while ((item = strsep(&paths, ":")) != NULL) {
snprintf(fullpath, sz, "%s/%s", item, name);
if (exists(fullpath)) {
found = true;
break;
}
}
free(tmp);
return found;
}
int main() {
char fullpath[512];
bool found = find_in_path("uname", fullpath, sizeof(fullpath));
if (found) {
printf("found: %s\n", fullpath);
}
return 0;
}
Using C++17 to get a vector of path elements.
% a.out ls
/bin/ls
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
using namespace std;
vector<string> get_paths (string str)
{
vector<string> result;
while(!str.empty())
{
if (auto pos { str.find_first_of (':') }; pos == string::npos)
{
result.push_back(str);
break;
}
else
{
result.emplace_back(str.substr(0, pos));
str.erase(0, pos + 1);
}
}
return move(result);
}
bool exist(const string& fname, int perm=F_OK) { return access(fname.c_str(), perm) == 0; }
int main (int argc, char *argv[])
{
auto result { get_paths(getenv("PATH")) };
for (auto pp : result)
{
string npath { pp };
if (*npath.rbegin() != '/')
npath += '/';
npath += argv[1];
if (exist(npath))
cout << npath << endl;
}
return 0;
}

Resources