home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
MISC
/
stf.lzh
/
renum.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-04-24
|
7KB
|
256 lines
/************************************************************/
/* renum.c */
/************************************************************/
/* this program will renumber a stfortune style quote file. */
/************************************************************/
/* written by Scott McGee 2/27/93 */
/************************************************************/
/* syntax: */
/* renum infile outfile */
/* (if outfile is not specified, stdout is used) */
/* (if infile is not specified, stdin is used) */
/************************************************************/
/* History: */
/* */
/* date who what */
/* 06/21/93 sam added multiline credit handling & code to */
/* protect against infile same as outfile. */
/* 06/22/93 sam added command line options, help, etc */
/************************************************************/
/* Copyright 1993 by Scott McGee. All rights reserved. */
/* */
/* This program is my own work and is protected by */
/* copyright laws. It is provided free of charge on the */
/* condition that it not be altered or sold for profit */
/* without my prior express written permission. If you make */
/* changes you feel improve the program, please send them */
/* to me. If I agree, I will add your changes to my code */
/* (with comments giving you *credit, of course). I can be */
/* reached by email at smcgee@microware.com */
/************************************************************/
/* NOTE TO USERS: if you are compiling this on anything other than an OS-9
system, or a sun3 or sun4, you may have to look through the following
definitions to see if you need to define any from the command line for
your compiler. For instance, if you are compiling on a system that has
no string.h header, you should define NO_STRING_H on the compiler command
line. Also, be sure to modify the default paths found below the defines!
*/
/* define some capabilities dealing with portability issues:
NO_STRING_H system DOES NOT HAVE a string.h header (use strings.h)
*/
#ifndef _UCC /* no Ultra C compiler */
# ifdef OSK /* old cc for osk */
# define NO_STRING_H
# endif
# ifdef _OS9000 /* old cc for os-9000 */
# define NO_STRING_H
# endif
#endif
/*
OSK_HELP system CAN use -? for help (otherwise, use -h)
*/
#ifdef OSK /* OS-9 680x0 system */
# define OSK_HELP
#endif
#ifdef _OS9000 /* OS-9000 system */
# define OSK_HELP
#endif
#ifdef OS9 /* OS-9 6809 system */
# define OSK_HELP
#endif
#include <stdio.h>
#ifndef NO_STRING_H
# include <string.h> /* good, we can use string.h */
#else
# include <strings.h> /* use strings.h and redefine rindex */
# define strrchr(a,b) rindex(a,b)
#endif
#define TRUE 1
#define FALSE 0
#define DEFAULT -1
/* define a global, prog_name, to hold the actual program name */
char *prog_name;
void do_help(){
fprintf(stderr, "\n%s: a program to renumber quote files for StFortune\n\n", prog_name);
fprintf(stderr, "Usage:\n");
fprintf(stderr, " %s (opts) infile outfile\n", prog_name);
fprintf(stderr, " %s (opts) <infile >outfile\n", prog_name);
fprintf(stderr, " %s (opts) infile >outfile\n", prog_name);
fprintf(stderr, " infile and outfile refer to the input file name and\n");
fprintf(stderr, " the output file name respetively. If the input file\n");
fprintf(stderr, " is specified, it must come first. If it is not specified,\n");
fprintf(stderr, " stdin is assumed. Similarly, if no output file is given,\n");
fprintf(stderr, " stdout is assumed. The input file and the output file\n");
fprintf(stderr, " may NOT be the same file!\n\n");
fprintf(stderr, "Options -q : quiet mode\n");
fprintf(stderr, " -v : verbose mode\n");
#ifdef OSK_HELP
fprintf(stderr, " -? : print this help message\n");
#else
fprintf(stderr, " -h : print this help message\n");
#endif
fprintf(stderr, "\n\t(Copyright 1993 by Scott McGee)\n\n");
exit(0);
}
main(argc, argv)
int argc;
char **argv;
{
FILE *inFile;
FILE *outFile;
char *inName = NULL;
char *outName = NULL;
char buff[128];
char buff2[128];
char progbuff[128];
char *ptr;
char *status;
int verbose = DEFAULT;
int count;
int i;
/* set up prog_name */
strcpy(progbuff, argv[0]);
ptr = strrchr(progbuff, '/');
if(ptr != NULL)
prog_name = ptr+1;
else
prog_name = progbuff;
/* parse arguements */
for(i=1;i<argc;i++){
if((argv[i][0] == '-') || (argv[i][0] == '/')){
switch (argv[i][1]){
case '?':
case 'h':
do_help();
case 'q':
verbose = FALSE;
break;
case 'v':
verbose = TRUE;
break;
default :
fprintf(stderr, "%s: unknown option \"%s\"\n", prog_name, argv[i]);
do_help();
}
}else if(inName == NULL){
inName = argv[i];
}else if(outName == NULL){
outName = argv[i];
if(strcmp(inName, outName) == 0){
fprintf(stderr, "%s: ERROR! Input file same as output file\n", prog_name);
fprintf(stderr, "%s: Cannot write to input file!\n", prog_name);
exit(0);
}
}else{
fprintf(stderr, "%s: Too many arguements!\n", prog_name);
exit(0);
}
}
if(inName != NULL){
inFile = fopen(inName, "r");
if(inFile == NULL){
fprintf(stderr, "%s: Couldn't open input file %s!\n", prog_name, inName);
exit(0);
}
}else
inFile = stdin;
if(outName != NULL){
outFile = fopen(outName, "w");
if(outFile == NULL){
fprintf(stderr, "%s: Couldn't open output file %s!\n", prog_name, outName);
exit(0);
}
if(verbose == DEFAULT)
verbose = TRUE;
}else
outFile = stdout;
status = fgets(buff, 128, inFile);
if((verbose == TRUE) && (inName != NULL))
fprintf(stderr, "\nFile %s says %d entries.\n", inName, atoi(buff));
fputs(buff, outFile);
count = 0;
while(status != NULL){
status = fgets(buff, 128, inFile);
if(status != NULL){
sprintf(buff, "%5d", count);
buff[5] = '%';
fputs(buff, outFile);
if(strncmp(buff+6, " --", 6) == 0)
count++;
}
}
count--;
if(verbose == TRUE)
fprintf(stderr, "%d actual entries.\n", count);
if(argc > 2){
fclose(outFile);
outFile = fopen(outName, "w");
fseek(inFile, 0, 0);
status = fgets(buff, 128, inFile);
sprintf(buff2, "%d", count);
count = 0;
ptr = buff;
while(isdigit(*ptr))
ptr++;
strcat(buff2, ptr);
fputs(buff2, outFile);
while(status != NULL){
status = fgets(buff, 128, inFile);
if(status != NULL){
if(strncmp(buff+5, "% --", 7) == 0){
sprintf(buff, "%5d", count);
buff[5] = '%';
fputs(buff, outFile);
count++;
}else if(strncmp(buff+5, "\\ --", 7) == 0){ /* contunuation */
sprintf(buff, "%5d", count);
buff[5] = '\\';
fputs(buff, outFile);
status = fgets(buff, 128, inFile);
if(status != NULL){
sprintf(buff, "%5d", count);
buff[5] = '%';
fputs(buff, outFile);
}
count++;
}else{
sprintf(buff, "%5d", count);
buff[5] = '%';
fputs(buff, outFile);
}
}
}
}
}