ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello everybody. I think I need some help on this. I am trying to write a program that simulates 'cp' command. It should copy whole directories recursively (meaning all files in the directory including subdirectories). The point is I am not allowed to use the 'cp' command at all and the program should not include any 'fork/exec' commands (in other words, the work has to be done by a single process).
I have to use functions like open(), read(), write() etc to open each file from its original path, read its contents and write them to the destination path. Any ideas how this can be done? I have managed to open the directory using 'struct dirent *dir' and 'opendir' and 'readdir', but the usage of 'open', 'read', 'write', 'creat'...is a bit confusing. I am totally puzzled.
//edit: I guess I am too tired by now....I meant Program simulating 'copy' command
Last edited by kalamaraki; 02-19-2007 at 07:47 PM.
I assume that you are doing this in C (is that correct?)
Have you studied recursive functions?
You need to break the problem down into parts. Tackle the following in order.
Can you display the name of the files in a directory
Can you tell the difference between a file and a directory
Can you read a file
Can you tell if a directory exists (the one to copy to)
Can you create a directory (if needed the one to copy to)
Can you write a file to a directory
Each of these smaller steps help you to tackle the problem. For any specific question about the code post the relevant code and where you are having problems.
First of all, thanks for the quick reply. Let's take things from the beginning.
The program is written in C. I have tackled all the parts of the problem, except for the last one, which is writing to a file. I don't know how I can read from a file and write to a file that does not exist (in other words I have to creat the destination file). I believe that opening the file with fopen, reading the data byte by byte and wrining them to the other file is not a very good idea. I would like to copy entire blocks of bytes. How do open(), read() and write() work? Can I have a loop that is something like:
while(!EOF-opened file for reading)
{read_from_fileA
write_to_fileB
}
You can use fopen() to open an existing file, you can also use fopen() to open a file that doesn't yet exist, that is by calling this function the file will be created.
fopen takes two arguments, the first is the full path to the file you wish to open, hopefully that is clear. The second argument is a flag telling fopen() how you want to open the file. An r is suitable for opening a file for read, whilst an a is appropriate when you want to open a file for writing when you also want the file to be created. Check out the man page: man fopen
Once the files are open you can then use fread()to read in a chunks of data at a time from the file that was opened in read mode, the paired function fwrite() can then be used to write the data out to the file that was opened in write mode. Again check out the man page, also look at the functions for error checking and spotting the eof condition ferror() feof().
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.