[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: The Internet-enabled Contiki Desktop OS



It occurred to me that Adam Dunkels wrote in comp.sys.cbm:
> On Tue, 11 Mar 2003 14:10:42 +0000, Martijn van Buul wrote:
>> Such a shame that it's written in ANSI-C, and not K&R C, otherwise I would've
>> been running it on a PDP11..
> 
> Now *that* would be quite nice; it would make Contiki every a few
> years older :-) When was the PDP-11 released? 1975?

The PDP-11/20 and 11/15 saw their first light in 1970. The last model in
1990...

> How much work would it be to convert the existing ANSI C code to K&R?

I don't know, I haven't taken a good look at the sourcecode, yet. There
are some annoying features about K&R which introduces possible problems in
combination with the platform, though. The PDP-11 is a 16-bit platform;
ints are therefore 16-bits too. If a program assumes that ints are 32 bits,
there might be issues. Even worse, if a program assumes that some datatypes
are issues, there might be problems, since K&R doesn't do automatical 
typecasts.

For instance, the seemingly perfectly valid

    fseek(file_descriptor,0,SEEK_SET);

goes terribly wrong - but you won't get any warning or error from that. It'll
just bomb out during runtime. Instead, it should have been

    fseek(file_descriptor,0L,SEEK_SET);

In other words, "it compiles" is not the same as "it works", to put it 
mildly :(

There's a reason why Ansi-C is so much better ;0

> The most evident part is the function prototypes, but these could be
> handled the same way the BSD code does, by using a __P() macro. How
> are function arguments handled? In K&R, these are put after the
> function preamble (or whatever the "int func(int arg)" is called) and
> not inside it.

Yes. A typical main() would look like

int main(argc, argv)
int argc;
char *argv[];
{
    printf("Hello world\n");
}

A correct prototype would look like

main()

;)

A common practice (and I think BSD's __P() does something like that) is
to make your prototypes look like

int main PROTO( (int argc, char *argv[] ) );

with PROTO(x) a macro, defined as

#define PROTO (x)  x

on ANSI-systems, and as

#define PROTO (x)  ()

on K&R systems.

> Perhaps several __P1(), __P2(), etc., could be defined, and these could
> automatically choose the correct "preamble" type. 

Similiarly, for the function definition, you could make your definition
look like

int main DECL2( int, argc, char **, argv)

with DECL2 being defined as

#define DECL2 (t1,v1,t2,v2)  (t1 v1, t2 v2)

on ANSI systems, and

#define DECL2 (t1,v1,t2,v2) (v1 v2) t1 v1; t2 v2;

on K&R systems.

Things get particulary annoying if you have const directives, though :-/

I'll take a peek at the code RSN.

-- 
    Martijn van Buul -  Pino@dohd.org - http://www.stack.nl/~martijnb/
	 Geek code: G--  - Visit OuterSpace: mud.stack.nl 3333
  Research is what I'm doing when I don't know what I'm doing (W. von Braun)