summaryrefslogtreecommitdiffstats
path: root/catopen/catopen.c
blob: 2f756bce683148ca185a863182bf38b0f0db3732 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* catopen.c - aeb, 960107 */

#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <nl_types.h>

extern char *index (const char *, int);         /* not always in <string.h> */
extern char *my_malloc(int);	/* in util.c */

#ifndef DEFAULT_NLSPATH
# if __GLIBC__ >= 2
#  define DEFAULT_NLSPATH "/usr/share/locale/%L/%N"
# else
#  define DEFAULT_NLSPATH "/usr/lib/locale/%N/%L"
# endif
#endif

static nl_catd my_catopenpath(char *name, char *path);

static				/* this source included in gripes.c */
nl_catd
my_catopen(char *name, int oflag) {
  nl_catd fd;

  fd = catopen(name, oflag);

  if (fd == (nl_catd) -1 && oflag) {
    oflag = 0;
    fd = catopen(name, oflag);
  }

  if (fd == (nl_catd) -1) {
    char *nlspath;

    /* The libc catopen fails - let us see if we can do better */
    /* The quotes below are from X/Open, XPG 1987, Vol. 3. */

    /*
     * "In catopen(), the oflag argument is reserved for future use
     * and should be set to 0."
     */

    if (oflag)
      return fd;		/* only treat the known case */

    /*
     * "If `name' contains a `/', then `name' specifies a pathname"
     */
    if (index(name, '/')) {
#ifdef __GLIBC__
      /* glibc uses a pointer type for nl_catd, not a fd */
      return fd;
#else
      /* this works under libc5 */
      return open(name, O_RDONLY);
#endif
    }

    /*
     * "If NLSPATH does not exist in the environment, or if a
     * message catalog cannot be opened in any of the paths specified
     * by NLSPATH, then an implementation defined default path is used"
     */

    nlspath = getenv("NLSPATH");
    if (nlspath)
      fd = my_catopenpath(name, nlspath);
    if (fd == (nl_catd) -1)
      fd = my_catopenpath(name, DEFAULT_NLSPATH);
  }
  return fd;
}

/*
 * XPG 1987, Vol. 3, Section 4.4 specifies the environment
 * variable NLSPATH (example:
 *     NLSPATH=/nlslib/%L/%N.cat:/nlslib/%N/%L
 * where %L stands for the value of the environment variable LANG,
 * and %N stands for the `name' argument of catopen().
 * The environ(5) page specifies more precisely that
 * LANG is of the form
 *     LANG=language[_territory[.codeset]]
 * and adds %l, %t, %c for the three parts of LANG, without the
 * _ or . and are NULL when these parts are missing.
 * It also specifies %% for a single % sign.
 * A leading colon, or a pair of adjacent colons, in NLSPATH
 * specifies the current directory. (Meant is that "" stands for "%N".)
 *
 */
static nl_catd
my_catopenpath(char *name, char *nlspath) {
  int fd;
  nl_catd cfd = (nl_catd) -1;
  char *path0, *path, *s, *file, *lang, *lang_l, *lang_t, *lang_c;
  int langsz, namesz, sz, lang_l_sz, lang_t_sz, lang_c_sz;

  namesz = strlen(name);

  lang = getenv("LANG");
  if (!lang)
    lang = "";
  langsz = strlen(lang);

  lang_l = lang;
  s = index(lang_l, '_');
  if (!s) {
    lang_t = lang_c = "";
    lang_l_sz = langsz;
    lang_t_sz = lang_c_sz = 0;
  } else {
    lang_l_sz = s - lang_l;
    lang_t = s+1;
    s = index(lang_t, '.');
    if (!s) {
      lang_c = "";
      lang_t_sz = langsz - lang_l_sz - 1;
      lang_c_sz = 0;
    } else {
      lang_t_sz = s - lang_t;
      lang_c = s+1;
      lang_c_sz = langsz - lang_l_sz - lang_t_sz - 2;
    }
  }

  /* figure out how much room we have to malloc() */
  sz = 0;
  s = nlspath;
  while(*s) {
    if(*s == '%') {
      s++;
      switch(*s) {
      case '%':
	sz++; break;
      case 'N':
	sz += namesz; break;
      case 'L':
	sz += langsz; break;
      case 'l':
	sz += lang_l_sz; break;
      case 't':
	sz += lang_t_sz; break;
      case 'c':
	sz += lang_c_sz; break;
      default:			/* unknown escape - ignore */
	break;
      }
    } else if (*s == ':' && (s == nlspath || s[-1] == ':')) {
      sz += (namesz + 1);
    } else
      sz++;
    s++;
  }

  /* expand the %L and %N escapes */
  path0 = my_malloc(sz + 1);
  path = path0;
  s = nlspath;
  while(*s) {
    if(*s == '%') {
      s++;
      switch(*s) {
      case '%':
	*path++ = '%'; break;
      case 'N':
	strncpy(path, name, namesz); path += namesz; break;
      case 'L':
	strncpy(path, lang, langsz); path += langsz; break;
      case 'l':
	strncpy(path, lang_l, lang_l_sz); path += lang_l_sz; break;
      case 't':
	strncpy(path, lang_t, lang_t_sz); path += lang_t_sz; break;
      case 'c':
	strncpy(path, lang_c, lang_c_sz); path += lang_c_sz; break;
      default:			/* unknown escape - ignore */
	break;
      }
    } else if (*s == ':' && (s == nlspath || s[-1] == ':')) {
      strncpy(path, name, namesz); path += namesz;
      *path++ = ':';
    } else
      *path++ = *s;
    s++;
  }
  *path = 0;

  path = path0;
  while(path) {
    file = path;
    s = index(path, ':');
    if (s) {
      *s = 0;
      path = s+1;
    } else
      path = 0;
    fd = open(file, O_RDONLY);
    if (fd != -1) {
      /* we found the right catalog - but we don't know the
	 type of nl_catd, so close it again and ask libc */
      close(fd);
      cfd = catopen(file, 0);
      break;
    }
  }

  free(path0);
  return cfd;
}