| 1 | /* Load needed message catalogs.\r |
| 2 | Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.\r |
| 3 | \r |
| 4 | This program is free software; you can redistribute it and/or modify\r |
| 5 | it under the terms of the GNU General Public License as published by\r |
| 6 | the Free Software Foundation; either version 2, or (at your option)\r |
| 7 | any later version.\r |
| 8 | \r |
| 9 | This program is distributed in the hope that it will be useful,\r |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of\r |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r |
| 12 | GNU General Public License for more details.\r |
| 13 | \r |
| 14 | You should have received a copy of the GNU General Public License\r |
| 15 | along with this program; if not, write to the Free Software Foundation,\r |
| 16 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA. */\r |
| 17 | \r |
| 18 | #include "intlconfig.h"\r |
| 19 | \r |
| 20 | #include <fcntl.h>\r |
| 21 | #include <sys/types.h>\r |
| 22 | #include <sys/stat.h>\r |
| 23 | \r |
| 24 | #if defined STDC_HEADERS || defined _LIBC\r |
| 25 | # include <stdlib.h>\r |
| 26 | #endif\r |
| 27 | \r |
| 28 | #if defined HAVE_UNISTD_H || defined _LIBC\r |
| 29 | # include <unistd.h>\r |
| 30 | #endif\r |
| 31 | \r |
| 32 | #if (defined HAVE_MMAP && defined HAVE_MUNMAP) || defined _LIBC\r |
| 33 | # include <sys/mman.h>\r |
| 34 | #endif\r |
| 35 | \r |
| 36 | #ifdef _WIN32\r |
| 37 | #include <io.h>\r |
| 38 | #ifdef _MSC_VER\r |
| 39 | #pragma warning (disable:4018)\r |
| 40 | #endif\r |
| 41 | #endif\r |
| 42 | \r |
| 43 | #include "gettext.h"\r |
| 44 | #include "gettextP.h"\r |
| 45 | \r |
| 46 | /* @@ end of prolog @@ */\r |
| 47 | \r |
| 48 | #ifdef _LIBC\r |
| 49 | /* Rename the non ISO C functions. This is required by the standard\r |
| 50 | because some ISO C functions will require linking with this object\r |
| 51 | file and the name space must not be polluted. */\r |
| 52 | # define open __open\r |
| 53 | # define close __close\r |
| 54 | # define read __read\r |
| 55 | # define mmap __mmap\r |
| 56 | # define munmap __munmap\r |
| 57 | #endif\r |
| 58 | \r |
| 59 | /* We need a sign, whether a new catalog was loaded, which can be associated\r |
| 60 | with all translations. This is important if the translations are\r |
| 61 | cached by one of GCC's features. */\r |
| 62 | int _nl_msg_cat_cntr = 0;\r |
| 63 | \r |
| 64 | \r |
| 65 | /* Load the message catalogs specified by FILENAME. If it is no valid\r |
| 66 | message catalog do nothing. */\r |
| 67 | void\r |
| 68 | internal_function\r |
| 69 | _nl_load_domain (domain_file)\r |
| 70 | struct loaded_l10nfile *domain_file;\r |
| 71 | {\r |
| 72 | int fd;\r |
| 73 | size_t size;\r |
| 74 | struct stat st;\r |
| 75 | struct mo_file_header *data = (struct mo_file_header *) -1;\r |
| 76 | #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \\r |
| 77 | || defined _LIBC\r |
| 78 | int use_mmap = 0;\r |
| 79 | #endif\r |
| 80 | struct loaded_domain *domain;\r |
| 81 | \r |
| 82 | domain_file->decided = 1;\r |
| 83 | domain_file->data = NULL;\r |
| 84 | \r |
| 85 | /* If the record does not represent a valid locale the FILENAME\r |
| 86 | might be NULL. This can happen when according to the given\r |
| 87 | specification the locale file name is different for XPG and CEN\r |
| 88 | syntax. */\r |
| 89 | if (domain_file->filename == NULL)\r |
| 90 | return;\r |
| 91 | \r |
| 92 | /* Try to open the addressed file. */\r |
| 93 | fd = open (domain_file->filename, O_RDONLY | O_BINARY); /*FRANCO - binary*/\r |
| 94 | if (fd == -1)\r |
| 95 | return;\r |
| 96 | \r |
| 97 | /* We must know about the size of the file. */\r |
| 98 | if (fstat (fd, &st) != 0\r |
| 99 | || (size = (size_t) st.st_size) != st.st_size\r |
| 100 | || size < sizeof (struct mo_file_header))\r |
| 101 | {\r |
| 102 | /* Something went wrong. */\r |
| 103 | close (fd);\r |
| 104 | return;\r |
| 105 | }\r |
| 106 | \r |
| 107 | #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \\r |
| 108 | || defined _LIBC\r |
| 109 | /* Now we are ready to load the file. If mmap() is available we try\r |
| 110 | this first. If not available or it failed we try to load it. */\r |
| 111 | data = (struct mo_file_header *) mmap (NULL, size, PROT_READ,\r |
| 112 | MAP_PRIVATE, fd, 0);\r |
| 113 | \r |
| 114 | if (data != (struct mo_file_header *) -1)\r |
| 115 | {\r |
| 116 | /* mmap() call was successful. */\r |
| 117 | close (fd);\r |
| 118 | use_mmap = 1;\r |
| 119 | }\r |
| 120 | #endif\r |
| 121 | \r |
| 122 | /* If the data is not yet available (i.e. mmap'ed) we try to load\r |
| 123 | it manually. */\r |
| 124 | if (data == (struct mo_file_header *) -1)\r |
| 125 | {\r |
| 126 | size_t to_read;\r |
| 127 | char *read_ptr;\r |
| 128 | \r |
| 129 | data = (struct mo_file_header *) malloc (size);\r |
| 130 | if (data == NULL)\r |
| 131 | return;\r |
| 132 | \r |
| 133 | to_read = size;\r |
| 134 | read_ptr = (char *) data;\r |
| 135 | do\r |
| 136 | {\r |
| 137 | long int nb = (long int) read (fd, read_ptr, to_read);\r |
| 138 | if (nb == -1)\r |
| 139 | {\r |
| 140 | close (fd);\r |
| 141 | return;\r |
| 142 | }\r |
| 143 | \r |
| 144 | read_ptr += nb;\r |
| 145 | to_read -= nb;\r |
| 146 | }\r |
| 147 | while (to_read > 0);\r |
| 148 | \r |
| 149 | close (fd);\r |
| 150 | }\r |
| 151 | \r |
| 152 | /* Using the magic number we can test whether it really is a message\r |
| 153 | catalog file. */\r |
| 154 | if (data->magic != _MAGIC && data->magic != _MAGIC_SWAPPED)\r |
| 155 | {\r |
| 156 | /* The magic number is wrong: not a message catalog file. */\r |
| 157 | #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \\r |
| 158 | || defined _LIBC\r |
| 159 | if (use_mmap)\r |
| 160 | munmap ((caddr_t) data, size);\r |
| 161 | else\r |
| 162 | #endif\r |
| 163 | free (data);\r |
| 164 | return;\r |
| 165 | }\r |
| 166 | \r |
| 167 | domain_file->data\r |
| 168 | = (struct loaded_domain *) malloc (sizeof (struct loaded_domain));\r |
| 169 | if (domain_file->data == NULL)\r |
| 170 | return;\r |
| 171 | \r |
| 172 | domain = (struct loaded_domain *) domain_file->data;\r |
| 173 | domain->data = (char *) data;\r |
| 174 | #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \\r |
| 175 | || defined _LIBC\r |
| 176 | domain->use_mmap = use_mmap;\r |
| 177 | #endif\r |
| 178 | domain->mmap_size = size;\r |
| 179 | domain->must_swap = data->magic != _MAGIC;\r |
| 180 | \r |
| 181 | /* Fill in the information about the available tables. */\r |
| 182 | switch (W (domain->must_swap, data->revision))\r |
| 183 | {\r |
| 184 | case 0:\r |
| 185 | domain->nstrings = W (domain->must_swap, data->nstrings);\r |
| 186 | domain->orig_tab = (struct string_desc *)\r |
| 187 | ((char *) data + W (domain->must_swap, data->orig_tab_offset));\r |
| 188 | domain->trans_tab = (struct string_desc *)\r |
| 189 | ((char *) data + W (domain->must_swap, data->trans_tab_offset));\r |
| 190 | domain->hash_size = W (domain->must_swap, data->hash_tab_size);\r |
| 191 | domain->hash_tab = (nls_uint32 *)\r |
| 192 | ((char *) data + W (domain->must_swap, data->hash_tab_offset));\r |
| 193 | break;\r |
| 194 | default:\r |
| 195 | /* This is an illegal revision. */\r |
| 196 | #if (defined HAVE_MMAP && defined HAVE_MUNMAP && !defined DISALLOW_MMAP) \\r |
| 197 | || defined _LIBC\r |
| 198 | if (use_mmap)\r |
| 199 | munmap ((caddr_t) data, size);\r |
| 200 | else\r |
| 201 | #endif\r |
| 202 | free (data);\r |
| 203 | free (domain);\r |
| 204 | domain_file->data = NULL;\r |
| 205 | return;\r |
| 206 | }\r |
| 207 | \r |
| 208 | /* Show that one domain is changed. This might make some cached\r |
| 209 | translations invalid. */\r |
| 210 | ++_nl_msg_cat_cntr;\r |
| 211 | }\r |
| 212 | \r |
| 213 | \r |
| 214 | #ifdef _LIBC\r |
| 215 | void\r |
| 216 | internal_function\r |
| 217 | _nl_unload_domain (domain)\r |
| 218 | struct loaded_domain *domain;\r |
| 219 | {\r |
| 220 | if (domain->use_mmap)\r |
| 221 | munmap ((caddr_t) domain->data, domain->mmap_size);\r |
| 222 | else\r |
| 223 | free ((void *) domain->data);\r |
| 224 | \r |
| 225 | free (domain);\r |
| 226 | }\r |
| 227 | #endif\r |