|
@@ -11,6 +11,7 @@
|
|
|
#include <unistd.h>
|
|
#include <unistd.h>
|
|
|
#include <stdlib.h>
|
|
#include <stdlib.h>
|
|
|
#include <stdio.h>
|
|
#include <stdio.h>
|
|
|
|
|
+#include <string.h>
|
|
|
|
|
|
|
|
/* Write the contents of a string descriptor to fd 1. Returns the
|
|
/* Write the contents of a string descriptor to fd 1. Returns the
|
|
|
number of bytes written. */
|
|
number of bytes written. */
|
|
@@ -54,6 +55,89 @@ long m2readint(void)
|
|
|
return neg ? -v : v;
|
|
return neg ? -v : v;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/* ---------------- sequential files (stdio) ---------------- */
|
|
|
|
|
+
|
|
|
|
|
+/* Copy a descriptor string into a NUL-terminated buffer. */
|
|
|
|
|
+static void m2cstr(long *desc, char *buf, size_t cap)
|
|
|
|
|
+{
|
|
|
|
|
+ long n = desc[0];
|
|
|
|
|
+ if ((size_t)n >= cap) n = (long)(cap - 1);
|
|
|
|
|
+ if (n > 0) memcpy(buf, (char *)(desc + 1), (size_t)n);
|
|
|
|
|
+ buf[n] = 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+long m2fopenread(long *desc)
|
|
|
|
|
+{
|
|
|
|
|
+ char buf[4096];
|
|
|
|
|
+ m2cstr(desc, buf, sizeof buf);
|
|
|
|
|
+ return (long)fopen(buf, "r");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+long m2fopenwrite(long *desc)
|
|
|
|
|
+{
|
|
|
|
|
+ char buf[4096];
|
|
|
|
|
+ m2cstr(desc, buf, sizeof buf);
|
|
|
|
|
+ return (long)fopen(buf, "w");
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+long m2fclose(long h)
|
|
|
|
|
+{
|
|
|
|
|
+ if (h != 0) fclose((FILE *)h);
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+long m2fgetc(long h)
|
|
|
|
|
+{
|
|
|
|
|
+ int c = fgetc((FILE *)h);
|
|
|
|
|
+ return (c == EOF) ? 0 : c;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+long m2fputc(long h, long c)
|
|
|
|
|
+{
|
|
|
|
|
+ fputc((int)c, (FILE *)h);
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+long m2fputs(long h, long *desc)
|
|
|
|
|
+{
|
|
|
|
|
+ long n = desc[0];
|
|
|
|
|
+ if (n > 0) fwrite((char *)(desc + 1), 1, (size_t)n, (FILE *)h);
|
|
|
|
|
+ return n;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+long m2freadint(long h)
|
|
|
|
|
+{
|
|
|
|
|
+ long v = 0;
|
|
|
|
|
+ int c, neg = 0;
|
|
|
|
|
+ do { c = fgetc((FILE *)h); } while (c == ' ' || c == '\t' ||
|
|
|
|
|
+ c == '\n' || c == '\r');
|
|
|
|
|
+ if (c == '-') { neg = 1; c = fgetc((FILE *)h); }
|
|
|
|
|
+ while (c >= '0' && c <= '9') {
|
|
|
|
|
+ v = v * 10 + (c - '0');
|
|
|
|
|
+ c = fgetc((FILE *)h);
|
|
|
|
|
+ }
|
|
|
|
|
+ return neg ? -v : v;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+long m2fwriteint(long h, long v)
|
|
|
|
|
+{
|
|
|
|
|
+ char buf[32];
|
|
|
|
|
+ int i = 0, neg = 0;
|
|
|
|
|
+ unsigned long u;
|
|
|
|
|
+ if (v < 0) { neg = 1; u = (unsigned long)(-v); }
|
|
|
|
|
+ else u = (unsigned long)v;
|
|
|
|
|
+ do { buf[i++] = (char)('0' + u % 10); u /= 10; } while (u != 0);
|
|
|
|
|
+ if (neg) buf[i++] = '-';
|
|
|
|
|
+ while (i > 0) fputc(buf[--i], (FILE *)h);
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+long m2fwriteln(long h)
|
|
|
|
|
+{
|
|
|
|
|
+ fputc('\n', (FILE *)h);
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/* Set a string descriptor's logical length (header). */
|
|
/* Set a string descriptor's logical length (header). */
|
|
|
long m2setlen(long *desc, long n)
|
|
long m2setlen(long *desc, long n)
|
|
|
{
|
|
{
|