📄 toast.c
字号:
#endif /* UTIMBUF */
}
#endif /* HAS_UTIME */
#endif /* HAS_UTIMES */
}
static int okay_as_input P3((name,f,st), char* name, FILE* f, struct stat * st)
{
# ifdef HAS_FSTAT
if (fstat(fileno(f), st) < 0)
# else
if (stat(name, st) < 0)
# endif
{
perror(name);
fprintf(stderr, "%s: cannot stat \"%s\"\n", progname, name);
return 0;
}
if (!S_ISREG(st->st_mode)) {
fprintf(stderr,
"%s: \"%s\" is not a regular file -- unchanged.\n",
progname, name);
return 0;
}
if (st->st_nlink > 1 && !f_cat && !f_precious) {
fprintf(stderr,
"%s: \"%s\" has %s other link%s -- unchanged.\n",
progname,name,st->st_nlink - 1,"s" + (st->st_nlink<=2));
return 0;
}
if (!f_decode && !f_cat && name && suffix( name, SUFFIX_TOASTED )) {
fprintf(stderr,
"%s: %s already has \"%s\" suffix -- unchanged.\n",
progname, name, SUFFIX_TOASTED );
return 0;
}
return 1;
}
static void prepare_io P1(( desc), struct fmtdesc * desc)
{
output = desc->output;
input = desc->input;
init_input = desc->init_input;
init_output = desc->init_output;
}
static struct fmtdesc * grok_format P1((name), char * name)
{
char * c;
struct fmtdesc ** f;
if (name) {
c = plainname(name);
for (f = alldescs; *f; f++)
if ( (*f)->suffix
&& *(*f)->suffix
&& suffix(c, (*f)->suffix)) {
free(c);
return *f;
}
free(c);
}
return (struct fmtdesc *)0;
}
static int open_input P2((name, st), char * name, struct stat * st)
{
struct fmtdesc * f = f_format;
st->st_nlink = 0; /* indicates `undefined' value */
if (!name) {
inname = (char *)NULL;
in = stdin;
}
else {
if (!f_decode) inname = strcpy(emalloc(strlen(name)+1), name);
else inname = codename(name);
if (!(in = fopen(inname, READ))) {
perror(inname); /* not guaranteed to be valid here */
fprintf(stderr, "%s: cannot open \"%s\" for reading\n",
progname, inname);
return 0;
}
if (!okay_as_input(inname, in, st)) return 0;
if (!f) f = grok_format(inname);
}
prepare_io( f ? f : & DEFAULT_FORMAT );
return 1;
}
static int open_output P1((name), char *name)
{
if (!name || f_cat) {
out = stdout;
outname = (char *)NULL;
}
else {
int outfd = -1;
char * o;
o = (*(f_decode ? plainname : codename))(name);
if (!length_okay(o)) return 0;
if ((outfd = open(o, O_WRITE_EXCL, 0666)) >= 0)
out = fdopen(outfd, WRITE);
else if (errno != EEXIST) out = (FILE *)NULL;
else if (ok_to_replace(o)) out = fopen(o, WRITE);
else return 0;
if (!out) {
perror(o);
fprintf(stderr,
"%s: can't open \"%s\" for writing\n",
progname, o);
if (outfd >= 0) (void)close(outfd);
return 0;
}
outname = o;
}
return 1;
}
int process_encode P0()
{
gsm r;
int n = 0;
gsm_signal s[ 160 ];
gsm_frame d;
int cc;
if (!(r = gsm_create())) return -1;
(void)gsm_option(r, GSM_OPT_FAST, &f_fast);
(void)gsm_option(r, GSM_OPT_VERBOSE, &f_verbose);
while ((cc = (*input)(s)) > 0) {
if (cc < sizeof(s) / sizeof(*s))
memset((char *)(s+cc), 0, sizeof(s)-(cc * sizeof(*s)));
gsm_encode(r, s, d);
if (fwrite((char *)d, sizeof(d), 1, out) != 1) {
perror(outname ? outname : "stdout");
fprintf(stderr, "%s: error writing to %s\n",
progname, outname ? outname : "stdout");
gsm_destroy(r);
return -1;
}
}
if (cc < 0) {
perror(inname ? inname : "<stdin>");
fprintf(stderr, "%s: error reading from %s\n",
progname, inname ? inname : "<stdin>");
gsm_destroy(r);
return -1;
}
gsm_destroy(r);
return 0;
}
int process_decode P0()
{
gsm r;
gsm_frame s;
gsm_signal d[ 160 ];
int cc, cw;
if (!(r = gsm_create())) return -1;
(void)gsm_option(r, GSM_OPT_FAST, &f_fast);
(void)gsm_option(r, GSM_OPT_VERBOSE, &f_verbose);
while ((cc = fread(s, 1, sizeof(s), in)) > 0) {
if (cc != sizeof(s)) {
if (cc >= 0) fprintf(stderr,
"%s: incomplete frame (%d byte%s missing) from %s\n",
progname, sizeof(s) - cc,
"s" + (sizeof(s) - cc == 1),
inname ? inname : "<stdin>" );
gsm_destroy(r);
return -1;
}
if (gsm_decode(r, s, d)) {
fprintf(stderr, "%s: bad frame in %s\n",
progname, outname ? outname : "stdout");
gsm_destroy(r);
return -1;
}
if ((*output)(d) < 0) {
perror(outname);
fprintf(stderr, "%s: error writing to %s\n",
progname, outname);
gsm_destroy(r);
return -1;
}
}
if (cc < 0) {
perror(inname ? inname : "<stdin>" );
fprintf(stderr, "%s: error reading from %s\n", progname,
inname ? inname : "<stdin>");
gsm_destroy(r);
return -1;
}
gsm_destroy(r);
return 0;
}
int process P1((name), char * name)
{
int outfd = -1;
char * tmp;
out = (FILE *)0;
in = (FILE *)0;
outname = (char *)0;
inname = (char *)0;
if ( !open_input( name, &instat )
|| !open_output( name )
|| (*(f_decode ? init_output : init_input))()
|| (*(f_decode ? process_decode : process_encode))()
|| fflush(out) < 0
|| ferror(out)) {
err: if (out) {
perror(outname ? outname : "stdout");
fprintf(stderr, "%s: error writing \"%s\"\n",
progname, outname ? outname : "stdout");
if (out != stdout) {
(void)fclose(out);
if ( unlink(outname) < 0
&& errno != ENOENT
&& errno != EINTR) {
perror(outname);
fprintf(stderr,
"%s: could not unlink \"%s\"\n",
progname, outname);
}
}
}
if (in && in != stdin) fclose(in);
if (inname && inname != name) free(inname);
if (outname && outname != name) free(outname);
return -1;
}
if (out != stdout) {
update_times();
update_mode ();
update_own ();
if (fclose(out) < 0) goto err;
free(outname);
outname = (char *)0;
}
if (in != stdin) {
fclose(in);
if (!f_cat && !f_precious) {
if (unlink(inname) < 0) {
perror(inname);
fprintf(stderr,
"%s: source \"%s\" not deleted.\n",
progname, inname);
}
}
free(inname);
inname = (char *)0;
}
return 0;
}
static void version P0()
{
printf( "%s 1.0, version %s\n",
progname,
"$Id: toast.c,v 1.1 1992/10/28 00:15:50 jutta Exp $" );
}
static void help P0()
{
printf("Usage: %s [-fcpdhvaulsF] [files...]\n", progname);
printf("\n");
printf(" -f force Replace existing files without asking\n");
printf(" -c cat Write to stdout, do not remove source files\n");
printf(" -d decode Decode data (default is encode)\n");
printf(" -p precious Do not delete the source\n");
printf("\n");
printf(" -u u-law Force 8 kHz/8 bit u-law in/output format\n");
printf(" -s sun .au Force Sun .au u-law in/output format\n");
printf(" -a A-law Force 8 kHz/8 bit A-law in/output format\n");
printf(" -l linear Force 16 bit linear in/output format\n");
printf("\n");
printf(" -F fast Sacrifice conformance to performance\n");
printf(" -v version Show version information\n");
printf(" -h help Print this text\n");
printf("\n");
}
void set_format P1((f), struct fmtdesc * f)
{
if (f_format && f_format != f) {
fprintf( stderr,
"%s: only one of -[uals] is possible (%s -h for help)\n",
progname, progname);
exit(1);
}
f_format = f;
}
int main P2((ac, av), int ac, char **av)
{
int opt;
extern int optind;
extern char * optarg;
parse_argv0( *av );
while ((opt = getopt(ac, av, "fcdpvhuaslVF")) != EOF)
switch (opt) {
case 'd': f_decode = 1; break;
case 'f': f_force = 1; break;
case 'c': f_cat = 1; break;
case 'p': f_precious = 1; break;
case 'F': f_fast = 1; break;
#ifndef NDEBUG
case 'V': f_verbose = 1; break; /* undocumented */
#endif
case 'u': set_format( &f_ulaw ); break;
case 'l': set_format( &f_linear ); break;
case 'a': set_format( &f_alaw ); break;
case 's': set_format( &f_audio ); break;
case 'v': version(); exit(0);
case 'h': help(); exit(0);
default:
fprintf(stderr,
"Usage: %s [-fcpdhvuaslF] [files...] (-h for help)\n",
progname);
exit(1);
}
f_precious |= f_cat;
av += optind;
ac -= optind;
catch_signals(onintr);
if (ac <= 0) process( (char *)0 );
else while (ac--) process( *av++ );
exit(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -