From ebde27381b403af79ba25cc2d8f3e3823a8ba764 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 3 Jan 2010 13:00:58 +0100 Subject: Preserve leading spaces, document shell problems with whitespaces & IFS --- man/1/envstore | 7 +++++++ src/envstore.c | 6 +++--- test/main | 12 ++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/man/1/envstore b/man/1/envstore index 6ee8f4e..7e65bce 100644 --- a/man/1/envstore +++ b/man/1/envstore @@ -56,6 +56,13 @@ by default, .Sh LIMITATIONS Variable names or values must not contain null bytes or newlines. .Pp +Due to limitations imposed by most shells, it is not possible to save +parameters containing more than one consecutive whitespace. +.Nm +will save and display them correctly, but unless you do +.Ev IFS +trickery, your shell will not be able to load them. +.Pp The current maximum length (in bytes) is 255 bytes for the variable name and 1023 bytes for its content. .Sh AUTHOR diff --git a/src/envstore.c b/src/envstore.c index 43e6d56..3592b29 100644 --- a/src/envstore.c +++ b/src/envstore.c @@ -20,7 +20,7 @@ #define PARAM_LENGTH 256 #define VALUE_LENGTH 1024 -#define SCAN_FORMAT "%255s %1023[^\n]\n" +#define SCAN_FORMAT "%255s%*1[ ]%1023[^\n]\n" static FILE * store_open(char *file) { struct stat finfo; @@ -108,13 +108,13 @@ static void command_disp(char *file, int command) { if (read_items == 0) errx(EXIT_FAILURE, "Unable to read items, store file '%s' corrupt?", file); - else if (read_items == 1) - vcontent[0] = '\0'; if (command == CMD_LIST) printf("%-15s = %s\n", vname, vcontent); else print_escaped(vname, vcontent); + vname[0] = '\0'; + vcontent[0] = '\0'; } if (fclose(fp) != 0) err(EXIT_FAILURE, "fclose %s", file); diff --git a/test/main b/test/main index 4a3d209..eded353 100755 --- a/test/main +++ b/test/main @@ -73,6 +73,18 @@ eval $(envstore eval) test "$hello" = 'your mom' unset hello +echo "# envstore save (leading space in value)" +envstore save hello ' world' +eval $(envstore eval) +test "$hello" = ' world' +unset hello + +echo "# envstore save (trailing space in value)" +envstore save hello 'world ' +eval $(envstore eval) +test "$hello" = 'world ' +unset hello + echo "# envstore save (overwrite)" envstore save hello world eval $(envstore eval) -- cgit v1.2.3