delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/03/26/16:30:01

From: Nate Eldredge <neldredge AT hmc DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: DJGPP & CGI, I found an easier way!
Date: 26 Mar 2000 12:59:37 -0800
Organization: InterWorld Communications
Lines: 45
Message-ID: <83bt41tpl2.fsf@mercury.st.hmc.edu>
References: <38D7C78E DOT 2708CFD1 AT pgmincorporated DOT com> <38D8529B DOT DDCCEFB AT home DOT com> <38D8D51F DOT BF79B499 AT americasm01 DOT nt DOT com> <38D8EE96 DOT 19ED9CB6 AT home DOT com> <CNXC4.15975$1C2 DOT 394644 AT news20 DOT bellglobal DOT com> <38DCBF45 DOT 4F14B20A AT iperbole DOT bologna DOT it> <fnjpdsktthe2vqiojmc335ndbe49ja9het AT 4ax DOT com> <5lgD4.17851$1C2 DOT 492032 AT news20 DOT bellglobal DOT com> <38DDBA63 DOT 4FE1B9C9 AT home DOT com>
NNTP-Posting-Host: mercury.st.hmc.edu
Mime-Version: 1.0
X-Trace: nntp1.interworld.net 954104480 73818 134.173.45.219 (26 Mar 2000 21:01:20 GMT)
X-Complaints-To: usenet AT nntp1 DOT interworld DOT net
NNTP-Posting-Date: 26 Mar 2000 21:01:20 GMT
User-Agent: Gnus/5.0802 (Gnus v5.8.2) Emacs/20.5
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Robin Johnson <robbat2 AT home DOT com> writes:

> No, your idea has the same flaw as the type command, to get
> functionality like cat you should use the following instead:
> 
> open file as binary;
> test if exist;

This automatically gets done by the open.

> get file size;
> while (current position < size)

I don't think this part is a good idea, or even necessary.  Not all
files have sizes (think devices like con, com1), and even ordinary
files may grow or shrink while you're reading them.  It is sufficient
just to open in binary mode, and stop when EOF is returned.  EOF is
different from every character (including ^Z) so this is safe.

>  { get character from file;
>    if (character <> eof)
>     		output to STDOUT
>     else don't output it 
>    }
> end program;

So I would write:

int c;
FILE *f = fopen(filename, "rb");
if (!f)
        handle_error();
while ((c=getc(f)) != EOF)
        putc(f, stdout);
if (ferror(f))
        handle_error();

It might be interesting to read the source for cat, and to see all the
weird stuff that they do, and why.  (See also GNU Hello for more of
the same ;-)

-- 

Nate Eldredge
neldredge AT hmc DOT edu

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019