Add option -r to env import to allow import of text files with CRLF as line endings

When this option is enabled, CRLF is treated like LF when importing environments
from text files, which means CRs ('\r') in front of LFs ('\n') are just ignored.

Drawback of enabling this option is that (maybe exported) variables which have
a trailing CR in their content will get imported without that CR. But this
drawback is very unlikely and the big advantage of letting Windows user create
a *working* uEnv.txt too is likely more welcome.

Signed-off-by: Alexander Holler <holler@ahsoftware.de>
This commit is contained in:
Alexander Holler 2014-07-14 17:49:55 +02:00 committed by Tom Rini
parent 8973601c38
commit ecd1446fe1
4 changed files with 36 additions and 9 deletions

View file

@ -776,7 +776,7 @@ static int drop_var_from_set(const char *name, int nvars, char * vars[])
int himport_r(struct hsearch_data *htab,
const char *env, size_t size, const char sep, int flag,
int nvars, char * const vars[])
int crlf_is_lf, int nvars, char * const vars[])
{
char *data, *sp, *dp, *name, *value;
char *localvars[nvars];
@ -841,6 +841,21 @@ int himport_r(struct hsearch_data *htab,
}
}
if(!size)
return 1; /* everything OK */
if(crlf_is_lf) {
/* Remove Carriage Returns in front of Line Feeds */
unsigned ignored_crs = 0;
for(;dp < data + size && *dp; ++dp) {
if(*dp == '\r' &&
dp < data + size - 1 && *(dp+1) == '\n')
++ignored_crs;
else
*(dp-ignored_crs) = *dp;
}
size -= ignored_crs;
dp = data;
}
/* Parse environment; allow for '\0' and 'sep' as separators */
do {
ENTRY e, *rv;