In <> dawn <> writes:
> I am now working on a C program under Unix.
And you're too stupid to realise that comp.unix.programmer is the right
place for your question.
> The requirement for the program is that:
> A file name is passed to program as a parameter. The program will
>Find files under a specified directory. The matched file must have the
>same content with the given file. It does not matter whether the
>filenames are the same.
Standard C provides no support for directories, which is why posting to
comp.lang.c was such a silly thing.
> It is easy to find file that has the same name with given file, but
>may be hard to find the files that with the same content. In my
>knowledge, i give two solutions:
>
> 1) Go throught the directory and its sub direcotry tree, and when
>meeting a file, Use the stand C library function to open that file and
>the given file, and then compare those contents in buffers to see if
>they are the same.
The Unix function ftw() does most of the job for you. You only have to
provide a function for comparing the contents of the two files and such
a function is damn fast if you do it right: compare the contents only
if the files have the same size, use fread() and memcmp() to perform the
comparison on BUFSIZ sized chunks. Note that ftw is giving you the stat
info about each directory entry, so you don't have to do anything special
in order to obtain the file size.
For extra points, lock *at least* the reference file, to be sure that its
contents is stable during the program execution. But don't ask here how!
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email:
Currently looking for a job in the European Union