Just as a learning experience, I’m trying to code the following problems in C.
If two flash drives are inserted each having a folder (say,
Course_Notes), then they get synced. That is, data is copied from one
to the other and if there a file already exists, then the newer one is
retained.
I would do this in bash by:
#!/bin/bash
while $1
do
cp -ur /media/PD_1/Course_Notes /media/PD_2/Course_Notes
cp -r /media/PD_2/Course_Notes /media/PD_1/Course_Notes
done
How do I do this in C without too many system calls ?
In the real world, you’d probably use something like
rsyncfor this.You’re probably looking for the
stat()family of functions to get size and modification date, along withopendir()/readdir()/closedir()for directory listings. The standard way for copying a file itself is to open the source and destination, and write to the latter what you read from the former with the usual read and write functions.Note also that the source code for GNU and BSD versions of the standard UNIX utilites, such as
cp, is freely available. (likewise forrsync) You may want to read some of that source code to see what you may have missed in your own approach.