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

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

/* $Id: attach.c,v 1.3 91/05/25 15:31:16 cap Exp $
 * Handle attachments
 */

#include <stdio.h>
#ifdef USG
#include <string.h>
#else
#include <strings.h>
#include <sys/param.h>
#endif
#include "config.h"
#include "tnextmail.h"

char *basename(), *pathname();
struct attach *noteattach();

extern int rtfpos;
#ifdef HAVE_GETCWD
extern char *getcwd();
#else
extern char *getwd();
#endif

extern char *malloc(), *realloc();

struct attach *attachments;
int lastattach;			/* number of next attachment */
int maxattach;			/* number of attachment slots allocated */
char cwd[MAXPATHLEN];		/* current directory */

/* 
 * rtfattach - generate the rtf information necessary for the 
 *             attachment named. Also, do the housekeeping to
 *             note the existence of a new attachment.
 */

rtfattach(out, filename)
     FILE *out;
     char *filename;
{
    struct attach *a;
    
    if ((a = noteattach(filename)) == NULL)
	return -1;
    fprintf(out, "\n{\\attachment%d %s\n}\n", rtfpos, a->basename);
    return 0;
}

/* basename - return the last component of a filename */

char *basename(fullname)
     char *fullname;
{
    char *base;

    if ((base = rindex(fullname, '/')) == NULL)
	return fullname;
    else
	return base + 1;
}

/*
 * pathname - return the path part of a filename. The pointer returned
 *            is in static space
 */

char *pathname(fullname)
     char *fullname;
{
    static char path[MAXPATHLEN];
    char *lastslash;
    
    strncpy(path, fullname, MAXPATHLEN);
    lastslash = rindex(path, '/');
    if (lastslash != NULL)
	*++lastslash = '\0';
    else
	path[0] = '\0';
    return path;
}

/* 
 * noteattach - note down in our table the existence of a new 
 *              attachment. We store the full path name and the file 
 *              name.
 */

struct attach *noteattach(filename)
     char *filename;
{
    char *base = basename(filename), *path = pathname(filename);
    char *p;
    int i;
    struct attach *a, *retval;
    
    if (++lastattach >= maxattach) {
	if (!attachments) {	/* first time through */
	    maxattach = 4;
	    attachments = (struct attach *) malloc(maxattach * sizeof (struct attach));
	} else {		/* allocate more */
	    maxattach *= 2;
	    attachments = (struct attach *) realloc(attachments, maxattach * sizeof (struct attach));
	}
	if (!attachments) {
	    perror("malloc");
	    fatalerrors++;
	    lastattach--;
	    return NULL;
	}
    }
    if (!strcmp(base, RTF_NAME)) {
	fprintf(stderr, "Please don't name an attachment %s.\n", RTF_NAME);
	fatalerrors++;
	retval = NULL;
    }
    if (cwd[0] == '\0') {	/* haven't figured out current dir yet */
#ifdef HAVE_GETCWD
	if (getcwd(cwd, sizeof cwd - 2) == NULL) {
	    perror("pwd");
	    fatalerrors++;
	}
#else
	if (getwd(cwd) == NULL) {
	    fprintf(stderr, cwd);
	    fatalerrors++;
	}
#endif
	strcat(cwd, "/");	/* add trailing / */
    }
    for (i = 0; i < lastattach; i++) {
	a = &attachments[i];
	if (!strcmp(a->basename, base)) {
	    fprintf(stderr, "You cannot have two attachments with the same basename %s\n", base);
	    fatalerrors++;
	    retval = NULL;
	} else
	    retval = a;
    }
    /* 
     * i is the slot for the new attachment. Now determine if it's a 
     * relative path or absolute, and possibly add the current 
     * directory accordingly.
     */
    if (path[0] == '\0')		/* in current directory */
	strcpy(a->pathname, cwd);
    else if (*path == '/')	/* absolute path was given */
	strcpy(a->pathname, path);
    else			/* relative path given */
	sprintf(a->pathname, "%s%s", cwd, path);
    p = &a->pathname[strlen(a->pathname) - 1];
    strcpy(a->basename, base);
    return retval;
}

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