Help!!! open func - Cannot open subdirectory

Help!!! open func - Cannot open subdirectory

Post by NIck AMIG » Fri, 03 Oct 1997 04:00:00



Hi~

Nice to meet U.

I made 'list', one of 'ls' in UNIX with C

but there is one problem.

that is, this program cannot open parent dir.

for exam,

        list .(current dir)
        -Do well
but
        list ..(parent dir)
        .  
        ..
        Cannot open~

Please help me.
What's problem?

Source is,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <fcntl.h>
#include <math.h>
#include <pwd.h>
#include <grp.h>

void main( int argc, char *argv[] )
{
    DIR *dirp;

    struct dirent *direntp;

    struct stat statbuf;

    char mode[3] = { 'r', 'w', 'x' };

    int i;

    mode_t fd_mode;

    char stime[40];

    if( argc != 2 ){

        fprintf( stderr, "Usage : %s dir_name\n", argv[0]);

        exit(1);
    }

    if((dirp = opendir( argv[1] )) == NULL ){

        fprintf( stderr, "Could not open %s directory: %s\n", argv[1], strerror(
errno ));

        exit(1);
    }

    while( (direntp = readdir( dirp )) != NULL ){

        if( stat(direntp->d_name - 2, &statbuf) == -1 ){

            /* Here !!!!!!!!!!!!!!!!!!!!! */
            fprintf( stderr, "Could not get stat on files %s: %s\n",
direntp->d_name - 2, strerror( errno ));

            exit(1);
        }

        ( statbuf.st_mode & S_IFDIR ) ? printf("d") : printf("-");

        fd_mode = 512;
        for( i = 0; i < 9; i ++){
            fd_mode /= 2;
            ( statbuf.st_mode & fd_mode) ? printf("%c", mode[i % 3]) :
printf("-");
        }

        printf( " %3d", statbuf.st_nlink);

        printf( " %.8s", getpwuid( statbuf.st_uid )->pw_name);

        printf( "  %.6s", getgrgid( statbuf.st_gid )->gr_name);

        printf( " %10d", statbuf.st_size);

        strncpy( stime, ctime(&statbuf.st_mtime), strlen(ctime(&statbuf.st_mtime))
- 1);
        printf(" %s", stime );

        printf(" %s\n", direntp->d_name - 2 );
    }

    closedir( dirp );

    exit(0);

Quote:}

--
?e|3a ??? ?Y?~


 
 
 

Help!!! open func - Cannot open subdirectory

Post by Kevin Robso » Fri, 03 Oct 1997 04:00:00



> Hi~

> Nice to meet U.

> I made 'list', one of 'ls' in UNIX with C

> but there is one problem.

> that is, this program cannot open parent dir.

> for exam,

>         list .(current dir)
>         -Do well
> but
>         list ..(parent dir)
>         .
>         ..
>         Cannot open~

The problem is that stat is only searching the current directory.  The
filename returned by readdir does not include the path - you need to
append the directory name (argv[1]) to this before calling the stat
command.

SNIP...

Quote:> void main( int argc, char *argv[] )
> {

      char fullname[1000];

Quote:>     DIR *dirp;

CHOP...

Quote:>     while( (direntp = readdir( dirp )) != NULL ){

         sprintf (fullname, "%s/%s", argv[1], direntp->d_name);
         if( stat(fullname, &statbuf) == -1 ){

SLASH...

Note that you really ought to trim of any tailing / from the directory
name as well, also, I don't know where you got 'direntp->dname - 2'
from, you should just have 'direntp->dname'

--

Kevin Robson,                Tel:   +44-1705-564114
Open Systems Specialist,


 
 
 

Help!!! open func - Cannot open subdirectory

Post by Andrew Giert » Mon, 13 Oct 1997 04:00:00


 NIck> I made 'list', one of 'ls' in UNIX with C
 NIck> but there is one problem.

 NIck>     while( (direntp = readdir( dirp )) != NULL ){

 NIck>            if( stat(direntp->d_name - 2, &statbuf) == -1 ){

This is wrong. Why are you subtracting 2 from the name pointer?

direntp->d_name contains the name of the directory entry. This name is
relative to the directory that you opened with opendir(), so in order
to stat() the entry, you either need to chdir() to the directory you're
reading from, or prepend the directory name yourself.

--
Andrew.

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

 
 
 

1. OpenWin Filemgr: 'cannot open file: too many files open' ?

Here's a strange problem with the FileManager in Solaris 2.3.  The
only way to fix it right now is to restart the file manager over.

 When double-clicking on a text file, file manager is set-up to
 start textedit on that file.  I quit textedit.  I double-click
 on a text file again.  And I quit again textedit.  I can do that
 approximately 54 times.  After that, you can't open any file or
 change directory within FileManager.

Does anybody know if this is a configuration problem or a bug ?
And if it's a bug, is there any patch for it ?

Thanks

Hugo Frappier
Montreal

2. NEED HELP! (SET PERMISSION IN AUTO FTP)

3. localhost portscan detects 2 randomly opened and closed ports - other hosts cannot see these open

4. Segmentation fault?

5. cannot open /dev/mem: too many open files??

6. wondering about linux on sparcs newsgroup

7. Help: What controls number of files a user can open (file open command)?

8. Installing QLogic Fast SCSI IDE HA

9. Open multiple files with open() from stdin.PLEASE HELP

10. HELP:/usr/i486-linux/bin/ld: cannot open -lX11

11. xf86OpenConsole:cannot open dev/console ? HELP

12. rcpbind: ticlts: cannot open ... Help!

13. Help: Problems starting X -- "Cannot open /dev/console"