Added O_CREAT because some geek wanted it. Sheesh.
[idzebra-moved-to-github.git] / bfile / bfile.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: bfile.c,v $
7  * Revision 1.7  1994-08-23 14:25:45  quinn
8  * Added O_CREAT because some geek wanted it. Sheesh.
9  *
10  * Revision 1.6  1994/08/23  14:21:38  quinn
11  * Fixed call to log
12  *
13  * Revision 1.5  1994/08/18  08:10:08  quinn
14  * Minimal changes
15  *
16  * Revision 1.4  1994/08/17  14:27:32  quinn
17  * last mods
18  *
19  * Revision 1.2  1994/08/17  14:09:32  quinn
20  * Compiles cleanly (still only dummy).
21  *
22  * Revision 1.1  1994/08/17  13:55:08  quinn
23  * New blocksystem. dummy only
24  *
25  */
26
27 #include <util.h>
28 #include <bfile.h>
29 #include <sys/types.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33
34 int bf_close (BFile bf)
35 {
36     close(bf->fd);
37     xfree(bf);
38     return(0);
39 }
40
41 BFile bf_open (const char *name, int block_size, int wflag)
42 {
43     BFile tmp = xmalloc(sizeof(BFile_struct));
44
45     if ((tmp->fd = open(name, wflag ? O_RDWR|O_CREAT : O_RDONLY, 0666)) < 0)
46     {
47         log(LOG_FATAL|LOG_ERRNO, "open"); 
48         return(0);
49     }
50     tmp->block_size = block_size;
51     return(tmp);
52 }
53
54 int bf_read (BFile bf, int no, int offset, int num, void *buf)
55 {
56     lseek(bf->fd, no * bf->block_size + offset, 0);
57     return(read(bf->fd, buf, num ? num : bf->block_size));
58 }
59
60 int bf_write (BFile bf, int no, int offset, int num, const void *buf)
61 {
62     lseek(bf->fd, no * bf->block_size + offset, 0);
63     return(write(bf->fd, buf, num ? num : bf->block_size));
64 }