Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux/ Open directory as a file

I've been reading Brian Kernighan and Dennis Ritchie - The C Programming Language and chapter 8.6 is about directory listing under UNIX OS. They say that everything and even directory is a file. This means that I should be able to open directory as a file? I've tried it using stdio functions and it didn't work. Now, I'm trying it with UNIX system functions. Of course, I'm not using UNIX, I'm using Ubuntu linux. Here is my code:

#include <syscall.h>
#include <fcntl.h>

int main(int argn, char* argv[]) {
    int fd;
    if (argn!=1) fd=open(argv[1],O_RDONLY,0);
    else fd=open(".",O_RDONLY,0);
    if (fd==-1) return -1;

    char buf[1024];
    int n;
    while ((n=read(fd,buf,1024))>0)
        write(1,buf,n);

    close (fd);
    return 0;
}

This writes nothing even when argn is 1 (no parameters) and I'm trying to read current directory. Any ideas/explanations? :)

like image 993
Viktor Avatar asked Jan 28 '14 11:01

Viktor


4 Answers

Files are also called regular files to distinguish them from special files.

Directory or not a regular file. The most common special file is the directory. The layout of a directory file is defined by the filesystem used.

So use opendir to open diretory.

like image 60
sujin Avatar answered Oct 20 '22 09:10

sujin


Nachiket's answer is correct (as indeed is sujin) but they don't clear up the mystery as to why open works and not read. Out of curiosity I made some changes to the given code to find out exactly what was going on.

#include <fcntl.h>
#include <stdio.h>
#include <errno.h>

int main(int argc, char* argv[]) {
    int fd = -1;
    if (argc!=1) fd=open(argv[1],O_RDONLY,0);
    else fd=open(".",O_RDONLY,0);
    if (fd < 0){
      perror("file open");
      printf("error on open = %d", errno);
      return -1;
    }
    printf("file descriptor is %d\n", fd);

    char buf[1024];
    int n;
    if ((n=read(fd,buf,1024))>0){
        write(1,buf,n);
    }
    else {
      printf("n = %d\n", n);
      if (n < 0) {
        printf("read failure %d\n", errno);
        perror("cannot read");
      }
    }
    close (fd);
    return 0;
}

The result of compiling and running this:

file descriptor is 3
n = -1
read failure 21
cannot read: Is a directory

That settles it, though I'd have expected open to fail, since the correct system function for opening directories is opendir().

like image 28
Achim Schmitz Avatar answered Oct 20 '22 10:10

Achim Schmitz


Though everything in unix is a file (directory also) but still filetype is concept is present in unix and applicable to all files. there are file types like regular file,directory etc and certain operations and functions are allowed/present for every file type.

In your case readdir is applicable for reading contents of directory.

like image 26
Nachiket Kate Avatar answered Oct 20 '22 09:10

Nachiket Kate


If you want to see the files in a directory you have to use the opendir and readdir functions.

like image 45
Some programmer dude Avatar answered Oct 20 '22 10:10

Some programmer dude