#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(void) {
    int fd;
    do { fd = open("myfifo", O_WRONLY); } while (fd < 0 && errno == EINTR);
    if (fd == -1) { perror("open write"); return EXIT_FAILURE; }
    puts("[writer] 已连接 FIFO，开始写入...");

    const char *data = "Hello via named pipe FIFO!\n";
    size_t remaining = strlen(data);
    while (remaining > 0) {
        ssize_t n = write(fd, data, remaining);
        if (n < 0 && errno == EINTR) continue;
        if (n <= 0) {
            perror("write");
            close(fd);
            return EXIT_FAILURE;
        }
        data += n;
        remaining -= (size_t)n;
    }
    close(fd);
    puts("[writer] 写入完成并关闭");
    return EXIT_SUCCESS;
}
