⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sun2mime.c

📁 linux下的E_MAIL客户端源码
💻 C
📖 第 1 页 / 共 2 页
字号:
	if (p != NULL) *p = '\0';	return(0);}/* * subroutine: Figure out what to put in the Content-Type header *	This is based partly on the input type parameter. If the type is *	just "binary", try to figure out if it's really a JPEG or MPEG. *	If just "default", see if it's maybe XWD or XBM. If it's "default" *	and not encoded, it's probably just ASCII text. */voidsetContentType( int type, char *fn, int isUU  ){	int  isText = 0;	char *p;	char cType[256] = "";	switch( type ) {	case T_TEXT:		strcpy( cType, "text/plain");		break;	case T_PS:		strcpy( cType, "application/postscript");		break;	case T_XBM:		strcpy( cType, "image/x-xbm");		break;	case T_XPM:		strcpy( cType, "image/x-xpm");		break;	case T_AUDIO:		strcpy( cType, "audio/x-adpcm-compress" );		break;	case T_GIF:		strcpy( cType, "image/gif" );		break;	case T_FRAME:		strcpy( cType, "application/x-frame" );		break;	case T_UNK:	default:		if(isUU) {		   /*		    * Look at the name of the encoded file. If it has a		    * recognizable suffix, trust it to be accurate...		    */		   p = strrchr( fn, '.' );		   if( p != NULL ) {			p++;			if((strncasecmp(p, "jpeg", sizeof("jpeg")-1) == 0) || 			   (strncasecmp(p, "jpg", sizeof("jpg")-1) == 0)) {				strcpy( cType, "image/jpeg" );			} else			if((strncasecmp(p, "mpeg", sizeof("mpeg")-1) == 0) || 			   (strncasecmp(p, "mpg", sizeof("mpg")-1) == 0)) {				strcpy( cType, "image/mpeg" );			} else			if (strncasecmp(p, "xbm", sizeof("xbm")-1) == 0) {				strcpy( cType, "image/x-xbm" );			} else			if (strncasecmp(p, "xwd", sizeof("xwd")-1) == 0) {				strcpy( cType, "image/x-xwd" );		/*		 * Can't figure out what it is, but it's encoded...		 * better just call it "binary".		 */			} else {				strcpy( cType, "application/octet-stream" );			}		   }		   else {			strcpy( cType, "application/octet-stream" );		   }		}		/*		 * Can't figure out what it is, but it's not encoded...		 * must be ASCII text. Examples include "c-file","h-file", etc.		 */		else {			isText = 1;			strcpy( cType, "text/plain");		}					break;	}	/*	 * output the Content-Type header	 */	if(isText) {	    fprintf(g.outFile, "Content-Type: %s\n", cType);	    fprintf(g.outFile,	    	    "Content-Disposition: attachment; filename=%s\n", fn);	}	else {	   fprintf(g.outFile, "Content-Type: %s; name=\"%s\"\n", cType, fn);	}}/* * subroutine: uudecode the body part and re-encode as base64 *	note: it is assumed that the input stream is positioned to the *	first line past the "begin" header line. */voiduu2base64(){	FILE *tmpFile;	char tn[256];	/*	 * create a temporary file name to hold the output of "uudecode".	 */	tmpnam( tn );	/*	 * uudecode the input into the temporary file.	 */	uuDecode( tn );	/*	 * re-encode the file in "base 64" and write it to standard output	 */	if( (tmpFile = fopen( tn, "r" )) == NULL )		perror("Couldn't open the temp file");	else {		To64( tmpFile, g.outFile, 0 );		fclose(tmpFile);	}	/*	 * remove the temporary file	 */	unlink (tn);}/* * subroutine: pipe uuencoded data into the uudecode command */voiduuDecode( char *tn ){	int pfd[2];	FILE *pout;	int pid;	/*	 * open a pipe to send data to "uudecode"	 */	if(pipe(pfd) == -1) {		perror("Can't create pipe");		return;	}		/*	 * create a new process	 */	if((pid = fork()) == -1) {		perror("Can't fork");		return;	}	/*	 * if this is the child process, close one of the pipes. close	 * stdin and dup the file descriptor for the other pipe,	 * thus connecting stdin to that pipe. then, exec "uudecode".	 */	if(pid == 0) {	/*	 * Set the effective group id to the real group id.  We don't want to	 *    run any child processes with the setgid permissions on.	 */#ifdef HAVE_SETEGID		setegid(getgid());#else		gid_t	gid = getgid();		setresgid(gid, gid, gid);#endif		close(pfd[1]);		fclose(stdin);		dup(pfd[0]);		execl( "/usr/bin/uudecode", "uudecode", (char *)0 );		perror("exec failed");		return;	}	/*	 * back in the parent process, close the other end of the pipe	 */ 	close(pfd[0]);	/*	 * get a file stream descriptor for the pipe	 */	pout = (FILE*)fdopen( pfd[1], "w" );	if( pout == NULL ) {		perror("Couldn't fdopen the pipe");		return;	}	/*	 * write the uuencode header line, substituting the temp file name	 * for the output file name	 */	fprintf( pout, "begin 644 %s\n", tn );	/*	 * copy the body part	 */        copyPart(pout);	fclose(pout);	close(pfd[1]);	/*	 * wait for uudecode to complete	 */	wait(&pid);}/* * copy a body part * * input: output file descriptor * static vars: contentLines, contentLength, line, infile * returns: nothing */voidcopyPart( FILE *outFile ){   int bdySeen = 0;   int bdyLoc;   char *buff;   char *srch;   int readSize;   /*    * body part includes a content-lines header    */   if(debug1 || debug2) {   	fprintf(stderr, "Copying a body part, len= %i, lines= %i\n",g.contentLength,g.contentLines);   }   if ( g.contentLines ) {	/*	 * copy while lines left in the message, and in the body part	 */	while( g.contentLines ) {		g.contentLines--;		if ((fgets(g.line, sizeof(g.line), g.inFile)) == NULL) {			if(debug2) {				fprintf(stderr, "Ran out of lines...\n");			}			g.inconsistent = 1;			return;		}		if( strncasecmp( g.line, XSUNBDY, sizeof(XSUNBDY)-1 ) == 0 ) {			if(debug2) {				fprintf(stderr, "Saw a boundary while copying...\n");			}			bdySeen = 1;			bdyLoc = ftell(g.inFile) - strlen(g.line);		}		fputs(g.line, outFile);	}	/*	 * peek ahead - next line should be a boundary, or end-of-file	 * blank lines are skipped	 */	while ((fgets(g.line, sizeof(g.line), g.inFile)) != NULL) {		/*		 * next line is a boundary, content-lines was right for a change!		 * put back the line and return		 */		if( strncasecmp( g.line, XSUNBDY, sizeof(XSUNBDY)-1 ) == 0 ) {			if(debug2) {				fprintf(stderr, "Content-lines was correct!\n");			}			fseek( g.inFile, 0-strlen(g.line), SEEK_CUR);			return;		}		else if (strlen(g.line) == 1 ) {			if(debug2) {				fprintf(stderr, "Skipping blank line at end of part.\n");			}			continue;		}		else {			g.inconsistent = 1;			/*			 * already seen a boundary (or something like it)?			 */			if (bdySeen) {				fseek( g.inFile, bdyLoc, SEEK_SET );				return;			}			/*			 * not yet at a boundary, keep copying until one is hit			 */			else {				if(debug2) {					fprintf(stderr, "Seeking next boundary\n");				}				copyLines( outFile );			}		}	}	if(debug2) {		fprintf(stderr, "End of part is also end of message\n");	}   } /* End - copy using content-lines */   /*    * body part includes a content-length header    */   else if ( g.contentLength ) {	/*	 * allocate a buffer to hold the body part	 */	buff = (char *)malloc( g.contentLength );	if ( buff == NULL ) {		if(debug1 || debug2) {			fprintf(stderr, "malloc failed, size = %i, copy line by line instead.\n", g.contentLength);		}		copyLines( outFile );		return;	}	readSize = fread(buff, 1, g.contentLength, g.inFile);	fwrite(buff, 1, readSize, outFile);	free(buff);	/*	 * peek ahead - next line should be a boundary, or end-of-file	 * blank lines are skipped	 */	while ((fgets(g.line, sizeof(g.line), g.inFile)) != NULL) {		/*		 * next line is a boundary, content-length was right for a change!		 * put back the line and return		 */		if( strncasecmp( g.line, XSUNBDY, sizeof(XSUNBDY)-1 ) == 0 ) {			if(debug2) {				fprintf(stderr, "Content-length was correct!\n");			}			fseek( g.inFile, 0-strlen(g.line), SEEK_CUR);			return;		}		else if (strlen(g.line) == 1 ) {			if(debug2) {				fprintf(stderr, "Skipping blank line at end of part.\n");			}			continue;		}		else {			g.inconsistent = 1;			/*			 * already seen a boundary (or something like it)?			 */			srch = buff;			while((srch = strchr(srch, '-')) != NULL) {				if( strncasecmp( srch, XSUNBDY, sizeof(XSUNBDY)-1 ) == 0 ) {					if(debug2) {						fprintf(stderr, "Saw a boundary while copying...\n");					}					bdySeen = 1;					bdyLoc = ftell(g.inFile) - strlen(g.line);					break;				}				srch++;			}			if (bdySeen) {				bdyLoc = ftell(g.inFile) - strlen(g.line);				return;			}						/*			 * not yet at a boundary, keep copying until one is hit			 */			else {				if(debug2) {					fprintf(stderr, "Seeking next boundary\n");				}				copyLines( outFile );			}		}	}	if(debug2) {		fprintf(stderr, "End of part is also end of message\n");	}   } /* End - copy using content-length */   /*    * body part has neither content-lines nor content-length header    */   else {	g.inconsistent = 1;	if(debug2) {		fprintf(stderr, "Copying a part with neither content-len nor lines\n");	}	copyLines( outFile );   }}/* * copy lines of a body part without regard to content-length or * content-lines */voidcopyLines ( FILE *outFile ){	while((fgets(g.line, sizeof(g.line), g.inFile)) != NULL) {		if( strncasecmp( g.line, XSUNBDY, sizeof(XSUNBDY)-1 ) == 0 ) {			if(debug2) {				fprintf(stderr, "Copying line by line, hit a boundary\n");			}			fseek( g.inFile, 0-strlen(g.line), SEEK_CUR);			return;		}		fputs(g.line, outFile);	}	if(debug2) {		fprintf(stderr, "Copying line by line, ran out of lines\n");	}}/* * find the "begin" line in the uuencoded data, and * extract the file name. format of the line is: * 	begin <mode> <file name> */voidfindBegin( char *name ){	char *p;	while((fgets(g.line, sizeof(g.line), g.inFile)) != NULL) {		/*		 * bail out if boundary reached - shouldn't happen		 */		if( strncasecmp( g.line, XSUNBDY, sizeof(XSUNBDY)-1 ) == 0 ) {			fseek( g.inFile, 0-strlen(g.line), SEEK_CUR);			break;		}		/*		 * decrement content length/lines		 */		if (g.contentLength) {			g.contentLength -= strlen(g.line);			if(debug2) {				if (g.contentLength<0) fprintf(stderr,"NEGATIVE content length!\n");			}			if (g.contentLength < 0) g.contentLength = 0;		}		if (g.contentLines) {			g.contentLines--;			if(debug2) {				if (g.contentLines<0) fprintf(stderr,"NEGATIVE content lines!\n");			}			if (g.contentLines < 0) g.contentLines = 0;		}		if( strncasecmp(g.line, "begin ", sizeof("begin ")-1 ) == 0 ) {			p = strchr(g.line, (int)' ');			if(p == NULL) break;			p = strchr(++p, (int)' ');			if(p == NULL) break;			strncpy(name, ++p, NAME_SIZE);			p = strrchr(name, '\n');			if(p != NULL) *p = '\0';			if(debug2) {				fprintf(stderr,"BEGIN line found, name = %s\n",name);			}			break;		}	}} /* end findBegin *//* * base64 conversion *//*Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)Permission to use, copy, modify, and distribute this material for any purpose and without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies, and that the name of Bellcore not be used in advertising or publicity pertaining to this material without the specific, prior written permission of an authorized representative of Bellcore.  BELLCORE MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.*/static int	InNewline = 0;/* the following gets a character, but fakes it properly into two chars if there's a newline character */intnextcharin(FILE *infile, int PortableNewlines){    int c;    if (!PortableNewlines) return(getc(infile));    if (InNewline) {        InNewline = 0;        return(10); /* LF */    }    c = getc(infile);    if (c == '\n') {        InNewline = 1;        return(13); /* CR */    }    return(c);}intoutput64chunk(int c1, int c2, int c3, int pads, FILE *outfile){   static char	basis_64[] =      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";   int	status = 0;   status = putc(basis_64[c1>>2], outfile);   if ( status == EOF ) return 0;   status = putc(basis_64[((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)], outfile);   if ( status == EOF ) return 0;   if (pads == 2) {      status = putc('=', outfile);      if ( status == EOF ) return 0;      status = putc('=', outfile);      if ( status == EOF ) return 0;   }   else if (pads) {      status = putc(basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)], outfile);      if ( status == EOF ) return 0;      status = putc('=', outfile);      if ( status == EOF ) return 0;   }   else {      status = putc(basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)], outfile);      if ( status == EOF ) return 0;      status = putc(basis_64[c3 & 0x3F], outfile);      if ( status == EOF ) return 0;   }   return 1;}intTo64(FILE *infile, FILE *outfile, int PortableNewlines) {    int c1, c2, c3, ct=0;    int	error = 0;    InNewline = 0; /* always reset it */    while ( !error && (c1 = nextcharin(infile, PortableNewlines)) != EOF) {        c2 = nextcharin(infile, PortableNewlines);        if (c2 == EOF) {            error = !output64chunk(c1, 0, 0, 2, outfile);        } else {            c3 = nextcharin(infile, PortableNewlines);            if (c3 == EOF) {                error = !output64chunk(c1, c2, 0, 1, outfile);            } else {                error = !output64chunk(c1, c2, c3, 0, outfile);            }        }        ct += 4;        if (ct > 71) {            error = (putc('\n', outfile) == EOF);            ct = 0;        }    }    if (!error && ct) error = (putc('\n', outfile) == EOF);    error = (fflush(outfile) == EOF);    return !error;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -