mirror of
https://github.com/QB64-Phoenix-Edition/QB64pe.git
synced 2024-09-19 01:34:58 +00:00
ef083bb4af
Pushed changes introduced by LUKE (Johny B on the QB64 forums) as follows : Fixed setup_lnx.sh, added _CWD$, fixed handling of errno. Converted setup_lnx.sh to Linux line endings (was preventing the script from running). We now include <errno.h>, so the errno variable actually works - before it was just a regular variable that wasn't being set on error. Added a _CWD$ function that returns the current working directory. Should work on all 3 systems, but testing is needed on Win/Mac. Can cause the following runtime errors: 7 (Out of memory), 51 (Internal error).
53 lines
1.1 KiB
C++
53 lines
1.1 KiB
C++
//Get Current Working Directory
|
|
qbs *func__cwd(){
|
|
qbs *final, *tqbs;
|
|
int length;
|
|
char *buf, *ret;
|
|
|
|
#if defined QB64_WINDOWS
|
|
length = GetCurrentDirectoryA(0, NULL);
|
|
buf = (char *)malloc(length);
|
|
if (!buf) {
|
|
error(7); //"Out of memory"
|
|
return tqbs;
|
|
}
|
|
if (GetCurrentDirectoryA(length, buf) != --length) { //Sanity check
|
|
free(buf); //It's good practice
|
|
tqbs = qbs_new(0, 1);
|
|
error(51); //"Internal error"
|
|
return tqbs;
|
|
}
|
|
#elif defined QB64_LINUX
|
|
length = 512;
|
|
while(1) {
|
|
buf = (char *)malloc(length);
|
|
if (!buf) {
|
|
tqbs = qbs_new(0, 1);
|
|
error(7);
|
|
return tqbs;
|
|
}
|
|
ret = getcwd(buf, length);
|
|
if (ret) break;
|
|
if (errno != ERANGE) {
|
|
tqbs = qbs_new(0, 1);
|
|
error(51);
|
|
return tqbs;
|
|
}
|
|
free(buf);
|
|
length += 512;
|
|
}
|
|
length = strlen(ret);
|
|
ret = (char *)realloc(ret, length); //Chops off the null byte
|
|
if (!ret) {
|
|
tqbs = qbs_new(0, 1);
|
|
error(7);
|
|
return tqbs;
|
|
}
|
|
buf = ret;
|
|
#endif
|
|
final = qbs_new(length, 1);
|
|
memcpy(final->chr, buf, length);
|
|
free(buf);
|
|
return final;
|
|
}
|
|
|