c - Why are directory files not read using fread? -


I was going through an example from Maurice Baque's Unix Book. He writes a simple copy program as outlined below. Although it fails when the input file is a directory file, I stumbled upon opendir and some other such APIs - should I use it?

If a binary file can work with this, then why is the directory file considered different? Unix is ​​not everything in the form of a file, even if it is being interpreted by this program.

How can I increase this program to support a directory file and then create a Maconod? I want to test this, suppose I am in / home / user1 and a $ / Copy / home / user user-home-clone and mknod to see how this directory will be different from home, I think the user-home-clone does not have any reference to itself, but all other files will be in the / home / user [even if it is a user called a file -hock-clone / home / user) because when we copy Used to command, so was not there?

  #include & lt; Stdlib.h & gt; # Include & lt; Stdio.h & gt; # Include & lt; Fcntl.h & gt; # Include & lt; Unistd.h & gt; Four buffer [2048]; Int copy (file * source, file * destination) {int count; While ((count = fread (buffer, 1, size buffer, source)) gt; 0) {fwrite (buffer, 1, count, destination); } Return 0; } Int main (int argc, char * argv []) {int position; File * source; FILE * destination; If (argc! = 3) {printf ("% s takes exactly 3 arguments \ n", argv [0]); Exit (1); } Source = FOPN (RGR [1], "R"); If (source == faucet) {printf ("Can not open to read% s \ n", argv [1]); Exit (1); } Destination = FOPN (argv [2], "wb"); If (destination == faucet) {printf ("Can not open to write% s \ n", argv [2]); Exit (1); } If (copy (source, destination) == 0) {status = 0; } And {status = 1; } Fclose (source); Fclose (destination); Exit (status); }  

I use the Santos 6.5 Linux Ext4 file system

< P> In the early version of Unix, directory files can be read as binary files However, when the network and other types of virtual filesystems were added, this capability was removed, because the different file system is separate, Implement different directories, though it technically does not And that driver to simulate a byte stream structure of these directories, it was not seen as a useful feature. Directories should be treated as opaque, abstract collections, and should be accessed using directory-specific functions.


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -