[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] Segfejl under kørsel, men ikke når jeg debugger!



On Mon, 21 Jun 2004 17:42:40 +0200, Michael Rasmussen wrote:

> Hej alle,
> 
> Jeg sidder med et mystiks problem: Normal kørsel giver segmentation
> fault, men når jeg debugger, fremkommer der ingen fejl!
> 
Her er så den ultimative løsning med brug af buffered input/output, samt
anvendelse af select til at bestemme om henholdsvis læse-/skrivefil er
klar til at levere/modtage data.

void do_cat(char *f, int fd) {
	char	*extension = file_type(f);
	char	*type = (char *) malloc(sizeof(int) * 25);
	int		bytes = 0;
	int		in, out, maxfd;
	int		io[2];
	fd_set	watchsetread, watchsetwrite;
	fd_set	insetread, insetwrite;
	char 	buf[4096];
	int		writeflag = 0;
	
	if (strcmp(extension, "html") == 0)
		strcpy(type, "text/html");
	else if (strcmp(extension, "gif") == 0)
		strcpy(type, "image/gif");
	else if (strcmp(extension, "jpg") == 0)
		strcpy(type, "image/jpeg");
	else if (strcmp(extension, "jpeg") == 0)
		strcpy(type, "image/jpeg");
	else if (strcmp(extension, "png") == 0)
		strcpy(type, "image/png");
	else
		strcpy(type, "text/plain");
	
	if ((io[0] = open(f, O_RDONLY)) < 0) { // read
		perror("open f");
		exit(1);
	}
	io[1] = fd; // write
	
	strcpy(buf, standardHeader(200, "OK", type));
	server_bytes_sent += write(io[1], buf, strlen(buf));
	FD_ZERO(&watchsetread);
	FD_ZERO(&watchsetwrite);
	FD_SET(io[0], &watchsetread);
	FD_SET(io[1], &watchsetwrite);
	maxfd = io[0] > io[1]? io[0]: io[1];
	while (FD_ISSET(io[0], &watchsetread) &&
			FD_ISSET(io[1], &watchsetwrite)) {
		insetread = watchsetread;
		insetwrite = watchsetwrite;
		if (select(maxfd+1, &insetread, &insetwrite, NULL, NULL) < 0) {
			perror("select");
			exit(1);
		}
		if (FD_ISSET(io[0], &insetread) && !writeflag) {
			in = read(io[0], buf, sizeof(buf));
			if (in < 0) {
				perror("read");
				exit(1);
			}
			else if (!in) {
				close(io[0]);
				FD_CLR(io[0], &watchsetread);
			}
			else
				writeflag = 1;
		}
		if (FD_ISSET(io[1], &insetwrite) && writeflag) {
			out = write(io[1], buf, in);
			if (out < 0) {
				perror("write");
				exit(1);
			}
			else if (!out) {
				close(io[1]);
				FD_CLR(io[1], &watchsetwrite);
			}
			else {
				writeflag = 0;
				bytes += out;
			}
		}
	}
	free(type);
	server_bytes_sent += bytes;
}

-- 
Hilsen/Regards
Michael Rasmussen

Get my public GnuPG keys:
mir <at> datanom <dot> net
http://search.keyserver.net:11371/pks/lookup?op=get&search=0xE501F51C
mir <at> miras <dot> org
http://search.keyserver.net:11371/pks/lookup?op=get&search=0xE3E80917
--------------------------------------------------------------
Don't let your mind wander -- it's too little to be let out alone.




 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 22:43 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *