2001-02-06  Ulrich Drepper  <drepper@redhat.com>

	* locale/programs/locale.c (write_locales): Use scandir to read
	directory so that the entries are sorted.
This commit is contained in:
Ulrich Drepper 2001-02-06 18:56:47 +00:00
parent 75387c9a18
commit fdc6c28a80
3 changed files with 147 additions and 135 deletions

View File

@ -1,3 +1,8 @@
2001-02-06 Ulrich Drepper <drepper@redhat.com>
* locale/programs/locale.c (write_locales): Use scandir to read
directory so that the entries are sorted.
2001-02-06 Andreas Jaeger <aj@suse.de> 2001-02-06 Andreas Jaeger <aj@suse.de>
* include/pthread.h: New file. * include/pthread.h: New file.

View File

@ -295,6 +295,37 @@ print_names (const void *nodep, VISIT value, int level)
} }
static int
select_dirs (const struct dirent *dirent)
{
int result = 0;
if (strcmp (dirent->d_name, ".") != 0 && strcmp (dirent->d_name, "..") != 0)
{
mode_t mode = 0;
#ifdef _DIRENT_HAVE_D_TYPE
if (dirent->d_type != DT_UNKNOWN && dirent->d_type != DT_LNK)
mode = DTTOIF (dirent->d_type);
else
#endif
{
struct stat64 st;
char buf[sizeof (LOCALEDIR) + strlen (dirent->d_name) + 1];
stpcpy (stpcpy (stpcpy (buf, LOCALEDIR), "/"), dirent->d_name);
if (stat64 (buf, &st) == 0)
mode = st.st_mode;
}
result = S_ISDIR (mode);
}
return result;
}
/* Write the names of all available locales to stdout. We have some /* Write the names of all available locales to stdout. We have some
sources of the information: the contents of the locale directory sources of the information: the contents of the locale directory
and the locale.alias file. To avoid duplicates and print the and the locale.alias file. To avoid duplicates and print the
@ -305,8 +336,9 @@ write_locales (void)
{ {
char linebuf[80]; char linebuf[80];
void *all_data = NULL; void *all_data = NULL;
DIR *dir; struct dirent **dirents;
struct dirent *dirent; int ndirents;
int cnt;
char *alias_path; char *alias_path;
size_t alias_path_len; size_t alias_path_len;
char *entry; char *entry;
@ -406,163 +438,133 @@ write_locales (void)
fclose (fp); fclose (fp);
} }
/* This is the directory with the locale files. */
dir = opendir (LOCALEDIR);
if (dir == NULL)
{
error (1, errno, gettext ("cannot read locale directory `%s'"),
LOCALEDIR);
return;
}
memset (linebuf, '-', sizeof (linebuf) - 1); memset (linebuf, '-', sizeof (linebuf) - 1);
linebuf[sizeof (linebuf) - 1] = '\0'; linebuf[sizeof (linebuf) - 1] = '\0';
/* Now we can look for all files in the directory. */ /* Now we can look for all files in the directory. */
while ((dirent = readdir (dir)) != NULL) ndirents = scandir (LOCALEDIR, &dirents, select_dirs, alphasort);
if (strcmp (dirent->d_name, ".") != 0 for (cnt = 0; cnt < ndirents; ++cnt)
&& strcmp (dirent->d_name, "..") != 0) {
{ /* Test whether at least the LC_CTYPE data is there. Some
mode_t mode; directories only contain translations. */
#ifdef _DIRENT_HAVE_D_TYPE char buf[sizeof (LOCALEDIR) + strlen (dirents[cnt]->d_name)
if (dirent->d_type != DT_UNKNOWN && dirent->d_type != DT_LNK) + sizeof "/LC_IDENTIFICATION"];
mode = DTTOIF (dirent->d_type); char *enddir;
else struct stat64 st;
#endif
{
struct stat64 st;
char buf[sizeof (LOCALEDIR) + strlen (dirent->d_name) + 1];
stpcpy (stpcpy (stpcpy (buf, LOCALEDIR), "/"), dirent->d_name); stpcpy (enddir = stpcpy (stpcpy (stpcpy (buf, LOCALEDIR), "/"),
dirents[cnt]->d_name),
"/LC_IDENTIFICATION");
if (stat64 (buf, &st) < 0) if (stat64 (buf, &st) == 0 && S_ISREG (st.st_mode))
continue; {
mode = st.st_mode; if (verbose)
} {
/* Provide some nice output of all kinds of
information. */
int fd;
if (S_ISDIR (mode)) if (! first_locale)
{ putchar_unlocked ('\n');
/* Test whether at least the LC_CTYPE data is there. Some first_locale = 0;
directories only contain translations. */
char buf[sizeof (LOCALEDIR) + strlen (dirent->d_name)
+ sizeof "/LC_IDENTIFICATION"];
char *enddir;
struct stat64 st;
stpcpy (enddir = stpcpy (stpcpy (stpcpy (buf, LOCALEDIR), "/"), printf ("locale: %-15.15s directory: %.*s\n%s\n",
dirent->d_name), dirents[cnt]->d_name, (int) (enddir - buf), buf,
"/LC_IDENTIFICATION"); linebuf);
if (stat64 (buf, &st) == 0 && S_ISREG (st.st_mode)) fd = open64 (buf, O_RDONLY);
{ if (fd != -1)
if (verbose) {
{ void *mapped = mmap64 (NULL, st.st_size, PROT_READ,
/* Provide some nice output of all kinds of MAP_SHARED, fd, 0);
information. */ if (mapped != MAP_FAILED)
int fd; {
/* Read the information from the file. */
if (! first_locale) struct
putchar_unlocked ('\n');
first_locale = 0;
printf ("locale: %-15.15s directory: %.*s\n%s\n",
dirent->d_name, (int) (enddir - buf), buf,
linebuf);
fd = open64 (buf, O_RDONLY);
if (fd != -1)
{ {
void *mapped = mmap64 (NULL, st.st_size, PROT_READ, unsigned int magic;
MAP_SHARED, fd, 0); unsigned int nstrings;
if (mapped != MAP_FAILED) unsigned int strindex[0];
{ } *filedata = mapped;
/* Read the information from the file. */
struct
{
unsigned int magic;
unsigned int nstrings;
unsigned int strindex[0];
} *filedata = mapped;
if (filedata->magic == LIMAGIC (LC_IDENTIFICATION) if (filedata->magic == LIMAGIC (LC_IDENTIFICATION)
&& (sizeof *filedata && (sizeof *filedata
+ (filedata->nstrings + (filedata->nstrings
* sizeof (unsigned int)) * sizeof (unsigned int))
<= (size_t) st.st_size)) <= (size_t) st.st_size))
{ {
const char *str; const char *str;
#define HANDLE(idx, name) \ #define HANDLE(idx, name) \
str = ((char *) mapped \ str = ((char *) mapped \
+ filedata->strindex[_NL_ITEM_INDEX (_NL_IDENTIFICATION_##idx)]); \ + filedata->strindex[_NL_ITEM_INDEX (_NL_IDENTIFICATION_##idx)]); \
if (*str != '\0') \ if (*str != '\0') \
printf ("%9s | %s\n", name, str) printf ("%9s | %s\n", name, str)
HANDLE (TITLE, "title"); HANDLE (TITLE, "title");
HANDLE (SOURCE, "source"); HANDLE (SOURCE, "source");
HANDLE (ADDRESS, "address"); HANDLE (ADDRESS, "address");
HANDLE (CONTACT, "contact"); HANDLE (CONTACT, "contact");
HANDLE (EMAIL, "email"); HANDLE (EMAIL, "email");
HANDLE (TEL, "telephone"); HANDLE (TEL, "telephone");
HANDLE (FAX, "fax"); HANDLE (FAX, "fax");
HANDLE (LANGUAGE, "language"); HANDLE (LANGUAGE, "language");
HANDLE (TERRITORY, "territory"); HANDLE (TERRITORY, "territory");
HANDLE (AUDIENCE, "audience"); HANDLE (AUDIENCE, "audience");
HANDLE (APPLICATION, "application"); HANDLE (APPLICATION, "application");
HANDLE (ABBREVIATION, "abbreviation"); HANDLE (ABBREVIATION, "abbreviation");
HANDLE (REVISION, "revision"); HANDLE (REVISION, "revision");
HANDLE (DATE, "date"); HANDLE (DATE, "date");
} }
munmap (mapped, st.st_size); munmap (mapped, st.st_size);
} }
close (fd); close (fd);
/* Now try to get the charset information. */ /* Now try to get the charset information. */
strcpy (enddir, "/LC_CTYPE"); strcpy (enddir, "/LC_CTYPE");
fd = open64 (buf, O_RDONLY); fd = open64 (buf, O_RDONLY);
if (fd != -1 && fstat64 (fd, &st) >= 0 if (fd != -1 && fstat64 (fd, &st) >= 0
&& ((mapped = mmap64 (NULL, st.st_size, PROT_READ, && ((mapped = mmap64 (NULL, st.st_size, PROT_READ,
MAP_SHARED, fd, 0)) MAP_SHARED, fd, 0))
!= MAP_FAILED)) != MAP_FAILED))
{ {
struct struct
{ {
unsigned int magic; unsigned int magic;
unsigned int nstrings; unsigned int nstrings;
unsigned int strindex[0]; unsigned int strindex[0];
} *filedata = mapped; } *filedata = mapped;
if (filedata->magic == LIMAGIC (LC_CTYPE) if (filedata->magic == LIMAGIC (LC_CTYPE)
&& (sizeof *filedata && (sizeof *filedata
+ (filedata->nstrings + (filedata->nstrings
* sizeof (unsigned int)) * sizeof (unsigned int))
<= (size_t) st.st_size)) <= (size_t) st.st_size))
{ {
const char *str; const char *str;
str = ((char *) mapped str = ((char *) mapped
+ filedata->strindex[_NL_ITEM_INDEX (_NL_CTYPE_CODESET_NAME)]); + filedata->strindex[_NL_ITEM_INDEX (_NL_CTYPE_CODESET_NAME)]);
if (*str != '\0') if (*str != '\0')
printf (" codeset | %s\n", str); printf (" codeset | %s\n", str);
} }
munmap (mapped, st.st_size); munmap (mapped, st.st_size);
} }
if (fd != -1) if (fd != -1)
close (fd); close (fd);
} }
} }
else else
/* If the verbose format is not selected we simply /* If the verbose format is not selected we simply
collect the names. */ collect the names. */
PUT (xstrdup (dirent->d_name)); PUT (xstrdup (dirents[cnt]->d_name));
} }
} }
} if (ndirents > 0)
free (dirents);
closedir (dir);
if (! verbose) if (! verbose)
{ {

View File

@ -1,3 +1,8 @@
2001-02-06 Ulrich Drepper <drepper@redhat.com>
* locales/zh_TW: Extend LC_IDENTIFICATION entries a bit.
* locales/zh_CN: Likewise.
2001-02-04 Bruno Haible <haible@clisp.cons.org> 2001-02-04 Bruno Haible <haible@clisp.cons.org>
* locales/translit_cjk_compat: Add missing characters in range * locales/translit_cjk_compat: Add missing characters in range