#include<stdio.h>


int main(int argc,char *argv[])
	{
	char buf[2048];
	int c,i;
	int pos,temp,count;
	FILE *fp;
	int pid;
	if(argc<2)
		{
		printf("Usage:  snoop <pid>\nThis will snoop on process <pid> copying it's output to stdout\n");
		exit(1);
		}
	pid=atoi(argv[1]);
	snprintf(buf,2047,"strace -e write=1 -p %d 2>&1",pid);
	printf("Opening: %s\n",buf);
	fp=popen(buf,"r");
	while(!feof(fp))
		{
		fgets(buf,2047,fp);
//		printf("Got a line: %s",buf);
		pos=0;
		if(sscanf(buf," | %d %n",&temp,&count)==0)	continue;
		pos+=count;
		for(i=0;i<16;i++)
			{
//			printf("Scanning in: %s",buf+pos);
			if(*(buf+pos)==' ')	pos++;
			if(*(buf+pos)==' ')	pos++;
			if(*(buf+pos)==' ')	break;
			if(sscanf(buf+pos,"%x%n",&c,&count)==0) break;
			pos+=count;
//			printf("%X\n",c);
			printf("%c",c);
			}
		}
	pclose(fp);
	return 0;
	}

