LSTSRV-L Archives

LISTSERV Site Administrators' Forum

LSTSRV-L

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Topic: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Listserv Admin <[log in to unmask]>
Mon, 23 Feb 1998 14:43:25 -0500
TEXT/PLAIN (203 lines)
On Sun, 22 Feb 1998, Roger Fajman wrote:

> Has anyone written a program for doing mass changes to list headers?
> We are using LISTSERV on Solaris.

   I would like to answer this question again, only *this* time
I promise to insert the correct version of the program.  I apologize
that the first little c thing didn't support wildcards... it was an
earlier version.  Thank you to those who actually tried it and let me
know it didn't work. Below is the correct version.

--Trish
---------
Trish Forrest, Queen's University Kingston On
-------------------------
/*
 *  Compile as -
 *              cc -o fandr fandr.c -lgen [-DDEBUG]
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <libgen.h>
#include <sys/stat.h>
#include <sys/types.h>

FILE *Input, *Output;
char OutputName[256], *UnixCmd;
char *ProgramName;
char *CurrentSearchPtr, *Buffer, *BufferPtr;

int     Character,
        BufferLen,
        Pid,
        Verbose = 0;

char    *Replace = (char *) NULL,
        *Search = (char *) NULL;


display_help()
{
   fprintf(stderr, "\nUSAGE: %s -s SearchPattern -r ReplacePattern file1 [file2 .....]\n", ProgramName);
   fprintf(stderr, "\t-r The pattern to search for.\n");
   fprintf(stderr, "\t-s The pattern to replace what you found by the -s.\n");
   fprintf(stderr, "\nThe pattern MUST be in quotes if it is more than one workd.\n\n");
   exit(0);
}

close_up(char *OutputName)
{
   if (Input != (FILE *) NULL) {
      fclose(Input);
      Input = (FILE *) NULL;
   }

   if (Output != (FILE *) NULL) {
      fclose(Output);
      Output = (FILE *) NULL;
      if (OutputName != (char *) NULL)
         remove(OutputName);
   }
}

process_file(char *Filename)
{
   char *ShortName;
   struct stat FileStat;

   ShortName = basename(Filename);
   if ((Input = fopen(Filename, "r")) == (FILE *) NULL) {
      fprintf(stderr, "ERROR: %s: Could not open %s\n", ProgramName, Filename);
      return;
   }

   if (Verbose)
      printf("Checking file: %s (%s)\n", Filename, ShortName);

   sprintf(OutputName, "/tmp/%s.%s.%d", ProgramName, ShortName, Pid);
   if ((Output = fopen(OutputName, "w")) == (FILE *) NULL) {
      fprintf(stderr, "ERROR: %s: Could not create %s\n", ProgramName, OutputName);
      close_up((char *) NULL);
      return;
   }

   if (stat(Filename, &FileStat) == -1) {
      fprintf(stderr, "ERROR: %s: Could not stat %s\n", ProgramName, Filename);
      close_up(OutputName);
      return;
   }

   CurrentSearchPtr = Search;
   BufferPtr = Buffer;

   while ((Character = fgetc(Input)) != EOF) {
      if (Character != *CurrentSearchPtr) {
         if (BufferPtr != Buffer) {
            *BufferPtr = '\0';
            fprintf(Output, "%s", Buffer);
         }
         fprintf(Output, "%c", Character);
         CurrentSearchPtr = Search;
         BufferPtr = Buffer;
      }
      else {
         *CurrentSearchPtr++;
         *BufferPtr++ = Character;
         if (*CurrentSearchPtr == '\0') {
            fprintf(Output, "%s", Replace);
            if (Verbose)
               printf("\tFound and occurrence.\n");
            CurrentSearchPtr = Search;
            BufferPtr = Buffer;
         }
      }
   }

   close_up((char *) NULL);

   if ((UnixCmd = (char *) malloc (strlen(OutputName) + strlen(Filename) + 8)) == (char *) NULL) {
      fprintf(stderr, "ERROR: %s: Insufficient memory to complete operation\n",
              ProgramName);
      fprintf(stderr, "   Enter the following command at the unix command prompt\n\n");
      fprintf(stderr, "        mv -f %s %s \n\n", OutputName, Filename);
      return;
   }

   sprintf(UnixCmd, "mv -f %s %s", OutputName, Filename);
   system(UnixCmd);
   free(UnixCmd);

   if (chmod(Filename, FileStat.st_mode) == -1)
      fprintf(stderr, "ERROR: %s: Could not chmod %s to %d perms",
           ProgramName, Filename, FileStat.st_mode);

   if (chown(Filename, FileStat.st_uid, FileStat.st_gid) == -1)
      fprintf(stderr, "ERROR: %s: Could not chown %s to %d.%d",
           ProgramName, Filename, FileStat.st_uid, FileStat.st_gid);
}

main(int argc, char *argv[])
{
   int Option;
   extern char *optarg;
   extern int optind;
   struct stat FileStat;

#ifdef DEBUG
   int i;

   printf("%d arguments received\n", argc);
   for (i=0; i< argc; i++)
      printf("Argv[%d]: %s\n", i, argv[i]);
   printf("\n");
#endif

   ProgramName = basename(argv[0]);

   while ((Option = getopt(argc, argv, "hr:s:v")) != EOF)
      switch(Option) {
         case 'h':
            display_help();
         case 'r':
            Replace = optarg;
            break;
         case 's':
            Search = optarg;
            break;
         case 'v':
            Verbose++;
            break;
         case '?':
            fprintf(stderr, "%s: ERROR: Unknown option -%c\n", ProgramName, Option);
            display_help();
   }

   if (Replace == (char *) NULL) {
      fprintf(stderr, "%s: ERROR: Missing the replace string.\n", ProgramName);
      display_help();
   }

   if (Search == (char *) NULL) {
      fprintf(stderr, "%s: ERROR: Missing the search string.\n", ProgramName);
      display_help();
   }

   Pid = getpid();

   if (Verbose) {
      printf(" Searching for \"%s\"\n", Search);
      printf("Replacing with \"%s\"\n", Replace);
   }

   if ((Buffer = (char *) malloc (strlen(Search) + 1)) == (char *) NULL) {
      fprintf(stderr, "ERROR: %s: Insufficient memory\n", ProgramName);
      exit(0);
   }

   for ( ; optind < argc; optind++)
       process_file(argv[optind]);
}

ATOM RSS1 RSS2