25 # include <crt_externs.h>
27 # define environ (*_NSGetEnviron())
44 }
else if (strcmp(
PN_STR_PTR(modestr),
"r+") == 0) {
46 }
else if (strcmp(
PN_STR_PTR(modestr),
"w") == 0) {
47 mode = O_WRONLY | O_TRUNC | O_CREAT;
48 }
else if (strcmp(
PN_STR_PTR(modestr),
"w+") == 0) {
49 mode = O_RDWR | O_TRUNC | O_CREAT;
50 }
else if (strcmp(
PN_STR_PTR(modestr),
"a") == 0) {
51 mode = O_WRONLY | O_CREAT | O_APPEND;
52 }
else if (strcmp(
PN_STR_PTR(modestr),
"a+") == 0) {
53 mode = O_RDWR | O_CREAT | O_APPEND;
58 if ((fd = open(
PN_STR_PTR(path), mode, 0755)) == -1)
75 file->
mode = fcntl(file->
fd, F_GETFL) | O_ACCMODE;
78 if (fstat(file->
fd, &st) == -1) perror(
"fstat");
79 file->
mode = st.st_mode;
89 while (retval = close(self->fd), retval == -1 && errno == EINTR) ;
101 int r = read(self->fd, buf, n);
124 long tmp =
PN_NUM(obj); len =
sizeof(tmp); ptr = (
char *)&tmp;
127 char tmp = (obj ==
PN_TRUE) ? 1 : 0; len = 1; ptr = (
char *)&tmp;
130 assert(0 &&
"Invalid primitive type");
137 double tmp =
PN_DBL(obj); len =
sizeof(tmp); ptr = (
char *)&tmp;
143 int r = write(self->fd, ptr, len);
162 int fd =
self->fd, rv;
165 if (self->path !=
PN_NIL && fd != -1) {
166 rv = asprintf(&buf,
"<file %s fd: %d>",
PN_STR_PTR(self->path), fd);
167 }
else if (fd != -1) {
168 rv = asprintf(&buf,
"<file fd: %d>", fd);
170 rv = asprintf(&buf,
"<closed file>");
183 if (fgets(line, 1024, stdin) != NULL)
198 while (*env != NULL) {
199 for (key = *env; *key !=
'='; key++);
PN potion_file_write(Potion *P, PN cl, pn_file self, PN obj)
PN potion_str(Potion *, const char *)
PN potion_file_string(Potion *P, PN cl, pn_file self)
PN potion_lobby_read(Potion *P, PN cl, PN self)
PN potion_file_read(Potion *P, PN cl, pn_file self, PN n)
PN potion_table_put(Potion *P, PN cl, PN self, PN key, PN value)
#define potion_method(RCV, MSG, FN, SIG)
PN potion_file_print(Potion *P, PN cl, pn_file self, PN obj)
PN potion_byte_str2(Potion *, const char *, size_t len)
a file is wrapper around a file descriptor, non-volatile but mutable.
void potion_file_init(Potion *P)
set Env global
PN potion_file_new(Potion *P, PN cl, PN self, PN path, PN modestr)
PN potion_table_empty(Potion *P)
PN potion_file_with_fd(Potion *P, PN cl, PN self, PN fd)
PN potion_io_error(Potion *P, const char *msg)
PN potion_type_error(Potion *P, PN obj)
struct PNFile *volatile pn_file
PN potion_file_close(Potion *P, PN cl, pn_file self)
the global interpreter state P. currently singleton (not threads yet)
the central table type, based on core/khash.h
#define potion_send(RCV, MSG, ARGS...)
method caches (more great stuff from ian piumarta)
void potion_allocation_error(void)
void potion_type_constructor_is(PN vt, PN cl)
set default constructor
#define potion_class_method(RCV, MSG, FN, SIG)
PN potion_object_new(Potion *P, PN cl, PN self)