home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Mail / tnextmail / Source / attach.c next >
Encoding:
C/C++ Source or Header  |  1992-08-06  |  3.6 KB  |  155 lines

  1. /* $Id: attach.c,v 1.3 91/05/25 15:31:16 cap Exp $
  2.  * Handle attachments
  3.  */
  4.  
  5. #include <stdio.h>
  6. #ifdef USG
  7. #include <string.h>
  8. #else
  9. #include <strings.h>
  10. #include <sys/param.h>
  11. #endif
  12. #include "config.h"
  13. #include "tnextmail.h"
  14.  
  15. char *basename(), *pathname();
  16. struct attach *noteattach();
  17.  
  18. extern int rtfpos;
  19. #ifdef HAVE_GETCWD
  20. extern char *getcwd();
  21. #else
  22. extern char *getwd();
  23. #endif
  24.  
  25. extern char *malloc(), *realloc();
  26.  
  27. struct attach *attachments;
  28. int lastattach;            /* number of next attachment */
  29. int maxattach;            /* number of attachment slots allocated */
  30. char cwd[MAXPATHLEN];        /* current directory */
  31.  
  32. /* 
  33.  * rtfattach - generate the rtf information necessary for the 
  34.  *             attachment named. Also, do the housekeeping to
  35.  *             note the existence of a new attachment.
  36.  */
  37.  
  38. rtfattach(out, filename)
  39.      FILE *out;
  40.      char *filename;
  41. {
  42.     struct attach *a;
  43.     
  44.     if ((a = noteattach(filename)) == NULL)
  45.     return -1;
  46.     fprintf(out, "\n{\\attachment%d %s\n}\n", rtfpos, a->basename);
  47.     return 0;
  48. }
  49.  
  50. /* basename - return the last component of a filename */
  51.  
  52. char *basename(fullname)
  53.      char *fullname;
  54. {
  55.     char *base;
  56.  
  57.     if ((base = rindex(fullname, '/')) == NULL)
  58.     return fullname;
  59.     else
  60.     return base + 1;
  61. }
  62.  
  63. /*
  64.  * pathname - return the path part of a filename. The pointer returned
  65.  *            is in static space
  66.  */
  67.  
  68. char *pathname(fullname)
  69.      char *fullname;
  70. {
  71.     static char path[MAXPATHLEN];
  72.     char *lastslash;
  73.     
  74.     strncpy(path, fullname, MAXPATHLEN);
  75.     lastslash = rindex(path, '/');
  76.     if (lastslash != NULL)
  77.     *++lastslash = '\0';
  78.     else
  79.     path[0] = '\0';
  80.     return path;
  81. }
  82.  
  83. /* 
  84.  * noteattach - note down in our table the existence of a new 
  85.  *              attachment. We store the full path name and the file 
  86.  *              name.
  87.  */
  88.  
  89. struct attach *noteattach(filename)
  90.      char *filename;
  91. {
  92.     char *base = basename(filename), *path = pathname(filename);
  93.     char *p;
  94.     int i;
  95.     struct attach *a, *retval;
  96.     
  97.     if (++lastattach >= maxattach) {
  98.     if (!attachments) {    /* first time through */
  99.         maxattach = 4;
  100.         attachments = (struct attach *) malloc(maxattach * sizeof (struct attach));
  101.     } else {        /* allocate more */
  102.         maxattach *= 2;
  103.         attachments = (struct attach *) realloc(attachments, maxattach * sizeof (struct attach));
  104.     }
  105.     if (!attachments) {
  106.         perror("malloc");
  107.         fatalerrors++;
  108.         lastattach--;
  109.         return NULL;
  110.     }
  111.     }
  112.     if (!strcmp(base, RTF_NAME)) {
  113.     fprintf(stderr, "Please don't name an attachment %s.\n", RTF_NAME);
  114.     fatalerrors++;
  115.     retval = NULL;
  116.     }
  117.     if (cwd[0] == '\0') {    /* haven't figured out current dir yet */
  118. #ifdef HAVE_GETCWD
  119.     if (getcwd(cwd, sizeof cwd - 2) == NULL) {
  120.         perror("pwd");
  121.         fatalerrors++;
  122.     }
  123. #else
  124.     if (getwd(cwd) == NULL) {
  125.         fprintf(stderr, cwd);
  126.         fatalerrors++;
  127.     }
  128. #endif
  129.     strcat(cwd, "/");    /* add trailing / */
  130.     }
  131.     for (i = 0; i < lastattach; i++) {
  132.     a = &attachments[i];
  133.     if (!strcmp(a->basename, base)) {
  134.         fprintf(stderr, "You cannot have two attachments with the same basename %s\n", base);
  135.         fatalerrors++;
  136.         retval = NULL;
  137.     } else
  138.         retval = a;
  139.     }
  140.     /* 
  141.      * i is the slot for the new attachment. Now determine if it's a 
  142.      * relative path or absolute, and possibly add the current 
  143.      * directory accordingly.
  144.      */
  145.     if (path[0] == '\0')        /* in current directory */
  146.     strcpy(a->pathname, cwd);
  147.     else if (*path == '/')    /* absolute path was given */
  148.     strcpy(a->pathname, path);
  149.     else            /* relative path given */
  150.     sprintf(a->pathname, "%s%s", cwd, path);
  151.     p = &a->pathname[strlen(a->pathname) - 1];
  152.     strcpy(a->basename, base);
  153.     return retval;
  154. }
  155.