From 42c6eae687354dff85ddc520097a3962bf926469 Mon Sep 17 00:00:00 2001
From: Morris Jette <jette@schedmd.com>
Date: Thu, 19 Jan 2012 10:28:19 -0800
Subject: [PATCH] sbatch environment setting mods

We replaced references to "pipe" with a more generic "file descriptor".
We also replaced a while loop in env.c with a for loop.
---
 doc/man/man1/sbatch.1 |  6 +++---
 src/common/env.c      | 24 +++++++++---------------
 src/sbatch/opt.c      |  4 ++--
 3 files changed, 14 insertions(+), 20 deletions(-)

diff --git a/doc/man/man1/sbatch.1 b/doc/man/man1/sbatch.1
index 61dbfa723b1..81441ee268f 100644
--- a/doc/man/man1/sbatch.1
+++ b/doc/man/man1/sbatch.1
@@ -376,12 +376,12 @@ option will implicitly be set to load other environment variables based upon
 the user's configuration on the cluster which executes the job.
 
 .TP
-\fB\-\-export\-file\fR=<\fIfilename\fR | \fIpipe_fd\fR>
+\fB\-\-export\-file\fR=<\fIfilename\fR | \fIfd\fR>
 If a number between 3 and OPEN_MAX is specified as the argument to
-this option, a pipe's read file descriptor will be assumed (STDIN and
+this option, a readable file descriptor will be assumed (STDIN and
 STDOUT are not supported as valid arguments).  Otherwise a filename is
 assumed.  Export environment variables defined in <\fIfilename\fR> or
-read from <\fIpipe_fd\fR> to the job's execution environment. The
+read from <\fIfd\fR> to the job's execution environment. The
 content is one or more environment variable definitions of the form
 NAME=value, each separated by a null character.  This allows the use
 of special characters in environment definitions. The
diff --git a/src/common/env.c b/src/common/env.c
index 529f1af4950..19e32418cdd 100644
--- a/src/common/env.c
+++ b/src/common/env.c
@@ -1581,9 +1581,9 @@ static int _bracket_cnt(char *value)
 }
 
 /*
- * Load user environment from a specified file or pipe.
+ * Load user environment from a specified file or file descriptor.
  *
- * This will read in a user specified file or pipe, that is invoked
+ * This will read in a user specified file or fd, that is invoked
  * via the --export-file option in sbatch. The NAME=value entries must
  * be NULL separated to support special characters in the environment
  * definitions.
@@ -1596,7 +1596,7 @@ static int _bracket_cnt(char *value)
 char **env_array_from_file(const char *fname)
 {
 	char *buf = NULL, *ptr = NULL, *eptr = NULL;
-	char *line, *value, *p;
+	char *value, *p;
 	char **env = NULL;
 	char name[256];
 	int buf_size = BUFSIZ, buf_left;
@@ -1605,7 +1605,8 @@ char **env_array_from_file(const char *fname)
 	int fd;
 
 	/*
-	 * If file name is a numeric value, then it is assumed to be a pipe.
+	 * If file name is a numeric value, then it is assumed to be a
+	 * file descriptor.
 	 */
 	fd = (int)strtol(fname, &p, 10);
 	if ((*p != '\0') || (fd < 3) || (fd > sysconf(_SC_OPEN_MAX)) ||
@@ -1617,7 +1618,7 @@ char **env_array_from_file(const char *fname)
 		}
 		verbose("Getting environment variables from %s", fname);
 	} else
-		verbose("Getting environment variables from pipe %d", fd);
+		verbose("Getting environment variables from fd %d", fd);
 
 	/*
 	 * Read in the user's environment data.
@@ -1647,25 +1648,18 @@ char **env_array_from_file(const char *fname)
 	 * and build the environment.
 	 */
 	env   = env_array_create();
-	line  = xmalloc(ENV_BUFSIZE);
 	value = xmalloc(ENV_BUFSIZE);
-	ptr = buf;
-	while (ptr) {
-		memset(line, 0, ENV_BUFSIZE);
+	for (ptr = buf; ; ptr = eptr+1) {
 		eptr = strchr(ptr, separator);
 		if ((ptr == eptr) || (eptr == NULL))
 			break;
-		strncpy(line, ptr,(eptr - ptr));
- 		ptr = eptr + 1;
-		if (_env_array_entry_splitter(line, name, sizeof(name),
+		if (_env_array_entry_splitter(ptr, name, sizeof(name),
 					      value, ENV_BUFSIZE) &&
-		    (!_discard_env(name, value)) &&
-		    (name[0] != ' ')) {
+		    (!_discard_env(name, value))) {
 			env_array_overwrite(&env, name, value);
 		}
 	}
 	xfree(buf);
-	xfree(line);
 	xfree(value);
 
 	return env;
diff --git a/src/sbatch/opt.c b/src/sbatch/opt.c
index 88aa8b03455..f9c55805eb5 100644
--- a/src/sbatch/opt.c
+++ b/src/sbatch/opt.c
@@ -2803,7 +2803,7 @@ static void _usage(void)
 "              [--network=type] [--mem-per-cpu=MB] [--qos=qos] [--gres=list]\n"
 "              [--cpu_bind=...] [--mem_bind=...] [--reservation=name]\n"
 "              [--switch=max-switches{@max-time-to-wait}]\n"
-"              [--export[=names]] [--export-file=file|pipe] executable [args...]\n");
+"              [--export[=names]] [--export-file=file|fd] executable [args...]\n");
 }
 
 static void _help(void)
@@ -2822,7 +2822,7 @@ static void _help(void)
 "  -D, --workdir=directory     set working directory for batch script\n"
 "  -e, --error=err             file for batch script's standard error\n"
 "      --export[=names]        specify environment variables to export\n"
-"      --export-file=file|pipe specify environment variables file or pipe to export\n"
+"      --export-file=file|fd   specify environment variables file or file descriptor to export\n"
 "      --get-user-env          load environment from local cluster\n"
 "      --gid=group_id          group ID to run job as (user root only)\n"
 "      --gres=list             required generic resources\n"
-- 
GitLab