Annotation of embedaddon/sudo/plugins/sudoers/locale.c, revision 1.1.1.3

1.1       misho       1: /*
                      2:  * Copyright (c) 2012-2013 Todd C. Miller <Todd.Miller@courtesan.com>
                      3:  *
                      4:  * Permission to use, copy, modify, and distribute this software for any
                      5:  * purpose with or without fee is hereby granted, provided that the above
                      6:  * copyright notice and this permission notice appear in all copies.
                      7:  *
                      8:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                      9:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     10:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     11:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     12:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     13:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     14:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     15:  */
                     16: 
                     17: #include <config.h>
                     18: 
                     19: #include <sys/types.h>
                     20: #include <stdio.h>
                     21: #ifdef STDC_HEADERS
                     22: # include <stdlib.h>
                     23: # include <stddef.h>
                     24: #else
                     25: # ifdef HAVE_STDLIB_H
                     26: #  include <stdlib.h>
                     27: # endif
                     28: #endif /* STDC_HEADERS */
                     29: #ifdef HAVE_STRING_H
                     30: # include <string.h>
                     31: #endif /* HAVE_STRING_H */
                     32: #ifdef HAVE_STRINGS_H
                     33: # include <strings.h>
                     34: #endif /* HAVE_STRINGS_H */
                     35: #ifdef HAVE_STDBOOL_H
                     36: # include <stdbool.h>
                     37: #else
                     38: # include "compat/stdbool.h"
                     39: #endif /* HAVE_STDBOOL_H */
                     40: 
1.1.1.3 ! misho      41: #define DEFAULT_TEXT_DOMAIN    "sudoers"
        !            42: #include "gettext.h"           /* must be included before missing.h */
        !            43: 
1.1       misho      44: #include "missing.h"
1.1.1.2   misho      45: #include "fatal.h"
1.1       misho      46: #include "alloc.h"
                     47: #include "logging.h"
                     48: 
                     49: static int current_locale = SUDOERS_LOCALE_USER;
                     50: static char *user_locale;
                     51: static char *sudoers_locale;
                     52: 
                     53: int
                     54: sudoers_getlocale(void)
                     55: {
                     56:     return current_locale;
                     57: }
                     58: 
                     59: void
                     60: sudoers_initlocale(const char *ulocale, const char *slocale)
                     61: {
                     62:     if (ulocale != NULL) {
                     63:        efree(user_locale);
                     64:        user_locale = estrdup(ulocale);
                     65:     }
                     66:     if (slocale != NULL) {
                     67:        efree(sudoers_locale);
                     68:        sudoers_locale = estrdup(slocale);
                     69:     }
                     70: }
                     71: 
                     72: /*
                     73:  * Set locale to user or sudoers value.
                     74:  * Returns true on success and false on failure,
                     75:  * If prevlocale is non-NULL it will be filled in with the
                     76:  * old SUDOERS_LOCALE_* value.
                     77:  */
                     78: bool
                     79: sudoers_setlocale(int newlocale, int *prevlocale)
                     80: {
                     81:     char *res = NULL;
                     82: 
                     83:     switch (newlocale) {
                     84:        case SUDOERS_LOCALE_USER:
                     85:            if (prevlocale)
                     86:                *prevlocale = current_locale;
                     87:            if (current_locale != SUDOERS_LOCALE_USER) {
                     88:                current_locale = SUDOERS_LOCALE_USER;
                     89:                res = setlocale(LC_ALL, user_locale ? user_locale : "");
                     90:                if (res != NULL && user_locale == NULL)
                     91:                    user_locale = estrdup(setlocale(LC_ALL, NULL));
                     92:            }
                     93:            break;
                     94:        case SUDOERS_LOCALE_SUDOERS:
                     95:            if (prevlocale)
                     96:                *prevlocale = current_locale;
                     97:            if (current_locale != SUDOERS_LOCALE_SUDOERS) {
                     98:                current_locale = SUDOERS_LOCALE_SUDOERS;
                     99:                res = setlocale(LC_ALL, sudoers_locale ? sudoers_locale : "C");
                    100:                if (res == NULL && sudoers_locale != NULL) {
                    101:                    if (strcmp(sudoers_locale, "C") != 0) {
                    102:                        efree(sudoers_locale);
                    103:                        sudoers_locale = estrdup("C");
                    104:                        res = setlocale(LC_ALL, "C");
                    105:                    }
                    106:                }
                    107:            }
                    108:            break;
                    109:     }
                    110:     return res ? true : false;
                    111: }
                    112: 
1.1.1.3 ! misho     113: #ifdef HAVE_LIBINTL_H
        !           114: char *
        !           115: warning_gettext(const char *msgid)
1.1       misho     116: {
1.1.1.3 ! misho     117:     int warning_locale;
        !           118:     char *msg;
1.1       misho     119: 
1.1.1.3 ! misho     120:     sudoers_setlocale(SUDOERS_LOCALE_USER, &warning_locale);
        !           121:     msg = gettext(msgid);
1.1       misho     122:     sudoers_setlocale(warning_locale, NULL);
1.1.1.3 ! misho     123: 
        !           124:     return msg;
1.1       misho     125: }
1.1.1.3 ! misho     126: #endif /* HAVE_LIBINTL_H */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>