Paste: am i doing it right?
Author: | blei |
Mode: | text |
Date: | Sun, 1 Aug 2010 16:30:52 |
Plain Text |
import unix.*;
currentDirectory() {
var buflen = 1024;
var buf = allocateMemory(Int8, buflen);
var result = getcwd(buf, UInt64(buflen));
while (result == Type(result)(0) and errno() == ERANGE) {
freeMemory(buf);
buflen *= 2;
buf = allocateMemory(Int8, buflen);
result = getcwd(buf, UInt64(buflen));
}
if (result == Type(result)(0)) {
freeMemory(buf);
error(errorMessage());
}
var stringbuf = CStringRef(buf);
var result = String(CStringRef(buf));
freeMemory(buf);
return result;
}
[T | String?(T)]
changeDirectory(path: T) {
syscall(chdir, cstring(path));
}
procedure directoryFiles;
overload directoryFiles() = directoryFiles(".");
[T | String?(T)]
overload directoryFiles(path: T) {
var dirp = opendir(cstring(path));
if (dirp == Type(dirp)(0))
error(errorMessage());
var files = Vector[String]();
var dirent = Struct_dirent();
var resultPointer = Pointer[Struct_dirent](0);
var returnVal = readdir_r(dirp, &dirent, &resultPointer);
while (returnVal == 0 and resultPointer != Type(resultPointer)(0)) {
var file = CStringRef(Pointer[Int8](&dirent.d_name));
if (file != "." and file != "..")
push(files, String(file));
returnVal = readdir_r(dirp, &dirent, &resultPointer);
}
if (returnVal != 0) {
var readdirErrorMsg = errorMessage();
syscallNoThrow(closedir, dirp);
error(readdirErrorMsg);
}
else {
syscall(closedir, dirp);
return files;
}
}
Author: | kssreeram |
Mode: | text |
Date: | Sun, 1 Aug 2010 16:45:14 |
Plain Text |
import unix.*;
import libc;
// 'resize' is not yet available
currentDirectory() {
var buf = String();
resize(buf, 1024);
while (true) {
var result = getcwd(&buf[0], size(buf));
if (not null?(result))
break;
if (errno() != ERANGE)
error(errorMessage());
resize(buf, 2*size(buf));
}
resize(buf, libc.strlen(&buf[0]));
return move(buf);
}
Author: | blei |
Mode: | factor |
Date: | Sun, 1 Aug 2010 17:08:56 |
Plain Text |
currentDirectory() {
var buf = String();
reserve(buf, 1024);
while (true) {
var result = getcwd(Pointer[Int8](&buf[0]), size(buf));
if (not null?(result))
break;
if (errno() != ERANGE)
error(errorMessage());
reserve(buf, 2*size(buf));
}
// 'resize' is not yet available
// resize(buf, libc.strlen(&buf[0]));
buf.size = libc.strlen(Pointer[Int8](&buf[0]));
return move(buf);
}
error: Invalid argument
New Annotation