Laharl 849
Recognized Expert Contributor
My Operating Systems professor has assigned homework that basically boils down to implementing ls -lra, but with a different output format. In other words, list the files and subdirectories (and a bit of data about them, for the files) in the current directory and its subdirectories using system calls. Unfortunately, it isn't working quite right. It works fine in the current directory, but when it goes into subdirectories, the stat() function always returns -1 (failure, error message is No such file or directory), yet when I print out the path and cd to the directory, the file is there. This is all occurring on FreeBSD 6.3 with gcc 3.4.6. - /* <name and email removed to protect the (sort of) innocent>
-
* This program lists all files and directories contained within the present working directory and its subdirectories.
-
* The following information is printed about each file: <filename> <size in bytes> <date and time last modified>
-
*/
-
-
#include <stdio.h>
-
#include <sys/types.h>
-
#include <sys/stat.h>
-
#include <dirent.h>
-
#include <errno.h>
-
#include <string.h>
-
#include <time.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-
#include <sys/param.h>
-
-
extern int errno;
-
-
void read_directory(const char* dirpath, int numspaces);
-
void print_file(struct dirent* file, struct stat* sb, int numspaces, char isdir);
-
-
int main(){
-
read_directory("./", 0);
-
return 0;
-
}
-
-
void read_directory(const char* dirpath, int numspaces){
-
struct dirent* entry;
-
DIR* dir = opendir(dirpath);
-
if (dir == NULL){
-
perror("Error reading directory: ");
-
return;
-
}
-
while ((entry = readdir(dir))){ //keep reading as long as there's something to read
-
if (entry == NULL) //all done or an error
-
return;
-
else if ((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)) //need to avoid infinite recursion!
-
continue;
-
struct stat* sb = (struct stat*) (malloc(sizeof(struct stat*)));
-
if (sb == NULL){
-
printf("Error allocating for stat pointer!\n");
-
return;
-
}
-
int i = stat(entry->d_name, sb); //This is the problematic call
-
if (i == -1){
-
printf("Attempting to read %s, but %s\n", entry->d_name, strerror(errno));
-
return;
-
}
-
else if (S_ISDIR(sb->st_mode)){
-
char* cwd = (char*)(malloc(MAXPATHLEN)); //gives 1023B
-
cwd = getcwd(cwd, MAXPATHLEN);
-
if (cwd == NULL){
-
printf("Error allocating absolute pathname!\n");
-
return;
-
}
-
sprintf(cwd, "%s/%s", cwd, entry->d_name);
-
print_file(entry, sb, numspaces, 1);
-
read_directory(cwd, numspaces+4);
-
free(cwd);
-
}
-
else if (S_ISREG(sb->st_mode)){
-
print_file(entry, sb, numspaces, 0);
-
}
-
free(sb);
-
}
-
}
-
-
void print_file(struct dirent* file, struct stat* sb, int numspaces, char isdir){
-
int i; //gcc 3.4 doesn't support bools as a builtin, seemingly
-
char spaces[numspaces+1];
-
spaces[numspaces] = '\0';
-
for (i = 0; i < numspaces * sizeof(char); i++)
-
spaces[i] = ' ';
-
if (!isdir){
-
char* clock = (char*)(malloc(27));
-
if (clock == NULL){
-
printf("Error allocating for clock characters!\n");
-
return;
-
}
-
ctime_r(&(sb->st_mtime), clock);
-
printf("%s%s %d %s", spaces, file->d_name, (unsigned int)(sb->st_size), clock);
-
free(clock);
-
}
-
else
-
printf("%s%s\n", spaces, file->d_name);
-
}
I have no idea what causes this, also when it goes into the subdirectories, it only looks at the first file if there's multiple. My only guess is that it might be an issue with how I obtain the absolute path...
4 7241 JosAH 11,448
Recognized Expert MVP - struct stat* sb = (struct stat*) (malloc(sizeof(struct stat*)));
-
The line above is line #39 in your program; you should allocate enough room for
an entire struct stat, not just enough room for a struct stat pointer.
kind regards,
Jos
Laharl 849
Recognized Expert Contributor
Whoops! Good catch on that one. Unfortunately, that doesn't fix the main problem.
JosAH 11,448
Recognized Expert MVP
Then print out that dirpath parameter when you (recursively) enter that function
and see the constructed path names ...
kind regards,
Jos
edit: also the stat() function needs an absolute path name, not a relative one.
Laharl 849
Recognized Expert Contributor
Thanks a ton, Jos! It turned out to be a pathname issue to stat() after all.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Oliver |
last post by:
Hi,
I wrote earlier this day about this problem but thought it had
something to do with images. However, I found out that for some reason
the readdir function is not working properly on my Suse...
|
by: Ask Josephsen |
last post by:
Hi NG
In wich order does "readdir()" read files from the disc? I've got an image
folder with images in the format "00001.jpg", "00002.jpg" etc. It seems
"readdir()" read the lowest first, but is...
|
by: Matt |
last post by:
Hello,
I am writing a script that opens needs to get a listing of files in a
directory, print that listing to a file and use that file as a quasi ftp
control file. My problem is that when I...
|
by: AES/newspost |
last post by:
My understanding -- I'm not an expert -- is that on (some? many? all?)
standard Internet servers a URL can point to a subdirectory name
followed by a backslash, and that links to this URL will...
|
by: Benton |
last post by:
Hi there,
I want to have an unrestricted root directory and some protected
subdirectories on my ASP.NET 2.0 application. I want each subdirectory to
have its own Login.aspx page.
The...
| |
by: kamalak |
last post by:
hi,
can someone help me in writing code in C# for getting the no of files in a directory and all its subdirectories and their subdirectories where the no of subdirectories and their...
|
by: Confused but working on it |
last post by:
Just wanted to say thanks for the posts helping me make ths work. Added
a few comments and after the readdir used a pattern match to test for
..jpg, and seems to work fine on my test setup. Maybe I...
|
by: mukeshrasm |
last post by:
Hi,
When I used readdir() function and echo the file name it prints . and .. as well.Whereas there is no such file with name . or.. , so why it is printing it. What does it mean? And one more...
|
by: dj |
last post by:
Hello All,
I am attempting to us os.walk to populate two lists with values from a
directory. The first list contains all the files in the directory and
subdirectories.
The second list contains...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |