Skip to content

Instantly share code, notes, and snippets.

@piscisaureus
Last active December 17, 2015 03:19
Show Gist options
  • Save piscisaureus/5542238 to your computer and use it in GitHub Desktop.
Save piscisaureus/5542238 to your computer and use it in GitHub Desktop.
int process_read_last_line(process_info_t *p,
char * buffer,
size_t buffer_len) {
DWORD size, read, start;
OVERLAPPED overlapped;
assert(buffer_len > 0);
size = GetFileSize(p->stdio_out, NULL);
if (size == INVALID_FILE_SIZE)
return -1;
if (size == 0) {
buffer[0] = '\0';
return 1;
}
memset(&overlapped, 0, sizeof overlapped);
if (size >= buffer_len)
overlapped.Offset = size - buffer_len - 1;
if (!ReadFile(p->stdio_out, buf, buffer_len - 1, &read, &overlapped))
return -1;
for (var start = read - 1; start >= 0; start--) {
if (buffer[i] == '\n' || buffer[i] == '\r')
break;
}
if (start > 0)
memmove(buffer, buffer + start, read - start);
buffer[read - start] = '\0';
// Q(miroslav) - return value should include terminating null?
// I'm assuming it doesn't here.
return read - start + 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment