ftp.nice.ch/pub/next/unix/mail/tnextmail.1.0.s.tar.gz#/tnextmail/main.c

This is main.c in view mode; [Download] [Up]

/* $Id: main.c,v 1.3 91/05/25 16:14:52 cap Exp $
 * Trivial NeXTMail - send messages with attachments from non-NeXT machines.
 * by Chris Paris (cap@cs.cmu.edu)
 * This code is in the public domain.
 */

#include <stdio.h>
#ifndef USG
#include <sys/param.h>
#endif
#include "config.h"
#include "tnextmail.h"

#define TMP_TEMPLATE "/tmp/tnmXXXXXX"

char tmpdir_name[MAXPATHLEN];
char rtf_name[MAXPATHLEN];
char subject_buffer[1024];
char *message_subject = subject_buffer;
int rtfpos;			/* printing characters in RTF output */
int fatalerrors;		/* number of fatal errors */

extern char *mktemp();

main(argc, argv)
     int argc;
     char *argv[];
{
    FILE *rtf_file;
    extern char tar_uu_name[];

    parseargs(argc, argv);
    if (message_subject[0] == '\0' && isatty(fileno(stdin)))
	ask_subject();
    
    /* create the temporary directory in which everything goes */
    strcpy(tmpdir_name, TMP_TEMPLATE);
    mktemp(tmpdir_name);
    if (mkdir(tmpdir_name, 0700) != 0) {
	perror(tmpdir_name);
	exit(1);
    }
    /* open the main RTF message file */
    sprintf(rtf_name, "%s/%s", tmpdir_name, RTF_NAME);
    if ((rtf_file = fopen(rtf_name, "w")) == NULL) {
	perror(rtf_name);
	exit(1);
    }
    rtf_start(rtf_file);
    msgtortf(stdin, rtf_file);
    rtf_end(rtf_file);
    fclose(rtf_file);
    
    check_file_access();	/* make sure all attachments are readable */
    if (fatalerrors) {
	cleanup(tmpdir_name, rtf_name, tar_uu_name);
	if (fatalerrors == 1)
	    fprintf(stderr, "There was 1 fatal error, my friend.\n");
	else
	    fprintf(stderr, "There were %d fatal errors, my friend.\n",
		    fatalerrors);
	exit(fatalerrors);
    }
    batch_message(tmpdir_name);
    mail_message(tmpdir_name);
    cleanup(tmpdir_name, rtf_name, tar_uu_name);
    exit(0);
}

/* parseargs - look for arguments on the command line */

parseargs(argc, argv)
     int argc;
     char *argv[];
{
    extern char **tolist;
    extern char *optarg;
    extern int optind;
    int c, errflg = 0;

    while ((c = getopt(argc, argv, "s:")) != EOF)
	switch (c) {
	  case 's':
	    message_subject = optarg;
	    break;
	  default:
	    errflg++;
	}
    if (optind == argc)		/* no addressee specified */
	errflg++;
    if (errflg) {
	fprintf(stderr, "Usage: %s [-s subject] to-addr ...\n", argv[0]);
	exit(1);
    }
    tolist = &argv[optind];
}

/* cleanup - remove the temporary files we may have created */

cleanup(tmpdir_name, rtf_name, tar_uu_name)
     char tmpdir_name[], rtf_name[], tar_uu_name[];
{
    unlink(rtf_name);
    unlink(tar_uu_name);
    if (rmdir(tmpdir_name))
	perror(tmpdir_name);
}
 
/* ask_subject - ask the user for a subject */

ask_subject()
{
    int len;
    
    printf("Subject: ");
    fflush(stdout);
    fgets(message_subject, sizeof subject_buffer - 1, stdin);
    /* remove the \n */
    if (len = strlen(message_subject))
	message_subject[len - 1] = '\0';
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.