int main() { char buf[K]; int len = 0; int s = 0; while (1) { int n = read(0, buf + len, K - len); if (n < 0) return -1; len += n; if (len == 0) break; int x; for (x = 0; x < len && buf[x] != '\n'; ++x); if (x == len) { if (x == K) { len = 0; s = 1; } else if (n == 0) { break; } } else { if (s) { s = 0; memmove(buf, buf + (x + 1), len - (x - 1)); len -= (x + 1); } else { reverseBuffer(buf, x); int n2 = 0; while (n2 < x + 1) { int n2Delta = write(1, buf + n2, x + 1 - n2); if (n2Delta == -1) return -1; n2 += n2Delta; } memmove(buf, buf + (x + 1), len - (x - 1)); len -= (x + 1); } } } return 0; }
// Return the big-endian 64-bit integer at the 8 bytes starting at 'ptr' double bigEndianDouble(char * ptr) { double retVal; reverseBuffer(ptr, (char *) &retVal, 8); return retVal; }
// Return the big-endian 64-bit integer at the 8 bytes starting at 'ptr' long bigEndianLong(char * ptr) { long retVal; reverseBuffer(ptr, (char *) &retVal, 8); return retVal; }
// Return the big-endian 32-bit float at the 4 bytes starting at 'ptr' float bigEndianFloat(char * ptr) { float retVal; reverseBuffer(ptr, (char *) &retVal, 4); return retVal; }