* pointer initialization of string constant
@ 2005-01-23 6:26 Krishna Mohan
2005-01-23 12:45 ` Progga
0 siblings, 1 reply; 7+ messages in thread
From: Krishna Mohan @ 2005-01-23 6:26 UTC (permalink / raw)
To: LinuxC
Dear All,
Plz see the following code. It gives segmentation fault. ofcourse, it's
right as char *ptr = "abc" sets pointer to the address of the "abc"
string (which may be stored in read-only memory and thus unchangeable).
My doubt : when i run gdb and execute p ptr either after or before
printf, pbc has been displayed. I'm getting segmentation fault only at
the end, it seems, because when i run ./a.out "pbc is not displayed"
but prompted segmentation fault error. why is it so? How does
segmentation fault raises, though pointer-pointing contents has been
changed & even displayed using gdb. Will U plz explain in detail
(internals).
main()
{
char *ptr = "abc";
ptr[0] = 'p';
printf("%s\n",ptr);
}
Thanx in advance.
Krishna Mohan
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: pointer initialization of string constant
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
0 siblings, 1 reply; 7+ messages in thread
From: Progga @ 2005-01-23 12:45 UTC (permalink / raw)
To: linux-c-programming; +Cc: gkmohan
On Sun, Jan 23, 2005 at 11:56:11AM +0530, Krishna Mohan wrote:
>
> main()
> {
> char *ptr = "abc";
> ptr[0] = 'p';
Since ptr points to read-only mem, changing ptr[0] does not seem to be a very
wise idea.
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: pointer initialization of string constant
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
0 siblings, 1 reply; 7+ messages in thread
From: Krishna Mohan @ 2005-01-23 13:36 UTC (permalink / raw)
To: Progga; +Cc: LinuxC
Dear progga
U are right. But my doubt is : when i run gdb and execute p ptr either
just after or before printf, pbc has been displayed. I'm getting
segmentation fault only at the end, it seems, because when i run
./a.out "pbc is not displayed" but prompted segmentation fault error.
why is it so? How does segmentation fault raises, though
pointer-pointing contents has been changed & even displayed using gdb. i
am interested to know the internals.
Thanq for ur interest.
Regards
Krishna Mohan
On Sun, 2005-01-23 at 18:15, Progga wrote:
On Sun, Jan 23, 2005 at 11:56:11AM +0530, Krishna Mohan wrote:
>
> main()
> {
> char *ptr = "abc";
> ptr[0] = 'p';
Since ptr points to read-only mem, changing ptr[0] does not seem to be a very
wise idea.
^ permalink raw reply [flat|nested] 7+ messages in thread* how to implement tail -n
2005-01-23 13:36 ` Krishna Mohan
@ 2005-01-24 5:49 ` Venkatesh Joshi
2005-01-24 6:30 ` Eric Bambach
2005-01-24 19:26 ` Glynn Clements
0 siblings, 2 replies; 7+ messages in thread
From: Venkatesh Joshi @ 2005-01-24 5:49 UTC (permalink / raw)
To: LinuxC
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 ?
thanks,
venkatesh
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: how to implement tail -n
2005-01-24 5:49 ` how to implement tail -n Venkatesh Joshi
@ 2005-01-24 6:30 ` Eric Bambach
2005-01-25 14:10 ` Robert Lorentz
2005-01-24 19:26 ` Glynn Clements
1 sibling, 1 reply; 7+ messages in thread
From: Eric Bambach @ 2005-01-24 6:30 UTC (permalink / raw)
To: Venkatesh Joshi; +Cc: linux-c-programming
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: how to implement tail -n
2005-01-24 6:30 ` Eric Bambach
@ 2005-01-25 14:10 ` Robert Lorentz
0 siblings, 0 replies; 7+ messages in thread
From: Robert Lorentz @ 2005-01-25 14:10 UTC (permalink / raw)
To: eric; +Cc: Venkatesh Joshi, linux-c-programming
Eric Bambach wrote:
>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
>>
>>
>
>
>
What Eric describes is exactly how it IS implemented in GNU tail. To
see this implementation you can look at the source which can be found in
this tarball -- http://ftp.gnu.org/gnu/coreutils/coreutils-5.2.0.tar.gz
(src/tail.c)
Sometimes looking at source is enlightening, sometimes it is not. In
this particular case the code is extremely well commented and clear, so
you should be able to get some good info fast. The function you're
interested in is 'file_lines(...'
Robert
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: how to implement tail -n
2005-01-24 5:49 ` how to implement tail -n Venkatesh Joshi
2005-01-24 6:30 ` Eric Bambach
@ 2005-01-24 19:26 ` Glynn Clements
1 sibling, 0 replies; 7+ messages in thread
From: Glynn Clements @ 2005-01-24 19:26 UTC (permalink / raw)
To: Venkatesh Joshi; +Cc: LinuxC
Venkatesh Joshi wrote:
> I would like to know how to implement "tail -n" in C.
Use a circular buffer which contains the last N lines read. Each time
you read a line, remove the first line and add the new line to the end
of the buffer. When you reach the end, display the contents of the
buffer.
E.g.
void tail(FILE *fp, int num_lines)
{
char **buffer = calloc(num_lines, sizeof(char *));
int index = 0;
int i;
for (;;)
{
char *line = NULL;
int len = 0;
if (getline(&line, &len, fp) < 0)
break;
if (buffer[index])
free(buffer[index]);
buffer[index] = line;
index++;
index %= num_lines;
}
for (i = 0; i < num_lines; i++)
puts(buffer[(index + i) % num_lines]);
}
In practice, you would probably want to do your own memory allocation
rather than using getline(). You also need to allow for files with
less than num_lines lines..
> 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 ?
Use lseek(SEEK_CUR) with a negative offset to seek backwards. You
would have to use trial and error to determine how much you need to
read.
Also, lseek() will only work on regular files. It won't work on pipes,
etc. The "tail" command works on pipes.
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2005-01-25 14:10 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2005-01-25 14:10 ` Robert Lorentz
2005-01-24 19:26 ` Glynn Clements
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).