ttyname() - question

Paul Vaughan vaughan at mcc.com
Thu Aug 31 00:56:08 AEST 1989


   From: cpcahil at virtech.UUCP (Conor P. Cahill)

   In article <99 at lkbpyr.UUCP>, jonas at lkbpyr.UUCP (Jonas Heyman) writes:
   > Hello,
   > 
   > I was wondering why this work:
   > 

       [ example 1 with ttyname() in main() ]

   > And why this don't:

       [ example 2 with ttyname() in test() called from main() ]

   > 
   > All the above should type out your current 'tty',example: '/dev/tty023'.
   > 

   This is really on of those RTFM kind of things.  ttyname() takes a
   file descriptor as an argument.  Both times you were getting whatever
   value happened to be on the stack at the time the ttyname was called,
   you were lucky the first time (within main) probably because the
   ttyname was called so early that the stack was relatively clean, but
   even this is a bad assumption because other processing has already
   been accomplished in the startup routines before main is called.

Good answer.  I was wondering why the compiler hadn't cited the
appropriate passages of TFM to him.  I guess that's something you give
up if you use <stdio.h> instead of <stream.h>.  I tried it using (the gnu
lib-g++) <stream.h> and got

io.cc:8: warning: type mismatch with previous external decl
/usr/local/gnu/lib/g++-include/std.h:204: warning: previous external
decl of `char *ttyname (int)'

The line number is different because I also bothered to declare test
first to get rid of the implicit declaration warning.  When I found
the argument passing error, I decided the original poster was
completely lost and that it wasn't worth looking at.  However, I think
it's useful to note that using <stream.h> instead of stdio.h would
have caught this one.  Not to mention that using cout instead of
printf would obviated the problem with the %d format specifier and
that the program is very clearly expressed in one line

cout << ttyname(2);


 Paul Vaughan, MCC CAD Program | ARPA: vaughan at mcc.com | Phone: [512] 338-3639
 Box 200195, Austin, TX 78720  | UUCP: ...!cs.utexas.edu!milano!cadillac!vaughan



More information about the Comp.lang.c mailing list