From 09b8a3cd7c827c3ef018d738802f378592ef1fe9 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Tue, 21 Jun 2011 21:18:06 +0200 Subject: [PATCH] Trims extra spaces in the configuration file. --- HISTORY | 1 + state.c | 20 +++++++++++++------- util.c | 4 ++-- util.h | 21 ++++++++++++++++++++- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/HISTORY b/HISTORY index ec1921ec..1ebcb6a9 100644 --- a/HISTORY +++ b/HISTORY @@ -3,6 +3,7 @@ SnapRAID HISTORY 1.5 2011/6 ========== +* In the configuration file extra space are ignored. * Changed the output of check/fix to allow a more easily postprocessing with other tools like awk and sort. * Added the hidden option -G/--gui to enable the output of progress diff --git a/state.c b/state.c index 197718c0..584910ea 100644 --- a/state.c +++ b/state.c @@ -108,16 +108,24 @@ void state_config(struct snapraid_state* state, const char* path, int verbose, i /* start */ s = buffer; + /* here we use strtoken_trim() to eat extra spaces inserted by the user */ + + /* skip ending spaces */ + while (ret>0 && isspace(s[ret-1])) { + --ret; + s[ret] = 0; + } /* skip initial spaces */ - while (*s && isspace(*s)) + while (*s && isspace(*s)) { ++s; + } /* ignore comments and empty lines */ if (*s == '#' || *s == 0) continue; tag = s; - s = strtoken(s); + s = strtoken_trim(s); if (strcmp(tag, "block_size") == 0) { ret = stru32(s, &state->block_size); @@ -158,7 +166,7 @@ void state_config(struct snapraid_state* state, const char* path, int verbose, i ++content_count; } else if (strcmp(tag, "disk") == 0) { char* name = s; - s = strtoken(s); + s = strtoken_trim(s); tommy_array_insert(&state->diskarr, disk_alloc(name, s)); } else if (strcmp(tag, "exclude") == 0) { struct snapraid_filter* filter = filter_alloc(-1, s); @@ -294,9 +302,7 @@ void state_read(struct snapraid_state* state) /* start */ s = buffer; - /* skip initial spaces */ - while (*s && isspace(*s)) - ++s; + /* here we use strtoken() to allow filenames starting and ending with spaces */ /* ignore comments and empty lines */ if (*s == '#' || *s == 0) @@ -304,7 +310,7 @@ void state_read(struct snapraid_state* state) tag = s; s = strtoken(s); - + if (strcmp(tag, "blk") == 0 || strcmp(tag, "inv") == 0) { char* pos; char* hash; diff --git a/util.c b/util.c index d81271aa..8b680ddb 100644 --- a/util.c +++ b/util.c @@ -55,7 +55,7 @@ int strgets(char* s, unsigned size, FILE* f) return -1; } if (i == s) - return 0; + return 0; /* return EOF only if nothing was read */ } /* remove ending carrige return to support the Windows CR+LF format */ @@ -64,7 +64,7 @@ int strgets(char* s, unsigned size, FILE* f) *i = 0; - return 1; + return i - s; } int stru32(const char* s, uint32_t* value) diff --git a/util.h b/util.h index fa28e19a..e7774913 100644 --- a/util.h +++ b/util.h @@ -35,9 +35,28 @@ static inline char* strtoken(char* s) return s; } +/** + * Like strtoken() but trim spaces. + */ +static inline char* strtoken_trim(char* s) +{ + while (*s && !isspace(*s)) + ++s; + if (*s) { + *s = 0; + ++s; + } + + /* skip ending spaces */ + while (*s && isspace(*s)) + ++s; + + return s; +} + /** * Gets a text line from a file. - * Returns -1 on error, 0 on oef, and 1 if more data is available. + * Returns -1 on error, 0 on oef, or the number of chars read. */ int strgets(char* s, unsigned size, FILE* f);