All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Bambach <eric@cisu.net>
To: Venkatesh Joshi <joshiv@cisco.com>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: how to implement tail -n
Date: Mon, 24 Jan 2005 00:30:29 -0600	[thread overview]
Message-ID: <200501240030.29947.eric@cisu.net> (raw)
In-Reply-To: <009a01c501d8$6d504cf0$dc846840@apac.cisco.com>

On Sunday 23 January 2005 11:49 pm, you wrote:
> Hi,
>
> I would like to know how to implement "tail -n" in C.
>
> The "-n" option will be used to print the last n lines of a file -
> rather than the default option of printing the last 10 lines of a file.
>
> I wish to do this in a single pass. I can use lseek() to go to the end
> of the file. How to traverse backwards from there ? Is there any
> function that does this ?

Not that I know of. There is no GOOD way to do this in a single pass since the 
lines can be of varying length. If by single pass you meant you want 
performace, then dont read a char at a time, rather just seek to the end of 
the file,  read in about 4K( a reasonable buffer) or so and see if you can 
find (n+1) newlines. If you dont, read in 4 more k and keep going backwards 
till you find them.

P.S. Please dont hi-jack subjects. Type in a new message instead of replying 
to something and deleting the original message; it screws with threading.


> thanks,
> venkatesh
> -
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

-- 
----------------------------------------
--EB

> All is fine except that I can reliably "oops" it simply by trying to read
> from /proc/apm (e.g. cat /proc/apm).
> oops output and ksymoops-2.3.4 output is attached.
> Is there anything else I can contribute?

The latitude and longtitude of the bios writers current position, and
a ballistic missile.

                --Alan Cox LKML-December 08,2000 

----------------------------------------
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  reply	other threads:[~2005-01-24  6:30 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-01-23  6:26 pointer initialization of string constant Krishna Mohan
2005-01-23 12:45 ` Progga
2005-01-23 13:36   ` Krishna Mohan
2005-01-24  5:49     ` how to implement tail -n Venkatesh Joshi
2005-01-24  6:30       ` Eric Bambach [this message]
2005-01-25 14:10         ` Robert Lorentz
2005-01-24 19:26       ` Glynn Clements

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200501240030.29947.eric@cisu.net \
    --to=eric@cisu.net \
    --cc=joshiv@cisco.com \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.