stupid question...

119 views
Skip to first unread message

Ville Laitinen

unread,
Apr 16, 2025, 4:58:45 PM4/16/25
to [PiDP-11]
Hi, 

is it expected a static argument value need to be cast to proper format ?

fseek for example: 
...
FILE *file  = fopen("filtest.c", "r");
...
        rewind(file);
        fseek(file, 10, SEEK_SET);
        fseek(file,  -3, SEEK_CUR);
        pos=ftell(file);
        printf("pos %lu\n",pos);

./a.out
pos 4294770689

whereas with values cast to proper format:
...
FILE *file  = fopen("filtest.c", "r");
...
        rewind(file);
        fseek(file,(long)10, SEEK_SET);
        fseek(file,  -3L, SEEK_CUR);
        pos=ftell(file);
        printf("pos %lu\n",pos);

./a.out
pos 7

with lseek however: 
...
int file  = open("filtest.c", O_RDONLY, 0755);
...
        lseek(file,(off_t)0,L_SET);
        pos=lseek(file,10,L_SET);
        /*pos=lseek(file,pos-3, L_SET);*/
        printf("pos %lu\n",pos);

./a.out
pos 10




br,
-V

Clem Cole

unread,
Apr 16, 2025, 5:14:55 PM4/16/25
to Ville Laitinen, [PiDP-11]
Which system (a.k.a.) which compiler are you referring to?   since you have stdio is likely either  v7, sysii, ultrix or 2.11bsd, but they have different C compilers.

That said how are you declaring everything and yes, make sure that the printf format matches the type of the variable placed in the argument.

--
You received this message because you are subscribed to the Google Groups "[PiDP-11]" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pidp-11+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/pidp-11/CAKktv_mwgdweSo%3D5b_R21VucREjoj%3DG19k8TLG8jvGCPB4EPnA%40mail.gmail.com.

Ville Laitinen

unread,
Apr 16, 2025, 5:35:00 PM4/16/25
to Clem Cole, [PiDP-11]
oops sorry for that.. this is 2.11BSD PL489

180% root--> more filtest.c
/*23456789012345*/
#include<stdio.h>
#include<unistd.h>

int main() {

        FILE *file  = fopen("filtest.c", "r");
        long pos;

        rewind(file);
        fseek(file,(long)10, SEEK_SET);

        pos=ftell(file);
        printf("pos %lu\n",pos);
        rewind(file);
        fseek(file,10,SEEK_SET);
        pos=ftell(file);
        printf("p2s %lu\n",pos);
        fclose(file);
        return 0;
}

.. 
182% root--> cc filtest.c
183% root--> ./a.out
pos 10
p2s 655360



br,
-V

Warner Losh

unread,
Apr 16, 2025, 5:36:01 PM4/16/25
to Ville Laitinen, [PiDP-11]
K&R compilers need to cast anything not an int for args. Lint is supposed to find these issues.

ANSI is supposed to fix that, but you need all the right headers included.

Warner

Ville Laitinen

unread,
Apr 16, 2025, 5:47:37 PM4/16/25
to [PiDP-11]
Mike Katz - this was just to demonstrate the "issue" .. checking for errors is obviously a good thing :) 

-V

Ville Laitinen

unread,
Apr 16, 2025, 5:55:04 PM4/16/25
to [PiDP-11]
no, this is not about printf formatting... 

196% root--> cat filtest.c

/*23456789012345*/
#include<stdio.h>
#include<unistd.h>

int main() {
        FILE *file  = fopen("filtest.c", "r");
        long pos;

        rewind(file);
        fseek(file,(long)10, SEEK_SET);
        pos=ftell(file);
        printf("pos %ld\n",pos);
        rewind(file);
        fseek(file,10,SEEK_SET);
        pos=ftell(file);
        printf("p2s %ld\n",pos);
        fclose(file);
        return 0;
}
197% root--> ./a.out
pos 10
p2s 655360



On Thu, Apr 17, 2025 at 12:44 AM Clem Cole <cl...@ccc.com> wrote:
The issue here is that  ftell(3) returns a  long and you told printf  to look for an unsigned long in the format.
The trying casting the variable the an unsigned long and see what it does.

Ville Laitinen

unread,
Apr 16, 2025, 6:03:22 PM4/16/25
to [PiDP-11]
yeah, so i guess "don't be lazy" is the answer :) 

(what really did confuse me is lseek will work ok without casting the argument to proper format.. maybe because it is a syscall..) 

-V

Clem Cole

unread,
Apr 16, 2025, 6:08:25 PM4/16/25
to Ville Laitinen, [PiDP-11]
The problem is the 10 in the the seek line... that needs to be 10L

Clem Cole

unread,
Apr 16, 2025, 6:18:27 PM4/16/25
to Ville Laitinen, [PiDP-11]
The issue is simple, "ints" are 16 bits in the PDP-11 compiler.  So if you want a 32 bits for the size, you need to add the L suffix.   The older compilers were not very good at matching types of parameters passed and defined.  That was an ANSI change where strong typing was added.  The compiler has know way of knowing what fseek needs, you put 10 as the parameter, which was a10 bit value, followed by the SEEK_SET macro which happens to be 0 and you probably had another something else that I bet caused ftell tofail and you missed the actually error and interretered as the seek pointer.

Warner Losh

unread,
Apr 16, 2025, 6:33:05 PM4/16/25
to Clem Cole, Ville Laitinen, [PiDP-11]
Even with the prototype in scope from stdio.h?

Anton Lavrentiev

unread,
Apr 16, 2025, 6:48:48 PM4/16/25
to Warner Losh, Clem Cole, Ville Laitinen, [PiDP-11]
That was one of the reasons C got prototypes.  In K&R C, whatever looks like an int will be passed as an int.  So, the fseek() call ends up with only one word pushed onto the stack (instead of 2 words that fseek() expects).  With prototypes in place, the problem should go away, as the compiler knows that "10" needs to be pushed as a two-word entity representing the value 10.  The story is a bit different for variadic calls, such as printf().

Three Jeeps

unread,
Apr 16, 2025, 7:41:27 PM4/16/25
to [PiDP-11]
I think part of the answer is that it depends on the compiler you are using.  Perhaps try an experiment with/wo the casting and see how it behaves?
I am a bit neurotic in situations like this and tend to be specific where I am unsure of the results.  Also helps with portability, no?

Johnny Billquist

unread,
Apr 16, 2025, 10:47:25 PM4/16/25
to pid...@googlegroups.com
The problem is this:

simh:bqt/cctest> grep fseek /usr/include/*
simh:bqt/cctest> grep ftell /usr/include/*
/usr/include/stdio.h:long ftell();
simh:bqt/cctest>

Basically, you don't have any proper prototypes for these functions.
This is true for a whole lot of functions.

If you do this:

#include <stdio.h>
#include <unistd.h>

void fseek(FILE *, off_t, int);
off_t ftell(FILE *);

int main(int argc, char *argv[])
{
FILE *file = fopen("filtest.c", "r");
off_t pos;

rewind(file);
fseek(file, 10, SEEK_SET);
fseek(file, -3, SEEK_CUR);
pos=ftell(file);
printf("pos %lu\n",pos);
fclose(file);
}


Then it will generate the right code without any castings.

However, the compiler does accept functions with no proper prototypes,
at which point it just does the standard K&R thing, and assumes any
argument is an int, unless known to be something else.

Johnny
> --
> You received this message because you are subscribed to the Google
> Groups "[PiDP-11]" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to pidp-11+u...@googlegroups.com
> <mailto:pidp-11+u...@googlegroups.com>.
> To view this discussion visit https://groups.google.com/d/msgid/pidp-11/
> CAKktv_mwgdweSo%3D5b_R21VucREjoj%3DG19k8TLG8jvGCPB4EPnA%40mail.gmail.com
> <https://groups.google.com/d/msgid/pidp-11/
> CAKktv_mwgdweSo%3D5b_R21VucREjoj%3DG19k8TLG8jvGCPB4EPnA%40mail.gmail.com?utm_medium=email&utm_source=footer>.

--
Johnny Billquist || "I'm on a bus
|| on a psychedelic trip
email: b...@softjar.se || Reading murder books
pdp is alive! || tryin' to stay hip" - B. Idol

Ville Laitinen

unread,
Apr 17, 2025, 2:30:15 PM4/17/25
to pid...@googlegroups.com
Thanks for the explanation, makes perfect sense. 


br,
-V

To unsubscribe from this group and stop receiving emails from it, send an email to pidp-11+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/pidp-11/bd387ea4-53dd-425d-b2da-fe2ea39ccae5%40softjar.se.

Warner Losh

unread,
Apr 17, 2025, 2:40:52 PM4/17/25
to Ville Laitinen, [PiDP-11]
Maybe we need to create a patch to add prototypes to the system headers...

Warner

Ville Laitinen

unread,
Apr 17, 2025, 2:50:57 PM4/17/25
to [PiDP-11]
On Thu, Apr 17, 2025 at 9:40 PM Warner Losh <i...@bsdimp.com> wrote:
Maybe we need to create a patch to add prototypes to the system headers...


That would be nice, man pages probably also will require attention - 

for 'fseek' it says stdio.h .. but the 'whence' flags SEEK_XXX are actually in unistd.h

similarly with 'lseek' the man page refers to sys/file.h - which does have the 
older L_SET/L_INCR/L_XTND flags 
.. but the prototype and the SEEK_XXX flags are still in unistd.h 

br,
-V

Ville Laitinen

unread,
Apr 17, 2025, 2:58:37 PM4/17/25
to [PiDP-11]

.. but otoh if the programmer does not take shortcuts like me and actually bother to use proper sized variables things will work out just fine too :) 

-V

Henry Bent

unread,
Apr 17, 2025, 3:01:28 PM4/17/25
to Warner Losh, Ville Laitinen, [PiDP-11]
I've been working on this behind the scenes, it's just a very large task. Hopefully I should be done in another month or so. I was initially told that it wasn't a high priority, but I've been doing a lot of cross compiling with GCC which effectively requires correct modern function prototypes.

-Henry

Johnny Billquist

unread,
Apr 17, 2025, 5:52:31 PM4/17/25
to pid...@googlegroups.com
Maybe we should, yes. Ragge is the one who have been doing all the
ansification of the compiler itself, and I think he has plans on more
updates of header files as well, but I don't have any details.

Johnny
> <mailto:pidp-11%2Bunsu...@googlegroups.com>
> > <mailto:pidp-11+u...@googlegroups.com
> <mailto:pidp-11%2Bunsu...@googlegroups.com>>.
> > To view this discussion visit https://groups.google.com/d/
> msgid/pidp-11/ <https://groups.google.com/d/msgid/pidp-11/>
> >
> CAKktv_mwgdweSo%3D5b_R21VucREjoj%3DG19k8TLG8jvGCPB4EPnA%40mail.gmail.com <http://40mail.gmail.com>
> > <https://groups.google.com/d/msgid/pidp-11/ <https://
> groups.google.com/d/msgid/pidp-11/>
> >
> CAKktv_mwgdweSo%3D5b_R21VucREjoj%3DG19k8TLG8jvGCPB4EPnA%40mail.gmail.com?utm_medium=email&utm_source=footer <http://40mail.gmail.com?utm_medium=email&utm_source=footer>>.
>
> --
> Johnny Billquist                 || "I'm on a bus
>                                   ||  on a psychedelic trip
> email: b...@softjar.se <mailto:b...@softjar.se>            ||
> Reading murder books
> pdp is alive!                     ||  tryin' to stay hip" - B. Idol
>
> --
> You received this message because you are subscribed to the
> Google Groups "[PiDP-11]" group.
> To unsubscribe from this group and stop receiving emails from
> it, send an email to pidp-11+u...@googlegroups.com
> <mailto:pidp-11%2Bunsu...@googlegroups.com>.
> To view this discussion visit https://groups.google.com/d/msgid/
> pidp-11/bd387ea4-53dd-425d-b2da-fe2ea39ccae5%40softjar.se
> <https://groups.google.com/d/msgid/pidp-11/bd387ea4-53dd-425d-
> b2da-fe2ea39ccae5%40softjar.se>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "[PiDP-11]" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to pidp-11+u...@googlegroups.com
> <mailto:pidp-11+u...@googlegroups.com>.
> To view this discussion visit https://groups.google.com/d/msgid/
> pidp-11/
> CAKktv_%3DVVR10hQR6n2XkJ%3DAhBe609AFBoA1jWimzq1M25zvgbg%40mail.gmail.com <https://groups.google.com/d/msgid/pidp-11/CAKktv_%3DVVR10hQR6n2XkJ%3DAhBe609AFBoA1jWimzq1M25zvgbg%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "[PiDP-11]" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to pidp-11+u...@googlegroups.com
> <mailto:pidp-11+u...@googlegroups.com>.
> To view this discussion visit https://groups.google.com/d/msgid/pidp-11/
> CANCZdfq8%2BEC66EFVRtmaSCMkejuSdUSPEpSJpUP5oh8fFrabrA%40mail.gmail.com
> <https://groups.google.com/d/msgid/pidp-11/
> CANCZdfq8%2BEC66EFVRtmaSCMkejuSdUSPEpSJpUP5oh8fFrabrA%40mail.gmail.com?

Johnny Billquist

unread,
Apr 18, 2025, 3:37:33 AM4/18/25
to [PiDP-11]
Ragge's comments on the header issues...

Johnny


-------- Forwarded Message --------
Subject: Re: [PiDP-11] stupid question...
Date: Fri, 18 Apr 2025 08:52:11 +0200
From: Anders Magnusson <ra...@tethuvudet.se>
To: Johnny Billquist <b...@softjar.se>, pid...@googlegroups.com

Morning,

the current stdio package in 2.11BSD is not entirely ansi-compliant, so
I have adapted the 4.4BSD stdio code to 2.11.
It has taken quite some time though, and since it affects everything in
the system I want to be careful.

-- Ragge
>>         at which point it just doesthe standard K&R thing, and
>> assumesany
>>         argument is an int, unless known to be something else.
>>
>>             Johnny
>>
>>         On 2025-04-16 22:58, Ville Laitinen wrote:
>>          > Hi,
>>          >
>>          > is it expected a static argument value need to be cast to
>>         proper format ?
>>          >
>>          > fseek for example:
>>          > ...
>>          > FILE *file  =fopen("filtest.c", "r");
>>          > ...
>>          >         rewind(file);
>>          >         fseek(file, 10, SEEK_SET);
>>          >         fseek(file,  -3, SEEK_CUR);
>>          >         pos=ftell(file);
>>          >         printf("pos %lu\n",pos);
>>          >
>>          > ./a.out
>>          > pos 4294770689
>>          >
>>          > whereas with valuescast to proper format:
>>          > ...
>>          > FILE *file  =fopen("filtest.c", "r");
>>          > ...
>>          >         rewind(file);
>>          >         fseek(file,(long)10, SEEK_SET);
>>          >         fseek(file,  -3L, SEEK_CUR);
>>          >         pos=ftell(file);
>>          >         printf("pos %lu\n",pos);
>>          >
>>          > ./a.out
>>          > pos 7
>>          >
>>          > with lseek however:
>>          > ...
>>          > int file  = open("filtest.c", O_RDONLY, 0755);
>>          > ...
>>          >         lseek(file,(off_t)0,L_SET);
>>          >         pos=lseek(file,10,L_SET);
>>          >         /*pos=lseek(file,pos-3, L_SET);*/
>>          >         printf("pos %lu\n",pos);
>>          >
>>          > ./a.out
>>          > pos 10
>>          >
>>          >
>>          >
>>          >
>>          > br,
>>          > -V
>>          >
>>          > --
>>          > You received this message because you are subscribed to the
>>         Google
>>          > Groups "[PiDP-11]" group.
>>          > To unsubscribe fromthis group and stop receiving emails from
Reply all
Reply to author
Forward
0 new messages