#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "open-po.h"
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dir-list.h"
#include "pathname.h"
#include "xalloc.h"
#include "error.h"
#include "gettext.h"
#define _(str) gettext (str)
#define SIZEOF(a) (sizeof(a)/sizeof(a[0]))
static FILE *
try_open_po_file (const char *input_name, char **real_file_name_p)
{
static const char *extension[] = { "", ".po", ".pot", };
char *file_name;
FILE *ret_val;
int j;
size_t k;
const char *dir;
if (strcmp (input_name, "-") == 0 || strcmp (input_name, "/dev/stdin") == 0)
{
*real_file_name_p = xstrdup (_("<stdin>"));
return stdin;
}
if (IS_ABSOLUTE_PATH (input_name))
{
for (k = 0; k < SIZEOF (extension); ++k)
{
file_name = concatenated_pathname ("", input_name, extension[k]);
ret_val = fopen (file_name, "r");
if (ret_val != NULL || errno != ENOENT)
{
*real_file_name_p = file_name;
return ret_val;
}
free (file_name);
}
}
else
{
for (j = 0; (dir = dir_list_nth (j)) != NULL; ++j)
for (k = 0; k < SIZEOF (extension); ++k)
{
file_name = concatenated_pathname (dir, input_name, extension[k]);
ret_val = fopen (file_name, "r");
if (ret_val != NULL || errno != ENOENT)
{
*real_file_name_p = file_name;
return ret_val;
}
free (file_name);
}
}
*real_file_name_p = xstrdup (input_name);
errno = ENOENT;
return NULL;
}
FILE *
open_po_file (const char *input_name, char **real_file_name_p,
bool exit_on_error)
{
FILE *fp = try_open_po_file (input_name, real_file_name_p);
if (fp == NULL && exit_on_error)
error (EXIT_FAILURE, errno,
_("error while opening \"%s\" for reading"), *real_file_name_p);
return fp;
}