lib: add [SAFE_]FILE_LINES_SCANF
PATCH 1/1 lib: add [SAFE_]FILE_LINES_SCANF
This patch adds 2 macros that try to parse each line from a file according to user-supplied (scanf) format. First line that matches all format directives ends the search. If EOF is reached, SAFE_ version triggers TBROK, non-SAFE_ returns non-zero retcode to user.
Main motivation is parsing various /proc files, for example:
if (FILE_LINES_SCANF("/proc/meminfo", "MemFree: %ld", &free))
do_something();
SAFE_FILE_LINES_SCANF("/proc/meminfo", "MemFree: %ld", &free);
// automatically calls TBROK if all directives can't be matched--- .gitignore | 1 + include/old/old_safe_file_ops.h | 8 +++++++ include/safe_file_ops_fn.h | 5 +++++ include/tst_safe_file_ops.h | 8 +++++++ lib/safe_file_ops.c | 47 +++++++++++++++++++++++++++++++++++++++++ lib/tests/tst_safe_fileops.c | 40 +++++++++++++++++++++++++++++++++++ 6 files changed, 109 insertions(+) create mode 100644 lib/tests/tst_safe_fileops.c
.gitignore +1 -0
@@ -73,3 +73,4 @@ logfile.* /lib/tests/tst_dataroot01 /lib/tests/tst_dataroot02 /lib/tests/tst_dataroot03 +/lib/tests/tst_safe_fileops
include/old/old_safe_file_ops.h +8 -0
@@ -38,6 +38,14 @@safe_file_scanf (__FILE__, __LINE__, (cleanup_fn), \ (path), (fmt), ## __VA_ARGS__) +#define FILE_LINES_SCANF (cleanup_fn, path, fmt, ...) \ + file_lines_scanf(__FILE__, __LINE__, (cleanup_fn), 0, \ + (path), (fmt), ## __VA_ARGS__) + +#define SAFE_FILE_LINES_SCANF (cleanup_fn, path, fmt, ...) \ + file_lines_scanf(__FILE__, __LINE__, (cleanup_fn), 1, \ + (path), (fmt), ## __VA_ARGS__) +#define FILE_PRINTF (path, fmt, ...) \ file_printf(__FILE__, __LINE__, \ (path), (fmt), ## __VA_ARGS__)
include/safe_file_ops_fn.h +5 -0
@@ -35,6 +35,11 @@ void safe_file_scanf(const char *file, const int lineno, const char *path, const char *fmt, ...) __attribute__ ((format (scanf, 5, 6))); +int file_lines_scanf (const char *file,const int lineno, +void (*cleanup_fn)(void ),int strict, +const char *path,const char *fmt, ...) + __attribute__ ((format (scanf,6 ,7 ))); +/* * All-in-one function that lets you printf directly into a file. */
include/tst_safe_file_ops.h +8 -0
@@ -30,6 +30,14 @@safe_file_scanf (__FILE__, __LINE__,NULL , \ (path), (fmt), ## __VA_ARGS__) +#define FILE_LINES_SCANF (path, fmt, ...) \ + file_lines_scanf(__FILE__, __LINE__, NULL, 0,\ + (path), (fmt), ## __VA_ARGS__) + +#define SAFE_FILE_LINES_SCANF (path, fmt, ...) \ + file_lines_scanf(__FILE__, __LINE__, NULL, 1,\ + (path), (fmt), ## __VA_ARGS__) +#define SAFE_FILE_PRINTF (path, fmt, ...) \ safe_file_printf(__FILE__, __LINE__, NULL, \ (path), (fmt), ## __VA_ARGS__)
lib/safe_file_ops.c +47 -0
53 lines not on this page · raw message
lib/tests/tst_safe_fileops.c new file +40 -0
@@ -0,0 +1,40 @@ +/* +* Copyright (c) 2016 Linux Test Project +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <stdio.h> +#include "tst_test.h" + +static void do_test (void ) +{ +long free; +long nproc; +long dummy; + +SAFE_FILE_LINES_SCANF ("/proc/meminfo" ,"MemFree: %ld" , &free); +if (FILE_LINES_SCANF ("/proc/stat" ,"processes %ld" , &nproc)) +tst_brk (TBROK ,"Could not parse processes" ); +tst_res (TPASS ,"Free: %ld, nproc: %ld" , free, nproc); + +if (FILE_LINES_SCANF ("/proc/stat" ,"non-existent %ld" , &dummy)) +tst_res (TPASS ,"non-existent not found" ); +SAFE_FILE_LINES_SCANF ("/proc/stat" ,"non-existent %ld" , &dummy); +} + +static struct tst_test test = { + .tid ="tst_safe_fileops" , + .test_all = do_test, +};
Signature · 1 line
1.8.3.1
PATCH 1/1 lib: add [SAFE_]FILE_LINES_SCANF
Hi!
Quoted 1 line
lib/tests/tst_safe_fileops.c | 40 +++++++++++++++++++++++++++++++++++
The last minor nitpick is that the lib/tests/ directory is full of oldlib tests and this is a newlib test. It may be better to add this file to the lib/newlib_tests/.
Anyway this version looks fine, acked.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 1/1 lib: add [SAFE_]FILE_LINES_SCANF
----- Original Message -----
Quoted 6 lines
From: "Cyril Hrubis" <[email protected]>
To: "Jan Stancek" <[email protected]>
Cc: [email protected]
Sent: Monday, 18 July, 2016 1:36:44 PM
Subject: Re: [LTP] [PATCH] lib: add [SAFE_]FILE_LINES_SCANF
Quoted 6 lines
Hi!
Quoted 1 line
lib/tests/tst_safe_fileops.c | 40 +++++++++++++++++++++++++++++++++++The last minor nitpick is that the
lib/tests/directory is full of oldlib tests and this is a newlib test. It may be better to add this file to thelib/newlib_tests/.
Moved and pushed.
Regards,
Jan
Quoted 7 lines
Anyway this version looks fine, acked.
--
Cyril Hrubis
[email protected]