Blob


1 version=pmwiki-2.3.20 ordered=1 urlencoded=1
2 agent=Mozilla/5.0 (X11; OpenBSD amd64; rv:91.0) Gecko/20100101 Firefox/91.0 SeaMonkey/2.53.17.1
3 author=mkf
4 charset=UTF-8
5 csum=
6 ctime=1692186649
7 host=198.251.81.133
8 name=9C.Exits
9 rev=3
10 targets=
11 text=Last we've learned how write a plan 9 C program, here are going to learn how to exit our program. suppose following program:%0a%0a[@%0a#include %3cu.h>%0a#include %3clibc.h>%0a%0avoid%0amain(void)%0a{%0a print("Hello, world");%0a}%0a@]%0a%0aAfter running it?, what would be result of @@$status@@(aka @@$?@@ in unix)?%0a[@%0a; 8.out%0aHello, world; echo $status%0a8.out 1277638: main%0a@]%0a%0aas you can see, it's name of binary, followed by random number, that is PID (process ID) and where program has exited.%0a%0alet's write another program, which exits elsewhere, to do so we use @@exits()@@.%0a[@%0a#include %3cu.h>%0a#include %3clibc.h>%0a%0avoid%0afn(void)%0a{%0a exits("Hello world!");%0a}%0a%0avoid%0amain(void)%0a{%0a fn();%0a}%0a@]%0a%0aand run it:%0a[@%0aterm%25 8.out%0aterm%25 echo $status%0a8.out 1284312: Hello world!%0a@]%0a%0aRather than classic @@return@@ in main function, in plan 9 we use @@exits()@@, to terminate program.%0a%0aas an example use case, consider following program, try to understand what it does first, and afterwards read what it does:%0a[@%0a#include %3cu.h>%0a#include %3clibc.h>%0a%0avoid%0amain(void)%0a{%0a char *arch;%0a %0a /* arch = $objtype */%0a arch = getenv("objtype");%0a if(arch == nil)%0a exits("No such variable");%0a else%0a print("%25s", arch);%0a exits(nil);%0a}%0a@]
12 time=1702912237
13 author:1702912237=mkf
14 diff:1702912237:1702912004:minor=11a12%0a> %0a14c15%0a%3c After running it?, what would be result of @@$status@@(aka @@$?@@ in unix)?%0a---%0a> After running it?, what would be result of @@$status@@(aka @@$?@@)?%0a
15 host:1702912237=198.251.81.133
16 author:1702912004=mkf
17 csum:1702912004=clear last example a bit
18 diff:1702912004:1692186649:=59,63c59,60%0a%3c char *arch;%0a%3c %0a%3c /* arch = $objtype */%0a%3c arch = getenv("objtype");%0a%3c if(arch == nil)%0a---%0a> char* arch = getenv("objtype"); // arch=$objtype in rc%0a> if(arch == 0) // same with if(!arch)%0a68a66%0a> %0a
19 host:1702912004=198.251.81.133
20 author:1692186649=mkf
21 diff:1692186649:1692186649:=1,67d0%0a%3c Last we've learned how write a plan 9 C program, here are going to learn how to exit our program. suppose following program:%0a%3c %0a%3c [@%0a%3c #include %3cu.h>%0a%3c #include %3clibc.h>%0a%3c %0a%3c void%0a%3c main(void)%0a%3c {%0a%3c print("Hello, world");%0a%3c }%0a%3c %0a%3c @]%0a%3c %0a%3c After running it?, what would be result of @@$status@@(aka @@$?@@)?%0a%3c [@%0a%3c ; 8.out%0a%3c Hello, world; echo $status%0a%3c 8.out 1277638: main%0a%3c @]%0a%3c %0a%3c as you can see, it's name of binary, followed by random number, that is PID (process ID) and where program has exited.%0a%3c %0a%3c let's write another program, which exits elsewhere, to do so we use @@exits()@@.%0a%3c [@%0a%3c #include %3cu.h>%0a%3c #include %3clibc.h>%0a%3c %0a%3c void%0a%3c fn(void)%0a%3c {%0a%3c exits("Hello world!");%0a%3c }%0a%3c %0a%3c void%0a%3c main(void)%0a%3c {%0a%3c fn();%0a%3c }%0a%3c @]%0a%3c %0a%3c and run it:%0a%3c [@%0a%3c term%25 8.out%0a%3c term%25 echo $status%0a%3c 8.out 1284312: Hello world!%0a%3c @]%0a%3c %0a%3c Rather than classic @@return@@ in main function, in plan 9 we use @@exits()@@, to terminate program.%0a%3c %0a%3c as an example use case, consider following program, try to understand what it does first, and afterwards read what it does:%0a%3c [@%0a%3c #include %3cu.h>%0a%3c #include %3clibc.h>%0a%3c %0a%3c void%0a%3c main(void)%0a%3c {%0a%3c char* arch = getenv("objtype"); // arch=$objtype in rc%0a%3c if(arch == 0) // same with if(!arch)%0a%3c exits("No such variable");%0a%3c else%0a%3c print("%25s", arch);%0a%3c exits(nil);%0a%3c }%0a%3c %0a%3c @]%0a\ No newline at end of file%0a
22 host:1692186649=2.178.249.170