Skip to content

Commit

Permalink
mx_util: Add mx_read_first_line_from_file()
Browse files Browse the repository at this point in the history
  • Loading branch information
mariux committed Sep 23, 2015
1 parent 881ce84 commit f42d526
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
28 changes: 28 additions & 0 deletions mx_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,34 @@ int mx_open_newfile(char *fname)
return fh;
}

int mx_read_first_line_from_file(char *fname, char **line)
{
_mx_cleanup_fclose_ FILE *fp;
char *buf = NULL;
size_t n = 0;
ssize_t res;

fp = fopen(fname, "r");
if (!fp)
return -errno;

res = getline(&buf, &n, fp);
if (res == -1)
return -errno;

*line = buf;

if (!res)
return res;

res--;

if (buf[res] == '\n')
buf[res] = 0;

return res;
}

int mx_sleep(unsigned int seconds)
{
if (seconds)
Expand Down
11 changes: 11 additions & 0 deletions mx_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>

#include "mx_log.h"

Expand Down Expand Up @@ -46,9 +47,17 @@ static inline void __mx_free(void *ptr) {
free(*(void **)ptr);
}

static inline void __mx_fclose(FILE **ptr) {
if (*ptr)
fclose(*ptr);
}

#undef _mx_cleanup_free_
#define _mx_cleanup_free_ _mx_cleanup_(__mx_free)

#undef _mx_cleanup_fclose_
#define _mx_cleanup_fclose_ _mx_cleanup_(__mx_fclose)

#undef likely
#define likely(x) __builtin_expect((x),1)

Expand Down Expand Up @@ -106,6 +115,8 @@ int mx_setenvf_forever(const char *name, char *fmt, ...) __attribute__ ((format(

int mx_open_newfile(char *fname);

int mx_read_first_line_from_file(char *fname, char **line);

int mx_sleep(unsigned int seconds);
int mx_sleep_nofail(unsigned int seconds);

Expand Down
22 changes: 22 additions & 0 deletions test_mx_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ static void test_mx_strtoul(void)
assert(mx_strtoul("-1", &l) == -ERANGE);
assert(mx_strtoul(" -1", &l) == -ERANGE);

assert(mx_strtoul("123 123", &l) == -EINVAL);
assert(mx_strtoul("123s", &l) == -EINVAL);
assert(mx_strtoul("0888", &l) == -EINVAL);
assert(mx_strtoul("1.2", &l) == -EINVAL);
Expand Down Expand Up @@ -271,6 +272,26 @@ static void test_mx_strtobytes(void)
assert(mx_strtobytes("test", &l) == -EINVAL);
}

static void test_mx_read_first_line_from_file(void)
{
char *str;

assert(mx_read_first_line_from_file("/proc/sys/kernel/random/boot_id", &str) == 36);
assert(str);
mx_free_null(str);

assert(mx_read_first_line_from_file("/proc/sys/kernel/random/uuid", &str) == 36);
assert(str);
mx_free_null(str);

assert(mx_read_first_line_from_file("/proc/no_such_file", &str) == -ENOENT);
assert(str == NULL);

assert(mx_read_first_line_from_file("/proc/self/stat", &str) > 0);
assert(str);
mx_free_null(str);
}

int main(int argc, char *argv[])
{
test_mx_strskipwhitespaces();
Expand All @@ -284,5 +305,6 @@ int main(int argc, char *argv[])
test_mx_strtoseconds();
test_mx_strtominutes();
test_mx_strtobytes();
test_mx_read_first_line_from_file();
return 0;
}

0 comments on commit f42d526

Please sign in to comment.