~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/nsprpub/pr/src/misc/prsystem.c

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* 
 
3
 * The contents of this file are subject to the Mozilla Public
 
4
 * License Version 1.1 (the "License"); you may not use this file
 
5
 * except in compliance with the License. You may obtain a copy of
 
6
 * the License at http://www.mozilla.org/MPL/
 
7
 * 
 
8
 * Software distributed under the License is distributed on an "AS
 
9
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
10
 * implied. See the License for the specific language governing
 
11
 * rights and limitations under the License.
 
12
 * 
 
13
 * The Original Code is the Netscape Portable Runtime (NSPR).
 
14
 * 
 
15
 * The Initial Developer of the Original Code is Netscape
 
16
 * Communications Corporation.  Portions created by Netscape are 
 
17
 * Copyright (C) 1998-2000 Netscape Communications Corporation.  All
 
18
 * Rights Reserved.
 
19
 * 
 
20
 * Contributor(s):
 
21
 * 
 
22
 * Alternatively, the contents of this file may be used under the
 
23
 * terms of the GNU General Public License Version 2 or later (the
 
24
 * "GPL"), in which case the provisions of the GPL are applicable 
 
25
 * instead of those above.  If you wish to allow use of your 
 
26
 * version of this file only under the terms of the GPL and not to
 
27
 * allow others to use your version of this file under the MPL,
 
28
 * indicate your decision by deleting the provisions above and
 
29
 * replace them with the notice and other provisions required by
 
30
 * the GPL.  If you do not delete the provisions above, a recipient
 
31
 * may use your version of this file under either the MPL or the
 
32
 * GPL.
 
33
 */
 
34
 
 
35
#include "primpl.h"
 
36
#include "prsystem.h"
 
37
#include "prprf.h"
 
38
 
 
39
#if defined(BEOS)
 
40
#include <kernel/OS.h>
 
41
#endif
 
42
 
 
43
#if defined(OS2)
 
44
#define INCL_DOS
 
45
#include <os2.h>
 
46
/* define the required constant if it is not already defined in the headers */
 
47
#ifndef QSV_NUMPROCESSORS
 
48
#define QSV_NUMPROCESSORS 26
 
49
#endif
 
50
#endif
 
51
 
 
52
/* BSD-derived systems use sysctl() to get the number of processors */
 
53
#if defined(BSDI) || defined(FREEBSD) || defined(NETBSD) \
 
54
    || defined(OPENBSD) || defined(DARWIN)
 
55
#define _PR_HAVE_SYSCTL
 
56
#include <sys/param.h>
 
57
#include <sys/sysctl.h>
 
58
#endif
 
59
 
 
60
#if defined(HPUX)
 
61
#include <sys/mpctl.h>
 
62
#endif
 
63
 
 
64
#if defined(XP_UNIX)
 
65
#include <unistd.h>
 
66
#include <sys/utsname.h>
 
67
#endif
 
68
 
 
69
PR_IMPLEMENT(char) PR_GetDirectorySeparator(void)
 
70
{
 
71
    return PR_DIRECTORY_SEPARATOR;
 
72
}  /* PR_GetDirectorySeparator */
 
73
 
 
74
/*
 
75
** OBSOLETE -- the function name is misspelled.
 
76
*/
 
77
PR_IMPLEMENT(char) PR_GetDirectorySepartor(void)
 
78
{
 
79
#if defined(DEBUG)
 
80
    static PRBool warn = PR_TRUE;
 
81
    if (warn) {
 
82
        warn = _PR_Obsolete("PR_GetDirectorySepartor()",
 
83
                "PR_GetDirectorySeparator()");
 
84
    }
 
85
#endif
 
86
    return PR_GetDirectorySeparator();
 
87
}  /* PR_GetDirectorySepartor */
 
88
 
 
89
PR_IMPLEMENT(char) PR_GetPathSeparator(void)
 
90
{
 
91
    return PR_PATH_SEPARATOR;
 
92
}  /* PR_GetPathSeparator */
 
93
 
 
94
PR_IMPLEMENT(PRStatus) PR_GetSystemInfo(PRSysInfo cmd, char *buf, PRUint32 buflen)
 
95
{
 
96
    PRUintn len = 0;
 
97
 
 
98
    if (!_pr_initialized) _PR_ImplicitInitialization();
 
99
 
 
100
    switch(cmd)
 
101
    {
 
102
      case PR_SI_HOSTNAME:
 
103
        if (PR_FAILURE == _PR_MD_GETHOSTNAME(buf, (PRUintn)buflen))
 
104
            return PR_FAILURE;
 
105
        /*
 
106
         * On some platforms a system does not have a hostname and
 
107
         * its IP address is returned instead.   The following code
 
108
         * should be skipped on those platforms.
 
109
         */
 
110
#ifndef _PR_GET_HOST_ADDR_AS_NAME
 
111
        /* Return the unqualified hostname */
 
112
            while (buf[len] && (len < buflen)) {
 
113
                if (buf[len] == '.') {
 
114
                    buf[len] = '\0';
 
115
                    break;
 
116
                }
 
117
                len += 1;
 
118
            }    
 
119
#endif
 
120
         break;
 
121
 
 
122
      case PR_SI_SYSNAME:
 
123
        /* Return the operating system name */
 
124
#if defined(XP_UNIX) || defined(WIN32)
 
125
        if (PR_FAILURE == _PR_MD_GETSYSINFO(cmd, buf, (PRUintn)buflen))
 
126
            return PR_FAILURE;
 
127
#else
 
128
        (void)PR_snprintf(buf, buflen, _PR_SI_SYSNAME);
 
129
#endif
 
130
        break;
 
131
 
 
132
      case PR_SI_RELEASE:
 
133
        /* Return the version of the operating system */
 
134
#if defined(XP_UNIX) || defined(WIN32)
 
135
        if (PR_FAILURE == _PR_MD_GETSYSINFO(cmd, buf, (PRUintn)buflen))
 
136
            return PR_FAILURE;
 
137
#endif
 
138
#if defined(XP_OS2)
 
139
        {
 
140
            ULONG os2ver[2] = {0};
 
141
            DosQuerySysInfo(QSV_VERSION_MINOR, QSV_VERSION_REVISION,
 
142
                            &os2ver, sizeof(os2ver));
 
143
            /* Formatting for normal usage (2.11, 3.0, 4.0, 4.5); officially,
 
144
               Warp 4 is version 2.40.00, WSeB 2.45.00 */
 
145
            if (os2ver[0] < 30)
 
146
              (void)PR_snprintf(buf, buflen, "%s%lu",
 
147
                                "2.", os2ver[0]);
 
148
            else if (os2ver[0] < 45)
 
149
              (void)PR_snprintf(buf, buflen, "%lu%s%lu",
 
150
                                os2ver[0]/10, ".", os2ver[1]);
 
151
            else
 
152
              (void)PR_snprintf(buf, buflen, "%.1f",
 
153
                                os2ver[0]/10.0);
 
154
        }
 
155
#endif /* OS2 */
 
156
        break;
 
157
 
 
158
      case PR_SI_ARCHITECTURE:
 
159
        /* Return the architecture of the machine (ie. x86, mips, alpha, ...)*/
 
160
        (void)PR_snprintf(buf, buflen, _PR_SI_ARCHITECTURE);
 
161
        break;
 
162
          default:
 
163
                        PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
 
164
                        return PR_FAILURE;
 
165
    }
 
166
    return PR_SUCCESS;
 
167
}
 
168
 
 
169
/*
 
170
** PR_GetNumberOfProcessors()
 
171
** 
 
172
** Implementation notes:
 
173
**   Every platform does it a bit different.
 
174
**     numCpus is the returned value.
 
175
**   for each platform's "if defined" section
 
176
**     declare your local variable
 
177
**     do your thing, assign to numCpus
 
178
**   order of the if defined()s may be important,
 
179
**     especially for unix variants. Do platform
 
180
**     specific implementations before XP_UNIX.
 
181
** 
 
182
*/
 
183
PR_IMPLEMENT(PRInt32) PR_GetNumberOfProcessors( void )
 
184
{
 
185
    PRInt32     numCpus;
 
186
#if defined(WIN32)
 
187
    SYSTEM_INFO     info;
 
188
 
 
189
    GetSystemInfo( &info );
 
190
    numCpus = info.dwNumberOfProcessors;
 
191
#elif defined(XP_MAC)
 
192
/* Hard-code the number of processors to 1 on the Mac
 
193
** MacOS/9 will always be 1. The MPProcessors() call is for
 
194
** MacOS/X, when issued. Leave it commented out for now. */
 
195
/*  numCpus = MPProcessors(); */
 
196
    numCpus = 1;
 
197
#elif defined(BEOS)
 
198
    system_info sysInfo;
 
199
 
 
200
    get_system_info(&sysInfo);
 
201
    numCpus = sysInfo.cpu_count;
 
202
#elif defined(OS2)
 
203
    DosQuerySysInfo( QSV_NUMPROCESSORS, QSV_NUMPROCESSORS, &numCpus, sizeof(numCpus));
 
204
#elif defined(_PR_HAVE_SYSCTL)
 
205
    int mib[2];
 
206
    int rc;
 
207
    size_t len = sizeof(numCpus);
 
208
 
 
209
    mib[0] = CTL_HW;
 
210
    mib[1] = HW_NCPU;
 
211
    rc = sysctl( mib, 2, &numCpus, &len, NULL, 0 );
 
212
    if ( -1 == rc )  {
 
213
        numCpus = -1; /* set to -1 for return value on error */
 
214
        _PR_MD_MAP_DEFAULT_ERROR( _MD_ERRNO() );
 
215
    }
 
216
#elif defined(HPUX)
 
217
    numCpus = mpctl( MPC_GETNUMSPUS, 0, 0 );
 
218
    if ( numCpus < 1 )  {
 
219
        numCpus = -1; /* set to -1 for return value on error */
 
220
        _PR_MD_MAP_DEFAULT_ERROR( _MD_ERRNO() );
 
221
    }
 
222
#elif defined(IRIX)
 
223
    numCpus = sysconf( _SC_NPROC_ONLN );
 
224
#elif defined(XP_UNIX)
 
225
    numCpus = sysconf( _SC_NPROCESSORS_ONLN );
 
226
#else
 
227
#error "An implementation is required"
 
228
#endif
 
229
    return(numCpus);
 
230
} /* end PR_GetNumberOfProcessors() */