Thread: Checking Directory and then Compiling

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    5

    Checking Directory and then Compiling

    USing a linux OS, which woud be the best way to look in a directory and then once i find code to compiling it.

    Mainly compiing and searching the directory for a certain file (*.c). What two packages are used for that on Linux? Thanks if you can help me.

    -Sean

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Usually, you'd use a Makefile per project, but
    Code:
    find . -name *.c -print
    will at least find the files you're looking for in the current directory and any of its subdirectories.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    Ok, I figured out how to search the local directory or any directory for that matter.
    Mainly using <dirent.h>,

    I can't exactly figure out how to catch a file, like one that says (8.c) at the end to compile it.

    I am making a batch system so of course there is more to it than this. I just need a little help with this, thanks.

    PX> to poster above: I need to write code that compiles it, not do it from the comman line, but it is still linux specific, or with the cc compiler.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    If you must do it in C, you could use scandir, which may be what you've already found. just write a filter which returns 1 if the file's extension is .c, and pass the function name as the third parameter to scandir.

    Code:
    int is_compilable_file(const struct direct *d)
    {
         return (strstr(d->d_name, ".c") ? 1 : 0);
    }
    
    struct dirent **files;
    int num_files = scandir(path_to_search, &files, is_compilable_file, 0);
    if (-1 == num_files)
    {
        // Handle error here
    }
    
    int i = 0;
    for (; i < num_files; ++i)
    {
        const char *compilable_file_name = files[i]->d_name;
        // Do whatever you need to compile it here.
    
        // Need to free memory allocated within scandir
        free(files[i]);
    }
    free(files);
    Last edited by rags_to_riches; 02-28-2008 at 06:19 AM. Reason: Mistakes to be fixed

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    How do i compile it? I wonder if i should just output cc -c struct->dname or something.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    system or popen would work.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    Code:
    
    #include <dirent.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    
    int copyfile(char *fromfile, char *tofile)
    {
    	FILE *ifp, *ofp;
    	int c;
    
    	if((ifp = fopen(fromfile, "r")) == NULL) return -1;
    	if((ofp = fopen(tofile, "w")) == NULL) { fclose(ifp); return -1; }
    
    	while((c = getc(ifp)) != EOF)
    		putc(c, ofp);
    
    	fclose(ifp);
    	fclose(ofp);
    
    	return 0;
    }
    
    int main()
    {
     static MAX = 1000;
     DIR *pdir;
     struct dirent *ent;
     char *c_file;
     char *c_file_hold[MAX];
    
     pdir = opendir(".");
    
     if (!pdir)
     {
     printf ("opendir() failure; terminating");
     exit(1);
     }
    
     int i = 0; //counter
     while((ent=readdir(pdir)))
     {       
    	//strncpy(c_file , ent->d_name, 10);
    	printf( "%s\n" , ent->d_name);
           
           if(istrstr(ent->d_name, ".c") ? 1 : 0)
    	{
              sprintf(c_file, "cc %s", ent->d_name); //THIS HERE IS GIVING ME A SEGMENTATION FAULT
              system(c_file);
    	  system("a.out");
            } 
     }
    
     closedir(pdir);
    }
    Anyone know hwat I am doing wrong here? How can i get this to compile ?

Popular pages Recent additions subscribe to a feed