]> git.proxmox.com Git - ceph.git/blob - ceph/src/pmdk/src/windows/include/unistd.h
import ceph 16.2.7
[ceph.git] / ceph / src / pmdk / src / windows / include / unistd.h
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright 2015-2020, Intel Corporation */
3
4 /*
5 * unistd.h -- compatibility layer for POSIX operating system API
6 */
7
8 #ifndef UNISTD_H
9 #define UNISTD_H 1
10
11 #include <stdio.h>
12
13 #define _SC_PAGESIZE 0
14 #define _SC_NPROCESSORS_ONLN 1
15
16 #define R_OK 04
17 #define W_OK 02
18 #define X_OK 00 /* execute permission doesn't exist on Windows */
19 #define F_OK 00
20
21 /*
22 * sysconf -- get configuration information at run time
23 */
24 static __inline long
25 sysconf(int p)
26 {
27 SYSTEM_INFO si;
28 int ret = 0;
29
30 switch (p) {
31 case _SC_PAGESIZE:
32 GetSystemInfo(&si);
33 return si.dwPageSize;
34
35 case _SC_NPROCESSORS_ONLN:
36 for (int i = 0; i < GetActiveProcessorGroupCount(); i++) {
37 ret += GetActiveProcessorCount(i);
38 }
39 return ret;
40
41 default:
42 return 0;
43 }
44
45 }
46
47 #define getpid _getpid
48
49 /*
50 * pread -- read from a file descriptor at given offset
51 */
52 static ssize_t
53 pread(int fd, void *buf, size_t count, os_off_t offset)
54 {
55 __int64 position = _lseeki64(fd, 0, SEEK_CUR);
56 _lseeki64(fd, offset, SEEK_SET);
57 int ret = _read(fd, buf, (unsigned)count);
58 _lseeki64(fd, position, SEEK_SET);
59 return ret;
60 }
61
62 /*
63 * pwrite -- write to a file descriptor at given offset
64 */
65 static ssize_t
66 pwrite(int fd, const void *buf, size_t count, os_off_t offset)
67 {
68 __int64 position = _lseeki64(fd, 0, SEEK_CUR);
69 _lseeki64(fd, offset, SEEK_SET);
70 int ret = _write(fd, buf, (unsigned)count);
71 _lseeki64(fd, position, SEEK_SET);
72 return ret;
73 }
74
75 #define S_ISBLK(x) 0 /* BLK devices not exist on Windows */
76
77 /*
78 * basename -- parse pathname and return filename component
79 */
80 static char *
81 basename(char *path)
82 {
83 char fname[_MAX_FNAME];
84 char ext[_MAX_EXT];
85 _splitpath(path, NULL, NULL, fname, ext);
86
87 sprintf(path, "%s%s", fname, ext);
88
89 return path;
90 }
91
92 /*
93 * dirname -- parse pathname and return directory component
94 */
95 static char *
96 dirname(char *path)
97 {
98 if (path == NULL)
99 return ".";
100
101 size_t len = strlen(path);
102 if (len == 0)
103 return ".";
104
105 char *end = path + len;
106
107 /* strip trailing forslashes and backslashes */
108 while ((--end) > path) {
109 if (*end != '\\' && *end != '/') {
110 *(end + 1) = '\0';
111 break;
112 }
113 }
114
115 /* strip basename */
116 while ((--end) > path) {
117 if (*end == '\\' || *end == '/') {
118 *end = '\0';
119 break;
120 }
121 }
122
123 if (end != path) {
124 return path;
125 /* handle edge cases */
126 } else if (*end == '\\' || *end == '/') {
127 *(end + 1) = '\0';
128 } else {
129 *end++ = '.';
130 *end = '\0';
131 }
132
133 return path;
134 }
135
136 #endif /* UNISTD_H */