#include "file.h" #include "pipe.h" file_operations_t pipe_fops = { NULL, /* open */ NULL, /* openat */ pipe_read, /* read */ pipe_write, /* write */ NULL, /* dir_next */ NULL, /* lseek */ NULL /* tell */ }; #define PIPE_BUFFER_SIZE 512 uint8_t pipe_buffer[PIPE_BUFFER_SIZE]; int read_head = 0; int write_head = 0; int pipe_readc() { if (write_head == read_head) { return -1; } int rh_next = (read_head + 1) % PIPE_BUFFER_SIZE; uint8_t res = pipe_buffer[read_head]; read_head = rh_next; return res; } int pipe_writec(uint8_t c) { int wh_next = (write_head + 1) % PIPE_BUFFER_SIZE; if ((wh_next + 1) % PIPE_BUFFER_SIZE == read_head) { return -1; } pipe_buffer[write_head] = c; write_head = wh_next; return c; } ssize_t pipe_read(file_t *file, char *buffer, size_t size) { size_t i = 0; (void)file; while (i < size) { int res = pipe_readc(); if (res == -1) { break; } buffer[i++] = (char)(res); } return i; } ssize_t pipe_write(file_t *file, const char *buffer, size_t size) { size_t i = 0; (void)file; while (i < size) { int res = pipe_writec(buffer[i++]); if (res == -1) { break; } } return i; }