Skip to content

Commit 99be99f

Browse files
committed
getPathOfStdout: return a static string
let the caller duplicate it if needed, allows for passing the return value straight to optionsParseFileName() also since STDOUT_FILENO is defined to be 1 there was no need to call snprintf().
1 parent 9d53ace commit 99be99f

File tree

2 files changed

+9
-19
lines changed

2 files changed

+9
-19
lines changed

src/options.c

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -336,26 +336,15 @@ static bool accessFileOk(const char *const pathName)
336336
return (0 == access(pathName, W_OK));
337337
}
338338

339-
static char *getPathOfStdout(void)
339+
static const char *getPathOfStdout(void)
340340
{
341-
char path[16] = {"/dev/stdout"};
342-
const size_t len = sizeof(path);
341+
const char *paths[] = { "/dev/stdout", "/dev/fd/1", "/proc/self/fd/1" };
343342

344-
if (!accessFileOk(path)) {
345-
346-
snprintf(path, len, "/dev/fd/%d", STDOUT_FILENO);
347-
348-
if (!accessFileOk(path)) {
349-
350-
snprintf(path, len, "/proc/self/fd/%d", STDOUT_FILENO);
351-
352-
if (!accessFileOk(path)) {
353-
// We quit because imlib2 will fail later anyway.
354-
err(EXIT_FAILURE, "access to stdout failed");
355-
}
356-
}
343+
for (size_t i = 0; i < ARRAY_COUNT(paths); ++i) {
344+
if (accessFileOk(paths[i]))
345+
return paths[i];
357346
}
358-
return strndup(path, len);
347+
err(EXIT_FAILURE, "access to stdout failed");
359348
}
360349

361350
void optionsParse(int argc, char *argv[])
@@ -506,8 +495,7 @@ void optionsParse(int argc, char *argv[])
506495
const bool redirectChar = ( opt.outputFile[0] == '-'
507496
&& opt.outputFile[1] == '\0');
508497
if (redirectChar) {
509-
free(opt.outputFile);
510-
opt.outputFile = getPathOfStdout();
498+
optionsParseFileName(getPathOfStdout());
511499
opt.overwrite = 1;
512500
opt.thumb = THUMB_DISABLED;
513501
}

src/util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2626

2727
#pragma once
2828

29+
#define ARRAY_COUNT(X) (sizeof(X) / sizeof(0[X]))
30+
2931
char *estrdup(const char *);
3032
void *ecalloc(size_t, size_t);

0 commit comments

Comments
 (0)