~ubuntu-branches/ubuntu/lucid/gbrainy/lucid

« back to all changes in this revision

Viewing changes to src/Unix.cs

  • Committer: Bazaar Package Importer
  • Author(s): Robert Ancell
  • Date: 2010-01-12 11:21:24 UTC
  • mfrom: (13.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20100112112124-o4ztomxa0xfh2ulj
Tags: 1.30-1ubuntu1
* debian/control:
  - Revert build-depends to lucid versions

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2007-2009 Jordi Mas i Hernàndez <jmas@softcatala.org>
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or
5
 
 * modify it under the terms of the GNU General Public License as
6
 
 * published by the Free Software Foundation; either version 2 of the
7
 
 * License, or (at your option) any later version.
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 
 * General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public
15
 
 * License along with this program; if not, write to the
16
 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17
 
 * Boston, MA 02111-1307, USA.
18
 
 */
19
 
 
20
 
using System;
21
 
using System.Runtime.InteropServices;
22
 
using System.Text;
23
 
using System.Globalization;
24
 
using System.Threading;
25
 
 
26
 
//
27
 
// Unix system calls
28
 
//
29
 
static public class Unix
30
 
{
31
 
        [DllImport("libc")]
32
 
        static extern IntPtr localeconv ();
33
 
 
34
 
        [DllImport ("libc")] // Linux
35
 
        static extern int prctl (int option, byte [] arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5);
36
 
 
37
 
        [DllImport ("libc")] // BSD
38
 
        static extern void setproctitle (byte [] fmt, byte [] str_arg);
39
 
 
40
 
        
41
 
        /* Taken from locale.h  */
42
 
        [StructLayout (LayoutKind.Sequential)]
43
 
        struct lconv
44
 
        {
45
 
                public string decimal_point;
46
 
                public string thousands_sep;            
47
 
                public string grouping;
48
 
                public string int_curr_symbol;
49
 
                public string currency_symbol;
50
 
                public string mon_decimal_point;
51
 
                public string mon_thousands_sep;
52
 
                public string mon_grouping;
53
 
                public string positive_sign;
54
 
                public string negative_sign;
55
 
                char int_frac_digits;
56
 
                char frac_digits;
57
 
                char p_cs_precedes;
58
 
                char p_sep_by_space;
59
 
                char n_cs_precedes;
60
 
                char n_sep_by_space;
61
 
                char p_sign_posn;
62
 
                char n_sign_posn;
63
 
                char int_p_cs_precedes;
64
 
                char int_p_sep_by_space;
65
 
                char int_n_cs_precedes;
66
 
                char int_n_sep_by_space;
67
 
                char int_p_sign_posn;
68
 
                char int_n_sign_posn;
69
 
        }
70
 
 
71
 
        // Mono supports less locales that Unix systems
72
 
        // To overcome this limitation we setup the right locale parameters
73
 
        // when the Mono locale is InvariantCulture, that is, when the user's locale
74
 
        // has not been identified and the default Mono locale is used
75
 
        //
76
 
        // See: https://bugzilla.novell.com/show_bug.cgi?id=420468
77
 
        // 
78
 
        static public void FixLocaleInfo ()
79
 
        {
80
 
                IntPtr st = IntPtr.Zero;
81
 
                lconv lv;
82
 
                int platform = (int) Environment.OSVersion.Platform;
83
 
                
84
 
                if (platform != 4 && platform != 128) // Only in Unix based systems
85
 
                        return;
86
 
 
87
 
                if (CultureInfo.CurrentCulture != CultureInfo.InvariantCulture) // Culture well supported
88
 
                        return;
89
 
 
90
 
                try {
91
 
                        st = localeconv ();
92
 
                        if (st == IntPtr.Zero) return;
93
 
 
94
 
                        lv = (lconv) Marshal.PtrToStructure (st, typeof (lconv));
95
 
                        CultureInfo culture =  (CultureInfo) CultureInfo.CurrentCulture.Clone ();
96
 
                        culture.NumberFormat.NumberDecimalSeparator = lv.decimal_point;
97
 
                        Thread.CurrentThread.CurrentCulture = culture;
98
 
                }
99
 
                catch (Exception) {}
100
 
        }
101
 
 
102
 
        public static void SetProcessName (string name)
103
 
        {
104
 
                int platform = (int) Environment.OSVersion.Platform;            
105
 
                if (platform != 4 && platform != 128)
106
 
                        return;
107
 
 
108
 
                try {
109
 
                        if (prctl (15 /* PR_SET_NAME */, Encoding.ASCII.GetBytes (name + "\0"),
110
 
                                IntPtr.Zero, IntPtr.Zero, IntPtr.Zero) != 0) {
111
 
                                throw new ApplicationException ("Error setting process name: " + 
112
 
                                        Mono.Unix.Native.Stdlib.GetLastError ());
113
 
                        }
114
 
                } catch (EntryPointNotFoundException) {
115
 
                        setproctitle (Encoding.ASCII.GetBytes ("%s\0"), 
116
 
                                Encoding.ASCII.GetBytes (name + "\0"));
117
 
                }
118
 
        }
119
 
}