home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / finger / part02 / kmem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-03  |  3.4 KB  |  162 lines

  1. /*
  2.  * kmem.c -- interact with /dev/kmem, /dev/mem etc.
  3.  *
  4.  * Copyright (C) 1986, 1990  Philip L. Budne
  5.  *
  6.  * This file is part of "Phil's Finger Program".
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 1, or (at your option)
  11.  * any later version.
  12.  *
  13.  */
  14.  
  15. # ifndef lint
  16. static char *rcsid = "$Id: kmem.c,v 3.0 90/07/06 13:11:09 budd Rel $";
  17. # endif /* lint not defined */
  18.  
  19. # include "finger.h"
  20. # if Umax != 42
  21. # include <sys/types.h>
  22. # include "kmem.h"
  23. # include "args.h"            /* sw_debug */
  24.  
  25. /* # define DEBUG            /**/
  26.  
  27. # ifdef sun
  28. # include "info.h"            /* info_[r]consdev */
  29. extern struct info I;            /* namelist values from names.c */
  30.  
  31. # include <sys/stat.h>            /* to stat /dev/console */
  32. GLOBAL dev_t consdev;
  33. GLOBAL dev_t rconsdev;
  34. GLOBAL dev_t dev_console;
  35. # endif /* sun defined */
  36.  
  37. GLOBAL FTYPE kmem;            /* fd for /dev/kmem */
  38. # if SunOS < 400
  39. GLOBAL FTYPE mem;            /* fd for /dev/mem */
  40. # ifdef SWAP_DEVICE
  41. GLOBAL FTYPE drum;            /* fd for /dev/drum */
  42. # endif /* SWAP_DEVICE defined */
  43. # else  /* not SunOS < 400 */
  44. # include <sys/file.h>            /* O_RDONLY */
  45. # include <stdio.h>            /* NULL */
  46. # endif /* not SunOS < 400 */
  47.  
  48.  
  49. GLOBAL int ini_kmem() {
  50. # ifdef sun
  51.     struct stat stb;
  52. # endif /* sun defined */
  53.  
  54. # if SunOS >= 400
  55.     if( (kmem = kvm_open( NULL, NULL, NULL, O_RDONLY, "finger" )) == NULL ) {
  56.     perror("kvm_open");
  57.     return( FALSE );
  58.     }
  59. # else  /* not SunOS >= 400 */
  60.     if( ISBADFILE( (kmem = OPEN("/dev/kmem")) ) ) {
  61.     perror("/dev/kmem");
  62.      kmem = BADFILE;     
  63.     return( FALSE );
  64.     }
  65.  
  66.     if( ISBADFILE( (mem = OPEN("/dev/mem")) ) ) {
  67.     perror("/dev/mem");
  68.     CLOSE(kmem);
  69.     kmem = BADFILE;
  70.     return( FALSE );
  71.     }
  72.  
  73. # ifdef SWAP_DEVICE
  74.     if( ISBADFILE( (drum = OPEN( SWAP_DEVICE )) ) ) {
  75.     perror(SWAP_DEVICE);
  76.     CLOSE(kmem);
  77.     CLOSE(mem);
  78.      kmem = BADFILE;
  79.      return( FALSE );
  80.     }
  81. # endif /* SWAP_DEVICE defined */
  82. # endif /* not SunOS >= 400 */
  83.  
  84.     if( !readnames() )            /* read in info struct */
  85.     return( FALSE );
  86.  
  87. # ifdef sun
  88.     KMEMREAD(I.info_consdev,  &consdev,  sizeof(consdev));
  89.     KMEMREAD(I.info_rconsdev, &rconsdev, sizeof(rconsdev));
  90.     if( stat("/dev/console", &stb ) == 0 )
  91.     dev_console = stb.st_rdev;
  92.     else
  93.     dev_console = 0xffff;        /* makedev(255,255) */
  94. # ifdef DEBUGSW
  95.     if( sw_debug )
  96.     printf("consdev %#x rconsdev %#x /dev/console %#x\n",
  97.            consdev, rconsdev, dev_console );
  98. # endif /* DEBUGSW defined */
  99. # endif /* sun defined */
  100.  
  101.     return( TRUE );
  102. } /* ini_kmem */
  103.  
  104. # if SunOS < 400            /* anything but SunOS 4.x */
  105. GLOBAL int kread(f, pos, buf, count)
  106. FTYPE f;
  107. int count;
  108. unsigned long pos;            /* caddr_t? */
  109. char *buf;
  110. {
  111.     int cc;
  112. # ifdef DEBUG
  113.     char *name;
  114.  
  115.     if( f == mem )
  116.     name = " mem";
  117.     else if( f == kmem )
  118.     name = "kmem";
  119.     else if( f == drum )
  120.     name = "drum";
  121.     else
  122.     name = "??";
  123. # endif /* DEBUG defined */
  124.  
  125.     if( SEEK(f, pos, 0) == -1 ) {
  126.         perror("seek");
  127.     cc = -1;
  128.     }
  129.     else 
  130.     cc = READ(f, buf, count);
  131.  
  132. # if BUFIO
  133.     if( cc > 0 )
  134.     cc *= count;
  135. # endif /* BUFIO */
  136.  
  137. # ifdef DEBUG
  138.     printf("kread: %s@%08x %d(%d) chars\n",
  139.        name,
  140.        pos,
  141.        count,
  142.        cc);
  143. # endif /* DEBUG defined */
  144.  
  145.     if( cc == -1 ) {
  146. # ifdef DEBUG
  147.     perror("read");
  148. # endif /* DEBUG defined */
  149.     return( FALSE );
  150.     } /* bad return */
  151.     return( TRUE );
  152. } /* kread */
  153. # endif /* SunOS < 400 */
  154.  
  155. # endif /* Umax != 42 */
  156.  
  157. /*
  158.  * Local variables:
  159.  * comment-column: 40
  160.  * End:
  161.  */
  162.