home *** CD-ROM | disk | FTP | other *** search
- /*
- * $Filename: ListMail.c $
- * $Revision: 1.6 $
- * $Date: 1994/01/17 16:02:18 $
- *
- * Copyright (C) 1993 by Peter Simons <simons@peti.GUN.de>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * $Id: ListMail.c,v 1.6 1994/01/17 16:02:18 simons Exp simons $
- */
-
-
- /************************************* includes ***********/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <libraries/netsupport.h>
- #include <proto/exec.h>
- #include <proto/dos.h>
- #include <proto/netsupport.h>
-
- /************************************* defines ************/
- #define USAGE "ListMail -- written by Peter Simons <simons@peti.GUN.de>\n" \
- "Usage: ListMail <mailfile address_list_file\n"
- #define MAXFILES 5000
- #define MAXCMDLEN 256
-
- /************************************* prototypes *********/
- char *LoadFile(char *);
- int strbpl(char **pointers, int maxfiles, const char *names);
-
- /************************************* global variables ***/
- static const char __RCSId[] = "$Id: ListMail.c,v 1.6 1994/01/17 16:02:18 simons Exp simons $";
- static const char __DOSVer[] = "\0$VER: ListMail 1.1 (2.1.94)";
- struct NetSupportLibrary *NetSupportBase;
-
- /************************************* program ************/
-
- int main(int argc, char **argv)
- {
- BPTR fh;
- char *buffer, *tmpfile, mailbuf[256], cmdbuf[MAXCMDLEN+1];
- char **addrbuf;
- int len, i, flag, rc = 0;
-
- if (argc != 2 || !stricmp(argv[1], "?") || !stricmp(argv[1], "-h")) {
- Printf(USAGE);
- return 0;
- }
-
- if (!(tmpfile = tmpnam(NULL)) || !(addrbuf = malloc(MAXFILES*4)))
- return 20;
-
- if (!(NetSupportBase = (struct NetSupportLibrary *) OpenLibrary(NETSUPPORTNAME, 0L)))
- return 10;
-
- if (fh = Open(tmpfile, MODE_NEWFILE)) {
- while (i = Read(Input(), mailbuf, 256)) {
- if (Write(fh, mailbuf, i) == -1L) {
- rc = 20;
- break;
- }
- }
- Close(fh);
- if (rc)
- return rc;
- }
-
- rc = 20;
- LockFile(argv[1]);
- if (buffer = LoadFile(argv[1])) {
- len = IoErr();
- for (i=0; i < len ; i++)
- if (buffer[i] == '\n')
- buffer[i] = '\0';
-
- if (len = strbpl(addrbuf, MAXFILES, buffer)) {
- sprintf(cmdbuf, "%s <%s", GetConfig(NULL, RMAIL, NULL, RMAIL), tmpfile);
- for (i=0, flag=0; i < len; )
- if (strlen(cmdbuf) + strlen(addrbuf[i]) < MAXCMDLEN-1) {
- strcat(cmdbuf, " ");
- strcat(cmdbuf, addrbuf[i]);
- flag = 1; i++;
- }
- else {
- Printf("%ld: %s\n", strlen(cmdbuf), cmdbuf);
- SystemTagList(cmdbuf, 0L);
- strcpy(cmdbuf, "RMail <");
- strcat(cmdbuf, tmpfile);
- flag = 0;
- }
- if (flag) {
- Printf("%ld: %s\n", strlen(cmdbuf), cmdbuf);
- SystemTagList(cmdbuf, 0L);
- rc = 0;
- }
- }
- free(buffer);
- }
- else
- PrintFault(IoErr(), "ListMail");
- UnLockFile(argv[1]);
-
- free(addrbuf); free(tmpfile);
- DeleteFile(tmpfile);
- CloseLibrary((struct Library *) NetSupportBase);
-
- return rc;
- }
-
-
-
- /*
- * LoadFile() -- determines the length of a given file, allocates the
- * appropiate buffer and actually loads the file. If everything works,
- * the address of the buffer is returned and the length of the file is
- * available via IoErr().
- *
- * ATTENTION: A zero-byte is appended to the loaded file, to enable the
- * usage of standard C string routines on the contents.
- *
- * The buffer should be freed after usage using free().
- */
-
- char *LoadFile(char *filename)
- {
- BPTR fh, fh2;
- struct FileInfoBlock *fib;
- char *memblock, *filebuffer = NULL;
- BOOL isfile = FALSE;
- int readlen;
-
- if (fh = Lock(filename, ACCESS_READ)) {
- if (fib = malloc(sizeof(struct FileInfoBlock))) {
- if (Examine(fh, fib)) {
- if (fh2 = OpenFromLock(fh)) {
- isfile = TRUE;
- if (memblock = malloc(fib->fib_Size + 1)) {
- if ((readlen = Read(fh2, memblock, fib->fib_Size)) != -1L) {
- memblock[readlen] = '\0';
- filebuffer = memblock;
- SetIoErr(readlen);
- }
- else
- free(memblock);
- }
- Close(fh2);
- }
- }
- free(fib);
- }
- if (!(isfile))
- UnLock(fh);
- }
-
- return filebuffer;
- }
-
-
-
- /*
- * Convert a line of several strings into an array of pointers. The
- * equivalent routine in the SAS/C 6.50 library seems to be broken.
- *
- * BTW, this routine will produce a const->volatile conversation warning.
- * Don't care, the >names< string is NOT modified and this version is
- * faster than working with offsets (e.g. names[i]) all the time.
- * -peter
- */
-
- int strbpl(char **pointers, int maxfiles, const char *names)
- {
- int count = 0;
-
- while(*names && count < maxfiles-1) {
- pointers[count++] = names;
- while(*(names++))
- ;
- }
-
- pointers[count] = NULL;
- return count;
- }
-
-
-