+getw(loc)
+ unsigned long loc;
+{
+ int word;
+
+ klseek(kmem, (long)loc, 0);
+ if (read(kmem, (char *)&word, sizeof (word)) != sizeof (word))
+ printf("error reading kmem at %x\n", loc);
+ return (word);
+}
+
+klseek(fd, loc, off)
+ int fd;
+ long loc;
+ int off;
+{
+ if (kflg) {
+ if ((loc = vtophys(loc)) == -1)
+ return;
+ }
+ (void) lseek(fd, (long)loc, off);
+}
+
+writepsdb(unixname)
+ char *unixname;
+{
+ register FILE *fp;
+ struct lttys *lt;
+
+ setgid(getgid());
+ setuid(getuid());
+ if ((fp = fopen(psdb, "w")) == NULL) {
+ perror(psdb);
+ exit(1);
+ } else
+ fchmod(fileno(fp), 0644);
+ fwrite((char *) &nllen, sizeof nllen, 1, fp);
+ fwrite((char *) nl, sizeof (struct nlist), nllen, fp);
+ fwrite((char *) cand, sizeof (cand), 1, fp);
+ fwrite((char *) &nttys, sizeof nttys, 1, fp);
+ for (lt = lallttys ; lt ; lt = lt->next)
+ fwrite((char *)<->ttys, sizeof (struct ttys), 1, fp);
+ fwrite((char *) &nchans, sizeof nchans, 1, fp);
+ fwrite((char *) wchanhd, sizeof (struct wchan), nchans, fp);
+ fwrite((char *) wchan_index, sizeof (caddr_t), NWCINDEX, fp);
+ fwrite(unixname, strlen(unixname) + 1, 1, fp);
+ fclose(fp);
+}
+
+readpsdb(unixname)
+ char *unixname;
+{
+ register i;
+ register FILE *fp;
+ char unamebuf[BUFSIZ];
+ char *p = unamebuf;
+ extern int errno;
+
+ if ((fp = fopen(psdb, "r")) == NULL) {
+ if (errno == ENOENT)
+ return (0);
+ perror(psdb);
+ exit(1);
+ }
+
+ fread((char *) &nllen, sizeof nllen, 1, fp);
+ nl = (struct nlist *) malloc (nllen * sizeof (struct nlist));
+ fread((char *) nl, sizeof (struct nlist), nllen, fp);
+ fread((char *) cand, sizeof (cand), 1, fp);
+ fread((char *) &nttys, sizeof nttys, 1, fp);
+ allttys = (struct ttys *)malloc(sizeof(struct ttys)*nttys);
+ if (allttys == NULL) {
+ fprintf(stderr, "ps: Can't malloc space for tty table\n");
+ exit(1);
+ }
+ fread((char *) allttys, sizeof (struct ttys), nttys, fp);
+ fread((char *) &nchans, sizeof nchans, 1, fp);
+ wchanhd = (struct wchan *) malloc(nchans * sizeof (struct wchan));
+ if (wchanhd == NULL) {
+ fprintf(stderr, "ps: Can't malloc space for wait channels\n");
+ nflg++;
+ fseek(fp, (long) nchans * sizeof (struct wchan), 1);
+ } else
+ fread((char *) wchanhd, sizeof (struct wchan), nchans, fp);
+ fread((char *) wchan_index, sizeof (caddr_t), NWCINDEX, fp);
+ while ((*p = getc(fp)) != '\0')
+ p++;
+ return (strcmp(unixname, unamebuf) == 0);
+}
+