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.
This is what I use on Solaris. No doubt someone probably has a better
solution, but it saved me loads of time making global changes to
headers (and other things as well).
syntax: type fandr
Ex: fandr -s bitnet -r xxxxx *
where "bitnet" is the string you want to replace
where "xxxxx" is what you want it replaced with
where "*" is typed inside the directory of files.
--Trish
Trish Forrest, Queen's University, Kingston, ON
---------find and replace
/*
* 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 ProgName[16], FileName[50];
char *CurrentSearchPtr, *Buffer, *BufferPtr;
int Character, BufferLen;
main(int argc, char *argv[])
{
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
strcpy(ProgName, basename(argv[0]));
if (argc != 4) {
fprintf(stderr, "%s filename search_pattern replace_pattern\n", ProgName);
exit(1);}
strcpy(FileName, basename(argv[1]));
if ((Input = fopen(argv[1], "r")) == (FILE *) NULL) {
fprintf(stderr, "ERROR: %s: Could not open %s\n", ProgName, argv[1]);
exit(1);}
sprintf(OutputName, "/tmp/%s.%s.%d", ProgName, FileName, getpid());
if ((Output = fopen(OutputName, "w")) == (FILE *) NULL) {
fprintf(stderr, "ERROR: %s: Could not create %s\n", ProgName, OutputName);
exit(1);}
if ((Buffer = (char *) malloc (strlen(argv[2]) + 1)) == (char *) NULL) {
fprintf(stderr, "ERROR: %s: Insufficient memory\n", ProgName);
exit(1);}
if (stat(argv[1], &FileStat) == -1) {
fprintf(stderr, "ERROR: %s: Could not stat %s\n", ProgName, argv[1]);
exit(-1);
}
CurrentSearchPtr = argv[2];
BufferPtr = Buffer;
while ((Character = fgetc(Input)) != EOF) {
if (Character != *CurrentSearchPtr) {
if (BufferPtr != Buffer) {
*BufferPtr = '\0';
fprintf(Output, "%s", Buffer);}
fprintf(Output, "%c", Character);
CurrentSearchPtr = argv[2];
BufferPtr = Buffer;}
else {
*CurrentSearchPtr++;
*BufferPtr++ = Character;
if (*CurrentSearchPtr == '\0') {
fprintf(Output, "%s", argv[3]);
CurrentSearchPtr = argv[2];
BufferPtr = Buffer;}
}
}
fclose(Input);
fclose(Output);
if ((UnixCmd = (char *) malloc (strlen(OutputName) + strlen(argv[1]) + 8)) == (char *) NULL) {
fprintf(stderr, "ERROR: %s: Insufficient memory to complete operation\n", ProgName);
fprintf(stderr, " Enter the following command at the unix command prompt\n\n");
fprintf(stderr, " mv -f %s %s \n\n", OutputName, argv[1]);
exit(1);}
sprintf(UnixCmd, "mv -f %s %s", OutputName, argv[1]);
system(UnixCmd);
if (chmod(argv[1], FileStat.st_mode) == -1) {
fprintf(stderr, "ERROR: %s: Could not chmod %s to %d perms", ProgName, argv[1], FileStat.st_mode);
exit(-1);
}
if (chown(argv[1], FileStat.st_uid, FileStat.st_gid) == -1) {
fprintf(stderr, "ERROR: %s: Could not chown %s to %d.%d", ProgName, argv[1], FileStat.st_uid, FileStat.st_gid);
exit(-1);
}
}
|