-
Notifications
You must be signed in to change notification settings - Fork 0
/
constrain.cc
75 lines (60 loc) · 1.48 KB
/
constrain.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include "apue.h"
#include <errno.h>
#include <limits.h>
using namespace std;
// function test macro;
#define _POSIX_C_SOURCE 200809l
// 1. limits.h -> static constant;
#ifdef PATH_MAX
static long pathmax = PATH_MAX;
#else
static long pathmax = 0;
#endif
static long posixVersion = 0;
static long xsiVersion = 0;
#define PATH_MAX_GUESS 1024
char* path_alloc(size_t *sizep) {
char *ptr;
size_t size;
if (posixVersion == 0) {
posixVersion = sysconf(_SC_VERSION);
}
if (xsiVersion == 0) {
xsiVersion = sysconf(_SC_XOPEN_VERSION);
}
if (pathmax == 0) {
errno = 0;
// 2. pathconf -> dynamic magic number;
if ((pathmax = pathconf("/", _PC_PATH_MAX)) < 0) {
if (errno == 0) {
pathmax = PATH_MAX_GUESS;
} else {
perror("error: ");
}
} else {
// including "/";
pathmax += 1;
}
}
// 3. compatibility (no null symbol);
if ((posixVersion < 200112L) && (xsiVersion < 4)) {
// not comply the SUS standard;
size = pathmax + 1;
} else {
size = pathmax;
}
if ((ptr = static_cast<char*>(malloc(size))) == nullptr) {
cerr << "system error!" << endl;
}
if (sizep != nullptr) {
*sizep = size;
}
return ptr;
}
int main(int argc, char** argv) {
size_t x = 0;
auto headPtr = path_alloc(&x);
cout << x << endl;
return 1;
}