-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmix.c
More file actions
69 lines (66 loc) · 1.18 KB
/
mix.c
File metadata and controls
69 lines (66 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
struct stat st;
int foo, bar, sts;
unsigned char *buf;
sts = stat("foo.dsk",&st);
if ( sts )
{
perror("Can't stat foo.dsk");
return 1;
}
buf = (unsigned char *)malloc(st.st_size);
if ( !buf )
{
char err[128];
snprintf(err,sizeof(err),"Can't malloc %ld bytes of memory", st.st_size);
perror(err);
return 1;
}
foo = open("foo.dsk", O_RDONLY);
if ( !foo )
{
perror("Can't open foo.dsk");
free(buf);
return 1;
}
bar = open("bar.dsk",O_RDWR);
if ( !bar )
{
perror("Can't open bar.dsk");
close(foo);
free(buf);
return 1;
}
sts = read(foo,buf,st.st_size);
if ( sts != st.st_size )
{
char err[128];
snprintf(err,sizeof(err),"Failed to read %ld bytes from foo.dsk", st.st_size);
perror(err);
close(foo);
close(bar);
free(buf);
return 1;
}
sts = write(bar,buf,st.st_size);
if ( sts != st.st_size )
{
char err[128];
snprintf(err,sizeof(err),"Failed to write %ld bytes to bar.dsk", st.st_size);
perror(err);
close(foo);
close(bar);
free(buf);
return 1;
}
close(foo);
close(bar);
free(buf);
return 0;
}