473,410 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

Adding directories/files to a list - help!

Hi, I am trying to get a program working whereby directory and file
names are read into a list. I have been given the original list
structure - it appears that the directory names should be added to the
list, then the filenames added to another separate but connected list
within each directory node.

So far I have the program working in that the directory and file names
are all added to the list, i.e. it currently doesn't distinguish
between directories/files, and I can print the contents out. I'm
really not sure how to go about adding the filenames to the content
list though so any help would be very much appreciated.

------------------------------------------------------------------------------------------------------------

enum Tnodetype {n_file, n_directory, n_symlink};
typedef enum Tnodetype Nodetype;

struct Tcell{
struct Tcell *next;
char * localname;
char * fullname;
struct Tcell * content; // list of files in directory
Nodetype nodetype;
};
typedef struct Tcell Cell, *List;

char * fullname(char *root, char * name)
{
const char * sep = "/";
char * res = (char *)
malloc(strlen(root)+strlen(sep)+strlen(name)+1);
strcpy(res,root);
strcat(res,sep);
strcat(res,name);
return res;
}

int notdot (char * s)
{
return strcmp(s,".") != 0 && strcmp(s,"..")!= 0 ;
}

int issymlink(char * name)
{
struct stat buf;
lstat(name, &buf);
return (buf.st_mode & S_IFMT) == S_IFLNK;
}

int isdir(char * name)
{
struct stat buf;
lstat(name, &buf);
return (buf.st_mode & S_IFMT) == S_IFDIR;
}

List cons(List lp, char * fname, char * lname, Nodetype nodetype)
{
List res = (List)malloc(sizeof(Cell));
res->localname = strdup(lname);
res->fullname = strdup(fname);
res->nodetype = nodetype;
res->next = lp;
return res;
}

List add_symlink(List lp, char *path, char * name)
{
Nodetype nodetype = n_symlink;
return cons(lp, path, name, n_symlink);
return lp;
}

List add_directory(List lp, char *path, char * name)
{
Nodetype nodetype = n_directory;
return cons(lp, path, name, n_directory);
return lp;
}

List add_file(List lp, char *path, char * name)
{
Nodetype nodetype = n_file;
return cons(lp, path, name, n_file);
return lp;
}

List filecheck(List lp, char * dirname)
{
DIR *dd;
struct dirent *dp;
int res = 0;
dd = opendir(dirname);
assert(dd);
while ((dp = readdir(dd)))
if (notdot(dp->d_name)){
char * name2 = fullname(dirname, dp->d_name);
if (issymlink(name2))
lp = add_symlink(lp, name2, dp->d_name);
else if (isdir(name2)){
lp = add_directory(filecheck(lp,name2), name2,
dp->d_name);
}
else
lp = add_file(lp, name2, dp->d_name);
}
closedir(dd);
return lp;
}

void showtree (List lp)
{
for ( ; lp ; lp = lp->next)
printf(" %s\n",lp->fullname);
}

int main(int argc, char * argv[]) {
List lp = 0;
char * dirname = strdup(( argc >1) ? argv[1] : getenv("HOME"));
showtree(filecheck(lp, dirname));
return (0);
}

-------------------------------------------------------------------------------------------------------------

Dec 6 '06 #1
4 2092
Use this instead:
http://www.imatix.com/html/sfl/

Dec 7 '06 #2

th******@gmail.com wrote:
Hi, I am trying to get a program working whereby directory and file
names are read into a list. I have been given ...
I smell homework...

Dec 7 '06 #3
dc*****@connx.com wrote:
Use this instead:
For what? Getting rid of Google's annoying habit of stimulating
contextless posting? Now that would be a useful program.

Richard
Dec 8 '06 #4
Richard Bos wrote:
dc*****@connx.com wrote:
Use this instead:

For what? Getting rid of Google's annoying habit of stimulating
contextless posting? Now that would be a useful program.
As far as I can tell, Google does provide default quoting. Those making
contextless postings do so by snipping the quoted text manually.

Dec 8 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tum | last post by:
Hi folks, I've been trying to make a decision and it's driving me crazy. Is a directory a file or is a directory NOT a file but a node? Should I have A)
3
by: Arpi Jakab | last post by:
I have a main project that depends on projects A and B. The main project's additional include directories list is: ...\ProjectA\Dist\Include ...\ProjectB\Dist\Include Each of the include...
10
by: Dan | last post by:
Hi - I'm about a week into learning VB.NET, and I'm finding I can't delete any of the VB.NET directory structures that contain my test projects I've been trying to create. I've never seen this...
3
by: simonharrison | last post by:
Hello everyone. Hopefully someone can point me in the right direction here. I'm wanting to write a script to open microsoft word and adobe pdf documents . Here is a little background: At the...
5
by: Justin Fancy | last post by:
Hi everyone, I need some help. I'm placing text files into a created database using vb.Net. The problem is that, i need two seperate sql statements to add both files because they are in...
4
by: rn5a | last post by:
I have a ListBox which should list all the files & directories that exist in a particular directory. The problem is I can get the ListBox to list either all the files or all the directories but not...
1
by: rn5a | last post by:
A ListBox lists all the folders & files existing in a directory named 'MyDir' on the server. Assume that the ListBox lists 2 directories - 'Dir1' & 'Dir2' i.e. these 2 directories reside in the...
5
by: Jandre | last post by:
Hi I am a python novice and I am trying to write a python script (most of the code is borrowed) to Zip a directory containing some other directories and files. The script zips all the files fine...
63
by: David Mathog | last post by:
There have been a series of questions about directory operations, all of which have been answered with "there is no portable way to do this". This raises the perfectly reasonable question, why,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.