stat() function call failing

stat() function call failing

Post by Greg Marti » Sat, 19 Jun 1999 04:00:00



I'm storing all the entries in a directory, in an array and then would like
to test and seperate the directories and files. I'm trying to use stat() to
get the attributes but it fails on me. I'm afraid I've been unable to grasp
why. Here is the listing for my code.
TIA and regards,
Greg Martin

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>

int main(void)
{
        struct dirent *pdirstruct;
        char **pfname;
        const char basedir[] = "/usr/home/gregm/public_html";
        int cnt, i;
        DIR *dp;
        struct stat *buf;

        cnt = 0;
        if((pfname = malloc(sizeof(char *))) == NULL)
        {
         puts("malloc() failed");
         exit(EXIT_FAILURE);
        }

        if((dp = opendir(basedir)) != NULL)
        {
                while((pdirstruct = readdir(dp)))
                {
                       if((pfname = realloc(pfname, (cnt + 2)
                             * sizeof(char *))) == NULL)
                       {
                            puts("realloc() failed");
                            while(cnt) free(pfname[--cnt]);
                            exit(EXIT_FAILURE);

                       }
                       if((pfname[cnt] = malloc(strlen(pdirstruct->d_name)
                             +1)) == NULL)
                       {
                            puts("malloc() failed");
                            while(cnt) free(pfname[--cnt]);
                            exit(EXIT_FAILURE);
                       }

                      strcpy(pfname[cnt], pdirstruct->d_name);
                            pfname[++cnt] = NULL;

                }
        }
        else
       {
                puts("opendir(failed)");
                free(pfname);
                exit(0);
        }

        for (i = 0; i < cnt; ++i)
        {
         if(stat(pfname[i], buf) != 0)

          puts("stat() failed");
          exit(EXIT_FAILURE);
         }
         if(S_ISDIR(buf->st_mode))
         {
                 puts(pfname[i]);
                }

                free(pfname[i]);
        }

        return 0;

Quote:}

 
 
 

stat() function call failing

Post by Fletcher Glen » Sat, 19 Jun 1999 04:00:00


Where is the real storage behind the "buf" pointer?

--
                Fletcher Glenn


> I'm storing all the entries in a directory, in an array and then would like
> to test and seperate the directories and files. I'm trying to use stat() to
> get the attributes but it fails on me. I'm afraid I've been unable to grasp
> why. Here is the listing for my code.
> TIA and regards,
> Greg Martin

> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <dirent.h>
> #include <sys/stat.h>
> #include <sys/types.h>

> int main(void)
> {
>         struct dirent *pdirstruct;
>         char **pfname;
>         const char basedir[] = "/usr/home/gregm/public_html";
>         int cnt, i;
>         DIR *dp;
>         struct stat *buf;

>         cnt = 0;
>         if((pfname = malloc(sizeof(char *))) == NULL)
>         {
>          puts("malloc() failed");
>          exit(EXIT_FAILURE);
>         }

>         if((dp = opendir(basedir)) != NULL)
>         {
>                 while((pdirstruct = readdir(dp)))
>                 {
>                        if((pfname = realloc(pfname, (cnt + 2)
>                              * sizeof(char *))) == NULL)
>                        {
>                             puts("realloc() failed");
>                             while(cnt) free(pfname[--cnt]);
>                             exit(EXIT_FAILURE);

>                        }
>                        if((pfname[cnt] = malloc(strlen(pdirstruct->d_name)
>                              +1)) == NULL)
>                        {
>                             puts("malloc() failed");
>                             while(cnt) free(pfname[--cnt]);
>                             exit(EXIT_FAILURE);
>                        }

>                       strcpy(pfname[cnt], pdirstruct->d_name);
>                             pfname[++cnt] = NULL;

>                 }
>         }
>         else
>        {
>                 puts("opendir(failed)");
>                 free(pfname);
>                 exit(0);
>         }

>         for (i = 0; i < cnt; ++i)
>         {
>          if(stat(pfname[i], buf) != 0)

>           puts("stat() failed");
>           exit(EXIT_FAILURE);
>          }
>          if(S_ISDIR(buf->st_mode))
>          {
>                  puts(pfname[i]);
>                 }

>                 free(pfname[i]);
>         }

>         return 0;
> }


 
 
 

stat() function call failing

Post by Greg Marti » Sat, 19 Jun 1999 04:00:00


Thanks Fletcher (and those who sent e-mail replies) I fixed the storage
foolishness but it still fails after printing the current and parent
directory( . and ..). Darn thing. Anymore advice would be appreciated.
Reading the gcc documentation (or the FreeBSD man page) doesn't seem to
point to it for me. I'm wondering if Steven's Advanced Unix Programming
would be a good resource for these sort of questions.
Greg


>Where is the real storage behind the "buf" pointer?

>--
> Fletcher Glenn


>> I'm storing all the entries in a directory, in an array and then would
like
>> to test and seperate the directories and files. I'm trying to use stat()
to
>> get the attributes but it fails on me. I'm afraid I've been unable to
grasp
>> why. Here is the listing for my code.
>> TIA and regards,
>> Greg Martin

>> #include <stdio.h>
>> #include <stdlib.h>
>> #include <string.h>
>> #include <dirent.h>
>> #include <sys/stat.h>
>> #include <sys/types.h>

>> int main(void)
>> {
>>         struct dirent *pdirstruct;
>>         char **pfname;
>>         const char basedir[] = "/usr/home/gregm/public_html";
>>         int cnt, i;
>>         DIR *dp;
>>         struct stat buf;

>>         cnt = 0;
>>         if((pfname = malloc(sizeof(char *))) == NULL)
>>         {
>>          puts("malloc() failed");
>>          exit(EXIT_FAILURE);
>>         }

>>         if((dp = opendir(basedir)) != NULL)
>>         {
>>                 while((pdirstruct = readdir(dp)))
>>                 {
>>                        if((pfname = realloc(pfname, (cnt + 2)
>>                              * sizeof(char *))) == NULL)
>>                        {
>>                             puts("realloc() failed");
>>                             while(cnt) free(pfname[--cnt]);
>>                             exit(EXIT_FAILURE);

>>                        }
>>                        if((pfname[cnt] =

malloc(strlen(pdirstruct->d_name)

- Show quoted text -

Quote:>>                              +1)) == NULL)
>>                        {
>>                             puts("malloc() failed");
>>                             while(cnt) free(pfname[--cnt]);
>>                             exit(EXIT_FAILURE);
>>                        }

>>                       strcpy(pfname[cnt], pdirstruct->d_name);
>>                             pfname[++cnt] = NULL;

>>                 }
>>         }
>>         else
>>        {
>>                 puts("opendir(failed)");
>>                 free(pfname);
>>                 exit(0);
>>         }

>>         for (i = 0; i < cnt; ++i)
>>         {
>>          if(stat(pfname[i], &buf) != 0)

>>           puts("stat() failed");
>>           exit(EXIT_FAILURE);
>>          }
>>          if(S_ISDIR(buf.st_mode))
>>          {
>>                  puts(pfname[i]);
>>                 }

>>                 free(pfname[i]);
>>         }

>>         return 0;
>> }

 
 
 

stat() function call failing

Post by Greg Marti » Sat, 19 Jun 1999 04:00:00


Thanks Andrew,
I'll have to learn to read between the lines.
Greg.
ps thanks for the faq url as well.


> Greg> Thanks Fletcher (and those who sent e-mail replies) I fixed the
> Greg> storage foolishness but it still fails after printing the
> Greg> current and parent directory( . and ..). Darn thing. Anymore
> Greg> advice would be appreciated.

>The name returned by readdir() doesn't include the path; you need to
>append it to the path passed to opendir() yourself.

>--
>Andrew.

>comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
>                           or <URL: http://www.whitefang.com/unix/>

 
 
 

stat() function call failing

Post by Andrew Giert » Sun, 20 Jun 1999 04:00:00


 Greg> Thanks Fletcher (and those who sent e-mail replies) I fixed the
 Greg> storage foolishness but it still fails after printing the
 Greg> current and parent directory( . and ..). Darn thing. Anymore
 Greg> advice would be appreciated.

The name returned by readdir() doesn't include the path; you need to
append it to the path passed to opendir() yourself.

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>

 
 
 

1. how to know the instruction address of calling function within called function?

Suppose function A call B, are there any ways to know the address of last instruction in A just
before calling B from within B? I would like to do some profiling by substituting B by a routine with record
the time spent on B. I also would like to know the time spent on B in different locations of A
where B is called. If I can know the address of last instruction in A just before calling B, from
debuging info of the excutables, I can find its source code address, therefore know the
time spent on B in differnet locations of A.

I don't want to make any changes to A. "-pg" compiler option for profiling is of no use either,
because there is no source code for B, and there is no object code for B with profiling codes in,
so you can't know the total time spent on B, but only the self time spent on B.

Thanks very much.
--



Kingston, Ontario K7L 3N6        Tel: (613) 545-2723 (o)
Canada                           Fax: (613) 545-6463
Home address:
58 1/2 Chatham St.               Tel: (613) 547-1353 (h)
Kingston, ON K7K 4G8

2. XFree-4.0.3 and Font-Problems

3. Browser calls CGI C function which sets an env var and call a c function crashes

4. Unable to remove SUNWaman

5. What is the stat function call for 64 bit in C++

6. x86 Video Driver

7. What is the 64 bit stat function call name for C in GNU

8. How to use > 1024 cylinder hard disks

9. stat(2) fails when calling find(1)

10. CGI C set env var and call a c external function failed on UNIX

11. The spawn function from /usr/bin/expect fails when called from "C" cgi program.

12. Call function within a function

13. Tree of Functions calling Functions etc.