Hello Everyone,
I am writing a program that does a whois and nslookup on domains listed in a text file. I am having a problem with popen and fseek. Here is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fptr1, *fptr2, *domreadout, *nsreadout;
char filename1[] = "virt.txt";
char filename2[] = "output.txt";
char buff[BUFSIZ];
char whoiscommand[150];
char nscommand[150];
char whoispopenout[BUFSIZ];
char nspopenout[BUFSIZ];
int count = 0;
int c;
printf("This program reads the file \"virt.txt\" and does a whois\n");
printf("and nslookup -q=mx on each domain listed in the file.\n");
printf("(we sleep for 5 seconds between whois and nslookup)\n\n");
printf("Let's get started...\n\n");
/*opening files to read and write*/
if ((fptr1 = fopen(filename1, "r")) == NULL)
{
printf("cannot open file %s", filename1);
return 0;
}
else if ((fptr2 = fopen(filename2, "w")) == NULL)
{
printf("cannot open file %s", filename2);
return 0;
}
else
{
/*main loop - fscanf reads each line in virt.txt into buff*/
while (fscanf(fptr1, "%s", buff) != EOF)
{
fprintf(fptr2, "\n%s\n\n", buff); // precede whois with domain
printf("working on domain: %s\n", buff); // print what's happening
/*read whois command*/
sprintf(whoiscommand, "whois.exe -h whois.networksolutions.com %s", buff);
/*read output from whois command*/
if ((domreadout = popen(whoiscommand, "rb")) == NULL)
{
printf("problem running whois with popen\n");
return 0;
}
fseek(domreadout, 0, SEEK_END); // move to end of domreadout
fseek(domreadout, -200, SEEK_CUR); // back up n bytes
while (ftell(domreadout) && count < 200) //if we've reached n bytes, stop looping
{
count++;
c = getc(domreadout);
fprintf(fptr2, "%c", c);
}
/*sleep for 5 seconds between whois and nslookup*/
sleep(5000);
/*now the nslookup*/
sprintf(nscommand, "nslookup -q=mx %s", buff);
if ((nsreadout = popen(nscommand, "r")) == NULL)
{
printf("problem running nslookup with popen\n");
return 0;
}
/*reading nslookup output and write it to output.txt*/
while (!feof(nsreadout))
{
if (fgets(nspopenout, BUFSIZ, nsreadout) != NULL)
{
fprintf(fptr2, nspopenout);
}
}
}
}
/*close popen*/
pclose(domreadout);
pclose(nsreadout);
/*close files*/
fclose(fptr1);
fclose(fptr2);
return 0;
}
The information in virt.txt is:
google.com
yahoo.com
I am having a problem moving around with fseek. It appears that when I try to fseek the information in domreadout, it's null. Do you know what I'm doing wrong? Can you use fseek with popen?
I am trying to use the last sixteen lines (approximately) of domreadout and thought that I could accomplish this with fseek. Is there a better way?
Sorry I stuffed all of this code into main, I plan to write separate functions once I get it right.
Thank you.