/* evlist/fd.c * * Very simple interface to already prepared fd's. * * Copyright (C) 2003, 2004 * Andy Goth * * This code is available under the GNU General Public License; see COPYING. */ #include #include #include #include #include #include #include #include "evlist/evlist.h" static int fd_cleanup(device_t* dev); static int fd_open (device_t* dev); static int fd_close (device_t* dev); typedef struct fd_pair { int fd_in; int fd_out; } fd_pair_t; int fd_init(device_t* dev, char* path, int fd_in, int fd_out) { char* path_dup; fd_pair_t* state; path_dup = strdup(path); if (path_dup == NULL) return -1; state = malloc(sizeof(fd_pair_t)); if (state == NULL) {free(path_dup); return -1;} state->fd_in = fd_in ; state->fd_out = fd_out; dev->path = path_dup ; dev->hw_state = state ; dev->hw_cleanup = fd_cleanup ; dev->hw_open = fd_open ; dev->hw_close = fd_close ; dev->hw_readable = device_readable; dev->hw_writable = device_writable; return 0; } static int fd_cleanup(device_t* dev) { free(dev->path); free(dev->hw_state); return 0; } static int fd_open(device_t* dev) { fd_pair_t* state = dev->hw_state; if (state->fd_in == state->fd_out) { if (state->fd_in != -1) device_attach_fd(dev, state->fd_in , O_RDWR ); } else { if (state->fd_in != -1) device_attach_fd(dev, state->fd_in , O_RDONLY); if (state->fd_out != -1) device_attach_fd(dev, state->fd_out, O_WRONLY); } return 0; } static int fd_close(device_t* dev) { device_detatch_fd(dev, O_RDWR); return 0; } /* vim: set ts=4 sts=4 sw=4 tw=80 et: */