show cpu number in fiq
[AROS.git] / tools / toollib / filesup.c
blob652ba96e569ab47982d5ef53850624cac0386ecc
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Functions to do interesting things with files.
6 */
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <sys/stat.h>
13 #include <toollib/toollib.h>
14 #include <toollib/filesup.h>
16 int filesdiffer( char *file1, char *file2 )
18 FILE *fd1, *fd2;
19 int cnt1, cnt2;
21 char buffer1[1], buffer2[1];
22 int retval = 0;
24 /* Do our files exist? */
25 fd1 = fopen(file1,"rb");
26 if(!fd1)
27 return -1;
28 fd2 = fopen(file2, "rb");
29 if(!fd2)
30 return -2;
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
42 file.
44 if(buffer1[0] != buffer2[0] || cnt1 != cnt2)
45 retval = 1;
47 fclose(fd1);
48 fclose(fd2);
49 return retval;
52 int moveifchanged(char *old, char *new)
54 struct stat *statold, *statnew;
55 char *bakname;
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);
70 exit(-1);
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)
79 rename(old, bakname);
80 rename(new, old);
82 else if( filesdiffer(old, new) )
84 rename(old, bakname);
85 rename(new, old);
87 else
88 remove(new);
90 free(bakname);
91 return 0;
94 int copy(char *src, char *dest)
96 FILE *in, *out;
97 int count;
98 char buffer[1024];
100 in = fopen(src, "rb");
101 if(!in)
102 return -1;
104 out = fopen(dest, "w");
105 if(!out)
106 return -1;
110 count = fread(buffer, 1, 1024, in);
111 fwrite(buffer, 1, count, out);
112 } while( count == 1024 );
114 fclose(in);
115 fclose(out);
117 return 0;