Skip to content

Commit 5819586

Browse files
committed
Refactor: remoted: Remove unnecessary nesting in load_env_vars()
Signed-off-by: Reid Wahl <[email protected]>
1 parent 6418f63 commit 5819586

File tree

1 file changed

+70
-69
lines changed

1 file changed

+70
-69
lines changed

daemons/execd/remoted_pidone.c

Lines changed: 70 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -100,95 +100,96 @@ load_env_vars(const char *filename)
100100
* descriptors open, and don't log -- silently ignore errors.
101101
*/
102102
FILE *fp = fopen(filename, "r");
103+
char line[LINE_MAX] = { 0, };
103104

104-
if (fp != NULL) {
105-
char line[LINE_MAX] = { '\0', };
105+
if (fp == NULL) {
106+
return;
107+
}
106108

107-
while (fgets(line, LINE_MAX, fp) != NULL) {
108-
char *name = NULL;
109-
char *end = NULL;
110-
char *value = NULL;
111-
char *quote = NULL;
109+
while (fgets(line, LINE_MAX, fp) != NULL) {
110+
char *name = NULL;
111+
char *end = NULL;
112+
char *value = NULL;
113+
char *quote = NULL;
112114

113-
// Look for valid name immediately followed by equals sign
114-
if (find_env_var_name(line, &name, &end) && (*++end == '=')) {
115+
// Look for valid name immediately followed by equals sign
116+
if (find_env_var_name(line, &name, &end) && (*++end == '=')) {
115117

116-
// Null-terminate name, and advance beyond equals sign
117-
*end++ = '\0';
118+
// Null-terminate name, and advance beyond equals sign
119+
*end++ = '\0';
118120

119-
// Check whether value is quoted
120-
if ((*end == '\'') || (*end == '"')) {
121-
quote = end++;
122-
}
123-
value = end;
124-
125-
if (quote) {
126-
/* Value is remaining characters up to next non-backslashed
127-
* matching quote character.
128-
*/
129-
while (((*end != *quote) || (*(end - 1) == '\\'))
130-
&& (*end != '\0')) {
131-
end++;
132-
}
133-
if (*end == *quote) {
134-
// Null-terminate value, and advance beyond close quote
135-
*end++ = '\0';
136-
} else {
137-
// Matching closing quote wasn't found
138-
value = NULL;
139-
}
121+
// Check whether value is quoted
122+
if ((*end == '\'') || (*end == '"')) {
123+
quote = end++;
124+
}
125+
value = end;
140126

127+
if (quote != NULL) {
128+
/* Value is remaining characters up to next non-backslashed
129+
* matching quote character.
130+
*/
131+
while (((*end != *quote) || (*(end - 1) == '\\'))
132+
&& (*end != '\0')) {
133+
end++;
134+
}
135+
if (*end == *quote) {
136+
// Null-terminate value, and advance beyond close quote
137+
*end++ = '\0';
141138
} else {
142-
/* Value is remaining characters up to next non-backslashed
143-
* whitespace.
144-
*/
145-
while ((!isspace(*end) || (*(end - 1) == '\\'))
146-
&& (*end != '\0')) {
147-
++end;
148-
}
149-
150-
if (end == (line + LINE_MAX - 1)) {
151-
// Line was too long
152-
value = NULL;
153-
}
154-
// Do NOT null-terminate value (yet)
139+
// Matching closing quote wasn't found
140+
value = NULL;
155141
}
156142

157-
/* We have a valid name and value, and end is now the character
158-
* after the closing quote or the first whitespace after the
159-
* unquoted value. Make sure the rest of the line is just
160-
* whitespace or a comment.
143+
} else {
144+
/* Value is remaining characters up to next non-backslashed
145+
* whitespace.
161146
*/
162-
if (value) {
163-
char *value_end = end;
147+
while ((!isspace(*end) || (*(end - 1) == '\\'))
148+
&& (*end != '\0')) {
149+
end++;
150+
}
164151

165-
while (isspace(*end) && (*end != '\n')) {
166-
++end;
167-
}
168-
if ((*end == '\n') || (*end == '#')) {
169-
if (quote == NULL) {
170-
// Now we can null-terminate an unquoted value
171-
*value_end = '\0';
172-
}
152+
if (end == (line + LINE_MAX - 1)) {
153+
// Line was too long
154+
value = NULL;
155+
}
156+
// Do NOT null-terminate value (yet)
157+
}
173158

174-
// Don't overwrite (bundle options take precedence)
175-
setenv(name, value, 0);
159+
/* We have a valid name and value, and end is now the character
160+
* after the closing quote or the first whitespace after the
161+
* unquoted value. Make sure the rest of the line is just whitespace
162+
* or a comment.
163+
*/
164+
if (value != NULL) {
165+
char *value_end = end;
176166

177-
} else {
178-
value = NULL;
167+
while (isspace(*end) && (*end != '\n')) {
168+
end++;
169+
}
170+
if ((*end == '\n') || (*end == '#')) {
171+
if (quote == NULL) {
172+
// Now we can null-terminate an unquoted value
173+
*value_end = '\0';
179174
}
175+
176+
// Don't overwrite (bundle options take precedence)
177+
setenv(name, value, 0);
178+
179+
} else {
180+
value = NULL;
180181
}
181182
}
183+
}
182184

183-
if ((value == NULL) && (strchr(line, '\n') == NULL)) {
184-
// Eat remainder of line beyond LINE_MAX
185-
if (fscanf(fp, "%*[^\n]\n") == EOF) {
186-
value = NULL; // Don't care, make compiler happy
187-
}
185+
if ((value == NULL) && (strchr(line, '\n') == NULL)) {
186+
// Eat remainder of line beyond LINE_MAX
187+
if (fscanf(fp, "%*[^\n]\n") == EOF) {
188+
value = NULL; // Don't care, make compiler happy
188189
}
189190
}
190-
fclose(fp);
191191
}
192+
fclose(fp);
192193
}
193194

194195
void

0 commit comments

Comments
 (0)