found nasty bug in intl/lang_tdb.c tdb structure was not tested to not be null before...
[samba.git] / source3 / web / neg_lang.c
1 /*
2    Unix SMB/CIFS implementation.
3    SWAT language handling
4    
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19    Created by Ryo Kawahara <rkawa@lbe.co.jp> 
20 */
21
22 #include "includes.h"
23 #include "../web/swat_proto.h"
24
25 /*
26   during a file download we first check to see if there is a language
27   specific file available. If there is then use that, otherwise 
28   just open the specified file
29 */
30 int web_open(const char *fname, int flags, mode_t mode)
31 {
32         char *p = NULL;
33         char *lang = lang_tdb_current();
34         int fd;
35         if (lang) {
36                 asprintf(&p, "lang/%s/%s", lang, fname);
37                 if (p) {
38                         fd = sys_open(p, flags, mode);
39                         free(p);
40                         if (fd != -1) {
41                                 return fd;
42                         }
43                 }
44         }
45
46         /* fall through to default name */
47         return sys_open(fname, flags, mode);
48 }
49
50
51 /*
52   choose from a list of languages. The list can be comma or space
53   separated
54   Keep choosing until we get a hit 
55   Changed to habdle priority -- Simo
56 */
57 void web_set_lang(const char *lang_string)
58 {
59         char **lang_list, **count;
60         float *pri;
61         int lang_num, i;
62
63         /* build the lang list */
64         lang_list = str_list_make(lang_string, ", \t\r\n");
65         if (!lang_list) return;
66         
67         /* sort the list by priority */
68         lang_num = 0;
69         count = lang_list;
70         while (*count && **count) {
71                 count++;
72                 lang_num++;
73         }
74         pri = (float *)malloc(sizeof(float) * lang_num);
75         for (i = 0; i < lang_num; i++) {
76                 char *pri_code;
77                 if ((pri_code=strstr(lang_list[i], ";q="))) {
78                         *pri_code = '\0';
79                         pri_code += 3;
80                         pri[i] = strtof(pri_code, NULL);
81                 } else {
82                         pri[i] = 1;
83                 }
84                 if (i != 0) {
85                         int l;
86                         for (l = i; l > 0; l--) {
87                                 if (pri[l] > pri[l-1]) {
88                                         char *tempc;
89                                         int tempf;
90                                         tempc = lang_list[l];
91                                         tempf = pri[l];
92                                         lang_list[l] = lang_list[l-1];
93                                         pri[i] = pri[l-1];
94                                         lang_list[l-1] = tempc;
95                                         pri[l-1] = tempf;
96                                 }
97                         }
98                  }
99         }
100
101         for (i = 0; i < lang_num; i++) {
102                 if (lang_tdb_init(lang_list[i])) return;
103         }
104         
105         /* it's not an error to not initialise - we just fall back to 
106            the default */
107 }