1 /* Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
31 #include "localedef.h"
32 #include "charmap-dir.h"
34 /* The data type of a charmap directory being traversed. */
38 /* The directory pathname, ending in a slash. */
41 /* Scratch area used for returning pathnames. */
46 /* Starts a charmap directory traversal.
47 Returns a CHARMAP_DIR, or NULL if the directory doesn't exist. */
49 charmap_opendir (const char *directory
)
51 struct charmap_dir
*cdir
;
56 dir
= opendir (directory
);
59 WITH_CUR_LOCALE (error (1, errno
, gettext ("\
60 cannot read character map directory `%s'"), directory
));
64 cdir
= (struct charmap_dir
*) xmalloc (sizeof (struct charmap_dir
));
67 len
= strlen (directory
);
68 add_slash
= (len
== 0 || directory
[len
- 1] != '/');
69 cdir
->directory
= (char *) xmalloc (len
+ add_slash
+ 1);
70 memcpy (cdir
->directory
, directory
, len
);
72 cdir
->directory
[len
] = '/';
73 cdir
->directory
[len
+ add_slash
] = '\0';
74 cdir
->directory_len
= len
+ add_slash
;
76 cdir
->pathname
= NULL
;
77 cdir
->pathname_size
= 0;
82 /* Reads the next directory entry.
83 Returns its charmap name, or NULL if past the last entry or upon error.
84 The storage returned may be overwritten by a later charmap_readdir
85 call on the same CHARMAP_DIR. */
87 charmap_readdir (CHARMAP_DIR
*cdir
)
91 struct dirent64
*dirent
;
97 dirent
= readdir64 (cdir
->dir
);
100 if (strcmp (dirent
->d_name
, ".") == 0)
102 if (strcmp (dirent
->d_name
, "..") == 0)
105 len
= strlen (dirent
->d_name
);
107 size
= cdir
->directory_len
+ len
+ 1;
108 if (size
> cdir
->pathname_size
)
110 free (cdir
->pathname
);
111 if (size
< 2 * cdir
->pathname_size
)
112 size
= 2 * cdir
->pathname_size
;
113 cdir
->pathname
= (char *) xmalloc (size
);
114 cdir
->pathname_size
= size
;
117 stpcpy (stpcpy (cdir
->pathname
, cdir
->directory
), dirent
->d_name
);
118 filename
= cdir
->pathname
+ cdir
->directory_len
;
120 #ifdef _DIRENT_HAVE_D_TYPE
121 if (dirent
->d_type
!= DT_UNKNOWN
&& dirent
->d_type
!= DT_LNK
)
122 mode
= DTTOIF (dirent
->d_type
);
128 if (stat (cdir
->pathname
, &statbuf
) < 0)
131 mode
= statbuf
.st_mode
;
137 /* For compressed charmaps, the canonical charmap name does not
138 include the extension. */
139 if (len
> 3 && memcmp (&filename
[len
- 3], ".gz", 3) == 0)
140 filename
[len
- 3] = '\0';
141 else if (len
> 4 && memcmp (&filename
[len
- 4], ".bz2", 4) == 0)
142 filename
[len
- 4] = '\0';
148 /* Finishes a charmap directory traversal, and frees the resources
149 attached to the CHARMAP_DIR. */
151 charmap_closedir (CHARMAP_DIR
*cdir
)
153 DIR *dir
= cdir
->dir
;
155 free (cdir
->directory
);
156 free (cdir
->pathname
);
158 return closedir (dir
);
161 /* Creates a subprocess decompressing the given pathname, and returns
162 a stream reading its output (the decompressed data). */
165 fopen_uncompressed (const char *pathname
, const char *compressor
)
169 pfd
= open (pathname
, O_RDONLY
);
175 if (fstat (pfd
, &statbuf
) >= 0
176 && S_ISREG (statbuf
.st_mode
)
180 = { (char *) compressor
, (char *) "-d", (char *) "-c", NULL
};
181 posix_spawn_file_actions_t actions
;
183 if (posix_spawn_file_actions_init (&actions
) == 0)
185 if (posix_spawn_file_actions_adddup2 (&actions
,
186 fd
[1], STDOUT_FILENO
) == 0
187 && posix_spawn_file_actions_addclose (&actions
, fd
[1]) == 0
188 && posix_spawn_file_actions_addclose (&actions
, fd
[0]) == 0
189 && posix_spawn_file_actions_adddup2 (&actions
,
190 pfd
, STDIN_FILENO
) == 0
191 && posix_spawn_file_actions_addclose (&actions
, pfd
) == 0
192 && posix_spawnp (NULL
, compressor
, &actions
, NULL
,
195 posix_spawn_file_actions_destroy (&actions
);
198 return fdopen (fd
[0], "r");
200 posix_spawn_file_actions_destroy (&actions
);
210 /* Opens a charmap for reading, given its name (not an alias name). */
212 charmap_open (const char *directory
, const char *name
)
214 size_t dlen
= strlen (directory
);
215 int add_slash
= (dlen
== 0 || directory
[dlen
- 1] != '/');
216 size_t nlen
= strlen (name
);
221 pathname
= alloca (dlen
+ add_slash
+ nlen
+ 5);
222 p
= stpcpy (pathname
, directory
);
225 p
= stpcpy (p
, name
);
227 stream
= fopen (pathname
, "rm");
231 memcpy (p
, ".gz", 4);
232 stream
= fopen_uncompressed (pathname
, "gzip");
236 memcpy (p
, ".bz2", 5);
237 stream
= fopen_uncompressed (pathname
, "bzip2");
244 /* An empty alias list. Avoids the need to return NULL from
246 static char *empty
[1];
248 /* Returns a NULL terminated list of alias names of a charmap. */
250 charmap_aliases (const char *directory
, const char *name
)
256 stream
= charmap_open (directory
, name
);
263 while (!feof (stream
))
268 if (fscanf (stream
, " <code_set_name> %as", &alias
) == 1
269 || fscanf (stream
, "%% alias %as", &alias
) == 1)
271 aliases
= (char **) xrealloc (aliases
,
272 (naliases
+ 2) * sizeof (char *));
273 aliases
[naliases
++] = alias
;
276 /* Read the rest of the line. */
277 if (fgets (junk
, sizeof junk
, stream
) != NULL
)
279 if (strstr (junk
, "CHARMAP") != NULL
)
280 /* We cannot expect more aliases from now on. */
283 while (strchr (junk
, '\n') == NULL
284 && fgets (junk
, sizeof junk
, stream
) != NULL
)
294 aliases
[naliases
] = NULL
;
298 /* Frees an alias list returned by charmap_aliases. */
300 charmap_free_aliases (char **aliases
)
302 if (aliases
!= empty
)
306 for (p
= aliases
; *p
; p
++)