simple programming examples for unix v5 cc unirubik.c for v5 sleep ----- main(argc, argv) char **argv; { int c, n; char *s; n = 0; if(argc < 2) { printf("arg count\n"); exit(); } s = argv[1]; while(c = *s++) { if(c<'0' || c>'9') { printf("bad character\n"); exit(); } n = n*10 + c - '0'; } sleep(n); } adding longs ------------ main() { int a[2], b[2], c[2]; char *num; a[0] = 1; a[1] = 2; b[0]=0; b[1]=1; ladd (c, a, b); num = locv (c[0], c[1]); printf ("\nThe num is %s", num); } create file ----------- main() { int mf; mf = creat ("test001", 0644); if (mf < 0) { printf ("\nFile not created"); } else { printf ("\nFile created"); } write (mf, "This is a test\n", 15); close(mf); } double to string ---------------- /* convert double to string */ /* dp is decimal point */ /* sig is sign */ main() { double x; char *z; int dp, sig; x = 1000000.1; z = ecvt (x , 8, &dp, &sig); printf ("\nNum is %s", z); printf ("\nDP is %d", dp); } math functions for v5 --------------------- /* examples of math functions for unix v5 to compile: cc math1.c -lc -la */ double sqrt(); double pow(); double gamma(); double exp(); main() { double x,y,z; double a,b,c; double d1, d2; x = 100.00; y = sqrt(x); printf ("\n%.2f is the square root of %.2f", y,x); a = 2.0; b=5.0; c=3.0; z = pow (a,b); printf ("\n%.2f to the power of %.2f equals %.2f", a,b,z); a = gamma (b+1.0); a = exp(a); printf ("\nfactorial of %.2f is %.2f\n", b, a); d1 = 2.0; d2 = 5.0; a = gamma (d2+1.0) - gamma (d1+1.0) - gamma (d2-d1+1.0); a = exp(a); printf ("\n%.0f choose %.0f is %.0f\n", d2, d1, a); } ceil and floor -------------- double ceil(); double floor(); main() { double x; x = ceil(1000.87); printf ("\nCeiling is %f", x); x= floor (1000.77); printf ("\nFloor is %f", x); } multi ----- #define single 0173030 #define reboot 0173040 main() { if (getcsw() !=single) { printf ("\nMulti-user mode"); } else { printf ("\nsingle-user mode"); } printf ("\ngetcsw returns %d", getcsw()); } reset terminal -------------- main() { struct tty { char ispeed,ospeed; char erase,kill; int trash; } tet; gtty(2,&tet); tet.erase = 010; tet.kill = '\@'; tet.trash = 0332; stty(2,&tet); exit(0); /* * * Kurt Shoens * * Very useful after crapping out in raw. */ } inviso ------ #define ECHO 010 struct { int regs[2]; int tflags; } ttyb; int sflags; char str[50]; main() { gtty(0, &ttyb); sflags = ttyb.tflags; ttyb.tflags =& ~ ECHO; stty(0, &ttyb); printf ("\nEnter a string: "); gets(str); printf ("\nYou entered: %s", str); /* return tty to normal */ ttyb.tflags = sflags; stty(0, &ttyb); } get uid ------- main() { int uid; uid = getuid() & 0377; printf ("\nUID: %d", uid); } get gid ------- main() { int gid; gid = getgid(); printf ("\nGID: %d", gid); } get pid ------- main() { int pid; pid = getpid(); printf ("\nPID: %d", pid); } getpw ----- main() { int x; char buf[80]; getpw( 4, buf); printf ("\nuser is %s", buf); } hex to decimal -------------- main() { int c; char buffer[30]; printf ("\nEnter decimal value: "); gets(buffer); c = atoi(buffer); printf ("\nHex: 0x%x\n", c); } cc hex1.c atoi.c gets.c (atoi.c from v6 and gets.c modded from AUSAM) filter ------ main() { int c; while ((c = getchar()) > 0) { putchar(tolower(c)); } return 0; } int tolower(c) int c; { if(c >= 'A' && c <= 'Z') c = c - ('A' - 'a'); return(c); }