home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************
- ******************************* EXTRACT *******************************
- *************************************************************************
-
- -------------------
- Copyright © 1995
- Nick Christie.
- 39 St Georges Drive
- Bransgore BH23 8EZ
- Great Britain.
- All Rights Reserved
- -------------------
-
- Tabsize: 4
-
- Version: $VER: Extract 1.00 (3.12.95)
-
- Author: Nick Christie
-
- E-Mail: nick.christie@oucs.ox.ac.uk
-
- Address: 39 St Georges Drive
- Bransgore BH23 8EZ
- Great Britain.
-
- Keywords: Programming, Shell, CLI, Text, Batch.
-
- Purpose: To find and print to stdout part of a text file, specified
- using either line or byte offset and length.
-
- Requires: Kickstart 2.04+ (V37+).
-
- Distribution: Freeware. Source in C included.
-
- Compiler: SAS/C V6.56
-
- To Make: smake -f Extract.smake
-
- Make sure that the compiler library picks up the amiga lib
- stdio stuff, not the ANSI C versions.
-
- You will need to edit the smakefile if you do not put
- TinyStart.o in LIB:. Note the use of registerized
- parameters and the GST file 'include:all.gst'.
-
- Usage: Extract FILE/A OFFSET/N LENGTH/N BYTES/S
-
- Where: FILE
- is the file to extract a section from.
- OFFSET
- is the 0-based line or byte offset to start at. If the
- offset is past the end of the file, nothing is printed.
- LENGTH
- is the number of lines or bytes to print. If the length
- extends beyond the end of the file, printing stops at EOF.
- BYTES
- is a switch that indicates the offset and length
- are in bytes rather than lines.
-
- Returns: 0 if all went hokey-dokey and the section was printed.
- 10 if the file was not found
- 20 if there was some other error, eg. not enough memory.
-
- Notes: If an error occurs, an error message will be printed to
- stdout. These will always start with "Error:", except for
- the usage message.
-
- *************************************************************************/
-
- /************************************************************************
- ****************************** INCLUDES *******************************
- ************************************************************************/
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <exec/memory.h>
- #include <dos/dos.h>
- #include <dos/stdio.h>
- #include <dos/dostags.h>
- #include <proto/exec.h>
- #include <proto/dos.h>
-
- /************************************************************************
- ***************************** DEFINITIONS *****************************
- *************************************************************************/
-
- /*
- * Program name, version, etc
- */
-
- #define PROGNAME "Extract"
- #define PROGVER "1.0"
- #define PROGVERNUM 1
- #define PROGREVNUM 0
- #define PROGDATE __AMIGADATE__
-
- /*
- * Command line parsing template
- */
-
- #define SHELLTEMPLATE "FILE/A,OFFSET/A/N,LENGTH/A/N,BYTES/S"
-
- /*
- * Buffer sizes
- */
-
- #define LINEBUFSIZE 1024
- #define BYTEBUFSIZE 8192
-
- /*
- * Indices into arg array
- */
-
- enum InitArgs {
- ARG_FILE=0,
- ARG_OFF,
- ARG_LEN,
- ARG_BYTES,
- ARG_TOTAL
- };
-
- /************************************************************************
- ***************************** REFERENCES ******************************
- ************************************************************************/
-
- /*
- * Data from startup module
- */
-
- extern struct Library *SysBase;
-
- /************************************************************************
- ***************************** PROTOTYPES ******************************
- *************************************************************************/
-
- ULONG main(char *cmdline,ULONG cmdlen);
-
- ULONG ExtractBytes(BPTR fh,ULONG off,ULONG len);
- ULONG ExtractLines(BPTR fh,ULONG off,ULONG len);
-
- /************************************************************************
- ******************************** DATA *********************************
- ************************************************************************/
-
- /*
- * Embedded version string
- */
-
- char VersionStr[] = "\0$VER: " PROGNAME " " PROGVER " " PROGDATE "\0";
-
- /************************************************************************
- ******************************* MAIN() ********************************
- *************************************************************************
- * Program entry point, from TinyStart.o startup module using register
- * parameters: cmdline ptr in a0, cmdline length in d0. Length is 0 if
- * from WB. Returns ULONG DOS result code.
- *
- *************************************************************************/
-
- ULONG main(char *cmdline,ULONG cmdlen)
- {
- struct RDArgs *rda;
- BPTR fh;
- LONG args[ARG_TOTAL];
- ULONG rc,off,len;
-
- rc = RETURN_FAIL;
-
- if (cmdlen && (SysBase->lib_Version >= 37))
- {
- memset(args,0,sizeof(args));
-
- if (rda = ReadArgs(SHELLTEMPLATE,args,NULL))
- {
- rc = RETURN_ERROR;
-
- if (fh = Open((char *)args[ARG_FILE],MODE_OLDFILE))
- {
- off = *((ULONG *)args[ARG_OFF]);
- len = *((ULONG *)args[ARG_LEN]);
-
- if (args[ARG_BYTES])
- rc = ExtractBytes(fh,off,len);
- else
- rc = ExtractLines(fh,off,len);
-
- Close(fh);
- }
- else
- PrintFault(IoErr(),"Error");
-
- FreeArgs(rda);
- }
- else
- PrintFault(IoErr(),"Error");
- }
-
- return(rc);
- }
-
- /************************************************************************
- *************************** EXTRACTBYTES() ****************************
- *************************************************************************
- * Print len bytes to stdout from file whose handle is supplied, starting
- * at byte offset off. Returns RETURN_OK if all went well, RETURN_FAIL
- * on error.
- *
- *************************************************************************/
-
- ULONG ExtractBytes(BPTR fh,ULONG off,ULONG len)
- {
- char *buf;
- BPTR outfh;
- ULONG rc,get,got;
- LONG err;
-
- rc = RETURN_FAIL;
- outfh = Output();
-
- Seek(fh,off,OFFSET_BEGINNING);
-
- if (IoErr())
- PrintFault(IoErr(),"Error");
- else
- {
- if (buf = AllocMem(BYTEBUFSIZE,MEMF_PUBLIC|MEMF_ANY))
- {
- get = BYTEBUFSIZE;
- err = 0;
-
- while(len && !err)
- {
- if (get > len)
- get = len;
- len -= get;
- got = Read(fh,buf,get);
- if (got != -1)
- {
- if (got)
- {
- if (Write(outfh,buf,got) == -1)
- err = IoErr();
- }
- else
- len = 0;
- }
- else
- err = IoErr();
- }
-
- if (err)
- PrintFault(err,"Error");
- else
- rc = RETURN_OK;
-
- FreeMem(buf,BYTEBUFSIZE);
- }
- else
- PrintFault(ERROR_NO_FREE_STORE,"Error");
- }
-
- return(rc);
- }
-
- /************************************************************************
- *************************** EXTRACTLINES() ****************************
- *************************************************************************
- * Print len lines to stdout from file whose handle is supplied, starting
- * at line number off. Returns RETURN_OK if all went well, RETURN_FAIL
- * on error.
- *
- *************************************************************************/
-
- ULONG ExtractLines(BPTR fh,ULONG off,ULONG len)
- {
- char *buf;
- BPTR outfh;
- LONG err;
- ULONG rc;
-
- rc = RETURN_FAIL;
- outfh = Output();
-
- if (buf = AllocMem(LINEBUFSIZE,MEMF_PUBLIC|MEMF_ANY))
- {
- err = 0;
- rc = RETURN_OK;
-
- while(off && !err)
- {
- if (FGets(fh,buf,LINEBUFSIZE-1))
- off--;
- else
- {
- off = 0;
- err = IoErr();
- }
- }
-
- while(len && !err)
- {
- if (FGets(fh,buf,LINEBUFSIZE-1))
- {
- if (FPuts(outfh,buf))
- err = IoErr();
- len--;
- }
- else
- {
- len = 0;
- err = IoErr();
- }
- }
-
- if (err)
- {
- rc = RETURN_FAIL;
- PrintFault(err,"Error");
- }
-
- FreeMem(buf,LINEBUFSIZE);
- }
- else
- PrintFault(ERROR_NO_FREE_STORE,"Error");
-
- return(rc);
- }
-
-