Skip to content

Instantly share code, notes, and snippets.

@seankelly
Created March 24, 2011 03:06
Show Gist options
  • Save seankelly/884477 to your computer and use it in GitHub Desktop.
Save seankelly/884477 to your computer and use it in GitHub Desktop.
ttystrip
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define BUFSIZE 8192
void usage(char *prgm) {
printf("Usage: %s [ttyrec ...]\n", prgm);
}
int min(int a, int b) {
return (a < b) ? a : b;
}
void strip(int fd) {
const int bufsize = BUFSIZE;
char buf[BUFSIZE];
int *size;
int frame_size;
int offset;
int n;
int bytes_read;
int bytes_offset;
frame_size = offset = n = 0;
bytes_offset = 0;
bytes_read = bufsize;
while ((n = read(fd, &buf[bytes_offset], bytes_read)) > 0) {
/* If there is data from the previous frame in the buffer,
* output that.
*/
if (offset > 0) {
write(1, buf, offset);
}
while ((offset + 12) < (n + bytes_offset)) {
/* Skip first eight bytes for each frame. */
size = (int *) &buf[offset+8];
frame_size = *size;
write(1, &buf[offset+12], min(frame_size, n + bytes_offset - offset - 12));
offset += frame_size + 12;
}
/* Check if the header was only partially in the buffer. */
if (offset >= bufsize) {
bytes_offset = 0;
bytes_read = bufsize;
offset -= bufsize;
}
else {
memmove(&buf[0], &buf[offset], bufsize - offset);
bytes_offset = bufsize - offset;
bytes_read = offset;
offset = 0;
}
}
}
int main(int argc, char **argv) {
int fd;
if (argc == 1) {
strip(0);
}
else if (strcmp(argv[1], "-h") == 0 ||
strcmp(argv[1], "--help") == 0) {
usage(argv[0]);
exit(0);
}
else {
int i;
for (i = 1; i < argc; i++) {
if ((fd = open(argv[i], O_RDONLY)) == -1) {
fprintf(stderr, "Unable to open %s\n", argv[1]);
exit(1);
}
strip(fd);
close(fd);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment