[comp.unix.aux] Porting MPW C code to A/UX, Can some one give me a hand?

djp862@anu.oz.au ("David J Peterson") (06/06/91)

I'm trying to port some code written in MPW C to A/UX and am having
some trouble with the variable argument declarations in MPW C.

In MPW the function are prototyped as:

function( int, ... )	/* one int, and then none to ? _ints_ */

then in the function definition:

#define PARM	, var
#define DECL	int var;

function( int, PARM )
  int anInt;
  DECL
{
 ...
}


The functions are expecting a list of integers (not a character
string) to be passed as the variable argument list. MPW C has no
problem compiling this, but A/UX cc (and gcc) just chokes with "number
of arguments doesn't match prototype" on the function definition line
(not the prototype).

Replacing the '...' with 'int' in the prototype allows it to compile
under A/UX, but loses the variable argument functionality.

Does anyone know how to kick A/UX into accepting this? (the
-traditional flag in gcc doesn't do it).

Thanks,
-dave.

tony@tui.marcam.dsir.govt.nz (Tony Cooper) (06/06/91)

In article <1991Jun6.071753.4907@newshost.anu.edu.au>, djp862@anu.oz.au
("David J Peterson") writes:
|> I'm trying to port some code written in MPW C to A/UX and am having
|> some trouble with the variable argument declarations in MPW C.
This is easily done in A/UX. The man page for varargs shows how to do
it. I think your problem is that you are using ANSI C whereas A/UX
cc isn't ANSI. I'm not sure because your code does not look like ANSI
either. But if you forget about all that and do it the way the
varargs method does it you should be OK.

Tony Cooper

ksand@apple.com (Kent Sandvik) (06/07/91)

In article <1991Jun6.071753.4907@newshost.anu.edu.au>, djp862@anu.oz.au ("David J Peterson") writes:
> 
> 
> I'm trying to port some code written in MPW C to A/UX and am having
> some trouble with the variable argument declarations in MPW C.
> 
> In MPW the function are prototyped as:
> 
> function( int, ... )	/* one int, and then none to ? _ints_ */
> 
> The functions are expecting a list of integers (not a character
> string) to be passed as the variable argument list. MPW C has no
> problem compiling this, but A/UX cc (and gcc) just chokes with "number
> of arguments doesn't match prototype" on the function definition line
> (not the prototype).
> 
> Does anyone know how to kick A/UX into accepting this? (the
> -traditional flag in gcc doesn't do it).



This is an ANSI C:ish thing, so I guess any gcc flags that turn on 
the ANSI behaviour should parse this context. I guess that both
the ... and the Apple special command-; to generate ... should work,
but I'm not sure about that either so check for that.

Kent

liam@dcs.qmw.ac.uk (William Roberts;) (06/11/91)

In <1991Jun6.071753.4907@newshost.anu.edu.au> djp862@anu.oz.au ("David J 
Peterson") writes:


>In MPW the function are prototyped as:

>function( int, ... )	/* one int, and then none to ? _ints_ */

This is official ANSI notation for a function which takes one or more 
arguments of which the first is definitely an integer.

>then in the function definition:

>#define PARM	, var
>#define DECL	int var;

>function( int, PARM )
>  int anInt;
>  DECL
>{
> ...
>}

This is *NOT* ANSI C. I suspect it goes on to do bizarre things to get at the 
second and subsequent arguments. The correct ANSI C way of defining the 
function would be something like:

#include <stdarg.h>

void myfunction(int fixed, ...)
{
    va_list ap;             /* used to get at the extra arguments */
    int ival1, ival2;
    
    va_start(ap, fixed);    /* make ap point to first unnamed arg */
    switch(fixed) {
    case 2:
       ival1 = va_arg(ap, int);
       ival2 = va_arg(ap, int);
       goto varargs_cleanup;
    /* more cases */
    }
varargs_cleanup:
    va_end(ap);             /* NB. this is MANDATORY */
}

There is an example in the 2nd Edition of K&R, and some more stuff in an 
appendix.


>The functions are expecting a list of integers (not a character
>string) to be passed as the variable argument list. MPW C has no
>problem compiling this, but A/UX cc (and gcc) just choke

As they should - your code has evil MPW-specific hacks in it.
There isn't an easy answer: you will have to pick either A/UX cc or gcc and 
then convert the MPW code to work with your chosen compiler.

I'd be inclined to choose gcc: it is a better compiler and Apple must 
eventually produce an ANSI C compiler as well.
--

% William Roberts                 Internet:  liam@dcs.qmw.ac.uk
% Queen Mary & Westfield College  UUCP:      liam@qmw-dcs.UUCP
% Mile End Road                   Telephone: +44 71 975 5234
% LONDON, E1 4NS, UK              Fax:       +44 81-980 6533

ksand@apple.com (Kent Sandvik) (06/15/91)

In article <3134@redstar.dcs.qmw.ac.uk>, liam@dcs.qmw.ac.uk (William Roberts;) writes:
> 
> As they should - your code has evil MPW-specific hacks in it.
> There isn't an easy answer: you will have to pick either A/UX cc or gcc and 
> then convert the MPW code to work with your chosen compiler.
> 
> I'd be inclined to choose gcc: it is a better compiler and Apple must 
> eventually produce an ANSI C compiler as well.

Yes, we announced our new ANSI C compiler for A/UX early this week,
it's a port of the System V.4 C compiler from AT&T. Yes, we also
have Cfront 2.1 for A/UX now as well. Availability, well, soon!

Regards,
Kent