home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / ListSERV2_3.lha / ListSERV / source / ListMail.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-17  |  7.0 KB  |  199 lines

  1. /*
  2.  *      $Filename: ListMail.c $
  3.  *      $Revision: 1.6 $
  4.  *      $Date: 1994/01/17 16:02:18 $
  5.  *
  6.  *      Copyright (C) 1993 by Peter Simons <simons@peti.GUN.de>
  7.  *
  8.  *      This program is free software; you can redistribute it and/or
  9.  *      modify it under the terms of the GNU General Public License as
  10.  *      published by the Free Software Foundation; either version 2 of
  11.  *      the License, or (at your option) any later version.
  12.  *
  13.  *      This program is distributed in the hope that it will be useful,
  14.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  *      General Public License for more details.
  17.  *
  18.  *      You should have received a copy of the GNU General Public License
  19.  *      along with this program; if not, write to the Free Software
  20.  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  *      $Id: ListMail.c,v 1.6 1994/01/17 16:02:18 simons Exp simons $
  23.  */
  24.  
  25.  
  26. /************************************* includes ***********/
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <libraries/netsupport.h>
  31. #include <proto/exec.h>
  32. #include <proto/dos.h>
  33. #include <proto/netsupport.h>
  34.  
  35. /************************************* defines ************/
  36. #define USAGE "ListMail -- written by Peter Simons <simons@peti.GUN.de>\n" \
  37.               "Usage: ListMail <mailfile address_list_file\n"
  38. #define MAXFILES 5000
  39. #define MAXCMDLEN 256
  40.  
  41. /************************************* prototypes *********/
  42. char *LoadFile(char *);
  43. int strbpl(char **pointers, int maxfiles, const char *names);
  44.  
  45. /************************************* global variables ***/
  46. static const char __RCSId[] = "$Id: ListMail.c,v 1.6 1994/01/17 16:02:18 simons Exp simons $";
  47. static const char __DOSVer[] = "\0$VER: ListMail 1.1 (2.1.94)";
  48. struct NetSupportLibrary *NetSupportBase;
  49.  
  50. /************************************* program ************/
  51.  
  52. int main(int argc, char **argv)
  53. {
  54.         BPTR fh;
  55.         char *buffer, *tmpfile, mailbuf[256], cmdbuf[MAXCMDLEN+1];
  56.         char **addrbuf;
  57.         int len, i, flag, rc = 0;
  58.  
  59.         if (argc != 2 || !stricmp(argv[1], "?") || !stricmp(argv[1], "-h")) {
  60.                 Printf(USAGE);
  61.                 return 0;
  62.         }
  63.  
  64.         if (!(tmpfile = tmpnam(NULL)) || !(addrbuf = malloc(MAXFILES*4)))
  65.                 return 20;
  66.  
  67.         if (!(NetSupportBase = (struct NetSupportLibrary *) OpenLibrary(NETSUPPORTNAME, 0L)))
  68.                 return 10;
  69.  
  70.         if (fh = Open(tmpfile, MODE_NEWFILE)) {
  71.                 while (i = Read(Input(), mailbuf, 256)) {
  72.                         if (Write(fh, mailbuf, i) == -1L) {
  73.                                 rc = 20;
  74.                                 break;
  75.                         }
  76.                 }
  77.                 Close(fh);
  78.                 if (rc)
  79.                         return rc;
  80.         }
  81.  
  82.         rc = 20;
  83.         LockFile(argv[1]);
  84.         if (buffer = LoadFile(argv[1])) {
  85.                 len = IoErr();
  86.                 for (i=0; i < len ; i++)
  87.                         if (buffer[i] == '\n')
  88.                                 buffer[i] = '\0';
  89.  
  90.                 if (len = strbpl(addrbuf, MAXFILES, buffer)) {
  91.                         sprintf(cmdbuf, "%s <%s", GetConfig(NULL, RMAIL, NULL, RMAIL), tmpfile);
  92.                         for (i=0, flag=0; i < len; )
  93.                                 if (strlen(cmdbuf) + strlen(addrbuf[i]) < MAXCMDLEN-1) {
  94.                                         strcat(cmdbuf, " ");
  95.                                         strcat(cmdbuf, addrbuf[i]);
  96.                                         flag = 1; i++;
  97.                                 }
  98.                                 else {
  99.                                         Printf("%ld: %s\n", strlen(cmdbuf), cmdbuf);
  100.                                         SystemTagList(cmdbuf, 0L);
  101.                                         strcpy(cmdbuf, "RMail <");
  102.                                         strcat(cmdbuf, tmpfile);
  103.                                         flag = 0;
  104.                                 }
  105.                         if (flag) {
  106.                                 Printf("%ld: %s\n", strlen(cmdbuf), cmdbuf);
  107.                                 SystemTagList(cmdbuf, 0L);
  108.                         rc = 0;
  109.                         }
  110.                 }
  111.                 free(buffer);
  112.         }
  113.         else
  114.                 PrintFault(IoErr(), "ListMail");
  115.         UnLockFile(argv[1]);
  116.  
  117.         free(addrbuf); free(tmpfile);
  118.         DeleteFile(tmpfile);
  119.         CloseLibrary((struct Library *) NetSupportBase);
  120.  
  121.         return rc;
  122. }
  123.  
  124.  
  125.  
  126.  /*
  127.   * LoadFile() -- determines the length of a given file, allocates the
  128.   * appropiate buffer and actually loads the file. If everything works,
  129.   * the address of the buffer is returned and the length of the file is
  130.   * available via IoErr().
  131.   *
  132.   * ATTENTION: A zero-byte is appended to the loaded file, to enable the
  133.   * usage of standard C string routines on the contents.
  134.   *
  135.   * The buffer should be freed after usage using free().
  136.   */
  137.  
  138. char *LoadFile(char *filename)
  139. {
  140.         BPTR fh, fh2;
  141.         struct FileInfoBlock *fib;
  142.         char *memblock, *filebuffer = NULL;
  143.         BOOL isfile = FALSE;
  144.         int readlen;
  145.  
  146.         if (fh = Lock(filename, ACCESS_READ)) {
  147.                 if (fib = malloc(sizeof(struct FileInfoBlock))) {
  148.                         if (Examine(fh, fib)) {
  149.                                 if (fh2 = OpenFromLock(fh)) {
  150.                                         isfile = TRUE;
  151.                                         if (memblock = malloc(fib->fib_Size + 1)) {
  152.                                                 if ((readlen = Read(fh2, memblock, fib->fib_Size)) != -1L) {
  153.                                                         memblock[readlen] = '\0';
  154.                                                         filebuffer = memblock;
  155.                                                         SetIoErr(readlen);
  156.                                                 }
  157.                                                 else
  158.                                                         free(memblock);
  159.                                         }
  160.                                         Close(fh2);
  161.                                 }
  162.                         }
  163.                         free(fib);
  164.                 }
  165.                 if (!(isfile))
  166.                         UnLock(fh);
  167.         }
  168.  
  169.         return filebuffer;
  170. }
  171.  
  172.  
  173.  
  174.  /*
  175.   * Convert a line of several strings into an array of pointers. The
  176.   * equivalent routine in the SAS/C 6.50 library seems to be broken.
  177.   *
  178.   * BTW, this routine will produce a const->volatile conversation warning.
  179.   * Don't care, the >names< string is NOT modified and this version is
  180.   * faster than working with offsets (e.g. names[i]) all the time.
  181.   *                                                     -peter
  182.   */
  183.  
  184. int strbpl(char **pointers, int maxfiles, const char *names)
  185. {
  186.         int count = 0;
  187.  
  188.         while(*names && count < maxfiles-1) {
  189.                 pointers[count++] = names;
  190.                 while(*(names++))
  191.                         ;
  192.         }
  193.  
  194.         pointers[count] = NULL;
  195.         return count;
  196. }
  197.  
  198.  
  199.