Chinaunix首页 | 论坛 | 博客
  • 博客访问: 128465
  • 博文数量: 62
  • 博客积分: 1476
  • 博客等级: 上尉
  • 技术积分: 662
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-03 16:38
文章分类

全部博文(62)

文章存档

2010年(14)

2009年(48)

我的朋友

分类: LINUX

2009-12-08 14:49:24

/*******************************************************************************
* header file
*    int mkdir( const char *path, mode_t mode ); mode & (~umask), return 0/-1
*    int rmdir( char *path ); rm an empty dir(nlink=0), return 0/-1
* header file
*    char *getcwd( char *buf, size_t size ); get current absolute path
*    char *getwd( char *pathname ); copy current path to pathname *
*    int chdir( const char *path ); same as cd
*    int fchdir( int fildes ); set fildes's directory as work dir. ret 0/-1
* header file
*    DIR *opendir( const char *dirname ); return DIR stream pointer or NULL
*    struct dirent *readdir( DIR *dirp ); return dirent pointer or NULL
*    int closedir( DIR *dirp ); close dir stream and release DIR struct
*
*    void seekdir( DIR *dirp, long int loc ); can't be assigned,loc=telldir()
*    void rewinddir( DIR *dirp );
*    long int telldir( DIR *dirp );
*
*******************************************************************************/

#include <stdio.h>
#include <unistd.h>
#include <dirent.h>

int main( int ac, char *av[] )
{
    char buf[255], *dir = "dir";
    long int loc = 0;
    DIR *pdir = NULL;
    struct dirent *pent = NULL;

    fprintf( stderr, "pwd = [%s]\n", getcwd(buf, sizeof(buf)) );
    fprintf( stderr, "create dir [%s], respcode = [%d]\n",
            dir, mkdir(dir, 0777) );
    chdir("./dir");
    fprintf( stderr, "pwd = [%s]\n", getcwd(buf, sizeof(buf)) );
    chdir("../");
    
    if ( (pdir = opendir("./")) == NULL )
    {
        fprintf( stderr, "open dir failed.\n" );
        return 1;
    }

    loc = telldir( pdir );
    fprintf( stderr, "current location: [%ld]\n", loc );

    while (1)
    {
        pent = readdir( pdir );
        if ( pent == NULL )
            break;
        fprintf( stderr, "%5ld %s\n", pent->d_ino, pent->d_name );
    }
    
    seekdir( pdir, loc );
    rewinddir( pdir ); //seekdir( pdir, 0 );

    closedir( pdir );
    
    rmdir(dir);

    return 0;
}


阅读(466) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~