LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-19-2007, 07:46 PM   #1
kalamaraki
LQ Newbie
 
Registered: Oct 2006
Location: Athens - Greece
Distribution: Ubuntu 6.06 LTS - the Dapper Drake
Posts: 13

Rep: Reputation: 0
Program simulatinh 'copy' command


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.
 
Old 02-19-2007, 10:38 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 02-20-2007, 05:09 AM   #3
kalamaraki
LQ Newbie
 
Registered: Oct 2006
Location: Athens - Greece
Distribution: Ubuntu 6.06 LTS - the Dapper Drake
Posts: 13

Original Poster
Rep: Reputation: 0
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
}
 
Old 02-20-2007, 05:26 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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().
 
Old 02-22-2007, 07:05 AM   #5
kalamaraki
LQ Newbie
 
Registered: Oct 2006
Location: Athens - Greece
Distribution: Ubuntu 6.06 LTS - the Dapper Drake
Posts: 13

Original Poster
Rep: Reputation: 0
Thank you for your help. I'll try that and I will come back with more questions if I can't make it work. Thanks!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it possible to copy a program from one dist to another? tobiasw Linux - General 2 10-19-2006 09:28 AM
Copy program not working skie_knite007 Programming 4 08-28-2005 02:13 PM
Looking for a program that will copy BDG CDs Tux_Phoenix Linux - Software 0 02-03-2005 10:51 PM
Viewing program messages when program isn't run from command line? Locura Linux - Software 1 09-27-2003 08:19 AM
Copy program CropA Programming 2 03-20-2001 10:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration