Thread: Networking Nslookup Utility using C.

  1. #1
    Registered User thriller500's Avatar
    Join Date
    Oct 2011
    Posts
    25

    Networking Nslookup Utility using C.

    I had to make a wrapper program on "nslookup" networking utility and I am facing the following "Segmentation Fault error"

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main(int argc, char* argv[])
    {
    		const char *d;
    		char *s=NULL;
    		char *x=NULL;
    		d=argv[1];
    		s="nslookup";
    		x=strcat(s,d);	
    		system(s);
    		return 0;
    }
    I have no compilation errors.

    Strcat function call:
    Code:
    char *strcat(char *s1, const char *s2);
    
    I need some help because i am not able to figure out what i am doing wrong.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    in order, as I can see it:

    1. your variable names are non-descriptive, and therefore bad. name your variables what they really are. all but one is unnecessary anyway.
    2. you're assigning to a pointer to const data after its declaration. while syntactically correct, it's far from good practice. it makes no sense for 'd' to be const in this case.
    3. you're appending to a string that has only enough space reserved for its initial content. this is definitely the cause of your segmentation faults.
    4. you don't need to store the return value of strcat. it appends to the buffer pointed to by the first parameter, and the return value is just a copy of that pointer. you're not using that return value anyway, so why store it?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You want to do something like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char* argv[]) {
    
    	char cmd[100] = "nslookup ";
    
    	if (argc < 2) {
    		fprintf(stderr, ":(\n");
    		return 1;
    	}
    
            strcat(cmd, argv[1]);  
            system(cmd);
    
            return 0;
    }

  4. #4
    Registered User thriller500's Avatar
    Join Date
    Oct 2011
    Posts
    25
    Quote Originally Posted by oogabooga View Post
    You want to do something like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char* argv[]) {
    
        char cmd[100] = "nslookup ";
    
        if (argc < 2) {
            fprintf(stderr, ":(\n");
            return 1;
        }
    
            strcat(cmd, argv[1]);  
            system(cmd);
    
            return 0;
    }
    GOT THE ANSWER
    Code:
    int main(int argc, char* argv[]){
    		char *d1;
    		char *s;
    		char d[50]="nslookup ";
    		//d1=(char *)malloc(70);
    		d1=argv[1];
    		s=(char *)malloc(500);
    		strcat(s,d);
    		strcat(s,d1);
    		printf("%s",s);	
    		printf("\n");
    		system(s);
    		return 0;
    }

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you can't use strcat with your buffer unless you initialize it first. it will look for the first nul character, which may not be at index zero. it may not, in fact, be anywhere within the memory you allocated. you must, at minimum, set s[0] = 0 before calling strcat.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Make utility
    By bob887 in forum Windows Programming
    Replies: 3
    Last Post: 03-16-2011, 09:01 AM
  2. Help On Simple .dat Utility
    By Led4urhead123 in forum C++ Programming
    Replies: 1
    Last Post: 07-08-2008, 03:22 PM
  3. Netstat, nbtstat, nslookup, what else?
    By Stan100 in forum Networking/Device Communication
    Replies: 3
    Last Post: 07-01-2004, 10:19 AM
  4. Cool utility
    By datainjector in forum Windows Programming
    Replies: 5
    Last Post: 07-08-2003, 01:50 PM
  5. zip utility
    By SeanMSimonsen in forum Tech Board
    Replies: 2
    Last Post: 04-11-2003, 08:54 PM