tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Calling setlocale() in utilities

I was wondering if there was any reason not to add a call to setlocale
in the utilities we ship in base (I'm thinking of what's in /sbin and
/bin if applicable)?

The intent is to get localised messages for the generic errors on the
system and that paves the way for localising the remaining messages if
desired as a follow on project.

This doesn't necessarily apply to tools which default to calling their
usage() if a criteria is not met.

eg with such a patch:
Index: sbin/apmlabel/apmlabel.c
RCS file: /cvsroot/src/sbin/apmlabel/apmlabel.c,v
retrieving revision 1.3
diff -u -p -r1.3 apmlabel.c
--- sbin/apmlabel/apmlabel.c    19 Oct 2013 01:09:58 -0000      1.3
+++ sbin/apmlabel/apmlabel.c    20 Sep 2016 02:33:24 -0000
@@ -39,6 +39,7 @@ __RCSID("$NetBSD: apmlabel.c,v 1.3 2013/
 #include <stdio.h>
 #include <err.h>
 #include <errno.h>
+#include <locale.h>
 #include <fcntl.h>
 #include <limits.h>
 #include <stdlib.h>
@@ -306,6 +307,9 @@ main(int argc, char **argv)
        raw = 0;
        verbose = 1;
        write_it = 0;
+       setlocale(LC_ALL, "");
        while ((ch = getopt(argc, argv, "fqrw")) != -1) {
                switch (ch) {
                case 'f':

$ export LC_ALL=de_DE.ISO8859-1
$ apmlabel doesntexist
doesntexist: No such file or directory
$ ./apmlabel doesntexist
doesntexist: Datei oder Verzeichnis nicht gefunden

touch /tmp/unreadable
chmod 600 /tmp/unreadable

$ apmlabel /tmp/unreadable
/tmp/unreadable: Permission denied
$ ./apmlabel /tmp/unreadable
/tmp/unreadable: Zugriff verboten


Home | Main Index | Thread Index | Old Index