C/Unix question

siftings81

Member
Sep 3, 2001
61
0
0
I want to write a c program that searches the current working directory to find out the file dependency among c(c++) source code files. I'm pretty new to the unix environment, and learned c++ first, so any help would be appreciated.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Start by learning to use the man command, type 'man opendir' and it will give you the manual page for the opendir function (provided the development man pages are installed).
 

siftings81

Member
Sep 3, 2001
61
0
0
Alright, got it to open the cwd, but is there a better way to check for c, cpp, h files than using a string compare with the filename. I'm not sorting all the non source code files out. Can I extract the file extension of each file and use that for comparison? thanks for the input.
 

Ynog

Golden Member
Oct 9, 2002
1,782
1
0
You can find the length of the string and work backwards to find the file extension.

Now I am not sure if this is a project or what exactly this is for, but if you all you want is dependencies for
source files. Like all the headers a c or c++ file uses, there are already things you can do for that.
Two examples would be.

gcc -M sourcefile.c This will give you all the header files sourcefile.c needs to compile.

makedepend also can do this, but its been a while since I have done this.

Now I am not sure if this is why you are writing this program. Or if you are just writing it for
practice. However your task isn't a easy one. Cause if file one includes file two that includes file three,
then you have multiple layers of dependency, not to mention if file one, includes file two and file three, who both
include file four, you don't want two file fours in the dependency list for file one.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
You could just use a globbing function instead of checking the extensions yourself, I'm not sure what libs you need though.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: siftings81
Alright, got it to open the cwd, but is there a better way to check for c, cpp, h files than using a string compare with the filename. I'm not sorting all the non source code files out. Can I extract the file extension of each file and use that for comparison? thanks for the input.

Here is a quick-and-dirty way of how you might accomplish comparing the extension to those extensions that are considered dependencies. I think the solutions suggested by others would be superior to rolling your own, but just for reference:

#define DEPENDENCY_COUNT 3

static char *dependencyExtensions[] =
{ "h", "cpp", "c" };

int isDependency(const char *filename);

int isDependency(const char *filename)
{
char *extension = NULL;
int z;

if (filename == NULL)
return 0;

if ((extension = strchr(filename, '.')) == NULL)
return 0;

extension++;

for (z = 0; z < DEPENDENCY_COUNT; z++)
if (!strcmp(extension, dependencyExtensions[z]))
return 1;

return 0;
}

[edit]Using 'i' as an indexer always gets me because the forums parse it[/edit]
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
man 3 glob :)

It kinda depends on how easy or hard you want to make this. I mean really, you could do without C altogether and just use find along with some shell and/or perl or whatever.
 

siftings81

Member
Sep 3, 2001
61
0
0
Thanks for the help. Yes, this is apart of the assignment below:

1. Write a c program that searches the current working directory to find out the file dependency among c(c++) source code files (including *.h, *.c or *.cpp). It will also generate a makefile that can be used to create the executable file for the project and clean up the object files in the directory.

2. Write a c program (let?s call it umake) that performs similar tasks as the make tool.
 

siftings81

Member
Sep 3, 2001
61
0
0
I got the first one working, but I'm undecided on how to go about writing the second program. I'm thinking if I could write the contents of the makefile to a temp file, but in reverse order(bottom is top), I could execute the rules for the dependencies first, then run the command for the main target. I'll worry about the substitution part later, but any input would be greatly appreciated. (like, how to reverse the order)