/* return value:
* 0, error or read nothing
* !0, read counts
*/
int read_from_file(const char* path, char* buf, int size)
{
if (!path) {
return 0;
}
int fd = open(path, O_RDONLY);
if (fd == -1) {
return 0;
}
int count = read(fd, buf, size);
if (count > 0) {
count = (count < size) ? count : size - 1;
while (count > 0 && buf[count-1] == '\n') count--;
buf[count] = '\0';
} else {
buf[0] = '\0';
}
close(fd);
return count;
}
* 0, error or read nothing
* !0, read counts
*/
int read_from_file(const char* path, char* buf, int size)
{
if (!path) {
return 0;
}
int fd = open(path, O_RDONLY);
if (fd == -1) {
return 0;
}
int count = read(fd, buf, size);
if (count > 0) {
count = (count < size) ? count : size - 1;
while (count > 0 && buf[count-1] == '\n') count--;
buf[count] = '\0';
} else {
buf[0] = '\0';
}
close(fd);
return count;
}