📄 uuencode.cpp
字号:
// Created:11-04-98
// By Jeff Connelly
// UUEncodes a binary file to plain text that uses ComprLib
// Note that the 'uu_encode()' function in ComprLib does not write a header,
// so this program does. .UUE files are also capable of storing mulitible
// files in one file. This program cannot handle that.
#include <stdio.h>
#include <stdlib.h>
#include "comprlib.h"
#include <string.h>
#include <conio.h>
#include <unistd.h>
#include <io.h>
#include <sys/stat.h>
#include <ctype.h>
const char* help = "UUEncodes a binary file to plain text\n"
"\tUUENCODE input...\n"
"Multible input files may be specified"
#ifdef __GNUC__
// DJGPP has an automatic argument globing feature that expands filenames
// containing wildcards such as '*' and '?'.
", wild cards OK\n"
#else
// Other compilers may not support this.
", wild cards may not work\n"
#endif
"Encoded files have .UUE extension."
"Options: \n"
"-t{t|b|a} UUEncode {text|binary|all} files\n"
"-a{y|n} {Exit|Continue} if cannot open files, n default.\n"
"-u{p|e|w} {Process|Terminate|warn} for .UUE extension\n"
"-e{o|p|x|i} {Overwrite|Prompt|Exit|Ignore} if output exists\n"
"Using '/' instead of '-' is OK\n"
"\n";
int main(int argc, char* argv[])
{
if (argc == 1)
{
printf (help);
exit (0);
}
// 'type_flag' is 0 for all files, -1 for binary files, or +1 for text
int type_flag = 0;
// Exit if cannot open?
bool exit_if_no_open_flag = 0;
// 'has_uue_extension_flag' is 0 to UUEncode it anyway, -1 terminates
// program with an error, or +1 just prints a warning
int has_uue_extension_flag = 0;
// 'already_exists_flag' is 1 for overwrite, 2 for prompt,
// 3 for exit, or 4 for ignore.
int already_exists_flag = 2;
register int i;
int mode = 0;
char* outfn;
char* buf;
FILE* fp;
char* fn;
char* newfn;
register char c;
struct stat sbuf;
FILE* out;
// Use file I/O
ComprLibFileIO::Set();
// Parse the arguments, executing them as we go along.
for (i = 1; i < argc; ++i)
{
if (argv[i][0] == '-' || argv[i][0] == '/')
{
// It is a flag
switch (argv[i][1])
{
case 't': switch(argv[i][2])
{
case 't': type_flag = +1; break;
case 'b': type_flag = -1; break;
case 'a': type_flag = 0; break;
};
break;
case 'a': switch(argv[i][2])
{
case 'y': exit_if_no_open_flag = 1; break;
case 'n': exit_if_no_open_flag = 0; break;
};
break;
case 'u': switch (argv[i][2])
{
case 'p': has_uue_extension_flag = 0;
case 'e': has_uue_extension_flag = -1;
case 'w': has_uue_extension_flag = +1;
};
break;
case 'e': switch(argv[i][2])
{
case 'o': already_exists_flag = 1;
case 'p': already_exists_flag = 2;
case 'x': already_exists_flag = 3;
case 'i': already_exists_flag = 4;
};
break;
default: printf ("Invalid flag '%c'\n", argv[i][1]);
};
continue;
}
fn = (char*)malloc(strlen(argv[i]));
strcpy (fn, argv[i]);
// Open the input file
fp = fopen(fn, "rb");
if (!fp)
{
printf ("Cannot open '%s'\n", fn);
if (exit_if_no_open_flag)
exit (1);
continue; // Skip this file
}
// 'newfn' is the encoded filename
newfn = (char*)malloc(strlen(fn));
strcpy (newfn, fn);
// The output filename has the .UUE extension
if (!strchr(newfn, '.')) // No extension, tack on .UUE
strcat (newfn, ".UUE");
else // Has an extension
{
// Store the extension in 'buf'
buf = strchr(newfn, '.');
// If the extension is .UUE, do what the flag tells me to do
if (!stricmp(buf, ".UUE"))
{
switch (has_uue_extension_flag)
{
case -1: printf ("File '%s' already has .UUE extension.\n",
fn);
exit (2);
case 0: break;
case +1: printf ("Warning: File '%s' has .UUE extension.\n",
fn);
break;
}
} else { // Does not have .UUE extension
strcpy (buf, ".UUE");
}
}
if (__file_exists(newfn))
{
// The output file already exists! What should we do?
switch (already_exists_flag)
{
case 1: break; // Use does not care, overwrite it
case 2: printf ("'%s' exists, Overwrite/Ignore/Rename/Abort? ",
newfn);
scanf("%c", c);
putc('\n', stdin);
switch (toupper(c))
{
case 'O': break;
case 'I': continue;
case 'R': printf ("New name: ");
scanf("%s\n", buf);
strcpy (newfn, buf);
break;
case 'A': exit (3);
};
break;
case 3: exit (3);
case 4: continue;
}
};
out = fopen(newfn, "wt");
if (!out)
{
printf ("Cannot open '%s'\n", newfn);
if (exit_if_no_open_flag)
exit (1);
continue;
}
printf ("Encoding %s as %s...\n", fn, newfn);
// Figure out the file mode (note that the 0 prefix means octal)...
if (fstat(fileno(fp), &sbuf) < 0 || !isatty(fileno(fp)))
mode = 0666 & ~0666;
else
mode = sbuf.st_mode & 0777;
fprintf (out, "begin %o %s\n", mode, fn);
// Tell ComprLib what files we want to compress
ComprLibFileIO::source_file = fp;
ComprLibFileIO::dest_file = out;
uu_encode();
// Print the end command
fprintf (out, "\nend\n");
// Clean-up stuff
fclose(fp);
fclose(out);
free(fn);
free(newfn);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -