2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
5 Desc: Functions to do interesting things with files.
13 #include <toollib/toollib.h>
14 #include <toollib/filesup.h>
16 int filesdiffer( char *file1
, char *file2
)
21 char buffer1
[1], buffer2
[1];
24 /* Do our files exist? */
25 fd1
= fopen(file1
,"rb");
28 fd2
= fopen(file2
, "rb");
32 /* Read and compare, character by character */
35 cnt1
= fread(buffer1
, 1, 1, fd1
);
36 cnt2
= fread(buffer2
, 1, 1, fd2
);
37 } while( cnt1
&& cnt2
&& buffer1
[0] == buffer2
[0] );
40 If the sizes are different, then one of the files must have hit
41 EOF, so they differ; otherwise it depends upon the data in the
44 if(buffer1
[0] != buffer2
[0] || cnt1
!= cnt2
)
52 int moveifchanged(char *old
, char *new)
54 struct stat
*statold
, *statnew
;
57 statold
= calloc(1, sizeof(struct stat
));
58 statnew
= calloc(1, sizeof(struct stat
));
60 if(stat(old
, statold
))
62 /* Couldn't stat old file, assume non-existant */
63 return rename(new, old
);
66 if(stat(new, statnew
))
68 /* Couldn't stat() new file, this shouldn't happen */
69 fprintf(stderr
, "Couldn't stat() file %s\n", new);
73 /* Fill an array with the old.bak filename */
74 bakname
= malloc( (strlen(old
) + 5) * sizeof(char) );
75 sprintf( bakname
, "%s.bak", old
);
77 if(statold
->st_size
!= statnew
->st_size
)
82 else if( filesdiffer(old
, new) )
94 int copy(char *src
, char *dest
)
100 in
= fopen(src
, "rb");
104 out
= fopen(dest
, "w");
110 count
= fread(buffer
, 1, 1024, in
);
111 fwrite(buffer
, 1, count
, out
);
112 } while( count
== 1024 );