uuenview.c
来自「UUDeview是一个编码解码器」· C语言 代码 · 共 1,348 行 · 第 1/3 页
C
1,348 行
/* * This file is part of uudeview, the simple and friendly multi-part multi- * file uudecoder program (c) 1994-2001 by Frank Pilhofer. The author may * be contacted at fp@fpx.de * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */#ifdef HAVE_CONFIG_H#include "config.h"#endif#ifdef SYSTEM_WINDLL#include <windows.h>#endif#ifdef SYSTEM_OS2#include <os2.h>#endif/* * Main function for standalone uuenview */#include <sys/types.h>#include <sys/stat.h>#include <ctype.h>#include <stdio.h>#include <time.h>#ifdef STDC_HEADERS#include <stdlib.h>#include <string.h>#endif#ifdef HAVE_UNISTD_H#include <unistd.h>#endif#ifdef HAVE_ERRNO_H#include <errno.h>#endif#include <uudeview.h>#include <fptools.h>#include <uufnflt.h>char * uuenview_id = "$Id: uuenview.c,v 1.22 2002/03/06 13:52:46 fp Exp $";/* * mail and news software */#ifdef PROG_INEWSchar * uue_inewsprog = PROG_INEWS;#elsechar * uue_inewsprog = NULL;#endif#ifdef PROG_MAILERchar * uue_mailprog = PROG_MAILER;#elsechar * uue_mailprog = NULL;#endif#ifdef MAILER_NEEDS_SUBJECTint uue_mpsubject = 1;#elseint uue_mpsubject = 0;#endif/* * defines */#define UUE_STDOUT 0#define UUE_TOFILE 1#define UUE_MAILTO 2#define UUE_POSTTO 3/* * the progress meter is only displayed if stderr is a terminal. * take care of systems where we can't check this condition. */#ifdef HAVE_ISATTY#define UUISATTY(f) (isatty(fileno(f)))#else#define UUISATTY(f) (1)#endif/* * Message Callback */static voidMessageCallback (void *param, char *message, int level){#if 0 if (UUGetOption (UUOPT_VERBOSE, NULL, NULL, 0) || level >= UUMSG_WARNING) { fprintf (stderr, "%70s\r", ""); fprintf (stderr, "%s\n", message); }#else if (level >= UUMSG_WARNING) { fprintf (stderr, "%70s\r", ""); fprintf (stderr, "%s\n", message); }#endif}static intBusyCallback (void *param, uuprogress *progress){ char stuff[26]; int count, pcts; char *ptr; /* * Display progress meter only if stderr is a terminal */ if (!UUISATTY(stderr)) return 0; if ((ptr = _FP_strrchr (progress->curfile, DIRSEPARATOR[0])) == NULL) ptr = progress->curfile; else ptr++; if (progress->action == 4 && UUGetOption (UUOPT_VERBOSE, NULL, NULL, 0)) { pcts = (int)((100*progress->partno+progress->percent-100) / progress->numparts); for (count=0, stuff[25]='\0'; count<25; count++) stuff[count] = (count<pcts/4)?'#':'.'; fprintf (stderr, "encoding part %3d/%3d %s\r", progress->partno, progress->numparts, stuff); fflush (stderr); } return 0;}/* * usage */static voidusage (char *argv0){ if (_FP_stristr (argv0, "uuencode") != NULL) { printf ("\n\ uuencode -- a simple encoder (w) 1995 Frank Pilhofer\n\n\ usage:\n\ uuencode [infile] remotefile\n\n\"); printf ("\\tinfile the local file, where to read data from. If no infile\n\\t is given, or infile is a single hyphen, the standard\n\\t input is used instead.\n\\tremotefile the name as which the recepient will receive the file\n\\n\ For much more powerful encoding facilities, try calling this program\n\ as 'uuenview'.\n\\n\"); return; } printf ("\n\ UUENVIEW %spl%s -- a simple encoder (w) 1995 Frank Pilhofer\n\n\ usage:\n\ uuenview [-type] [options] file [...]\n\n", VERSION, PATCH); printf (" Options:\n"); printf ("\t-b,-u,-x,-y Encode files as Base64, Uuencoding, Xx, yEnc,\n"); printf ("\t-t,-q Plain text or Quoted-Printable, respectively\n"); printf ("\t-o Encode to file(s) using the original's base name\n"); printf ("\t-od path Same, but write files to selected path\n");#if defined(HAVE_POPEN) && defined(PROG_MAILER) printf ("\t-m addr Send file(s) by mail\n");#endif#if defined(HAVE_POPEN) && defined(PROG_INEWS) printf ("\t-p group Post file(s) to newsgroups\n");#endif printf ("\t-a Read message from stdin and attach files\n"); printf ("\t-lines Chunk into messages of lines lines each\n"); printf ("\n");#if defined(SYSTEM_DOS) || defined(SYSTEM_QUICKWIN) printf (" See Manual for more details\n\n");#else printf (" See uuenview(1) for more details.\n\n");#endif printf (" Example:\n");#ifndef HAVE_POPEN printf (" uuenview -b -2000 -o uuenview.exe\n"); printf ("\tEncodes uuenview.exe as Base64 into 2000-line chunks, and\n"); printf ("\twrites the result to uuenview.001 and uuenview.002.\n\n");#else printf (" uuenview -b -2000 -m root -o uudeview.tar.gz\n"); printf ("\tEncodes 'uudeview.tar.gz' as Base64 into 2000-line chunks.\n"); printf ("\tIt is both mailed to your system administrator and written\n"); printf ("\tto the files uudeview.001 and uudeview.002\n\n");#endif return;}/* * ----------------------------------------------------------------------- * Stolen from uuscan.c to parse boundary * ----------------------------------------------------------------------- *//* * Extract the value from a MIME attribute=value pair. This function * receives a pointer to the attribute. */static char *ParseValue (char *attribute){ static char uuscan_pvvalue[256]; char *ptr=uuscan_pvvalue; int length=0; if (attribute == NULL) return NULL; while (*attribute && *attribute != '=') attribute++; if (*attribute == '=') { attribute++; while (isspace (*attribute)) attribute++; } if (!*attribute) return NULL; if (*attribute == '"') { /* quoted-string */ attribute++; while (*attribute && *attribute != '"' && length < 255) { if (*attribute == '\\') *ptr++ = *++attribute; else *ptr++ = *attribute; attribute++; length++; } *ptr = '\0'; } else { /* tspecials from RFC1521 */ while (*attribute && !isspace (*attribute) && *attribute != '(' && *attribute != ')' && *attribute != '<' && *attribute != '>' && *attribute != '@' && *attribute != ',' && *attribute != ';' && *attribute != ':' && *attribute != '\\' &&*attribute != '"' && *attribute != '/' && *attribute != '[' && *attribute != ']' && *attribute != '?' && *attribute != '=' && length < 255) *ptr++ = *attribute++; *ptr = '\0'; } return uuscan_pvvalue;}/* * ----------------------------------------------------------------------- * Stolen from uuscan.c (end) * ----------------------------------------------------------------------- *//* * Create command line to mail or post a file. The result is malloc()ed * and must be freed manually */static char *SendMkCommand (char **rcptlist, char *towhom, char *subject, int isemail){ char *command, *ptr; int len, count; *rcptlist = NULL; if (isemail && (uue_mailprog == NULL || *uue_mailprog == '\0')) { fprintf (stderr, "error: Cannot Email file: option not configured\n"); return NULL; } else if (!isemail && (uue_inewsprog == NULL || *uue_inewsprog == '\0')) { fprintf (stderr, "error: Cannot Post file: option not configured\n"); return NULL; } len = strlen ((isemail)?uue_mailprog:uue_inewsprog) + ((uue_mpsubject)?strlen(subject):0) + ((isemail)?strlen(towhom):0) + 32; if ((command = (char *) malloc (len)) == NULL) { fprintf (stderr, "error: Out of memory allocating %d bytes\n", len); return NULL; } if ((*rcptlist = (char *) malloc (strlen (towhom) + 16)) == NULL) { fprintf (stderr, "error: Out of memory allocating %d bytes\n", strlen (towhom)+16); _FP_free (command); return NULL; } if (isemail) { if (uue_mpsubject && subject!=NULL) sprintf (command, "%s -s \"%s\"", uue_mailprog, subject); else sprintf (command, "%s", uue_mailprog); /* * Attach list of recipients to mailer command and compose another list * of recipients */ count = 0; (*rcptlist)[0] = '\0'; ptr = _FP_strtok (towhom, ",; "); while (ptr) { strcat (command, " \""); strcat (command, ptr); strcat (command, "\""); if (count++) strcat (*rcptlist, ","); strcat (*rcptlist, ptr); ptr = _FP_strtok (NULL, ",; "); } } else { /* posting */ sprintf (command, "%s", uue_inewsprog); count = 0; (*rcptlist)[0] = '\0'; ptr = _FP_strtok (towhom, ";, "); while (ptr) { if (count++) strcat (*rcptlist, ","); strcat (*rcptlist, ptr); ptr = _FP_strtok (NULL, ";, "); } } return command;}/* * Attach something. A text is read from stdin and converted into a proper * MIME message. All files from the command line are then attached MIME- * style. The result is mailed or posted. * If towhom==NULL, the result is sent to stdout. */static intAttachFiles (char *towhom, char *subject, char *from, char *replyto, int isemail, int encoding, int argc, char *argv[]){ static char input[1024], *ctype=NULL, *cte=NULL, boundary[64]; char *command=NULL, *rcptlist=NULL, *ptr; FILE *thepipe; int index, res; /* remember the headers we care about */ int hadsubject=0, hadgroups=0, hadto=0, hadmime=0, hadmulti=0; int hadfrom=0, hadreplyto=0; if (towhom) {#ifndef HAVE_POPEN fprintf (stderr, "error: Your system does not support %s of files\n", (isemail)?"mailing":"posting"); return UURET_ILLVAL;#else if ((command = SendMkCommand (&rcptlist, towhom, subject, isemail)) == NULL) { return UURET_ILLVAL; } if ((thepipe = popen (command, "w")) == NULL) { fprintf (stderr, "error: could not open pipe %s\n", command); _FP_free (rcptlist); _FP_free (command); return UURET_IOERR; }#endif } else { thepipe = stdout; } /* * okay, copy and scan header */ index=0; while (!feof (stdin)) { if (_FP_fgets (input, 1024, stdin) == NULL) break; if (input[0] == '\0' || input[0] == '\012' || input[0] == '\015') break; /* * If the first line does not appear to be a header */ if (index == 0) { ptr = input; while (*ptr && !isspace(*ptr) && *ptr!=':') ptr++; if (*ptr != ':' && _FP_strnicmp (input, "From ", 5) != 0) { break; } index++; } if (_FP_strnicmp (input, "Subject:", 8) == 0) { if (subject) { fprintf (thepipe, "Subject: %s\n", subject); } else { fprintf (thepipe, "%s", input); } hadsubject = 1; } else if (_FP_strnicmp (input, "Newsgroups:", 11) == 0) { if (towhom && !isemail && rcptlist) { fprintf (thepipe, "Newsgroups: %s\n", rcptlist); } else { fprintf (thepipe, "%s", input); } hadgroups = 1; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?