C & LINUX

C / LINUX opendir(3) readdir(3) rewinddir(3) closedir(3) telldir(3) seekdir(3)

Ocean_ 2022. 5. 2. 19:44

함수 기능

opendir

디렉토리를 열어주는 함수

readdir

디렉토리 내부파일 읽는 함수

closedir

디렉토리를 닫는 함수

rewinddir

디렉토리 포인터를 다시 처음으로 돌려놓는 함수

telldir

디렉토리 포인터의 현재위치 알림 함수

seekdir

디렉토리 포인터의 현재위치 수정 함수

함수 원형

#include <dirent.h> 
#include <sys/types.h> 

DIR *opendir(const char *filepath);
struct dirent *readdir(DIR *dp);
void rewinddir(DIR *dp);
int closedir(DIR *dp);
long telldir(DIR *dp); 
void seekdir(DIR *dp, long loc);

함수 파라메터

filepath

디렉토리 경로

dp

디렉토리 포인터

loc

새로운 디렉토리 포인터

함수 예제

directory1

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

#define DIRECTORY_SIZE MAXNAMLEN

int main(int argc, char *argv[]){
    struct dirent *dentry; 
    struct stat statbuf; 
    char filename[DIRECTORY_SIZE+1];
    DIR *dirp;

    if(argc<2){
        fprintf(stderr, "usage:%s<directory>\n",argv[0]);
        exit(1);
    }
    if((dirp=opendir(argv[1]))==NULL|| chdir(argv[1])==-1){
        fprintf(stderr, "opendir , chdir error for %s\n",argv[1]);
        exit(1);
    } 

    while ( (dentry = readdir( dirp)) != NULL) { 
        if (dentry->d_ino == 0) 
            continue;
        memcpy(filename, dentry->d_name, DIRECTORY_SIZE);

        if (stat(filename, &statbuf) == -1) { 
            fprintf(stderr, "stat error for %s\n " , filename); 
            break;
        }

        if ((statbuf.st_mode & S_IFMT) == S_IFREG) 
            printf ("%-14s %ld\n", filename, statbuf.st_size); 
        else 
            printf ("%-14s\n", filename);

    }

    exit(0);
}

directory2

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


#ifdef PATH_MAX
static int pathmax = PATH_MAX;
#else
static int pathmax =0;
#endif

#define MAX_PATH_GUESSED 1024

#ifndef LINE_MAX
#define LINE_MAX 2048
#endif

char *pathname;
char command[LINE_MAX], grep_cmd[LINE_MAX];

int ssu_do_grep(void){
    struct dirent *dirp;
    struct stat statbuf;
    char *ptr;
    DIR *dp;

    if(lstat(pathname,&statbuf)<0){
        fprintf(stderr, "lstat error for %s\n",pathname);
        return 0;
    }

    if(S_ISDIR(statbuf.st_mode)==0){
        sprintf(command, "%s %s",grep_cmd, pathname);
        printf("%s : \n",pathname);
        system(command);
        return 0;
    }

    ptr = pathname + strlen(pathname);
    *ptr++ = '/';
    *ptr = '\0';

    if((dp=opendir(pathname))==NULL){
        fprintf(stderr,"opendir error for %s\n",pathname);
        return 0;
    }

    while((dirp =readdir(dp)) !=NULL)
        if(strcmp(dirp->d_name,".") && strcmp(dirp->d_name, "..")){
            strcpy(ptr, dirp->d_name);

            if(ssu_do_grep()<0)
                break;
        }
        ptr[-1]=0;
        closedir(dp);
        return 0;
    
}

void ssu_make_grep(int argc, char *argv[]){
    int i;
    strcpy(grep_cmd, "grep");

    for(i=1; i<argc-1; i++){
        strcat(grep_cmd," ");
        strcat(grep_cmd, argv[i]);
    }
}

int main(int argc, char *argv[]){
    if(argc<2){
        fprintf(stderr, "usage: %s<-CVbchilnsvwx><-num> <-A num><-B num> <-f file> \n"
                        "               <-e> expr <directory>\n",argv[0]);
                        exit(1);
    }
    if(pathmax ==0){
        if((pathmax = pathconf("/", _PC_PATH_MAX))<0)
            pathmax - MAX_PATH_GUESSED;
        else
            pathmax++;
    }

    if((pathname = (char * )malloc(pathmax+1))==NULL){
        fprintf(stderr, "malloc error\n");
        exit(1);
    }

    strcpy(pathname, argv[argc-1]);
    ssu_make_grep(argc, argv);
    ssu_do_grep();
    exit(0);
}

함수 결과

 

리눅스시스템 프로그래밍 - 홍지만 저

교재 내에 있는 예제를 바탕으로 작성한 글 입니다.