void discover( int y, int x ) { int stack[height*width]; /* Selected tile. */ if( x < 0 || y < 0 || x > width - 1 || y > height - 1 || minefield[y][x].known || minefield[y][x].mine || minefield[y][x].flag ) return; minefield[y][x].known = 1; /* Exit if the tile is not empty. (no mines nearby) */ if( minefield[y][x].neighbors ) return; push( stack, y, x ); /* Scan all nearby tiles. If we meet a tile with a number we just unveil * it. If we meet an empty tile, we push the location in stack. For each * location in stack we do the same thing. (scan again all nearby tiles) */ while( stack_pos ) { /* Pop x, y from stack. */ x = stack[stack_pos--]; y = stack[stack_pos--]; unveil( stack, y-1, x-1 ); unveil( stack, y-1, x ); unveil( stack, y-1, x+1 ); unveil( stack, y, x+1 ); unveil( stack, y+1, x+1 ); unveil( stack, y+1, x ); unveil( stack, y+1, x-1 ); unveil( stack, y, x-1 ); } }
bool discover( int y, int x, bool explore_neighbors ) { /* Selected tile. */ if( x < 0 || y < 0 || x > width - 1 || y > height - 1) return false; if( minefield[y][x].known || minefield[y][x].mine || minefield[y][x].flag ) { if( !minefield[y][x].flag && minefield[y][x].mine ) return true; if( explore_neighbors && minefield[y][x].known && minefield[y][x].neighbors == neighbors_flagged( y, x ) ) { return discover( y-1, x-1, false ) || discover( y-1, x, false ) || discover( y-1, x+1, false ) || discover( y, x-1, false ) || discover( y, x, false ) || discover( y, x+1, false ) || discover( y+1, x-1, false ) || discover( y+1, x, false ) || discover( y+1, x+1, false ); } return false; } minefield[y][x].known = 1; /* Exit if the tile is not empty. (no mines nearby) */ if( minefield[y][x].neighbors ) return false; int stack[height*width]; push( stack, y, x ); /* Scan all nearby tiles. If we meet a tile with a number we just unveil * it. If we meet an empty tile, we push the location in stack. For each * location in stack we do the same thing. (scan again all nearby tiles) */ while( stack_pos ) { /* Pop x, y from stack. */ x = stack[stack_pos--]; y = stack[stack_pos--]; unveil( stack, y-1, x-1 ); unveil( stack, y-1, x ); unveil( stack, y-1, x+1 ); unveil( stack, y, x+1 ); unveil( stack, y+1, x+1 ); unveil( stack, y+1, x ); unveil( stack, y+1, x-1 ); unveil( stack, y, x-1 ); } return false; }
#include "jni_funcs.h" /* ------------------------------------------------------------------------------ * GetField functions * ------------------------------------------------------------------------------ */ jobject KaffeJNI_GetObjectField(JNIEnv* env UNUSED, jobject obj, jfieldID fld) { jobject nobj; jobject obj_local; BEGIN_EXCEPTION_HANDLING(NULL); obj_local = unveil(obj); nobj = KNI_GET_FIELD(jobject, obj_local, fld); ADD_REF(nobj); END_EXCEPTION_HANDLING(); return (nobj); } jboolean KaffeJNI_GetBooleanField(JNIEnv* env UNUSED, jobject obj, jfieldID fld) { jboolean r; jobject obj_local; BEGIN_EXCEPTION_HANDLING(0);
int main(int argc, char *argv[]) { struct kinfo_proc *kp, **kinfo; struct varent *vent; struct winsize ws; dev_t ttydev; pid_t pid; uid_t uid; int all, ch, flag, i, fmt, lineno, nentries; int prtheader, showthreads, wflag, kflag, what, Uflag, xflg; char *nlistf, *memf, *swapf, *cols, errbuf[_POSIX2_LINE_MAX]; setlocale(LC_CTYPE, ""); termwidth = 0; if ((cols = getenv("COLUMNS")) != NULL) termwidth = strtonum(cols, 1, INT_MAX, NULL); if (termwidth == 0 && (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0 || ioctl(STDERR_FILENO, TIOCGWINSZ, &ws) == 0 || ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == 0) && ws.ws_col > 0) termwidth = ws.ws_col - 1; if (termwidth == 0) termwidth = 79; if (argc > 1) argv[1] = kludge_oldps_options(argv[1]); all = fmt = prtheader = showthreads = wflag = kflag = Uflag = xflg = 0; pid = -1; uid = 0; ttydev = NODEV; memf = nlistf = swapf = NULL; while ((ch = getopt(argc, argv, "AaCcegHhjkLlM:mN:O:o:p:rSTt:U:uvW:wx")) != -1) switch (ch) { case 'A': all = 1; xflg = 1; break; case 'a': all = 1; break; case 'C': break; /* no-op */ case 'c': commandonly = 1; break; case 'e': /* XXX set ufmt */ needenv = 1; break; case 'g': break; /* no-op */ case 'H': showthreads = 1; break; case 'h': prtheader = ws.ws_row > 5 ? ws.ws_row : 22; break; case 'j': parsefmt(jfmt); fmt = 1; jfmt[0] = '\0'; break; case 'k': kflag = 1; break; case 'L': showkey(); exit(0); case 'l': parsefmt(lfmt); fmt = 1; lfmt[0] = '\0'; break; case 'M': memf = optarg; break; case 'm': sortby = SORTMEM; break; case 'N': nlistf = optarg; break; case 'O': parsefmt(o1); parsefmt(optarg); parsefmt(o2); o1[0] = o2[0] = '\0'; fmt = 1; break; case 'o': parsefmt(optarg); fmt = 1; break; case 'p': pid = atol(optarg); xflg = 1; break; case 'r': sortby = SORTCPU; break; case 'S': sumrusage = 1; break; case 'T': if ((optarg = ttyname(STDIN_FILENO)) == NULL) errx(1, "stdin: not a terminal"); /* FALLTHROUGH */ case 't': { struct stat sb; char *ttypath, pathbuf[PATH_MAX]; if (strcmp(optarg, "co") == 0) ttypath = _PATH_CONSOLE; else if (*optarg != '/') { int r = snprintf(pathbuf, sizeof(pathbuf), "%s%s", _PATH_TTY, optarg); if (r < 0 || r > sizeof(pathbuf)) errx(1, "%s: too long\n", optarg); ttypath = pathbuf; } else ttypath = optarg; if (stat(ttypath, &sb) == -1) err(1, "%s", ttypath); if (!S_ISCHR(sb.st_mode)) errx(1, "%s: not a terminal", ttypath); ttydev = sb.st_rdev; break; } case 'U': if (uid_from_user(optarg, &uid) == -1) errx(1, "%s: no such user", optarg); Uflag = xflg = 1; break; case 'u': parsefmt(ufmt); sortby = SORTCPU; fmt = 1; ufmt[0] = '\0'; break; case 'v': parsefmt(vfmt); sortby = SORTMEM; fmt = 1; vfmt[0] = '\0'; break; case 'W': swapf = optarg; break; case 'w': if (wflag) termwidth = UNLIMITED; else if (termwidth < 131) termwidth = 131; wflag = 1; break; case 'x': xflg = 1; break; default: usage(); } argc -= optind; argv += optind; #define BACKWARD_COMPATIBILITY #ifdef BACKWARD_COMPATIBILITY if (*argv) { nlistf = *argv; if (*++argv) { memf = *argv; if (*++argv) swapf = *argv; } } #endif if (nlistf == NULL && memf == NULL && swapf == NULL) { kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf); kvm_sysctl_only = 1; } else { kd = kvm_openfiles(nlistf, memf, swapf, O_RDONLY, errbuf); } if (kd == NULL) errx(1, "%s", errbuf); if (unveil(_PATH_DEVDB, "r") == -1 && errno != ENOENT) err(1, "unveil"); if (unveil(_PATH_DEV, "r") == -1 && errno != ENOENT) err(1, "unveil"); if (swapf) if (unveil(swapf, "r") == -1) err(1, "unveil"); if (nlistf) if (unveil(nlistf, "r") == -1) err(1, "unveil"); if (memf) if (unveil(memf, "r") == -1) err(1, "unveil"); if (pledge("stdio rpath getpw ps", NULL) == -1) err(1, "pledge"); if (!fmt) { if (showthreads) parsefmt(tfmt); else parsefmt(dfmt); } /* XXX - should be cleaner */ if (!all && ttydev == NODEV && pid == -1 && !Uflag) { uid = getuid(); Uflag = 1; } /* * scan requested variables, noting what structures are needed, * and adjusting header widths as appropriate. */ scanvars(); if (neednlist && !nlistread) (void) donlist(); /* * get proc list */ if (Uflag) { what = KERN_PROC_UID; flag = uid; } else if (ttydev != NODEV) { what = KERN_PROC_TTY; flag = ttydev; } else if (pid != -1) { what = KERN_PROC_PID; flag = pid; } else if (kflag) { what = KERN_PROC_KTHREAD; flag = 0; } else { what = KERN_PROC_ALL; flag = 0; } if (showthreads) what |= KERN_PROC_SHOW_THREADS; /* * select procs */ kp = kvm_getprocs(kd, what, flag, sizeof(*kp), &nentries); if (kp == NULL) errx(1, "%s", kvm_geterr(kd)); /* * print header */ printheader(); if (nentries == 0) exit(1); /* * sort proc list, we convert from an array of structs to an array * of pointers to make the sort cheaper. */ if ((kinfo = reallocarray(NULL, nentries, sizeof(*kinfo))) == NULL) err(1, "failed to allocate memory for proc pointers"); for (i = 0; i < nentries; i++) kinfo[i] = &kp[i]; qsort(kinfo, nentries, sizeof(*kinfo), pscomp); /* * for each proc, call each variable output function. */ for (i = lineno = 0; i < nentries; i++) { if (showthreads == 0 && (kinfo[i]->p_flag & P_THREAD) != 0) continue; if (xflg == 0 && ((int)kinfo[i]->p_tdev == NODEV || (kinfo[i]->p_psflags & PS_CONTROLT ) == 0)) continue; if (showthreads && kinfo[i]->p_tid == -1) continue; for (vent = vhead; vent; vent = vent->next) { (vent->var->oproc)(kinfo[i], vent); if (vent->next != NULL) (void)putchar(' '); } (void)putchar('\n'); if (prtheader && lineno++ == prtheader - 4) { (void)putchar('\n'); printheader(); lineno = 0; } } exit(eval); }