feat[OS/lab5]: completed part B

This commit is contained in:
2025-10-03 00:24:30 +03:00
parent 949f1b8ac4
commit 72d5756b79
4 changed files with 206 additions and 0 deletions

80
OSs/lab5/part-B/client.c Normal file
View File

@ -0,0 +1,80 @@
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#include "common.h"
int client(int fd_read, int fd_write);
int main()
{
int read_fd, write_fd;
FAIL_ON((write_fd=open(FIFO_SERVER_PATH, O_WRONLY)) < 0);
FAIL_ON((read_fd=open(FIFO_CLIENT_PATH, O_RDONLY)) < 0);
FAIL_ON(client(read_fd, write_fd));
FAIL_ON(unlink(FIFO_SERVER_PATH) < 0);
printf("unlinked %s\n", FIFO_SERVER_PATH);
FAIL_ON(unlink(FIFO_CLIENT_PATH) < 0);
printf("unlinked %s\n", FIFO_CLIENT_PATH);
return EXIT_SUCCESS;
}
#define BUF_SIZE 256
char *ltrim(char *s)
{
while(isspace(*s)) s++;
return s;
}
char *rtrim(char *s)
{
char* back = s + strlen(s);
while(isspace(*--back));
*(back+1) = '\0';
return s;
}
char *trim(char *s)
{
return rtrim(ltrim(s));
}
/**
* Performs client actions
*
* @fd_read - pipe to read from
* @fd_write - pipe to write t\no
*
* returns 0 on success or -errno on fail
*/
int client(int fd_read, int fd_write)
{
char buf[BUF_SIZE] = {0};
if (!fgets(buf, BUF_SIZE, stdin)) return -EFAULT;
const char *s = trim(buf);
size_t len = strlen(s);
ssize_t n_written = write(fd_write, s, len);
if (n_written < 0 || n_written < len) {
return -EFAULT;
}
memset(buf, 0, BUF_SIZE);
size_t n;
struct custom_ds ds;
while ((n = recv_msg(fd_read, &ds)) > 0) {
len = ds.hdr.len;
n_written = fwrite(ds.msg, sizeof(ds.msg[0]), len, stdout);
if (n_written < 0 || n_written < len) {
return -EFAULT;
}
memset(buf, 0, sizeof(buf));
}
return 0;
}