#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <time.h>
int meets_criteria(char * entry_name, char * name) {
int criteria_met = 0;
if (name != NULL) {
if(strcmp(entry_name, name) == 0) {
criteria_met = 1;
}
}
return criteria_met;
}
void find(char * where, char * name) {
DIR * dir;
struct dirent * entry;
char * e_name;
char path[1024];
char curr_dir[] = ".";
char pre_dir[] = "..";
int e_type;
int is_dots;
dir = opendir(where);
if (dir) {
while ((entry = readdir(dir)) != NULL) {
e_type = entry -> d_type;
e_name = entry -> d_name;
is_dots = strcmp(e_name, curr_dir) == 0 || strcmp(e_name, pre_dir) == 0;
snprintf(path, sizeof(path),"%s/%s", where, e_name);
if (!is_dots) {
if (meets_criteria(e_name, name)) {
printf("%s\n", path);
}
if (e_type == DT_DIR) {
find(path, name);
}
}
}
closedir(dir);
}
}
int main () {
char *where;
char *name;
char c[1024];
char d[1024];
printf("Enter dir\n");
scanf("%s", c);
where = c;
printf("Enter file\n");
scanf("%s", d);
name = d;
find(where, name);
return 0;
}
I have this code that does the following. Pass in a directory to start searching for a file.
Pass in a string of a name of the file you want to “find” in your filesystem on your machine.
Start at the given directory and for each file that matches the search string, print the absolute path + filename to the screen.
I want to take the program and add threading in place of the recursive part of the find function. I'm having problem spawning detached threads and ensuring they properly run without locks or any limitations.
Any help would be appreciated.
Related
prev problem was not described well.
I need to retrieve a file name and assign that name to a variable. The only thing is that the file can have any name.
The thing that I know is the file extension and there can only be one file of this extension.
Is there any way to implement this in C?
Update #1
File extension is *.knb and target is Linux system.
#include <stdio.h>
#include <dirent.h>
#define LOG_() do{fprintf(stdout, " UNIT TESTING...\n");}while(0)
char* knbname;
void fName(void);
int main()
{
LOG_();
fName(void);
//do some more code
return 0;
}
void fName(void){
DIR *d;
struct dirent *dir;
d = opendir(".");
if (d)
{
//find file with *.knb extension and place name into knbname
}
closedir(d);
}
I don't know how to find for a *.knb file in the fName() function and how to assign name to knbname. Any help will be appreciated
You are reinventing POSIX's glob() function. Here is an example of how to use it (see the flags in manpage for more options):
#include <glob.h>
int main(void)
{
glob_t glob_res = { 0 };
glob("*.knb", 0, NULL, &glob_res);
if (glob_res.gl_pathc == 0) puts("No match.");
else printf("First match is %s\n", glob_res.gl_pathv[0]);
globfree(&glob_res);
return 0;
}
After Andrew pointed out some major flaw in the previous design, i will make another attempt for a solution...
int hasExtension(char *filename, char *extension);
int main(void)
{
DIR *directory;
struct dirent *entry;
if(NULL == (directory = opendir(".")))
{
perror("Failed to open directory\n");
exit(1);
}
while( NULL != (entry = readdir(directory)) )
{
if(DT_REG == entry->d_type) { // Regualar files only
if(hasExtension(entry->d_name, ".knb"))
{
printf("Match - %s\n", entry->d_name);
} else {
printf("No match - %s\n", entry->d_name);
}
}
}
closedir(directory);
return 0;
}
int hasExtension(char *filename, char *extension)
{
size_t filename_len, extension_len;
if((NULL == filename) || (NULL ==extension))
return 0;
filename_len = strlen(filename);
extension_len = strlen(extension);
if(filename_len < extension_len)
return 0;
return !strcmp(filename + filename_len - extension_len, extension);
}
EDIT : Note that It's not that I can't access the memory allocated by storeContents() in main() if you think so. Program crashes during the execution of storeContents()
The program fails here :
strcpy(contents[count], dir->d_name);
printf("Stored %s(out hiddenVisible)\n", dir->d_name); // for testing
It's strcpy() not the printf(), I added it just for the reference.
The debugger(gdb) says :
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7f3cd72 in __strcpy_ssse3 () from /usr/lib/libc.so.6
I am making a program that involves the following function "storeContents"(It stores contents' names of a directory in a dynamic array). There are two issues with this function : (1) It says "Stored file_name" twice for the first file and (2) says "Segmentation fault". I can't figure out either of them. Thanks for your efforts!
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/types.h>
#include <limits.h>
static short hiddenVisible = 0;
/* Store directory's contents in **contents */
static char ** storeContents(struct dirent *dir, DIR *dirp, unsigned numOfContents);
/* Count files/directories in a directory */
static unsigned getNumOfContents(struct dirent *dir, DIR *dirp);
int main(int argc, char const *argv[])
{
char **contents;
DIR *dirp;
struct dirent *dir;
unsigned numOfContents;
dirp = opendir("/home/gaurav");
if((dir = readdir(dirp)) == NULL) {
perror("readdir");
exit(1);
}
/* Getting number of files/directories */
numOfContents = getNumOfContents(dir, dirp);
printf("There are %u files.\n", numOfContents);
/* To position again to the first entry */
rewinddir(dirp);
contents = storeContents(dir, dirp, numOfContents);
/* Print contents */
for(unsigned i = 0; i < numOfContents; ++i)
printf("%s\n", contents[i]);
closedir(dirp);
return 0;
}
char **
storeContents(struct dirent *dir, DIR *dirp, unsigned numOfContents) {
char **contents;
unsigned count = 0;
/* Allocating memory for entries */
contents = malloc(numOfContents * sizeof(*contents));
/* Allocating memory for each '*contents' */
for(unsigned i = 0; i < numOfContents; i++)
contents[i] = (char *)malloc(NAME_MAX); /* we know char is 1 byte, so no "sizeof" */
while(count < numOfContents) {
/* Ignore "." and ".." */
if(!(strcmp(dir->d_name, ".")) || !(strcmp(dir->d_name, ".."))) {
if((dir = readdir(dirp)) == NULL) {
perror("readdir");
exit(1);
}
continue;
}
if(hiddenVisible) {
strcpy(contents[count], dir->d_name);
if((dir = readdir(dirp)) == NULL) {
perror("readdir");
exit(1);
}
count++;
} else {
if(dir->d_name[0] == '.')
if((dir = readdir(dirp)) == NULL) {
perror("readdir");
exit(1);
}
else {
strcpy(contents[count], dir->d_name);
if((dir = readdir(dirp)) == NULL) {
perror("readdir");
exit(1);
}
count++;
}
}
}
return contents;
}
unsigned
getNumOfContents(struct dirent *dir, DIR *dirp) {
unsigned count = 0;
while(dir) {
if(hiddenVisible) {
/* Ignore "." and ".." */
if(!(strcmp(dir->d_name, ".")) || !(strcmp(dir->d_name, ".."))) {
if((dir = readdir(dirp)) == NULL) {
perror("readdir a");
exit(1);
}
continue;
}
count++;
if((dir = readdir(dirp)) == NULL) {
perror("readdir b");
exit(1);
}
} else {
if(dir->d_name[0] == '.') {
if((dir = readdir(dirp)) == NULL) {
perror("readdir c");
exit(1);
}
}
else {
count++;
if((dir = readdir(dirp)) == NULL) {
perror("readdir d");
exit(1);
}
}
}
}
return count;
}
contents in the function storeContents is a local copy of contents from main.
Changing it in the function does not change the variable in main.
You should return the array. Change
static void storeContents(struct dirent *dir, DIR *dirp, char **contents, unsigned numOfContents);
to
static char **storeContents(struct dirent *dir, DIR *dirp, unsigned numOfContents);
,return contents; in the function and call it like char **contents = storeContents(...);
Some bugs:
contents is a local parameter to the function, it will not get returned to main(). See Dynamic memory access only works inside function.
contents = (char **)malloc(numOfContents); is wrong, you need to allocate room for numOfContents pointers. Change this to contents = malloc(numOfContents * sizeof(*contents)).
You should check each call to readdir and make sure it doesn't return NULL.
This is for a project for university (small replica of a Catan game) and I'm struggling a bit with this part, we have the read an INI file with fairly simple formatting, it only has some comments starting with ';' and then it's just tags with a value in front:
xdim=4
ydim=5
N=D
S=L2
E=S10
W=D
etc...
I have this function to read from an INI file and address the read values to the correct struct element. But it seems like it doesn't even read the file, the struct is a simple struct with xdim and ydim, after I call the func xdim is '&d&d&d&d etc...' and ydim is 0
I've tried placing in some printf's just to see if the values from the INI file itself where being read wrong, but nothing is printed.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 128
typedef struct UNIT { /**struct used in an array for the rest of the INI values*/
char N[4];
char S[4];
char W[4];
char E[4];
char Building;
}UNIT;
typedef struct{ /**This is declared in main and passed to the functions*/
UNIT *grid;
unsigned int xdim;
unsigned int ydim;
} MAP_CONFIG;
void set_config_val(MAP_CONFIG *config, const char *key, int val) {
if (config == NULL)
return;
if (strcmp(key, "xdim") == 0){
printf("here");
config->xdim = val;
}
else if (strcmp(key, "ydim") == 0){
printf("here");
config->ydim = val;
}
else{
;
}
}
void read_config(MAP_CONFIG *config,FILE *f) {
char str[MAX];
char *token;
const char *delim = "=\n";
while (1) {
fgets(str, MAX, f);
if(feof(f)!= 0) break;
puts(str);
if (strchr(str, '=')!=NULL) {
char varname[MAX];
int value;
token = strtok(str, delim);
strcpy(varname, token);
token = strtok(NULL, delim);
value = atoi(token);
printf("&d", token);
set_config_val(config, varname, value);
}
}
config = malloc(sizeof(MAP_CONFIG));
config->grid = calloc(config->xdim * config->ydim, sizeof(UNIT));
close(f);
return;
}
open file function:
FILE *openFile(char *nome, char *mode) {
FILE *f;
printf("Opening file %s\n", nome);
f = fopen(nome, mode);
if (f == NULL) {
fprintf(stderr, "*** It was not possible to open the file %s.", nome);
exit(1);
}
return f;
}
test main im using:
int main(int argc, char **argv) {
MAP_CONFIG map;
MAP_CONFIG *mapa = ↦
FILE *f;
char *filename;
for (int i = 0; i < argc; i++)
printf("Parametro %d: %s\n", i, argv[i]);
if (argc >= 2) {
filename = argv[1];
}
else {
printf("Opening base map file..\n");
filename = "mapa.ini";
}
f = openFile(filename, "r");
read_config(mapa, f);
printf("%d %d", map.xdim, map.ydim);
return 0;
}
I just want it to read the xdim and ydim, and then repeat the process to an array of structs for each struct to get the correct value of the N,S,E,W present in the INI file... Help!
I am doing a recursive walk through directories to make changes to files. My change file function needs the full path of the file to be able to do stuff. However, what my program is doing right now is just getting the name of the current file or folder but not the full path.
My approach is that I would make a string and keeps appending names to it until I get the full path. However, because I'm doing recursion, I'm having troubles passing the string around to append more strings to it.
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
void recursiveWalk(const char *pathName, char *fullPath, int level) {
DIR *dir;
struct dirent *entry;
if (!(dir = opendir(pathName))) {
fprintf(stderr, "Could not open directory\n");
return;
}
if (!(entry = readdir(dir))) {
fprintf(stderr, "Could not read directory\n");
return;
}
do {
if (entry->d_type == DT_DIR) { // found subdirectory
char path[1024];
int len = snprintf(path, sizeof(path)-1, "%s/%s", pathName, entry->d_name); // get depth
path[len] = 0;
// skip hidden paths
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
fprintf(stdout, "%*s[%s]\n", level*2, "", entry->d_name);
// Append fullPath to entry->d_name here
recursiveWalk(path, fullPath, level + 1);
}
else { // files
fprintf(stdout, "%*s- %s\n", level*2, "", entry->d_name);
//changeFile(fullPath);
}
} while (entry = readdir(dir));
closedir(dir);
}
int main(int argn, char *argv[]) {
int level = 0;
recursiveWalk(".", "", level);
return 0;
}
Well there are a number of little problems in your code.
you never use nor change fullPath in recursiveWalk
your formats are weird : you use level*2 to limit the number of characters printed from an empty string
you compute the actual path only when you have found a directory, while you say you need it to change a file.
you add path[len] = 0 after a snprintf when snprintf guarantees that but buffer is null terminated
But apart from that, you correctly pass the path to the analyzed dir append to the path passed in initial call, but in pathName variable, and computed as path.
So a possible fix for your code would be :
fix the formats for printf
remove the unused fullPath parameter from recursiveWalk
allways compute path and use it in the file branch
comment out the unnecessary path[len] = '\0'
I also replaced while (entry = readdir(dir)); with while ((entry = readdir(dir))); to explicitely tell the compiler that I want to set entry and then test its value - and remove the warning
Possible code:
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
void recursiveWalk(const char *pathName, int level) {
DIR *dir;
struct dirent *entry;
if (!(dir = opendir(pathName))) {
fprintf(stderr, "Could not open directory\n");
return;
}
if (!(entry = readdir(dir))) {
fprintf(stderr, "Could not read directory\n");
return;
}
do {
char path[1024];
int len = snprintf(path, sizeof(path)-1, "%s/%s", pathName, entry->d_name); // get depth
// path[len] = 0;
if (entry->d_type == DT_DIR) { // found subdirectory
// skip hidden paths
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
fprintf(stdout, "%s [%s] (%d)\n", pathName, entry->d_name, level);
// Append fullPath to entry->d_name here
recursiveWalk(path, level + 1);
}
else { // files
fprintf(stdout, "%s (%d)\n", path, level);
//changeFile(fullPath);
}
} while ((entry = readdir(dir)));
closedir(dir);
}
int main(int argn, char *argv[]) {
int level = 0;
recursiveWalk(".", level);
return 0;
}
Recursion is a succinct way to express things (especially walking directories), but actually you should normally avoid it in practice. If the directory tree is deep enough, it will crash your software.
Using a queue eliminates the need for recursion, and is generally an efficient way to traverse.
I'm including the code I use to process directory trees in a project...
static int on_dir(const char* const dir, struct duplicate** dp) {
bool r = opts.recurse;
DIR* d = opendir(dir);
if (!d)
return - 1;
struct dirent* de;
while ((de = readdir(d))) {
struct stat s;
size_t bs = strlen(dir) + strlen(de->d_name) + 2;
char b[bs];
const char* const a = strjoin(b, dir, de->d_name, '/');
if (lstat(a, &s)) {
print_error("unable to stat %s", d);
continue;
}
if (S_ISREG(s.st_mode))
if (on_file(a, &s, dp))
print_error("unable to process file %s/%s", dir, de->d_name);
}
if (!r) {
if (closedir(d))
on_fatal("unable to close directory %s", dir);
return 0;
}
rewinddir(d);
while ((de = readdir(d))) {
struct stat ds;
size_t bs = strlen(dir) + strlen(de->d_name) + 2;
char b[bs];
const char* const d = strjoin(b, dir, de->d_name, '/');
if (lstat(d, &ds)) {
print_error("unable to stat %s", d);
continue;
}
if (S_ISDIR(ds.st_mode)) {
const char* const dot = ".";
const char* const dotdot = "..";
if (!strcmp(dot, de->d_name) || !strcmp(dotdot, de->d_name))
continue;
struct path* p = path_create(strcpy(fmalloc(bs), d));
queue_add(&paths, &p->queue);
}
}
if (closedir(d))
print_error("unable to close directory %s", dir);
return 0;
}
and the code for strjoin
static inline char* strjoin(char* restrict const d, const char* restrict const a, const char* restrict const b, const char c) {
size_t na = strlen(a);
size_t nb = strlen(b);
memcpy(d, a, na);
d[na] = c;
memcpy(d + na + 1, b, nb);
d[na + nb + 1] = '\0';
return d;
}
I'm hoping this helps. Please feel free to use any of the code you find in the git repository.
I have a program that recursively prints the cwd size plus containing file sizes and repeats for each sub directory.
Recursive directory traversal function: (Note the reason for printf in this function and passing two strings is that the output needs to be in a special format so I can't just output the actual filepath. Also I am just learning about system calls to work with directories in ubuntu so if you have any comments on improvements on the code I would appreciate them (style or using something more simple to accomplish the same).
#include <sys/stat.h>
#include <limits.h>
#include <dirent.h>
#include <libgen.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define FIRST_ARG 1
#define SECOND_ARG 2
char* fileName;
FILE* fileToRead;
DIR* directoryToRead;
int printFileSize(char*);
int printWorkingSize(char*, char*);
int printSize(char*, char*);
int printDir(char*, char*);
int printCurrentDir(char*, char*);
int bytesToKbytes(long long, char*);
int main(int argc, char** argv) {
if(argc == FIRST_ARG) {
char currentDir[PATH_MAX + 1] = "";
char* currentDirectory;
directoryToRead = opendir (".");
if (directoryToRead == NULL){
exit (EXIT_FAILURE);
}
closedir(directoryToRead);
printCurrentDir(currentDirectory, ".");
}
return 0;
}
int printCurrentDir(char* name, char* path) {
struct dirent *dir;
struct stat statBuffer;
char fileName[PATH_MAX + 1];
char filePath[PATH_MAX + 1];
DIR* openDir;
if((openDir = opendir(path)) == NULL) {
printf("Could not open %s\n", path);
}
stat(path, &statBuffer);
if(strcmp(path, ".") == 0) {
printf("%lld .\n", (long long)statBuffer.st_size);
} else {
printf("%lld %s\n", (long long)statBuffer.st_size, name);
}
while (TRUE) { // go through contents of current directory
dir = readdir(openDir);
if(!dir) {
break;
}
if((strcmp(dir->d_name, "..") == 0) || (strcmp(dir->d_name,".") == 0)) {
continue;
}
if(name == NULL) {
strcpy(fileName, dir->d_name);
} else {
strcpy(fileName, name);
strcat(fileName, "/");
strcat(fileName, dir->d_name);
}
strcpy(filePath, path);
strcat(filePath, "/");
strcat(filePath, dir->d_name);
if(dir->d_type == DT_DIR) { // if the next file is a directory
if(!printCurrentDir(fileName, filePath)) {
return FALSE;
}
}
else if(!printWorkingSize(fileName, filePath)) {
return FALSE;
}
}
return TRUE;
}
//output file size in bytes followed by name-> (char* file)
int printWorkingSize(char* file, char* path) {
struct stat statBuffer;
stat(path, &statBuffer);
char result[PATH_MAX];
if(bytesToKbytes((long long)statBuffer.st_size, result) == FALSE) {
sprintf(result, "%lld", (long long)statBuffer.st_size);
}
printf("%5s %s\n", result, path);
return TRUE;
}
// convert bytes to kilobytes if more than 5 digits
int bytesToKbytes(long long bytes, char* result) {
if(!(bytes > 99999)) {
return FALSE;
}
char size[PATH_MAX];
sprintf(size, "%lld", bytes);
size[3] = 'K';
size[4] = '\0';
strcpy(result, size);
return TRUE;
}
It is operating system specific. On Linux and POSIX, you should simply use the nftw(3) library function, which is recursively reading the directories and already calling stat(2) for you.
The problem is effectively in the way you use bytesToKbytes. It returns a boolean to indicate if it wrote anything in result and :
you do not preload anything in result before calling bytesToKbytes
you do not test return value to write anything in resul if size was less than 99999
You could use :
int printWorkingSize(char* file, char* path) {
struct stat statBuffer;
stat(path, &statBuffer);
char result[PATH_MAX];
if (! bytesToKbytes((long long)statBuffer.st_size, result)) {
sprintf(result, "%lld", (long long)statBuffer.st_size);
}
printf("%5s %s\n", result, path);
return TRUE;
}
You could also change bytesToKbytes to put length in bytes into resul if size is less than 99999