keep getting Segmentation fault when executing

This is a discussion on keep getting Segmentation fault when executing within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include <fcntl.h> #include <sys/time.h> #include <math.h> // Sockets includes. ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    26

    keep getting Segmentation fault when executing

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <sys/time.h>
    #include <math.h>
    
    
    // Sockets includes.
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    
    // Constants
    #define HOSTLEN 256
    #define BUFLEN 40960
    #define CMDLEN 256
    
    // Cache data structures
    char* cache;
    int cache_size = 0;
    int num_compacts = 0;   // Spread number of compactions over total sites visited.
    
    
    
    
    void surf(int argc, char **argv);
    void browse(char* host, char* page, int id);
    
    int main()
    {
    	int size=10;
    	char* surf_arg[10];
    	surf_arg[0]="./websurf";
    	surf_arg[1]="-f";
    	surf_arg[2]="urls.txt";
    	surf_arg[3]="-s";
    	surf_arg[4]="1";
    	surf_arg[5]="-n";
    	surf_arg[6]="1";
    	surf_arg[7]="-c";
    	surf_arg[8]="1";
    	surf_arg[9]=NULL;
    	surf(size,surf_arg);
    	return 0;
    }
    
    void surf(int argc, char **argv) 
    {
    	// Program arguments
    	int s, num_websites = 0, page_index;
    	char filename[HOSTLEN];
    	FILE* fp;
    	struct timeval tval;
    	struct timezone tzone;
    	// Web host and pages
    	char host[HOSTLEN];
    	char **pages;
    	int num_pages = 0;
    	// Get program arguments.
    	for (s=1; s<argc; s++) {
    		// Filename
    		if ((strcmp (argv[s],"-f")) == 0) {
    			s++;
    			strcpy (filename, argv[s]);
    		}
    		// Number of compactions
    		else if ((strcmp (argv[s],"-c")) == 0) {
    			s++;
    			num_compacts = atoi (argv[s]);
    		}
    		// Number of websites to randomly visit
    		else if ((strcmp (argv[s],"-n")) == 0) {
    			s++;
    			num_websites = atoi (argv[s]);
    		}
    		// Cache size
    		else if ((strcmp (argv[s],"-s")) == 0) {
    			s++;
    			cache_size = atoi (argv[s]);
    		}
    		// Print usage
    		else {
    			printf ("Usage: ./websurf -f <filename> -s <queue size> -n <# websites to visit> -c <# of compactions>\n");
    			exit (0);
    		}
    	}
    	// Print program arguments
    	printf ("Filename: %s\n", filename);
    	printf ("Number of websites to access = %i\n", num_websites);
    	printf ("Cache size = %i\n", cache_size);
    	printf ("Number of times to compact memory = %i\n", num_compacts);
    	// Open file and store host and page names into pages array.
    	fp = fopen (filename, "r+");
    	if (fp == NULL) {
    		printf ("Error - could not open URLs file\n");
    		exit (-1);
    	}
    	fscanf (fp, "%i %s\n", &num_pages, host);
    	pages = (char**) calloc (num_pages, sizeof(char*));
    	for (s=0; s<num_pages; s++) {
    		pages[s] = (char*) calloc (HOSTLEN, sizeof(char));
    		fscanf (fp, "%s\n", pages[s]);
    		//printf ("URL: %s%s\n", host, pages[s]);
    	}
    	// Seed random number generator
    	gettimeofday (&tval, &tzone);
    	srand48 (tval.tv_usec);
    	// Start browsing randomly in list of available pages.
    	for (s=0; s<num_websites; s++) {
    		page_index = 0;  // Get random page index.
    		printf ("Page index %i URL: %s%s\n", page_index, host, pages[page_index]);
    		browse (host, pages[page_index], page_index);
    	} 
    } 
    /**********************************************************************************/ 
    // Main function where host and page names are initialized, and threads are created. 
    void browse (char* host, char* page, int id) {
    	//Socket data structures - Need only 1 socket to browse many pages.
    	int sckt;
    	struct hostent *he;
    	struct sockaddr_in srvaddr;
    	char cmd[CMDLEN];  // Command line to send to web server
    	// Webpage data structures
    	int nread;
    	char buf[BUFLEN];
    	char *body;
    	int temp_fd;
    	int child;
    	int result;
    	char tempfilename[16];
    	int webpage_size = 0; // Initialize page size to 0
    	
    	// Child process
    	int pid;
    	
    	// create TCP connection to web server
    	if ((sckt = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    		perror("socket"), exit(1);
    	if ((he = gethostbyname(host)) == NULL)
    		perror("gethostbyname"), exit(1);
    	//bzero(&srvaddr, sizeof(srvaddr));
    	srvaddr.sin_family = AF_INET;
    	srvaddr.sin_addr.s_addr = *(int *)(he->h_addr_list[0]);
    	srvaddr.sin_port = htons (80);
    	if (connect(sckt, (struct sockaddr *)&srvaddr, sizeof(srvaddr)) == -1)
           		perror("connect"), exit(1);
    	// create GET request
    	sprintf(cmd, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", page, host);
    	printf ("cmd: %s\n", cmd);
    	// send GET request
    	if (write(sckt, cmd, strlen(cmd)) != strlen(cmd))
    		perror("GET write"), exit(1);
    	// read response
    	if ((nread = read(sckt, buf, BUFLEN)) < 0)
    		perror("read"), exit(1);
    	if (nread >= BUFLEN)
    		fprintf(stderr, "web: need bigger (HTTP) read buffer\n"), exit(1);
    	buf[nread] = 0;
    	printf("web: nread = %d\n", nread);
    	if (nread < 512) printf("web: response: %s\n", buf);
    	else printf("web: initial chars of response: %.300s\n", buf);
    	// check return code
    	//if (strncmp(buf, "HTTP/1.1 200 OK", 15))
    	// fprintf(stderr, "web: GET failed\n"), exit(1);
    	// bail out if chunked encoding
    	if (strstr(buf, "Transfer-Encoding: chunked")) {
    		fprintf(stderr, "web: can't handle chunked encoding\n");
    		//pthread_exit(NULL);
    		exit (0);
    	}
    	// trim HTTP protocol header from response
    	if ((body = strstr(buf, "\r\n\r\n")) == NULL)
    		fprintf(stderr, "web: can't find end of HTTP header in response\n"), exit(1);
    	body = body + 4;
    	// create & open temp file
    	sprintf(tempfilename, "temp%i.html", id);
    	temp_fd = open (tempfilename, O_CREAT | O_RDWR | O_TRUNC, S_IRWXU);
    	// if server sent only header: read further response & dump in a loop
    	webpage_size = 0; // Initialize page size to 0
    	if (body-buf >= nread) {
    		do {
    			if ((nread = read(sckt, buf, BUFLEN)) < 0)
    				perror("read"), exit(1);
    			else if (nread == 0) break;
    			if (nread >= BUFLEN)
    				fprintf(stderr, "web: need bigger (HTTP) read buffer\n"), exit(1);
    			if (write(temp_fd, buf, nread) != nread)
    				perror("file write"), exit(1);
    			webpage_size += nread; // Accumulate page size.
    		} while (nread > 0);
    		close(temp_fd);
    	}
    	// if server sent header & some body: assume it's the whole page & dump it into file
    	else {
    		webpage_size = strlen(body);
    		if (write(temp_fd, body, strlen(body)) != strlen(body))
    			perror("file write"), exit(1);
    		close(temp_fd);
    	}
    	
    	// Print out size of webpage content
    	printf ("Webpage size %i \n", webpage_size);
    	// Fork off process to display webpage.
    	/* pid = fork ();
    	printf ("Process id %i\n", pid);
    	if (pid == 0) {
    		// Run Lynx
    		if (execl("/opt/gnu/bin/lynx", "/opt/gnu/bin/lynx", tempfilename, NULL) < 0)
    			perror("execl"), exit(1);
    		// Better to run netscape if possible
    		//if (execl("/usr/local/bin/netscape", "/usr/local/bin/netscape", tempfilename, NULL) < 0)
    		// perror("execl"), exit(1);
    	}
    	else {
    		printf ("After forking child process, parent process will now start writing to the file descriptor (pipe or file).\n");
    	} */ 
    }
    I get a segmentation fault, core dumped when trying to execute the a.out file. I am working under NetBSD.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Change this:
    Code:
    	surf_arg[9]=NULL;
    to this

    Code:
    	surf_arg[9]="";
    I think this will get you a little farther along.

    Regards,

    Dave

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    26
    that did the trick thanks dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault when executing bounded buffer program
    By Megalodon01 in forum C Programming
    Replies: 7
    Last Post: 03-31-2008, 12:19 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21