"stat" returning incorrect file size, why?

"stat" returning incorrect file size, why?

Post by Pascal Sartorett » Fri, 13 Feb 1998 04:00:00



Hello,

I use the C function "stat" to get the size of files. Sometimes, it
returns an incorrect file size, generally a few bytes more than
what "ls -l" says. Any idea of what is happening? Hint: such files
are often soft links...

Environment: AIX 4.1.5

Thanks for any help

-----------------------------------------------------------------------
Pascal Sartoretti
ELCA Informatique SA        Phone  : (+41 21) 613 21 11
Avenue de la Harpe 22-24    Fax    : (+41 21) 613 21 00

-----------------------------------------------------------------------

 
 
 

"stat" returning incorrect file size, why?

Post by Herve Couppe de Lahongrais - CD » Fri, 13 Feb 1998 04:00:00



> Hello,

> I use the C function "stat" to get the size of files. Sometimes, it
> returns an incorrect file size, generally a few bytes more than
> what "ls -l" says. Any idea of what is happening? Hint: such files
> are often soft links...

> Environment: AIX 4.1.5

> Thanks for any help

For a symbolic link, you have to use "lstat" instead of "stat" to get
symbolic link status.
From 'man lstat':

The lstat() function has the same effect as stat(), except when path
refers to a symbolic link. In that case lstat() returns information
about the link, while stat() returns information about the file the
link references.

--
Herve Couppe de Lahongrais (SEU)       | Eurocontrol Experimental Centre


 
 
 

"stat" returning incorrect file size, why?

Post by Pascal Sartorett » Fri, 13 Feb 1998 04:00:00




> > Hello,

> > I use the C function "stat" to get the size of files. Sometimes, it
> > returns an incorrect file size, generally a few bytes more than
> > what "ls -l" says. Any idea of what is happening? Hint: such files
> > are often soft links...

> > Environment: AIX 4.1.5

> > Thanks for any help

> For a symbolic link, you have to use "lstat" instead of "stat" to get
> symbolic link status.

No no, I really want to know the _file_ size, which means that I need
to use "stat" and not "lstat". According to "man stat", it should
work...

-----------------------------------------------------------------------
Pascal Sartoretti
ELCA Informatique SA        Phone  : (+41 21) 613 21 11
Avenue de la Harpe 22-24    Fax    : (+41 21) 613 21 00

-----------------------------------------------------------------------

 
 
 

"stat" returning incorrect file size, why?

Post by Andrew Giert » Fri, 13 Feb 1998 04:00:00


[c.l.c removed from followups]


 >>

 >>
 >> > Hello,
 >> >
 >> > I use the C function "stat" to get the size of files. Sometimes, it
 >> > returns an incorrect file size, generally a few bytes more than
 >> > what "ls -l" says. Any idea of what is happening? Hint: such files
 >> > are often soft links...
 >> >
 >> > Environment: AIX 4.1.5
 >> >
 >> > Thanks for any help
 >>
 >> For a symbolic link, you have to use "lstat" instead of "stat" to get
 >> symbolic link status.

 Pascal> No no, I really want to know the _file_ size, which means
 Pascal> that I need to use "stat" and not "lstat". According to "man
 Pascal> stat", it should work...

Give some examples. Either you are misinterpreting what you see
(possible), or there is a bug in your code (possible), or you are
tripping over a most unusual kernel bug (possible but highly unlikely).

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>

 
 
 

"stat" returning incorrect file size, why?

Post by Stephan Wilm » Fri, 13 Feb 1998 04:00:00



> Hello,

> I use the C function "stat" to get the size of files. Sometimes, it
> returns an incorrect file size, generally a few bytes more than
> what "ls -l" says. Any idea of what is happening? Hint: such files
> are often soft links...

Hi Pascal Sartoretti,

There is no "stat" function in the standard C language. But there is
AFAIK a Unix specific language extension to that name. Please ask the
Unix programming experts for specific advice:

Stephan
(initiator of the campaign against grumpiness in c.l.c)

 
 
 

"stat" returning incorrect file size, why?

Post by walter schwendeman » Fri, 13 Feb 1998 04:00:00


There most certainly is a stat function in libc, and is clearly documentated
in the K&R C programming language book.

--
Walter Schwendemann



>> Hello,

>> I use the C function "stat" to get the size of files. Sometimes, it
>> returns an incorrect file size, generally a few bytes more than
>> what "ls -l" says. Any idea of what is happening? Hint: such files
>> are often soft links...

>Hi Pascal Sartoretti,

>There is no "stat" function in the standard C language. But there is
>AFAIK a Unix specific language extension to that name. Please ask the
>Unix programming experts for specific advice:

>Stephan
>(initiator of the campaign against grumpiness in c.l.c)

 
 
 

"stat" returning incorrect file size, why?

Post by Kaz Kylhe » Fri, 13 Feb 1998 04:00:00




>There most certainly is a stat function in libc, and is clearly documentated
>in the K&R C programming language book.

You are confusing ``libc'' with ``The Standard Library of the C Language'',
and ``K&R'' with ``The ANSI/ISO C Language Standard''. Libc is just
a library implementation on your UNIX system which contains a mixture
of standard as well as UNIX-specific functions. K&R are authors who wrote
a book _about_ the C language, not a book which _defines_
the C language.

The second edition of _The C Programming Language_  mentions stat() in a
chapter which about the UNIX programming interface; the authors don't claim
that stat() is actually part of the language.

Conversely, many standard library functions are not even mentioned in K&R2.
This doesn't mean that they aren't standard. For example, none of the
multi-byte character-handling functions such as mblen() or wcstombs() are
mentioned.

Try reading your man pages nearly all the way to the bottom, where a
``STANDARDS CONFORMANCE'' or ``CONFORMING TO'' section may appear. In this
section you may see funny acronyms such as ``ANSI/ISO 9899:1990'', ``XPG4'' or
``POSIX.1'' These are the standards which require the described feature to
exist. Sometimes multiple features are listed separately, each with
its list of standards.

Hint: you will *not* find a mention of ``ISO 9899:1990'' in the man page
for stat. This document happens to be the C language standard. Some older
man pages may have something like ``ANSI C3.159-1989'' instead.

 
 
 

"stat" returning incorrect file size, why?

Post by Martin Ambuh » Fri, 13 Feb 1998 04:00:00



|There most certainly is a stat function in libc, and is clearly
documentated
|in the K&R C programming language book.

Please don't post flame bait to comp.lang.c
There is no standard function named 'stat', and K&R documentation of
the UNIX System Interface hardly counts as documentation of standard C.

 
 
 

"stat" returning incorrect file size, why?

Post by Bob Nelso » Fri, 13 Feb 1998 04:00:00




> >There is no "stat" function in the standard C language. But there is
> >AFAIK a Unix specific language extension to that name.
> There most certainly is a stat function in libc, and is clearly
> documentated in the K&R C programming language book.

My personal preferences regarding operating systems are well-known
in comp.lang.c. Much as I'd like to wishfully _think_ that Unix dictates
the C programming language, it doesn't. The ISO 9899:1990 document is
the standard for the language, not the contents of libc nor the chapter
in K&R2 titled ``The Unix System Interface''.

Therefore, stat is every bit as much standard C as is uname.

--
========================================================================

             http://www.geocities.com/ResearchTriangle/6375
========================================================================

 
 
 

"stat" returning incorrect file size, why?

Post by Andy Knigh » Sat, 14 Feb 1998 04:00:00


In the K&R2 example, where they refer to stat(), they are dealing
specifically with a UNIX implementation. stat() is defined by POSIX and is
therefore portable across POSIX compliant UNIX systems. Unfortunately,
stat() is not an ANSI standard function and does not therefore form part of
the C language.

--
Andy Knight (There's no X in my E-mail address)

>There most certainly is a stat function in libc, and is clearly
documentated
>in the K&R C programming language book.

>--
>Walter Schwendemann



>>> Hello,

>>> I use the C function "stat" to get the size of files. Sometimes, it
>>> returns an incorrect file size, generally a few bytes more than
>>> what "ls -l" says. Any idea of what is happening? Hint: such files
>>> are often soft links...

>>Hi Pascal Sartoretti,

>>There is no "stat" function in the standard C language. But there is
>>AFAIK a Unix specific language extension to that name. Please ask the
>>Unix programming experts for specific advice:

>>Stephan
>>(initiator of the campaign against grumpiness in c.l.c)

 
 
 

"stat" returning incorrect file size, why?

Post by Lawrence Kir » Sat, 14 Feb 1998 04:00:00




Quote:>There most certainly is a stat function in libc,

libc is just a library (that is usually linked in by default) that contains
a general hotch-potch of functions including may (but not necessarily all)
of the standard C library funcitons. Whether a function is in libc or not
has no real bearing on whether it is a standard C library function (I suspect
that most of the functions you'd find in it are not).

Quote:>and is clearly documentated
>in the K&R C programming language book.

Yes, it is documented in Chapter 8 which is entitled "The UNIX system
interface". The introduction to that chapter makes it clear that the
functions described in it are not part of the standard C library.

--
-----------------------------------------


-----------------------------------------

 
 
 

"stat" returning incorrect file size, why?

Post by Barninga » Sat, 14 Feb 1998 04:00:00


Shashamane!
Please look at the signature for my actual email address...


> >> > I use the C function "stat" to get the size of files. Sometimes, it
> >> > returns an incorrect file size, generally a few bytes more than
> >> > what "ls -l" says. Any idea of what is happening? Hint: such files
> >> > are often soft links...

> >> > Environment: AIX 4.1.5

> >> For a symbolic link, you have to use "lstat" instead of "stat" to get
> >> symbolic link status.

> Pascal> No no, I really want to know the _file_ size, which means
> Pascal> that I need to use "stat" and not "lstat". According to "man
> Pascal> stat", it should work...

I wrote and compiled a small program to test this (I run aix 4.1.5
too) and found that stat() returns the same length as ls does for
files, directories and hard links. The file sizes differ on symbolic
links - yes, lstat must be used.

I found some more differences, but they were files that got modified
between the time I gave the ls command and the time I run my test
prog.

HTH

              Kumasee
              Barninga Z!

Email: barninga at usa.net / barninga at geocities.com

Home:  http://barninga.base.org
ZMK:   http://zmk.base.org
Roxy:  http://rossanacasale.base.org

 
 
 

1. Urgent: "lpc stat" and "lpc stat lj02" gives different result

Hi All,

"lpc stat" gives the following:

HPLJ4100|lj02:
        printer is on remote host aaa.bbb.ccc.ddd with name text
        queuing is enabled
        printing is enabled
        no entries
        no daemon present

"lpc stat lj02" gives the following:

lj02:
        printer is on device '/dev/lp' speed -1
        queuing is enabled
        printing is enabled
        no entries
        no daemon present

I copied the printcap over from another Alpha on the same network. I can
ping the "ccc" network.
Printing on the other Alpha works fine, and both have the same Tru64 V4.0f.

Any idea please!
Thanks.

--

2. kt600 mainboard (epox) linux

3. truncated a file with "> file" but size comes back with "ls"

4. Define subject in crontab mails

5. GETSERVBYNAME()????????????????????"""""""""""""

6. helloooo

7. syslog() "returns" "bad file number"

8. control-o pine command problem when telnetting

9. """"""""My SoundBlast 16 pnp isn't up yet""""""""""""

10. elf_begin() returns "get file size" error

11. Incorrect key "translation" behavior for "xterm"

12. Change "return EBLAH" to "return -EBLAH in drivers/*

13. strtod returns incorrect terminating char pointer when converting "Inf"