9C /

Exits

Last we've learned how write a plan 9 C program, here are going to learn how to exit our program. suppose following program:

#include <u.h>
#include <libc.h>

void
main(void)
{
	print("Hello, world");
}

After running it?, what would be result of $status(aka $? in unix)?

; 8.out
Hello, world; echo $status
8.out 1277638: main

as you can see, it's name of binary, followed by random number, that is PID (process ID) and where program has exited.

let's write another program, which exits elsewhere, to do so we use exits().

#include <u.h>
#include <libc.h>

void
fn(void)
{
	exits("Hello world!");
}

void
main(void)
{
	fn();
}

and run it:

term% 8.out
term% echo $status
8.out 1284312: Hello world!

Rather than classic return in main function, in plan 9 we use exits(), to terminate program.

as an example use case, consider following program, try to understand what it does first, and afterwards read what it does:

#include <u.h>
#include <libc.h>

void
main(void)
{
	char *arch;

	/* arch = $objtype */
	arch = getenv("objtype");
	if(arch == nil)
		exits("No such variable");
	else
		print("%s", arch);
	exits(nil);
}