EMILE/tools/device.c

64 lines
1.1 KiB
C
Raw Normal View History

2005-11-13 19:43:28 +00:00
/*
*
* (c) 2005 Laurent Vivier <Laurent@lvivier.info>
2005-11-13 19:43:28 +00:00
*
*/
2005-11-12 22:28:50 +00:00
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
2005-11-12 22:28:50 +00:00
#include <stdio.h>
#include <fcntl.h>
2005-11-12 22:28:50 +00:00
2005-11-21 22:09:16 +00:00
#include "device.h"
int device_sector_size = 2048;
#define BLOCKS(X) (((X)/device_sector_size)+(((X)%device_sector_size)?1:0))
2005-11-12 22:28:50 +00:00
int device_get_blocksize(void *data)
{
return device_sector_size;
}
2005-11-21 22:09:16 +00:00
int device_read_sector(void *data,off_t offset, void* buffer, size_t size)
2005-11-12 22:28:50 +00:00
{
int fd = (int)data;
int ret;
lseek(fd, offset * device_sector_size, SEEK_SET);
ret = read(fd, buffer, BLOCKS(size) * device_sector_size);
if (ret != BLOCKS(size) * device_sector_size)
return -1;
return 0;
}
2005-11-21 22:09:16 +00:00
int device_write_sector(void *data,off_t offset, void* buffer, size_t size)
{
int fd = (int)data;
int ret;
lseek(fd, offset * device_sector_size, SEEK_SET);
ret = write(fd, buffer, BLOCKS(size) * device_sector_size);
if (ret != BLOCKS(size) * device_sector_size)
return -1;
return 0;
2005-11-12 22:28:50 +00:00
}
2005-11-21 22:09:16 +00:00
void device_close(void *data)
2005-11-12 22:28:50 +00:00
{
close((int)data);
2005-11-12 22:28:50 +00:00
}
int device_open(char *device, int flags)
2005-11-12 22:28:50 +00:00
{
int fd;
2005-11-21 22:09:16 +00:00
if (device == NULL)
return -1;
fd = open(device, flags);
2005-11-21 22:09:16 +00:00
return fd;
2005-11-12 22:28:50 +00:00
}