home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume3 / pcmail / part05 / spoolfil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-03  |  3.9 KB  |  155 lines

  1. /*++
  2. /* NAME
  3. /*    spoolfil,metafile 3
  4. /* SUMMARY
  5. /*    create message file and meta file
  6. /* PROJECT
  7. /*    pc-mail
  8. /* PACKAGE
  9. /*    general utilities
  10. /* SYNOPSIS
  11. /*    int spoolfil(mesg,meta_info,mesg_prefix,meta_prefix)
  12. /*    char *mesg;
  13. /*    char *meta_info;
  14. /*    char *mesg_prefix;
  15. /*    char *meta_prefix;
  16. /*
  17. /*    int metafile(string,path)
  18. /*    char *string;
  19. /*    char *path;
  20. /* DESCRIPTION
  21. /*    spoolfil() creates a message, meta file pair in the spool
  22. /*    directory. 
  23. /*
  24. /*    "mesg" should be null-terminated string with the name of an existing 
  25. /*    file. The contents of that file are filtered to produce a
  26. /*    clean ASCII file.
  27. /*
  28. /*    "meta-info" is a string with additional information that is 
  29. /*    written to a meta file (usually name of mail recipient, mail
  30. /*    originator or a comment describing the contents of the message file).
  31. /*
  32. /*    "mesg_prefix," "meta_prefix" are strings that will be used for building
  33. /*    names for files in the spool directory.
  34. /*
  35. /*    metafile() creates a file, writes a string to it and terminates
  36. /*    the file with a newline character. This function is typically used
  37. /*    to create a file with meta information (mail originator, message
  38. /*    summary etc.).
  39. /* FUNCTIONS AND MACROS
  40. /*    ascopen(), ascget(), ascclose(), newseqno()
  41. /* DIAGNOSTICS
  42. /*    A nonzero return value indicates an error condition (see status.h)
  43. /* SEE ALSO
  44. /*    status(5)    return values
  45. /*    ascf(3)        ASCII filter
  46. /* BUGS
  47. /*    Wordprocessor control codes etc will be lost when spoolfil copies 
  48. /*    a file.
  49. /* AUTHOR(S)
  50. /*    W.Z. Venema
  51. /*    Eindhoven University of Technology
  52. /*    Department of Mathematics and Computer Science
  53. /*    Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  54. /* CREATION DATE
  55. /*    Mon May 18 18:45:10 GMT+1:00 1987
  56. /* LAST MODIFICATION
  57. /*    Mon Apr  4 23:50:14 MET 1988
  58. /* VERSION/RELEASE
  59. /*    1.3
  60. /*--*/
  61.  
  62. #include "defs.h"
  63. #include "path.h"
  64. #include "ascf.h"
  65. #include "status.h"
  66.  
  67. /* metafile - create a one-liner file */
  68.  
  69. public int metafile(string,file)
  70. char *string;
  71. char *file;
  72. {
  73.     register FILE *fp;
  74.  
  75.     if ((fp = fopen(file,"w")) == 0) {
  76.     return(E_WRITERR);
  77.     } else if (fprintf(fp,"%s\n",string),ferror(fp)) {
  78.     fclose(fp);
  79.     return(E_WRITERR);
  80.     } else {
  81.     fclose(fp);
  82.     return(0);
  83.     }
  84. }
  85.     
  86. /* spoolfil - make arbitrary spool file */
  87.  
  88. public int spoolfil(fname,meta,msgpfx,auxpfx)
  89. char *fname;
  90. char *meta;
  91. char *msgpfx;
  92. char *auxpfx;
  93. {
  94.     register int newid = newseqno();            /* new message id */
  95.     register int stat;                    /* some status */
  96.     char *msgpath;                    /* new message file */
  97.     char *auxpath;                    /* new meta file */
  98.  
  99.     msgpath = mesg_file(msgpfx,newid);            /* message file path */
  100.     auxpath = meta_file(auxpfx,newid);            /* meta file path */
  101.  
  102.     /* copy disk file to spool file, check for errors */
  103.  
  104.     if (stat = copyfile(fname,msgpath)) {        /* read/write error */
  105.     unlink(msgpath);                /* delete msg file */
  106.     return(stat);                    /* notify caller */
  107.  
  108.     /* create file for meta information, check for errors */
  109.  
  110.     } else if (stat = metafile(meta,auxpath)) {        /* metafile error */
  111.     unlink(msgpath);                /* delete msg file */
  112.     unlink(auxpath);                /* delete meta file */
  113.     return(stat);                    /* notify caller */
  114.  
  115.     /* when nothing went wrong */
  116.  
  117.     } else {
  118.     chmod(msgpath,0444);                /* message read-only */
  119.     chmod(auxpath,0444);                /* metafile read-only */
  120.     return(0);                    /* own error handling */
  121.     }
  122. }
  123.  
  124. /* copyfile - copy a file and filter it */
  125.  
  126. public int copyfile(from,to)
  127. char *from;
  128. char *to;
  129. {
  130.     register FILE *in,*out;            /* file pointers */
  131.     int rerr,werr;                /* error status */
  132.  
  133.     if ((in = ascopen(from,"r")) == 0) {    /* cannot read ?? */
  134.     return(E_READERR);
  135.     } else if ((out = fopen(to,"w")) == 0) {    /* cannot write */
  136.     ascclose(in);
  137.     return(E_WRITERR);
  138.     } else {
  139.     register int c;
  140.     while ((c = ascget(in)) != EOF)        /* ASCII filter */
  141.        putc(c,out);
  142.     rerr = ferror(in);            /* check read status */
  143.     werr = ferror(out);            /* check write status */
  144.     ascclose(in);
  145.     fclose(out);
  146.     if (rerr) {
  147.         return(E_READERR);
  148.     } else if (werr) {
  149.         return(E_WRITERR);
  150.     } else {
  151.         return(0);
  152.     }
  153.     }
  154. }
  155.