Last time I asked my Solaris a question, what is your current runlevel I found no tool that answered it :) So I had to create my own.
Later on somebody told me that actually one can use who command with -r option to find out the runlevel, and I should've guessed, since who is responsible for reading utmp. But the program remains, and if you need to find out the runlevel from your code, you can use this one.
To compile this program with gcc:
$ gcc -o runlevel -O3 -Wall runlevel.c $
To compile with Sun WorkShop:
% /opt/SUNWspro/bin/cc -o runlevel -xO3 runlevel.c %
The program will print some debugging information if you define
If everything goes fine you should see the following:
$ ./runlevel 3 $
The program doesn't need suid privileges except when ordinary users can't read /var/adm/utmp. Anyway the program is safe to be suid.
(C) Pawel S. Veselov, 2001.