micro-utils/include/libmu/get_string.h

23 lines
408 B
C

#ifndef _GET_STRING_H
#define _GET_STRING_H
#include <stdio.h>
#include <string.h>
#include <errno.h>
int mu_get_string(const char *prog_name, char *buf, const size_t len) {
off_t rbytes = read(STDIN_FILENO, buf, len);
if (rbytes <= 0) {
fprintf(stderr, "%s: %s\n", prog_name, strerror(errno));
return 0;
}
if (rbytes > 0)
buf[rbytes - 1] = '\0';
buf[rbytes] = '\0';
return rbytes;
}
#endif