1
/****************************************************************************
2
**
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
6
**
7
** This file is part of the QtCore module of the Qt Toolkit.
8
**
9
** $QT_BEGIN_LICENSE:LGPL$
10
** GNU Lesser General Public License Usage
11
** This file may be used under the terms of the GNU Lesser General Public
12
** License version 2.1 as published by the Free Software Foundation and
13
** appearing in the file LICENSE.LGPL included in the packaging of this
14
** file. Please review the following information to ensure the GNU Lesser
15
** General Public License version 2.1 requirements will be met:
16
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17
**
18
** In addition, as a special exception, Nokia gives you certain additional
19
** rights. These rights are described in the Nokia Qt LGPL Exception
20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21
**
22
** GNU General Public License Usage
23
** Alternatively, this file may be used under the terms of the GNU General
24
** Public License version 3.0 as published by the Free Software Foundation
25
** and appearing in the file LICENSE.GPL included in the packaging of this
26
** file. Please review the following information to ensure the GNU General
27
** Public License version 3.0 requirements will be met:
28
** http://www.gnu.org/copyleft/gpl.html.
29
**
30
** Other Usage
31
** Alternatively, this file may be used in accordance with the terms and
32
** conditions contained in a signed written agreement between you and Nokia.
33
**
34
**
35
**
36
**
37
**
38
** $QT_END_LICENSE$
39
**
40
****************************************************************************/
41
42
#include "qglobal.h"
43
44
#ifdef Q_OS_VXWORKS
45
46
#include "qplatformdefs.h"
47
#include "qfunctions_vxworks.h"
48
49
#include <vmLib.h>
50
#include <selectLib.h>
51
#include <ioLib.h>
52
53
QT_USE_NAMESPACE
54
55
#ifdef __cplusplus
56
extern "C" {
57
#endif
58
59
// no lfind() - used by the TIF image format
60
void *lfind(const void* key, const void* base, size_t* elements, size_t size,
61
            int (*compare)(const void*, const void*))
62
{
63
    const char* current = (char*) base;
64
    const char* const end = (char*) (current + (*elements) * size);
65
    while (current != end) {
66
        if (compare(current, key) == 0)
67
            return (void*)current;
68
        current += size;
69
    }
70
    return 0;
71
}
72
73
74
// no rand_r(), but rand()
75
// NOTE: this implementation is wrong for multi threaded applications,
76
// but there is no way to get it right on VxWorks (in kernel mode)
77
int rand_r(unsigned int * /*seedp*/)
78
{
79
    return rand();
80
}
81
82
// no usleep() support
83
int usleep(unsigned int usec)
84
{
85
    div_t dt = div(usec, 1000000);
86
    struct timespec ts = { dt.quot, dt.rem * 1000 };
87
88
    return nanosleep(&ts, 0);
89
}
90
91
92
// gettimeofday() is declared, but is missing from the library
93
// It IS however defined in the Curtis-Wright X11 libraries, so
94
// we have to make the symbol 'weak'
95
#if defined(Q_CC_DIAB)
96
#  pragma weak gettimeofday
97
#endif
98
int gettimeofday(struct timeval *tv, void /*struct timezone*/ *)
99
{
100
    // the compiler will optimize this and will only use one code path
101
    if (sizeof(struct timeval) == sizeof(struct timespec)) {
102
        int res = clock_gettime(CLOCK_REALTIME, (struct timespec *) tv);
103
        if (!res)
104
            tv->tv_usec /= 1000;
105
        return res;
106
    } else {
107
        struct timespec ts;
108
109
        int res = clock_gettime(CLOCK_REALTIME, &ts);
110
        if (!res) {
111
            tv->tv_sec = ts.tv_sec;
112
            tv->tv_usec = ts.tv_nsec / 1000;
113
        }
114
        return res;
115
    }
116
}
117
118
// neither getpagesize() or sysconf(_SC_PAGESIZE) are available
119
int getpagesize()
120
{
121
    return vmPageSizeGet();
122
}
123
124
// symlinks are not supported (lstat is now just a call to stat - see qplatformdefs.h)
125
int symlink(const char *, const char *)
126
{
127
    errno = EIO;
128
    return -1;
129
}
130
131
ssize_t readlink(const char *, char *, size_t)
132
{
133
    errno = EIO;
134
    return -1;
135
}
136
137
// there's no truncate(), but ftruncate() support...
138
int truncate(const char *path, off_t length)
139
{
140
    int fd = open(path, O_WRONLY, 00777);
141
    if (fd >= 0) {
142
        int res = ftruncate(fd, length);
143
        int en = errno;
144
        close(fd);
145
        errno = en;
146
        return res;
147
    }
148
    // errno is already set by open
149
    return -1;
150
}
151
152
153
154
// VxWorks doesn't know about passwd & friends.
155
// in order to avoid patching the unix fs path everywhere
156
// we introduce some dummy functions that simulate a single
157
// 'root' user on the system.
158
159
uid_t getuid()
160
{
161
    return 0;
162
}
163
164
gid_t getgid()
165
{
166
    return 0;
167
}
168
169
uid_t geteuid()
170
{
171
    return 0;
172
}
173
174
struct passwd *getpwuid(uid_t uid)
175
{
176
    static struct passwd pwbuf = { "root", 0, 0, 0, 0, 0, 0 };
177
178
    if (uid == 0) {
179
        return &pwbuf;
180
    } else {
181
        errno = ENOENT;
182
        return 0;
183
    }
184
}
185
186
struct group *getgrgid(gid_t gid)
187
{
188
    static struct group grbuf = { "root", 0, 0, 0 };
189
190
    if (gid == 0) {
191
        return &grbuf;
192
    } else {
193
        errno = ENOENT;
194
        return 0;
195
    }
196
}
197
198
#ifdef __cplusplus
199
} // extern "C"
200
#endif
201
202
#endif // Q_OS_VXWORKS