home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2119 < prev    next >
Internet Message Format  |  1990-12-28  |  67KB

  1. From: robert@olsen.UUCP (Robert Ward)
  2. Newsgroups: alt.sources
  3. Subject: SPS for SunOS4.1 and DEC 3100's - Shar 2 of 3
  4. Message-ID: <200@frey.UUCP>
  5. Date: 19 Nov 90 15:43:16 GMT
  6.  
  7.  
  8. #!/bin/sh
  9. #
  10. # This is a shar (shell archive) file.
  11. # In order to extract the contents of this archive, remove everything
  12. # above the "#!/bin/sh" line.  Then execute the remaining file with
  13. # /bin/sh.  The following file(s) will be extracted:
  14. #    filecount.c
  15. #    findtty.c
  16. #    flagdecode.c
  17. #    flags.h
  18. #    flagsetup.c
  19. #    getcmd.c
  20. #    getupage.c
  21. #    globals1.c
  22. #    globals2.c
  23. #    hashuid.c
  24. #    initialise.c
  25. #    initsymbols.c
  26. #    inittty.c
  27. #    main.c
  28. #    mktree.c
  29. #    needed.c
  30. #    openfiles.c
  31. #
  32. #
  33. # This archive was generated on Mon Aug  6 17:53:37 MET DST 1990
  34. #
  35. #
  36. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  37. if [ -f 'filecount.c' -a "${1}" != "-c" ] ; then
  38.     echo shar: Won\'t overwrite existing file "filecount.c"
  39.     exit 2
  40. fi
  41. echo x - filecount.c
  42. sed -e 's/^X//' > filecount.c << '---END-OF-filecount.c---'
  43. X# ifndef lint
  44. Xstatic char SccsId[] =  "@(#)filecount.c    1.2\t7/4/90" ;
  45. X# endif
  46. X
  47. X# include       "sps.h"
  48. X
  49. X/* FILECOUNT - Counts the # open files for the current process */
  50. Xfilecount ( p )
  51. X
  52. Xstruct process         *p ;
  53. X
  54. X{
  55. X    register int            i ;
  56. X    register struct file    **f ;
  57. X    register int            count ;
  58. X    extern union userstate  User ;
  59. X# ifdef SUNOS41
  60. X    /*
  61. X     * The open file list is in User.u_us.u_ofile_arr
  62. X     * if User.u_us.u_ofile points to it; otherwise we'll have
  63. X     * do it the hard way by reading the list from kmem.
  64. X     *
  65. X     * Read the comment to u_ofile in /usr/include/sys/user.h.
  66. X     */
  67. X
  68. X    int            len ;
  69. X    static char        *files = 0 ;
  70. X    static int         files_len = 0 ;
  71. X    extern char        *getcore () ;
  72. X# endif SUNOS41
  73. X
  74. X
  75. X# ifdef SUNOS41
  76. X
  77. X#  ifndef offsetof
  78. X#   define offsetof(type,member)    ((long) &(((type *) 0)->member))
  79. X#  endif offsetof
  80. X
  81. X    if ( (long) User.u_us.u_ofile ==
  82. X        (long) p->pr_p.p_uarea + offsetof(struct user, u_ofile_arr[0]) )
  83. X        f = &User.u_us.u_ofile_arr[ 0 ] ;
  84. X    else
  85. X    {
  86. X        len = User.u_us.u_lastfile * sizeof (struct file *) ;
  87. X        if (len <= 0)
  88. X            return 0;
  89. X        if (files == 0 || len < files_len)
  90. X        {
  91. X            if (files != 0)
  92. X                free (files) ;
  93. X            files = (char *) getcore(len) ;
  94. X            files_len = len ;
  95. X        }
  96. X        if ( getkmem( (long)User.u_us.u_ofile, (char *)files, len)
  97. X        != len )
  98. X            return 0 ;
  99. X        f = (struct file **)files ;
  100. X    }
  101. X    count = 0 ;
  102. X    for ( i = 0 ; i < User.u_us.u_lastfile ; i++ )
  103. X        if ( *f++ )
  104. X            count++ ;
  105. X    return ( count ) ;
  106. X# else SUNOS41
  107. X    count = 0 ;
  108. X    for ( i = 0, f = User.u_us.u_ofile ; i < NOFILE ; i++ )
  109. X        if ( *f++ )
  110. X            count++ ;
  111. X    return ( count ) ;
  112. X# endif SUNOS41
  113. X}
  114. ---END-OF-filecount.c---
  115. LEN=`wc -c < filecount.c`
  116. if [ $LEN !=  1585 ] ; then
  117.     echo shar: File "filecount.c" was $LEN, should have been 1585 bytes
  118. fi
  119. if [ -f 'findtty.c' -a "${1}" != "-c" ] ; then
  120.     echo shar: Won\'t overwrite existing file "findtty.c"
  121.     exit 2
  122. fi
  123. echo x - findtty.c
  124. sed -e 's/^X//' > findtty.c << '---END-OF-findtty.c---'
  125. X# ifndef lint
  126. Xstatic char SccsId[] =  "@(#)findtty.c    1.4\t8/6/90" ;
  127. X# endif
  128. X
  129. X# include       "sps.h"
  130. X# include       <h/ioctl.h>
  131. X# ifdef SUNOS40
  132. X# include       <h/stream.h>
  133. X# else
  134. X# include       <h/tty.h>
  135. X# endif
  136. X# ifdef SUNOS41
  137. X# include    <h/session.h>
  138. X# endif
  139. X
  140. X/* FINDTTY - Attempts to determine to which tty a process is connected */
  141. Xstruct ttyline  *findtty ( p )
  142. X
  143. Xregister struct process         *p ;
  144. X
  145. X{
  146. X    register struct ttyline *lp ;
  147. X    extern struct info      Info ;
  148. X    extern struct ttyline   Notty ;
  149. X# ifdef SUNOS41
  150. X    struct sess        *s ;
  151. X    extern struct sess    *find_session () ;
  152. X# else
  153. X    extern union userstate  User ;
  154. X# endif SUNOS41
  155. X
  156. X
  157. X# ifdef SUNOS41
  158. X    if ( !p->pr_p.p_pgrp || !p->pr_p.p_sessp )
  159. X        return ( &Notty ) ;
  160. X    s = find_session( p->pr_p.p_sessp ) ;
  161. X    if ( s == 0 || s->s_ttyp == 0 )
  162. X        return &Notty;
  163. X    for ( lp = Info.i_ttyline ; lp->l_name[0] ; lp++ )
  164. X        if ( lp->l_dev == s->s_ttyd )
  165. X            return ( lp ) ;
  166. X    /* Kludge from outer space    ++sja */
  167. X    if ( s->s_ttyd == 256 )
  168. X        return &Info.i_ttyline[0] ;
  169. X    return ( &Notty ) ;
  170. X# else
  171. X#  ifdef ULTRIX30
  172. X    if ( !p->pr_p.p_pgrp || !p->pr_p.p_ttyp )
  173. X#  else
  174. X    if ( !p->pr_p.p_pgrp || !User.u_us.u_ttyp )
  175. X#  endif
  176. X        return ( &Notty ) ;
  177. X    for ( lp = Info.i_ttyline ; lp->l_name[0] ; lp++ )
  178. X        if ( lp->l_dev == User.u_us.u_ttyd )
  179. X            return ( lp ) ;
  180. X    return ( &Notty ) ;
  181. X#endif SUNOS41
  182. X}
  183. ---END-OF-findtty.c---
  184. LEN=`wc -c < findtty.c`
  185. if [ $LEN !=  1324 ] ; then
  186.     echo shar: File "findtty.c" was $LEN, should have been 1324 bytes
  187. fi
  188. if [ -f 'flagdecode.c' -a "${1}" != "-c" ] ; then
  189.     echo shar: Won\'t overwrite existing file "flagdecode.c"
  190.     exit 2
  191. fi
  192. echo x - flagdecode.c
  193. sed -e 's/^X//' > flagdecode.c << '---END-OF-flagdecode.c---'
  194. X# ifndef lint
  195. Xstatic char SccsId[] =  "@(#)flagdecode.c    1.1\t10/1/88" ;
  196. X# endif
  197. X
  198. X# include       "sps.h"
  199. X# include       "flags.h"
  200. X
  201. X/* FLAGDECODE - Looks at the argument list and sets various internal switches */
  202. Xflagdecode ( argc, argv )
  203. X
  204. Xregister int                    argc ;
  205. Xregister char                   **argv ;
  206. X
  207. X{
  208. X    register char           *chp ;
  209. X    union flaglist          *plist ;
  210. X    union flaglist          *tlist ;
  211. X    union flaglist          *ulist ;
  212. X    static char             usage[] =
  213. X    "sps - Unknown option %s\nUsage - sps [ -dcefgijkoqrsvwyABFNPSTUWZ ][ process|tty|user ] ...\n";
  214. X    union flaglist          *getflgsp() ;
  215. X    extern struct flags     Flg ;
  216. X
  217. X    plist = tlist = ulist = (union flaglist*)0 ;
  218. X    for ( argv++ ; --argc ; argv++ )
  219. X    {
  220. X        chp = *argv ;
  221. X        while ( *chp )
  222. X            switch ( *chp++ )
  223. X            {
  224. X                case '-' :
  225. X                    /* Separation character */
  226. X                    continue ;
  227. X                case 'c' :
  228. X                case 'C' :
  229. X                    /* Print stored command, not args */
  230. X                    Flg.flg_c = 1 ;
  231. X                    continue ;
  232. X                case 'd' :
  233. X                case 'D' :
  234. X                    /* List disc orientated information */
  235. X                    Flg.flg_d = 1 ;
  236. X                    Flg.flg_v = 0 ;
  237. X                    continue ;
  238. X                case 'e' :
  239. X                case 'E' :
  240. X                    /* List environment strings */
  241. X                    Flg.flg_e = 1 ;
  242. X                    continue ;
  243. X                case 'f' :
  244. X                    /* List the father's process id */
  245. X                    Flg.flg_f = 1 ;
  246. X                    continue ;
  247. X                case 'g' :
  248. X                case 'G' :
  249. X                    /* List the process group id */
  250. X                    Flg.flg_g = 1 ;
  251. X                    continue ;
  252. X                case 'i' :
  253. X                case 'I' :
  254. X                    /* Initialise (super-user only) */
  255. X                    Flg.flg_i = 1 ;
  256. X                    continue ;
  257. X                case 'j' :
  258. X                case 'J' :
  259. X                    /* The next argument specifies the
  260. X                       name of the information file */
  261. X                    if ( argc <= 1 )
  262. X                        prexit(
  263. X          "sps - Name of an information file expected after `-j' flag\n" ) ;
  264. X                    argc-- ;
  265. X                    Flg.flg_j = *++argv ;
  266. X                    continue ;
  267. X                case 'k' :
  268. X                case 'K' :
  269. X                    /* Use a disc file such as /vmcore
  270. X                       rather than /dev/{k}mem for
  271. X                       accessing kernel data. The next
  272. X                       argument specifies the file name. */
  273. X                    if ( argc <= 1 )
  274. X                        prexit(
  275. X           "sps - Name of a memory dump file expected after `-k' flag\n" ) ;
  276. X                    argc-- ;
  277. X                    Flg.flg_k = *++argv ;
  278. X                    Flg.flg_o = 1 ;
  279. X                    continue ;
  280. X                case 'l' :
  281. X                case 'v' :
  282. X                case 'L' :
  283. X                case 'V' :
  284. X                    /* Verbose output */
  285. X                    Flg.flg_d = 0 ;
  286. X                    Flg.flg_v = 1 ;
  287. X                    continue ;
  288. X                case 'o' :
  289. X                case 'O' :
  290. X                    /* Avoid looking at the swap device */
  291. X                    Flg.flg_o = 1 ;
  292. X                    continue ;
  293. X                case 'q' :
  294. X                case 'Q' :
  295. X                    /* Show only the user time, not the
  296. X                       user + system times together. */
  297. X                    Flg.flg_q = 1 ;
  298. X                    continue ;
  299. X                case 'r' :
  300. X                case 'R' :
  301. X                    /* Repeat output every n seconds.
  302. X                       The next argument specifies n which
  303. X                       defaults to 5 if omitted. */
  304. X                    Flg.flg_r = 1 ;
  305. X                    if ( argc > 1 )
  306. X                    {
  307. X                        if ( **++argv >= '0'
  308. X                        && **argv <= '9' )
  309. X                        {
  310. X                            argc-- ;
  311. X                            Flg.flg_rdelay
  312. X                                   = atoi( *argv ) ;
  313. X                            continue ;
  314. X                        }
  315. X                        argv-- ;
  316. X                    }
  317. X                    Flg.flg_rdelay = 0 ;
  318. X                    continue ;
  319. X                case 's' :
  320. X                    /* Next argument specifies a symbol
  321. X                       file rather than the default
  322. X                       /vmunix. */
  323. X                    if ( argc <= 1 )
  324. X                        prexit(
  325. X            "sps - Name of a symbol file expected after `-s' flag\n" ) ;
  326. X                    argc-- ;
  327. X                    Flg.flg_s = *++argv ;
  328. X                    continue ;
  329. X                case 'w' :
  330. X                    /* Wide output, exceeding 79 columns */
  331. X                    Flg.flg_w = 1 ;
  332. X                    continue ;
  333. X                case 'y' :
  334. X                case 'Y' :
  335. X                    /* List current tty information */
  336. X                    Flg.flg_y = 1 ;
  337. X                    continue ;
  338. X                case 'a' :
  339. X                case 'A' :
  340. X                    /* List all processes */
  341. X                    Flg.flg_AZ = 1 ;
  342. X                    Flg.flg_A = 1 ;
  343. X                    continue ;
  344. X                case 'b' :
  345. X                case 'B' :
  346. X                    /* List only busy processes */
  347. X                    Flg.flg_AZ = 1 ;
  348. X                    Flg.flg_B = 1 ;
  349. X                    continue ;
  350. X                case 'F' :
  351. X                    /* List only foreground processes */
  352. X                    Flg.flg_AZ = 1 ;
  353. X                    Flg.flg_F = 1 ;
  354. X                    continue ;
  355. X                case 'n' :
  356. X                case 'N' :
  357. X                    /* No processes, just the summary line*/
  358. X                    Flg.flg_AZ = 1 ;
  359. X                    Flg.flg_N = 1 ;
  360. X                    continue ;
  361. X                case 'p' :
  362. X                case 'P' :
  363. X                    /* List only the given process ids */
  364. X                    Flg.flg_AZ = 1 ;
  365. X                    Flg.flg_P = 1 ;
  366. X                    if ( !plist )
  367. X                       plist=Flg.flg_Plist=getflgsp( argc );
  368. X                    while ( argc > 1 )
  369. X                    {
  370. X                        if ( **++argv == '-' )
  371. X                        {
  372. X                            --argv ;
  373. X                            break ;
  374. X                        }
  375. X                        --argc ;
  376. X                        plist->f_chp = *argv ;
  377. X                        (++plist)->f_chp = (char*)0 ;
  378. X                    }
  379. X                    continue ;
  380. X                case 'S' :
  381. X                    /* List only stopped processes */
  382. X                    Flg.flg_AZ = 1 ;
  383. X                    Flg.flg_S = 1 ;
  384. X                    continue ;
  385. X                case 't' :
  386. X                case 'T' :
  387. X                    /* List only processes attached to the
  388. X                       specified terminals */
  389. X                    Flg.flg_AZ = 1 ;
  390. X                    Flg.flg_T = 1 ;
  391. X                    if ( !tlist )
  392. X                       tlist=Flg.flg_Tlist=getflgsp( argc );
  393. X                    while ( argc > 1 )
  394. X                    {
  395. X                        if ( **++argv == '-' )
  396. X                        {
  397. X                            --argv ;
  398. X                            break ;
  399. X                        }
  400. X                        --argc ;
  401. X                        tlist->f_chp = *argv ;
  402. X                        (++tlist)->f_chp = (char*)0 ;
  403. X                    }
  404. X                    continue ;
  405. X                case 'u' :
  406. X                case 'U' :
  407. X                    /* List only processes belonging to the
  408. X                       specified users */
  409. X                    Flg.flg_AZ = 1 ;
  410. X                    Flg.flg_U = 1 ;
  411. X                    if ( !ulist )
  412. X                       ulist=Flg.flg_Ulist=getflgsp( argc );
  413. X                    while ( argc > 1 )
  414. X                    {
  415. X                        if ( **++argv == '-' )
  416. X                        {
  417. X                            --argv ;
  418. X                            break ;
  419. X                        }
  420. X                        --argc ;
  421. X                        ulist->f_chp = *argv ;
  422. X                        (++ulist)->f_chp = (char*)0 ;
  423. X                    }
  424. X                    continue ;
  425. X                case 'W' :
  426. X                    /* List only waiting processes */
  427. X                    Flg.flg_AZ = 1 ;
  428. X                    Flg.flg_W = 1 ;
  429. X                    continue ;
  430. X                case 'z' :
  431. X                case 'Z' :
  432. X                    /* List only zombie processes */
  433. X                    Flg.flg_AZ = 1 ;
  434. X                    Flg.flg_Z = 1 ;
  435. X                    continue ;
  436. X                default :
  437. X                    prexit( usage, *argv ) ;
  438. X                    /* NOTREACHED */
  439. X            }
  440. X    }
  441. X}
  442. ---END-OF-flagdecode.c---
  443. LEN=`wc -c < flagdecode.c`
  444. if [ $LEN !=  5627 ] ; then
  445.     echo shar: File "flagdecode.c" was $LEN, should have been 5627 bytes
  446. fi
  447. if [ -f 'flags.h' -a "${1}" != "-c" ] ; then
  448.     echo shar: Won\'t overwrite existing file "flags.h"
  449.     exit 2
  450. fi
  451. echo x - flags.h
  452. sed -e 's/^X//' > flags.h << '---END-OF-flags.h---'
  453. X# ifndef lint
  454. Xstatic char FlagsHId[] =  "@(#)flags.h    1.1\t10/1/88" ;
  455. X# endif
  456. X
  457. X/* Structure holding information specified in the option list ... */
  458. Xunion flaglist
  459. X{
  460. X    char                    *f_chp ;        /* Option specified as string */
  461. X    int                     f_uid ;         /* Numerical user id */
  462. X    int                     f_pid ;         /* Numerical process id */
  463. X    struct ttyline          *f_ttyline ;    /* Specified tty */
  464. X} ;
  465. X
  466. X/* Structure holding global information specifed by arg list options ... */
  467. Xstruct flags
  468. X{
  469. X    int                     flg_c:1 ;       /* print command from upage */
  470. X    int                     flg_d:1 ;       /* disc orientated output */
  471. X    int                     flg_e:1 ;       /* print environment string */
  472. X    int                     flg_f:1 ;       /* print process father # */
  473. X    int                     flg_g:1 ;       /* print process group # */
  474. X    int                     flg_i:1 ;       /* initialise sps */
  475. X    char                    *flg_j ;        /* Use this as the info file */
  476. X    char                    *flg_k ;        /* Use this as the {k}mem file*/
  477. X    int                     flg_o:1 ;       /* avoid the swap device */
  478. X    int                     flg_q:1 ;       /* show user time only */
  479. X    int                     flg_r:1 ;       /* repeat output */
  480. X    unsigned                flg_rdelay ;    /* ... with this much delay */
  481. X    char                    *flg_s ;        /* Use this as the symbol file*/
  482. X    int                     flg_v:1 ;       /* print verbose listing */
  483. X    int                     flg_w:1 ;       /* print wide output */
  484. X    int                     flg_y:1 ;       /* print tty information */
  485. X    int                     flg_A:1 ;       /* print all processes */
  486. X    int                     flg_B:1 ;       /* print busy processes */
  487. X    int                     flg_F:1 ;       /* print foreground processes */
  488. X    int                     flg_N:1 ;       /* print no processes */
  489. X    int                     flg_P:1 ;       /* print specified process #'s*/
  490. X    int                     flg_S:1 ;       /* print stopped processes */
  491. X    int                     flg_T:1 ;       /* print procs for given ttys */
  492. X    int                     flg_U:1 ;       /* print procs for given users*/
  493. X    int                     flg_W:1 ;       /* print waiting processes */
  494. X    int                     flg_Z:1 ;       /* print zombie processes */
  495. X    int                     flg_AZ:1 ;      /* One of A to Z was specified*/
  496. X    union flaglist          *flg_Plist ;    /* List of specified processes*/
  497. X    union flaglist          *flg_Tlist ;    /* List of specified ttys */
  498. X    union flaglist          *flg_Ulist ;    /* List of specified users */
  499. X} ;
  500. ---END-OF-flags.h---
  501. LEN=`wc -c < flags.h`
  502. if [ $LEN !=  2638 ] ; then
  503.     echo shar: File "flags.h" was $LEN, should have been 2638 bytes
  504. fi
  505. if [ -f 'flagsetup.c' -a "${1}" != "-c" ] ; then
  506.     echo shar: Won\'t overwrite existing file "flagsetup.c"
  507.     exit 2
  508. fi
  509. echo x - flagsetup.c
  510. sed -e 's/^X//' > flagsetup.c << '---END-OF-flagsetup.c---'
  511. X# ifndef lint
  512. Xstatic char SccsId[] =  "@(#)flagsetup.c    1.1\t10/1/88" ;
  513. X# endif
  514. X
  515. X# include       "sps.h"
  516. X# include       "flags.h"
  517. X# include       <h/ioctl.h>
  518. X# ifdef SUNOS40
  519. X# include       <h/stream.h>
  520. X# endif
  521. X# include       <h/tty.h>
  522. X
  523. X/*
  524. X** FLAGSETUP - Replaces any users or processes specified by flagdecode()
  525. X** with numerical equivalents. The lists are terminated by negative values.
  526. X** or null pointers. Ttystatus() must have been previously called to
  527. X** initialise the Info structure with chaos tty values.
  528. X*/
  529. Xflagsetup ()
  530. X{
  531. X    register union flaglist *fp ;
  532. X    register char           *chp ;
  533. X    register int            i ;
  534. X    register struct ttyline *lp ;
  535. X    int                     found ;
  536. X    extern struct flags     Flg ;
  537. X    extern struct info      Info ;
  538. X
  539. X    /* Look for specified users */
  540. X    if ( Flg.flg_U )                
  541. X    {
  542. X        if ( !Flg.flg_Ulist->f_chp )
  543. X            prexit( "sps - User name was expected after -u flag\n");
  544. X        for ( fp = Flg.flg_Ulist ; chp = fp->f_chp ; fp++ )
  545. X        {
  546. X            found = 0 ;
  547. X            for ( i = 0 ; i < MAXUSERS ; i++ )
  548. X                if ( !strncmp( chp, Info.i_hnames[i].h_uname,
  549. X                    UNAMELEN ) )
  550. X                {
  551. X                    fp->f_uid = Info.i_hnames[i].h_uid ;
  552. X                    found = 1 ;
  553. X                    break ;
  554. X                }
  555. X            if ( !found )
  556. X                prexit( "sps - Unknown user: %s\n", chp ) ;
  557. X        }
  558. X        fp->f_uid = -1 ;
  559. X    }
  560. X    /* Look for specified process ids */
  561. X    if ( Flg.flg_P )                
  562. X    {
  563. X        if ( !Flg.flg_Plist->f_chp )
  564. X            prexit(
  565. X                 "sps - Process id was expected after -p flag\n" ) ;
  566. X        for ( fp = Flg.flg_Plist ; chp = fp->f_chp ; fp++ )
  567. X        {
  568. X            if ( chp[0] < '0' || chp[0] > '9' )
  569. X                prexit( "sps - Bad process id: %s\n", chp ) ;
  570. X            fp->f_pid = atoi( chp ) ;
  571. X        }
  572. X        fp->f_pid = -1 ;
  573. X    }
  574. X    /* Look for specified ttys */
  575. X    if ( !Flg.flg_T )               
  576. X        return ;
  577. X    if ( !Flg.flg_Tlist->f_chp )
  578. X        prexit( "sps - Tty name was expected after -t flag\n" ) ;
  579. X    for ( fp = Flg.flg_Tlist ; chp = fp->f_chp ; fp++ )
  580. X    {       /* Under VMUNIX, all ttys have two character names.
  581. X           Thus, a flag of the form `t 8' should be expanded to
  582. X           become `t 08'. */
  583. X        if ( !chp[1] )
  584. X            chp[1] = chp[0], chp[0] = '0' ;
  585. X        found = 0 ;
  586. X        for ( lp = Info.i_ttyline ; lp->l_name[0] ; lp++ )
  587. X            if ( !strncmp( chp, lp->l_name, 2 ) )
  588. X            {
  589. X                fp->f_ttyline = lp ;
  590. X                found = 1 ;
  591. X                break ;
  592. X            }
  593. X        if ( !found )
  594. X            prexit( "sps - Unknown tty name: %.2s\n", chp ) ;
  595. X    }
  596. X    fp->f_ttyline = (struct ttyline*)0 ;
  597. X}
  598. ---END-OF-flagsetup.c---
  599. LEN=`wc -c < flagsetup.c`
  600. if [ $LEN !=  2342 ] ; then
  601.     echo shar: File "flagsetup.c" was $LEN, should have been 2342 bytes
  602. fi
  603. if [ -f 'getcmd.c' -a "${1}" != "-c" ] ; then
  604.     echo shar: Won\'t overwrite existing file "getcmd.c"
  605.     exit 2
  606. fi
  607. echo x - getcmd.c
  608. sed -e 's/^X//' > getcmd.c << '---END-OF-getcmd.c---'
  609. X# ifndef lint
  610. Xstatic char SccsId[] =  "@(#)getcmd.c    1.7\t8/6/90" ;
  611. X# endif
  612. X
  613. X# include       "sps.h"
  614. X# include       "flags.h"
  615. X# ifdef KVM
  616. X#  include      <kvm.h>
  617. X#  include      <ctype.h>
  618. X# else
  619. X#  include      <h/vm.h>
  620. X#  ifdef BSD42
  621. X#   include    <machine/pte.h>
  622. X#  else BSD42
  623. X#   include      <h/pte.h>
  624. X#  endif BSD42
  625. X# endif KVM
  626. X
  627. X/*
  628. X** GETCMD - Returns a character string read from a process' upage.
  629. X** This character string should represent the arguments to the current process.
  630. X*/
  631. Xchar    *getcmd ( p )
  632. X
  633. Xregister struct process         *p ;
  634. X
  635. X{
  636. X# ifdef KVM
  637. X    char                    **ap ;
  638. X    char                    *cp ;
  639. X    char                    *sp ;
  640. X    char                    **argv ;
  641. X    char                    **env ;
  642. X    extern kvm_t            *Flkvm ;
  643. X# else
  644. X    register int            *ip ;
  645. X    register char           *cp ;
  646. X    register char           *cp0 ;
  647. X    struct dblock           db ;
  648. X    struct pte              ptetbl[ UPAGES + CLSIZE ] ;
  649. X    extern int              Flmem, Flswap ;
  650. X# endif
  651. X    unsigned                nbad ;
  652. X    union
  653. X    {
  654. X        char            a_argc[ CLSIZE * NBPG ] ;
  655. X        int             a_argi[ CLSIZE * NBPG / sizeof( int ) ] ;
  656. X    } argbuf ;
  657. X    extern struct flags     Flg ;
  658. X    extern union userstate  User ;
  659. X    char                    *strcat(), *strncpy(), *strsave() ;
  660. X
  661. X    p->pr_csaved = 0 ;
  662. X    p->pr_upag = 0 ;
  663. X    if ( p->pr_p.p_stat == SZOMB )
  664. X        return ( "** Exit **" ) ;
  665. X    if ( !(p->pr_p.p_flag & SLOAD) && Flg.flg_o )
  666. X        return ( "** Swapped out **" ) ;
  667. X    /* Find the process' upage */
  668. X# ifdef KVM
  669. X    if ( !getupage( p ) )           
  670. X# else
  671. X    if ( !getupage( p, ptetbl ) )           
  672. X# endif
  673. X        return ( "** No upage **" ) ;
  674. X    p->pr_upag = 1 ;
  675. X    /* Is this a system process ? */
  676. X    if ( p->pr_p.p_flag & SSYS )            
  677. X        switch ( p->pr_p.p_pid )
  678. X        {
  679. X            case 0 :
  680. X                return ( "Unix Swapper" ) ;
  681. X            case 2 :
  682. X                return ( "Unix Pager" ) ;
  683. X# ifdef SUNOS40
  684. X            case 3 :
  685. X            case 4 :
  686. X                return ( "Unix Idle" ) ;
  687. X# endif
  688. X            default :
  689. X                break ;
  690. X        }
  691. X# ifdef DEC3100
  692. X    /* Reading the command arguments doesn't work on the DEC 3100 so
  693. X       we resort to this kludge until one day it does. */
  694. X    if ( 1 )
  695. X# else DEC3100
  696. X    if ( Flg.flg_c )
  697. X# endif DEC3100
  698. X    {
  699. X        p->pr_csaved = 1 ;
  700. X        (void)strncpy( argbuf.a_argc, User.u_us.u_comm,
  701. X            sizeof( User.u_us.u_comm ) ) ;
  702. X        argbuf.a_argc[ sizeof ( User.u_us.u_comm ) ] = '\0' ;
  703. X        return ( strsave( argbuf.a_argc ) ) ;
  704. X    }
  705. X# ifdef KVM
  706. X    if ( kvm_getcmd( Flkvm, &p->pr_p, &User.u_us, &argv,
  707. X        Flg.flg_e ? &env : (char ***)NULL ) < 0 || argv == NULL )
  708. X            goto getsysargs ;
  709. X    p->pr_csaved = 1 ;
  710. X    sp = argbuf.a_argc ;
  711. X    nbad = 0 ;
  712. X    ap = argv ;
  713. X    do {
  714. X        /* Copy one string from argv or env */
  715. X        for ( cp = *ap++; *cp; )
  716. X            if ( isprint( *cp ) )
  717. X                *sp++ = *cp++ ;
  718. X            else
  719. X            {
  720. X                /* Replace control characters with ?'s */
  721. X                if ( ++nbad > 5 )
  722. X                {
  723. X                      *sp++ = ' ' ;
  724. X                      break ;
  725. X                  }
  726. X                *sp++ = '?' ;
  727. X                cp++ ;
  728. X            }
  729. X        *sp++ = ' ' ;
  730. X        /* Check if at end of argv and user wants to see env */
  731. X        if ( *ap == 0 && Flg.flg_e && argv != 0 )
  732. X        {
  733. X            free( (char *) argv ) ;
  734. X            argv = NULL ;
  735. X            ap = env ;
  736. X            if ( ap == NULL )
  737. X                break ;
  738. X        }
  739. X    } while ( *ap ) ;
  740. X     if ( Flg.flg_e )
  741. X                free( (char*)env ) ;
  742. X    while ( *--sp == ' ' )
  743. X        *sp = '\0' ;
  744. X    return ( strsave( argbuf.a_argc ) ) ;
  745. X# else
  746. X    /* Fix by Alexander Dupuy <dupuy@amsterdam.columbia.edu> */
  747. X    /* Check for lack of stack, jack! (Sun 3.0 biod's) */
  748. X    if (User.u_us.u_ssize == 0)
  749. X        goto getsysargs ;
  750. X    /* Look at the top of the upage to locate the command arguments.
  751. X       The page is loaded if the process itself is loaded and the pte
  752. X       contains is marked as valid. */
  753. X    if ( (p->pr_p.p_flag & SLOAD)
  754. X    && !ptetbl[0].pg_fod && ptetbl[0].pg_pfnum )
  755. X    {       /* If the page is loaded, read the arguments from
  756. X           physical memory. */
  757. X        memseek( Flmem, (long)ctob( ptetbl[0].pg_pfnum ) ) ;
  758. X        if ( read( Flmem, argbuf.a_argc, CLSIZE*NBPG ) != CLSIZE*NBPG )
  759. X            return ( "** Memory read error **" ) ;
  760. X    }
  761. X    else                            
  762. X    {       /* Otherwise the page is on the swap device */
  763. X        vstodb( 0, ctod( CLSIZE ), &User.u_us.u_smap, &db, 1 ) ;
  764. X#  ifdef BSD42
  765. X        swseek( (long)dtob( db.db_base ) ) ;
  766. X#  else
  767. X        swseek( (long)ctob( db.db_base ) ) ;
  768. X#  endif
  769. X        if ( Flg.flg_o )
  770. X            return ( "** Swapped page **" ) ;
  771. X        if ( read( Flswap, argbuf.a_argc, CLSIZE*NBPG ) != CLSIZE*NBPG )
  772. X            return ( "** Swap device read error **" ) ;
  773. X    }
  774. X    /* Look down until the end of command arguments is found. */
  775. X    ip = &argbuf.a_argi[ CLSIZE*NBPG / sizeof( int ) ] ;
  776. X    ip -= 2 ;
  777. X    while ( *--ip )
  778. X        if ( ip == &argbuf.a_argi[0] )
  779. X            goto getsysargs ;
  780. X    p->pr_csaved = 1 ;
  781. X    /* Process the command arguments, looking for nulls and unprintable
  782. X       characters. */
  783. X    cp0 = (char*)(ip + 1) ;
  784. X    if ( !*cp0 )                    
  785. X        cp0++ ;                 
  786. X    if ( *cp0 )
  787. X    {
  788. X        nbad = 0 ;                      
  789. X        for ( cp = cp0 ; cp < &argbuf.a_argc[ CLSIZE*NBPG ] ; cp++ )
  790. X        {
  791. X            *cp &= 0177 ;
  792. X            if ( !*cp )             
  793. X            {       /* Replace nulls with spaces */
  794. X                *cp = ' ' ;
  795. X                continue ;
  796. X            }
  797. X            if ( *cp < ' ' || *cp == 0177 )
  798. X            {       /* Replace control characters with ?'s */
  799. X                if ( ++nbad > 5 )
  800. X                {
  801. X                    *cp++ = ' ' ;
  802. X                    break ;
  803. X                }
  804. X                *cp = '?' ;
  805. X                continue ;
  806. X            }
  807. X            if ( !Flg.flg_e && *cp == '=' )
  808. X            {       /* Break on an `=' if we are not interested
  809. X                   in the environment strings. */
  810. X                *cp = '\0' ;
  811. X                while ( cp > cp0 && *--cp != ' ' )
  812. X                    *cp = '\0' ;
  813. X                break ;
  814. X            }
  815. X        }
  816. X        while ( *--cp == ' ' )
  817. X            *cp = '\0' ;
  818. X        return ( strsave( cp0 ) ) ;
  819. X    }
  820. X# endif KVM
  821. Xgetsysargs :
  822. X    /* If the command arguments cannot be accessed from the user's memory
  823. X       space, get the command name from the system's idea of what the
  824. X       name should be. */
  825. X    p->pr_csaved = 1 ;
  826. X    argbuf.a_argc[0] = '(' ;
  827. X    (void)strncpy( &argbuf.a_argc[1], User.u_us.u_comm,
  828. X        sizeof( User.u_us.u_comm ) ) ;
  829. X    argbuf.a_argc[ sizeof ( User.u_us.u_comm ) + 1 ] = '\0' ;
  830. X    (void)strcat( &argbuf.a_argc[0], ")" ) ;
  831. X    return ( strsave( argbuf.a_argc ) ) ;
  832. X}
  833. X
  834. X# ifndef KVM
  835. X/*
  836. X** VSTODB - Given a base/size pair in virtual swap area,
  837. X** return a physical base/size pair which is the
  838. X** (largest) initial, physically contiguous block.
  839. X/* This code is stolen from the kernel file /sys/sys/vm_drum.c.
  840. X*/
  841. Xvstodb ( vsbase, vssize, dmp, dbp, rev )
  842. X
  843. Xregister int                    vsbase ;
  844. Xregister int                    vssize;
  845. Xstruct dmap                     *dmp ;
  846. Xregister struct dblock          *dbp ;
  847. Xint                             rev ;
  848. X
  849. X{
  850. X    register int            blk ;
  851. X    register swblk_t        *ip ;
  852. X# ifdef BSD42
  853. X    extern struct info      Info ;
  854. X# endif
  855. X
  856. X# ifdef BSD42
  857. X    blk = Info.i_dmmin ;
  858. X# else
  859. X    blk = DMMIN ;
  860. X# endif
  861. X    ip = dmp->dm_map ;
  862. X    while ( vsbase >= blk )
  863. X    {
  864. X        vsbase -= blk ;
  865. X# ifdef BSD42
  866. X        if ( blk < Info.i_dmmax )
  867. X# else
  868. X        if ( blk < DMMAX )
  869. X# endif
  870. X            blk *= 2 ;
  871. X        ip++ ;
  872. X    }
  873. X    dbp->db_size = vssize < blk - vsbase ? vssize : blk - vsbase ;
  874. X    dbp->db_base = *ip + (rev ? blk - (vsbase + dbp->db_size) : vsbase);
  875. X}
  876. X# endif
  877. ---END-OF-getcmd.c---
  878. LEN=`wc -c < getcmd.c`
  879. if [ $LEN !=  6872 ] ; then
  880.     echo shar: File "getcmd.c" was $LEN, should have been 6872 bytes
  881. fi
  882. if [ -f 'getupage.c' -a "${1}" != "-c" ] ; then
  883.     echo shar: Won\'t overwrite existing file "getupage.c"
  884.     exit 2
  885. fi
  886. echo x - getupage.c
  887. sed -e 's/^X//' > getupage.c << '---END-OF-getupage.c---'
  888. X# ifndef lint
  889. Xstatic char SccsId[] =  "@(#)getupage.c    1.3\t8/2/90" ;
  890. X# endif
  891. X
  892. X# include       "sps.h"
  893. X# ifdef KVM
  894. X#  include      <kvm.h>
  895. X# else
  896. X#  include      <h/vm.h>
  897. X#  ifdef BSD42
  898. X#  include    <machine/pte.h>
  899. X#  else
  900. X#  include      <h/pte.h>
  901. X#  endif
  902. X# endif
  903. X# include       <stdio.h>
  904. X
  905. X/*
  906. X** GETUPAGE - Reads the upage for the specified process as well as sufficient
  907. X** page tables entries for reading the command arguments. The pte's are read
  908. X** into the argument `ptetbl'. The upage is read into the external variable
  909. X** `User'. This procedure returns 1 if the upage was successfully read.
  910. X*/
  911. X
  912. X# ifndef KVM
  913. X
  914. X# define        usrpt           (Info.i_usrpt)
  915. X
  916. Xgetupage ( p, ptetbl )
  917. X
  918. Xregister struct process         *p ;
  919. Xregister struct pte             *ptetbl ;
  920. X
  921. X{
  922. X    register int            i ;
  923. X    register int            ncl ;
  924. X    struct pte              pte ;
  925. X    extern struct info      Info ;
  926. X    extern union userstate  User ;
  927. X    extern int              Flmem, Flkmem, Flswap ;
  928. X
  929. X    /* If the process is not loaded, look for the upage on the swap device*/
  930. X    if ( !(p->pr_p.p_flag & SLOAD) )
  931. X    {                               
  932. X# ifdef BSD42
  933. X        swseek( (long)dtob( p->pr_p.p_swaddr ) ) ;
  934. X# else BSD42
  935. X        swseek( (long)ctob( p->pr_p.p_swaddr ) ) ;
  936. X# endif BSD42
  937. X# ifdef SUN
  938. X        if ( read( Flswap, (char*)&User.u_us, sizeof( union userstate ))
  939. X        != sizeof( union userstate ) )
  940. X# else SUN
  941. X        if ( read( Flswap, (char*)&User.u_us, sizeof( struct user ) )
  942. X        != sizeof( struct user ) )
  943. X# endif SUN
  944. X        {
  945. X            fprintf( stderr,
  946. X                "sps - Can't read upage of process %d\n",
  947. X                p->pr_p.p_pid ) ;
  948. X            return ( 0 ) ;
  949. X        }
  950. X        return ( 1 ) ;          
  951. X    }                               
  952. X    /* The process is loaded. Locate the process pte's by reading
  953. X       the pte of their base address from system virtual address space. */
  954. X# ifdef DEC3100
  955. X    /* This method of accessing the upage suffices on the DEC Station
  956. X       but only provides sufficient pte's to read the upage, leaving the
  957. X       command arguments inaccessible. */
  958. X    memseek( Flkmem, (long)p->pr_p.p_addr ) ;
  959. X    if ( read( Flkmem, (char*)ptetbl, (UPAGES+CLSIZE)*sizeof( struct pte ) )
  960. X    != (UPAGES+CLSIZE)*sizeof( struct pte ) )
  961. X    {
  962. X        fprintf( stderr, "sps - Can't read page table of process %d\n",
  963. X            p->pr_p.p_pid ) ;
  964. X        return ( 0 ) ;
  965. X    }
  966. X# else DEC3100
  967. X    memseek( Flkmem, (long)&Info.i_usrptmap[ btokmx(p->pr_p.p_p0br)
  968. X        + p->pr_p.p_szpt-1 ] ) ;
  969. X    if ( read( Flkmem, (char*)&pte, sizeof( struct pte ) )
  970. X    != sizeof( struct pte ) )
  971. X    {
  972. X        fprintf( stderr,
  973. X              "sps - Can't read indir pte for upage of process %d\n",
  974. X            p->pr_p.p_pid ) ;
  975. X        return ( 0 ) ;
  976. X    }                               
  977. X    /* Now read the process' pte's from physical memory. We need to access
  978. X       sufficient pte's for the upage and for the command arguments. */
  979. X    memseek( Flmem, (long)ctob( pte.pg_pfnum+1 )
  980. X        - (UPAGES+CLSIZE)*sizeof( struct pte ) ) ;
  981. X    if ( read( Flmem, (char*)ptetbl, (UPAGES+CLSIZE)*sizeof( struct pte ) )
  982. X    != (UPAGES+CLSIZE)*sizeof( struct pte ) )
  983. X    {
  984. X        fprintf( stderr, "sps - Can't read page table of process %d\n",
  985. X            p->pr_p.p_pid ) ;
  986. X        return ( 0 ) ;
  987. X    }
  988. X# endif DEC3100
  989. X    /* Now we can read the pages belonging to the upage.
  990. X       Here we read in an entire click at one go. */
  991. X    ncl = (sizeof( struct user ) + NBPG*CLSIZE - 1) / (NBPG*CLSIZE) ;
  992. X    while ( --ncl >= 0 )            
  993. X    {                               
  994. X        i = ncl * CLSIZE ;
  995. X# ifdef DEC3100
  996. X        memseek( Flmem, (long)ctob( ptetbl[ i ].pg_pfnum ) ) ;
  997. X# else DEC3100
  998. X        memseek( Flmem, (long)ctob( ptetbl[ CLSIZE+i ].pg_pfnum ) ) ;
  999. X# endif DEC3100
  1000. X        if ( read( Flmem, User.u_pg[i], CLSIZE*NBPG ) != CLSIZE*NBPG )
  1001. X        {
  1002. X            fprintf( stderr,
  1003. X                "sps - Can't read page 0x%x of process %d\n",
  1004. X                ptetbl[ CLSIZE+i ].pg_pfnum, p->pr_p.p_pid ) ;
  1005. X            return ( 0 ) ;
  1006. X        }
  1007. X    }
  1008. X    return ( 1 ) ;
  1009. X}
  1010. X
  1011. X# else KVM
  1012. X
  1013. Xgetupage ( p )
  1014. X
  1015. Xregister struct process         *p ;
  1016. X
  1017. X{
  1018. X        struct user            *upage ;
  1019. X    extern union userstate  User ;
  1020. X    extern kvm_t           *Flkvm ;
  1021. X
  1022. X    if (upage = kvm_getu( Flkvm, &p->pr_p ) )
  1023. X    {
  1024. X            bcopy( (char *)upage, User.u_pg[0], sizeof( struct user ) ) ;
  1025. X        return ( 1 ) ;
  1026. X    }
  1027. X    fprintf( stderr, "sps - Can't read upage of process %d\n",
  1028. X        p->pr_p.p_pid ) ;
  1029. X    return ( 0 ) ;
  1030. X}
  1031. X
  1032. X# endif KVM
  1033. ---END-OF-getupage.c---
  1034. LEN=`wc -c < getupage.c`
  1035. if [ $LEN !=  4161 ] ; then
  1036.     echo shar: File "getupage.c" was $LEN, should have been 4161 bytes
  1037. fi
  1038. if [ -f 'globals1.c' -a "${1}" != "-c" ] ; then
  1039.     echo shar: Won\'t overwrite existing file "globals1.c"
  1040.     exit 2
  1041. fi
  1042. echo x - globals1.c
  1043. sed -e 's/^X//' > globals1.c << '---END-OF-globals1.c---'
  1044. X# ifndef lint
  1045. Xstatic char SccsId[] =  "@(#)globals1.c    1.1\t10/1/88" ;
  1046. X# endif
  1047. X
  1048. X# include       "sps.h"
  1049. X# include       "flags.h"
  1050. X# ifdef KVM
  1051. X# include       <kvm.h>
  1052. X# endif
  1053. X
  1054. X/* Read/Write Variables global to the code of sps */
  1055. X
  1056. Xstruct info                     Info ;          /* Information structure */
  1057. X
  1058. Xstruct flags                    Flg ;           /* Flag options */
  1059. X
  1060. Xstruct summary                  Summary ;       /* Summary of processes */
  1061. X
  1062. Xunion  userstate                User ;          /* Upage of one process */
  1063. X
  1064. X# ifdef KVM
  1065. Xkvm_t                          *Flkvm ;         /* Kernel VM descriptor */
  1066. X# else
  1067. Xint                             Flmem, Flkmem, Flswap ; /* File descriptors */
  1068. X# endif
  1069. X
  1070. Xunsigned                        Termwidth ;     /* Width of output device */
  1071. X
  1072. Xshort                           Lastpgrp ;      /* Last process pgrp printed */
  1073. X
  1074. Xshort                           Lastuid ;       /* Last process uid printed */
  1075. ---END-OF-globals1.c---
  1076. LEN=`wc -c < globals1.c`
  1077. if [ $LEN !=  945 ] ; then
  1078.     echo shar: File "globals1.c" was $LEN, should have been 945 bytes
  1079. fi
  1080. if [ -f 'globals2.c' -a "${1}" != "-c" ] ; then
  1081.     echo shar: Won\'t overwrite existing file "globals2.c"
  1082.     exit 2
  1083. fi
  1084. echo x - globals2.c
  1085. sed -e 's/^X//' > globals2.c << '---END-OF-globals2.c---'
  1086. X# ifndef lint
  1087. Xstatic char SccsId[] =  "@(#)globals2.c    1.2\t7/4/90" ;
  1088. X# endif
  1089. X
  1090. X# include       "sps.h"
  1091. X
  1092. X/* Read Only variables, global to the code of sps ... */
  1093. X
  1094. X/* Null ttyline device ... */
  1095. Xstruct ttyline                  Notty = { "  " } ;
  1096. X
  1097. X/*
  1098. X** The symbol table. For each address read from the kernel during
  1099. X** initialisation, this table shows the following:
  1100. X**      i.   the name of that symbol within the kernel ;
  1101. X**      ii.  whether an extra indirection is needed through the kernel,
  1102. X**           i.e. whether the value of that symbol should be obtained
  1103. X**           rather than its address.
  1104. X**      iii. where the obtained value/address is placed in the Info structure ;
  1105. X**      iv.  whether the obtained value is associated with a reason for
  1106. X**           a process wait state.
  1107. X*/
  1108. X/* The order of entries in this table is unimportant. */
  1109. X
  1110. Xextern struct info              Info ;
  1111. X
  1112. Xstruct symbol   Symbollist[] =
  1113. X{       
  1114. X    /* Kernel addresses required in order to access process,
  1115. X       tty and upage information. All these addresses should be
  1116. X       located in the symbol file during initialisation. */
  1117. X    { "_proc",      1,  (caddr_t*)&Info.i_proc0,    (char*)0        },
  1118. X    { "_nproc",     1,  (caddr_t*)&Info.i_nproc,    (char*)0        },
  1119. X# ifdef ULTRIX20
  1120. X    { "_gnode",     1,  (caddr_t*)&Info.i_inode0,   (char*)0        },
  1121. X    { "_ngnode",    1,  (caddr_t*)&Info.i_ninode,   (char*)0        },
  1122. X# else
  1123. X#  ifndef SUNOS41
  1124. X    { "_inode",     1,  (caddr_t*)&Info.i_inode0,   (char*)0        },
  1125. X#  endif SUNOS41
  1126. X    { "_ninode",    1,  (caddr_t*)&Info.i_ninode,   (char*)0        },
  1127. X# endif ULTRIX20
  1128. X
  1129. X# ifndef SUNOS40
  1130. X    { "_text",      1,  (caddr_t*)&Info.i_text0,    (char*)0        },
  1131. X    { "_ntext",     1,  (caddr_t*)&Info.i_ntext,    (char*)0        },
  1132. X    { "_swbuf",     1,  (caddr_t*)&Info.i_swbuf0,   (char*)0        },
  1133. X    { "_nswbuf",    1,  (caddr_t*)&Info.i_nswbuf,   (char*)0        },
  1134. X    { "_buf",       1,  (caddr_t*)&Info.i_buf0,     (char*)0        },
  1135. X    { "_nbuf",      1,  (caddr_t*)&Info.i_nbuf,     (char*)0        },
  1136. X    { "_ecmx",      1,  (caddr_t*)&Info.i_ecmx,     (char*)0        },
  1137. X    { "_Usrptmap",  0,  (caddr_t*)&Info.i_usrptmap, (char*)0        },
  1138. X    { "_usrpt",     0,  (caddr_t*)&Info.i_usrpt,    (char*)0        },
  1139. X    { "_dmmin",     1,  (caddr_t*)&Info.i_dmmin,    (char*)0        },
  1140. X    { "_dmmax",     1,  (caddr_t*)&Info.i_dmmax,    (char*)0        },
  1141. X# endif SUNOS40
  1142. X
  1143. X    { "_cdevsw",    0,  (caddr_t*)&Info.i_cdevsw,   (char*)0        },
  1144. X# ifdef BSD42
  1145. X#  ifdef NFS
  1146. X#   ifndef NOQUOTA
  1147. X    { "_dquot",     1,  (caddr_t*)&Info.i_quota0,   (char*)0        },
  1148. X    { "_ndquot",    1,  (caddr_t*)&Info.i_nquota,   (char*)0        },
  1149. X#   endif NOQUOTA
  1150. X#  else NFS
  1151. X    { "_quota",     1,  (caddr_t*)&Info.i_quota0,   (char*)0        },
  1152. X    { "_nquota",    1,  (caddr_t*)&Info.i_nquota,   (char*)0        },
  1153. X#  endif NFS
  1154. X    { "_mbutl",     0,  (caddr_t*)&Info.i_mbutl,    (char*)0        },
  1155. X# else BSD42
  1156. X    { "_hz",        1,  (caddr_t*)&Info.i_hz,       (char*)0        },
  1157. X# endif BSD42
  1158. X
  1159. X# ifdef CHAOS
  1160. X    { "_Chconntab", 0,  &Info.i_Chconntab,          (char*)0        },
  1161. X# endif CHAOS
  1162. X
  1163. X# ifdef SUNOS40
  1164. X    { "_maxmem",    1,  (caddr_t*)&Info.i_ecmx,    (char*)0    },
  1165. X    { "_segvn_ops",    0,  (caddr_t*)&Info.i_segvn_ops,(char*)0    },
  1166. X    { "_pty_softc",    0,  (caddr_t*)&Info.i_ptybase,    (char*)0    },
  1167. X    { "_npty",    1,  (caddr_t*)&Info.i_npty,    (char*)0    },
  1168. X#  ifdef SUNOS41
  1169. X    { "_strst",    0,  (caddr_t*)&Info.i_strst,    (char*)0    },
  1170. X    { "_allstream",    1,  (caddr_t*)&Info.i_allstream, (char*)0    },
  1171. X#  else SUNOS41
  1172. X    { "_streams",    0,  (caddr_t*)&Info.i_streams,    (char*)0    },
  1173. X    { "_streamsNSTREAMS", 1, (caddr_t*)&Info.i_streamsNSTREAMS ,(char*)0 },
  1174. X#  endif SUNOS41
  1175. X    { "_Sysbase",    1,  (caddr_t*)&Info.i_sysbase,    (char*)0    },
  1176. X# endif SUNOS40
  1177. X
  1178. X    /* Kernel addresses associated with process wait states.
  1179. X       It is not important if some of these addresses are unresolved
  1180. X       at initialisation. */
  1181. X# ifndef SUN
  1182. X    { "_fltab",     0,  &Info.i_waitstate[0],       "floppy"        },
  1183. X    { "_tu",        0,  &Info.i_waitstate[1],       "tu58"          },
  1184. X    { "_lp_softc",  0,  &Info.i_waitstate[3],       "printr"        },
  1185. X# endif SUN
  1186. X    { "_bfreelist", 0,  &Info.i_waitstate[2],       "buffer"        },
  1187. X    { "_lbolt",     0,  &Info.i_waitstate[4],       "lbolt"         },
  1188. X    { "_runin",     0,  &Info.i_waitstate[5],       "runin"         },
  1189. X    { "_runout",    0,  &Info.i_waitstate[6],       "runout"        },
  1190. X    { "_ipc",       0,  &Info.i_waitstate[7],       "ptrace"        },
  1191. X# ifdef SUNOS41
  1192. X    { "_uunix",     0,  &Info.i_waitstate[8],       "pause"         },
  1193. X# else SUNOS41
  1194. X    { "_u",         0,  &Info.i_waitstate[8],       "pause"         },
  1195. X# endif SUNOS41
  1196. X    { "_freemem",   0,  &Info.i_waitstate[9],       "freemm"        },
  1197. X    { "_kernelmap", 0,  &Info.i_waitstate[10],      "kermap"        },
  1198. X    { "_cwaiting",  0,  &Info.i_waitstate[11],      "cwait"         },
  1199. X# ifdef BSD42
  1200. X    { "_selwait",   0,  &Info.i_waitstate[12],      "select"        },
  1201. X# endif BSD42
  1202. X# ifdef CHAOS
  1203. X    { "_Chrfclist", 0,  &Info.i_waitstate[13],      "chrfc"         },
  1204. X# endif CHAOS
  1205. X# ifndef SUN
  1206. X    { "_rhpbuf",    0,  &Info.i_waitstate[14],      "rhpbuf"        },
  1207. X    { "_rhtbuf",    0,  &Info.i_waitstate[15],      "rhtbuf"        },
  1208. X    { "_ridcbuf",   0,  &Info.i_waitstate[16],      "ridcbf"        },
  1209. X    { "_rikbuf",    0,  &Info.i_waitstate[17],      "rikbuf"        },
  1210. X    { "_rmtbuf",    0,  &Info.i_waitstate[18],      "rmtbuf"        },
  1211. X    { "_rrkbuf",    0,  &Info.i_waitstate[19],      "rrkbuf"        },
  1212. X    { "_rrlbuf",    0,  &Info.i_waitstate[20],      "rrlbuf"        },
  1213. X    { "_rrxbuf",    0,  &Info.i_waitstate[21],      "rrxbuf"        },
  1214. X    { "_rswbuf",    0,  &Info.i_waitstate[22],      "rswbuf"        },
  1215. X    { "_rtmbuf",    0,  &Info.i_waitstate[23],      "rtmbuf"        },
  1216. X    { "_rtsbuf",    0,  &Info.i_waitstate[24],      "rtsbuf"        },
  1217. X    { "_rudbuf",    0,  &Info.i_waitstate[25],      "rudbuf"        },
  1218. X    { "_rupbuf",    0,  &Info.i_waitstate[26],      "rupbuf"        },
  1219. X    { "_rutbuf",    0,  &Info.i_waitstate[27],      "rutbuf"        },
  1220. X    { "_rvabuf",    0,  &Info.i_waitstate[28],      "rvabuf"        },
  1221. X    { "_rvpbuf",    0,  &Info.i_waitstate[29],      "rvpbuf"        },
  1222. X    { "_chtbuf",    0,  &Info.i_waitstate[30],      "chtbuf"        },
  1223. X    { "_cmtbuf",    0,  &Info.i_waitstate[31],      "cmtbuf"        },
  1224. X    { "_ctmbuf",    0,  &Info.i_waitstate[32],      "ctmbuf"        },
  1225. X    { "_ctsbuf",    0,  &Info.i_waitstate[33],      "ctsbuf"        },
  1226. X    { "_cutbuf",    0,  &Info.i_waitstate[34],      "cutbuf"        },
  1227. X#  ifdef NFS
  1228. X    { "_async_bufhead", 0,  &Info.i_waitstate[35],  "async"        },
  1229. X#  endif NFS
  1230. X# else SUN
  1231. X    { "_async_bufhead", 0,  &Info.i_waitstate[14],  "async"        },
  1232. X    { "_desktops",    0,  &Info.i_waitstate[15],    "dtops"        },
  1233. X# endif SUN
  1234. X# ifdef ULTRIX20
  1235. X    { "_async_bufhead", 0,  &Info.i_waitstate[35],  "async"        },
  1236. X# endif ULTRIX20
  1237. X    { (char*)0,     0,  (caddr_t*)0,                (char*)0        }
  1238. X} ;
  1239. ---END-OF-globals2.c---
  1240. LEN=`wc -c < globals2.c`
  1241. if [ $LEN !=  6807 ] ; then
  1242.     echo shar: File "globals2.c" was $LEN, should have been 6807 bytes
  1243. fi
  1244. if [ -f 'hashuid.c' -a "${1}" != "-c" ] ; then
  1245.     echo shar: Won\'t overwrite existing file "hashuid.c"
  1246.     exit 2
  1247. fi
  1248. echo x - hashuid.c
  1249. sed -e 's/^X//' > hashuid.c << '---END-OF-hashuid.c---'
  1250. X# ifndef lint
  1251. Xstatic char SccsId[] =  "@(#)hashuid.c    1.1\t10/1/88" ;
  1252. X# endif
  1253. X
  1254. X# include       "sps.h"
  1255. X
  1256. X/* The hashing functions themselves ... */
  1257. X# define        HASHFN1( a )            (((unsigned)(a)*91 + 17) % MAXUSERS)
  1258. X# define        HASHFN2( a )            (((unsigned)(a) + 47) % MAXUSERS)
  1259. X
  1260. X/*
  1261. X** HASHUID - Returns a pointer to a slot in the hash table that corresponds
  1262. X** to the hash table entry for `uid'. It returns a null pointer if there is
  1263. X** no such slot.
  1264. X*/
  1265. Xstruct hashtab  *hashuid ( uid )
  1266. X
  1267. Xint                             uid ;
  1268. X
  1269. X{
  1270. X    register struct hashtab *hp ;
  1271. X    register int            i ;
  1272. X    register int            j ;
  1273. X    extern struct info      Info ;
  1274. X
  1275. X    j = HASHFN1( uid ) ;
  1276. X    for ( i = 0 ; i < MAXUSERS ; i++ )
  1277. X    {
  1278. X        hp = &Info.i_hnames[ j ] ;
  1279. X        if ( !hp->h_uname[0] )
  1280. X            return ( (struct hashtab*)0 ) ;
  1281. X        if ( hp->h_uid == uid )
  1282. X            return ( hp ) ;
  1283. X        j = HASHFN2( j ) ;
  1284. X    }
  1285. X    return ( (struct hashtab*)0 ) ;
  1286. X}
  1287. X
  1288. X/*
  1289. X** HASHNEXT - Returns a pointer to the next slot in the hash table that
  1290. X** may be use for storing information for `uid'. It returns a null pointer
  1291. X** if there are no more free slots available.
  1292. X*/
  1293. Xstruct hashtab  *hashnext ( uid )
  1294. X
  1295. Xint                             uid ;
  1296. X
  1297. X{
  1298. X    register struct hashtab *hp ;
  1299. X    register int            i ;
  1300. X    register int            j ;
  1301. X    extern struct info      Info ;
  1302. X
  1303. X    j = HASHFN1( uid ) ;
  1304. X    for ( i = 0 ; i < MAXUSERS ; i++ )
  1305. X    {
  1306. X        hp = &Info.i_hnames[ j ] ;
  1307. X        if ( !hp->h_uname[0] )
  1308. X            return ( hp ) ;
  1309. X        j = HASHFN2( j ) ;
  1310. X    }
  1311. X    return ( (struct hashtab*)0 ) ;
  1312. X}
  1313. ---END-OF-hashuid.c---
  1314. LEN=`wc -c < hashuid.c`
  1315. if [ $LEN !=  1515 ] ; then
  1316.     echo shar: File "hashuid.c" was $LEN, should have been 1515 bytes
  1317. fi
  1318. if [ -f 'initialise.c' -a "${1}" != "-c" ] ; then
  1319.     echo shar: Won\'t overwrite existing file "initialise.c"
  1320.     exit 2
  1321. fi
  1322. echo x - initialise.c
  1323. sed -e 's/^X//' > initialise.c << '---END-OF-initialise.c---'
  1324. X# ifndef lint
  1325. Xstatic char SccsId[] =  "@(#)initialise.c    1.1\t10/1/88" ;
  1326. X# endif
  1327. X
  1328. X# include       "sps.h"
  1329. X# include       "flags.h"
  1330. X# include       <pwd.h>
  1331. X# include       <stdio.h>
  1332. X
  1333. X/*
  1334. X** INITIALISE - Called to reset the `Info' structure with new kernel
  1335. X** addresses and user and tty information.
  1336. X*/
  1337. Xinitialise ()
  1338. X{
  1339. X    register FILE           *fd ;
  1340. X    char                    *fileinfo ;
  1341. X    extern struct flags     Flg ;
  1342. X    extern struct info      Info ;
  1343. X    FILE                    *fopen() ;
  1344. X
  1345. X    fileinfo = Flg.flg_j ? Flg.flg_j : FILE_INFO ;
  1346. X    /* Read kernel addresses */
  1347. X    initsymbols() ;                 
  1348. X    /* Read user names */
  1349. X    initusers() ;                   
  1350. X    (void)umask( ~0644 ) ;          
  1351. X    if ( !(fd = fopen( fileinfo, "w" )) )
  1352. X    {
  1353. X        fprintf( stderr, "sps - Can't create info file %s", fileinfo ) ;
  1354. X        sysperror() ;
  1355. X    }
  1356. X    /* Find tty addresses */
  1357. X    inittty() ;                     
  1358. X    if ( fwrite( (char*)&Info, sizeof( struct info ), 1, fd ) != 1 )
  1359. X    {
  1360. X        fprintf( stderr, "sps - Can't write info file %s", fileinfo ) ;
  1361. X        sysperror() ;
  1362. X        exit( 1 ) ;
  1363. X    }
  1364. X    (void)fclose( fd ) ;
  1365. X    printf( "sps is initialised\n" ) ;
  1366. X}
  1367. X
  1368. X/* INITUSERS - Read the passwd file and fill in the user name arrays */
  1369. Xinitusers ()
  1370. X{
  1371. X    register struct passwd  *pw ;
  1372. X    register struct hashtab *hp ;
  1373. X    struct passwd           *getpwent() ;
  1374. X    char                    *strncpy() ;
  1375. X    struct hashtab          *hashuid(), *hashnext() ;
  1376. X
  1377. X    while ( pw = getpwent() )
  1378. X    {       /* For each user in the passwd file, first see if that uid
  1379. X           has been already allocated in the hash table. */
  1380. X        if ( hp = hashuid( pw->pw_uid ) )
  1381. X        {
  1382. X            fprintf( stderr,
  1383. X           "sps - Names %s and %s conflict in passwd file for uid %d\n",
  1384. X                hp->h_uname, pw->pw_name, pw->pw_uid ) ;
  1385. X            continue ;
  1386. X        }
  1387. X        /* Try to find a free slot in the hash table and fill it. */
  1388. X        if ( !(hp = hashnext( pw->pw_uid )) )
  1389. X            prexit( "sps - Too many users in passwd file\n" ) ;
  1390. X        hp->h_uid = pw->pw_uid ;
  1391. X        (void)strncpy( hp->h_uname, pw->pw_name, UNAMELEN ) ;
  1392. X    }
  1393. X    (void)endpwent() ;
  1394. X}
  1395. ---END-OF-initialise.c---
  1396. LEN=`wc -c < initialise.c`
  1397. if [ $LEN !=  1986 ] ; then
  1398.     echo shar: File "initialise.c" was $LEN, should have been 1986 bytes
  1399. fi
  1400. if [ -f 'initsymbols.c' -a "${1}" != "-c" ] ; then
  1401.     echo shar: Won\'t overwrite existing file "initsymbols.c"
  1402.     exit 2
  1403. fi
  1404. echo x - initsymbols.c
  1405. sed -e 's/^X//' > initsymbols.c << '---END-OF-initsymbols.c---'
  1406. X# ifndef lint
  1407. Xstatic char SccsId[] =    "@(#)initsymbols.c    1.4\t8/6/90" ;
  1408. X# endif lint
  1409. X
  1410. X# include       "sps.h"
  1411. X# include       "flags.h"
  1412. X# ifdef BSD42
  1413. X#  include       <sys/file.h>
  1414. X# endif BSD42
  1415. X# ifdef KVM
  1416. X#  include       <kvm.h>
  1417. X# endif KVM
  1418. X# include       <nlist.h>
  1419. X# include       <stdio.h>
  1420. X
  1421. X/* INITSYMBOLS - Reads kmem values into the Info structure */
  1422. X/*
  1423. X** THIS CODE COPIES KMEM VALUES INTO THE INFO STRUCTURE ASSUMING THAT
  1424. X** VALUES READ FROM THE KERNEL HAVE TYPE CADDR_T. THEREFORE, WE ARE
  1425. X** MAKING THE DUBIOUS ASSUMPTION THAT INTS, POINTERS AND CADDR_T's
  1426. X** HAVE IDENTICAL SIZES.
  1427. X*/
  1428. Xinitsymbols ()
  1429. X{
  1430. X    register struct nlist   *np ;
  1431. X    register struct symbol  *s ;
  1432. X    register struct nlist   *np0 ;
  1433. X    char                    *filesymbol ;
  1434. X# ifdef KVM
  1435. X    extern kvm_t           *Flkvm ;
  1436. X# endif
  1437. X    extern struct flags     Flg ;
  1438. X    extern struct symbol    Symbollist[] ;
  1439. X    extern struct info      Info ;
  1440. X    char                    *getcore() ;
  1441. X    char                    *strncpy() ;
  1442. X
  1443. X    filesymbol = Flg.flg_s ? Flg.flg_s : FILE_SYMBOL ;
  1444. X    /* Find the length of the symbol table */
  1445. X    for ( s = Symbollist ; s->s_kname ; s++ )
  1446. X        ;
  1447. X    /* Construct an nlist structure by copying names from the symbol table*/
  1448. X    np0 = (struct nlist*)getcore( (s-Symbollist+1)*sizeof( struct nlist ) );
  1449. X    for ( s = Symbollist, np = np0 ; s->s_kname ; s++, np++ )
  1450. X    {
  1451. X# ifdef SUN386I
  1452. X        /* Remove '_' prefix because 386i uses COFF format -
  1453. X           Provided by Martin Reed <mr@ritd.co.uk> */
  1454. X        np->n_name = &s->s_kname[1] ;       
  1455. X# else SUN386I
  1456. X                                     
  1457. X        np->n_name = s->s_kname ; 
  1458. X# endif SUN386I     
  1459. X        np[1].n_name = (char*)0 ;       
  1460. X        np->n_value = 0 ;
  1461. X    }
  1462. X# ifdef KVM
  1463. X    if ( kvm_nlist( Flkvm, np0 ) == -1 )
  1464. X    {
  1465. X        fprintf( stderr, "sps - Can't read symbol file %s", filesymbol);
  1466. X        sysperror() ;
  1467. X    }
  1468. X              
  1469. X# else KVM
  1470. X#  ifdef BSD42
  1471. X    if ( access( filesymbol, R_OK ) < 0 )
  1472. X#  else BSD42
  1473. X    if ( access( filesymbol, 4 ) < 0 )
  1474. X#  endif BSD42
  1475. X    {
  1476. X        fprintf( stderr, "sps - Can't open symbol file %s", filesymbol);
  1477. X        sysperror() ;
  1478. X    }
  1479. X    /* Get kernel addresses */
  1480. X    (void)nlist( filesymbol, np0 ) ;              
  1481. X    if ( np0[0].n_value == -1 )
  1482. X    {
  1483. X        fprintf( stderr, "sps - Can't read symbol file %s", filesymbol);
  1484. X        sysperror() ;
  1485. X    }
  1486. X# endif KVM
  1487. X    for ( s = Symbollist, np = np0 ; s->s_kname ; s++, np++ )
  1488. X    {                                       
  1489. X        if ( !np->n_value )             
  1490. X        {
  1491. X            fprintf( stderr, "sps - Can't find symbol %s in %s",
  1492. X                np->n_name, filesymbol ) ;
  1493. X            /* Assume this error to be unimportant if the address
  1494. X               is only associated with a process wait state.
  1495. X               This may happen if the system has been configured
  1496. X               without a particular device. */
  1497. X            fprintf( stderr, &Info.i_waitstate[ 0 ] <= s->s_info
  1498. X                && s->s_info < &Info.i_waitstate[ NWAITSTATE ]
  1499. X                ? " (error is not serious)\n"
  1500. X                : " (ERROR MAY BE SERIOUS)\n" ) ;
  1501. X            *s->s_info = (caddr_t)0 ;
  1502. X            continue ;
  1503. X        }
  1504. X        /* If no indirection is required, just copy the obtained value
  1505. X           into the `Info' structure. */
  1506. X        if ( !s->s_indirect )           
  1507. X        {                               
  1508. X        /* DUBIOUS ASSUMPTION THAT KMEM VALUE HAS SIZE OF A CADDR_T */
  1509. X            *s->s_info = (caddr_t)np->n_value ;
  1510. X            continue ;              
  1511. X        }                               
  1512. X        /* Otherwise one level of indirection is required. Using the
  1513. X           obtained address, look again in the kernel for the value */
  1514. X        /* DUBIOUS ASSUMPTION THAT KMEM VALUE HAS SIZE OF A CADDR_T */
  1515. X        (void)getkmem( (long)np->n_value, (char*)s->s_info,
  1516. X            sizeof(caddr_t) ) ;
  1517. X    }
  1518. X    free( (char*)np0 ) ;
  1519. X}
  1520. ---END-OF-initsymbols.c---
  1521. LEN=`wc -c < initsymbols.c`
  1522. if [ $LEN !=  3515 ] ; then
  1523.     echo shar: File "initsymbols.c" was $LEN, should have been 3515 bytes
  1524. fi
  1525. if [ -f 'inittty.c' -a "${1}" != "-c" ] ; then
  1526.     echo shar: Won\'t overwrite existing file "inittty.c"
  1527.     exit 2
  1528. fi
  1529. echo x - inittty.c
  1530. sed -e 's/^X//' > inittty.c << '---END-OF-inittty.c---'
  1531. X# ifndef lint
  1532. Xstatic char SccsId[] =  "@(#)inittty.c    1.1\t10/1/88" ;
  1533. X# endif
  1534. X
  1535. X# include       "sps.h"
  1536. X# include       <h/conf.h>
  1537. X# include       <h/ioctl.h>
  1538. X# ifdef SUNOS40
  1539. X# include       <h/stream.h>
  1540. X# else
  1541. X# include       <h/tty.h>
  1542. X# endif
  1543. X# include       <sys/stat.h>
  1544. X# include       <stdio.h>
  1545. X
  1546. X/* INITTTY - Initialise the tty part of the info structure */
  1547. Xinittty ()
  1548. X{
  1549. X    register struct ttyline *lp ;
  1550. X# ifdef BSD42
  1551. X    register struct direct  *dp ;
  1552. X    DIR                     *dfd ;
  1553. X# else
  1554. X    struct direct           dir ;
  1555. X    FILE                    *dfd ;
  1556. X# endif
  1557. X    struct stat             statbuf ;
  1558. X    static char             filedev[] = FILE_DEV ;
  1559. X    extern struct info      Info ;
  1560. X# ifdef BSD42
  1561. X    DIR                     *opendir() ;
  1562. X    struct direct           *readdir() ;
  1563. X# else
  1564. X    FILE                    *fopen() ;
  1565. X# endif
  1566. X
  1567. X    lp = Info.i_ttyline ;
  1568. X# ifdef BSD42
  1569. X    if ( !(dfd = opendir( filedev )) )
  1570. X# else
  1571. X    if ( !(dfd = fopen( filedev, "r" )) )
  1572. X# endif
  1573. X        prexit( "Can't open %s\n", filedev ) ;
  1574. X    if ( chdir( filedev ) < 0 )
  1575. X        prexit( "sps - Can't chdir to %s\n", filedev ) ;
  1576. X# ifdef BSD42
  1577. X    /* Read all entries in the device directory, looking for ttys */
  1578. X    while ( dp = readdir( dfd ) )
  1579. X    {       /* Skip entries that do not match "tty" or "console" */
  1580. X        if ( strncmp( "tty", dp->d_name, 3 )
  1581. X        &&   strcmp( "console", dp->d_name ) )
  1582. X            continue ;
  1583. X        /* Skip "tty" itself */
  1584. X        if ( dp->d_namlen == 3 )
  1585. X            continue ;
  1586. X# ifdef CHAOS
  1587. X        /* Skip chaos ttys ; they are accessed during ttystatus() */
  1588. X        if ( dp->d_namelen > 3 &&
  1589. X        dp->d_name[ sizeof( "tty" ) - 1 ] == 'C' )
  1590. X            continue ;
  1591. X# endif
  1592. X        if ( lp >= &Info.i_ttyline[ MAXTTYS ] )
  1593. X            prexit( "sps - Too many ttys in %s\n", filedev ) ;
  1594. X        /* Copy the tty name into the information entry */
  1595. X        if ( !strcmp( dp->d_name, "console" ) )
  1596. X        {
  1597. X            lp->l_name[0] = 'c' ;
  1598. X            lp->l_name[1] = 'o' ;
  1599. X        }
  1600. X        else
  1601. X        {
  1602. X            lp->l_name[0] = dp->d_name[3] ;
  1603. X            lp->l_name[1] = dp->d_name[4] ;
  1604. X        }
  1605. X        /* Ensure that this tty is actually a valid character device */
  1606. X        if ( stat( dp->d_name, &statbuf ) < 0 )
  1607. X            continue ;
  1608. X# else
  1609. X    /* Read all entries in the device directory, looking for ttys */
  1610. X    while ( fread( (char*)&dir, sizeof( struct direct ), 1, dfd ) == 1 )
  1611. X    {       /* Skip entries that do not match "tty" or "console" */
  1612. X        if ( strncmp( "tty", dir.d_name, 3 )
  1613. X        &&   strcmp( "console", dir.d_name ) )
  1614. X            continue ;
  1615. X        /* Skip "tty" itself */
  1616. X        if ( dir.d_name[3] == '\0' )
  1617. X            continue ;
  1618. X# ifdef CHAOS
  1619. X        /* Skip chaos ttys ; they are accessed during ttystatus() */
  1620. X        if ( dir.d_name[ sizeof( "tty" ) - 1 ] == 'C' )
  1621. X            continue ;
  1622. X# endif
  1623. X        if ( lp >= &Info.i_ttyline[ MAXTTYS ] )
  1624. X            prexit( "sps - Too many ttys in %s\n", filedev ) ;
  1625. X        /* Copy the tty name into the information entry */
  1626. X        if ( !strcmp( dir.d_name, "console" ) )
  1627. X        {
  1628. X            lp->l_name[0] = 'c' ;
  1629. X            lp->l_name[1] = 'o' ;
  1630. X        }
  1631. X        else
  1632. X        {
  1633. X            lp->l_name[0] = dir.d_name[3] ;
  1634. X            lp->l_name[1] = dir.d_name[4] ;
  1635. X        }
  1636. X        /* Ensure that this tty is actually a valid character device */
  1637. X        if ( stat( dir.d_name, &statbuf ) < 0 )
  1638. X            continue ;
  1639. X# endif
  1640. X        if ( (statbuf.st_mode & S_IFMT) != S_IFCHR )
  1641. X            continue ;
  1642. X        /* Find the device # of the tty and the address of its
  1643. X           associated struct tty in /dev/kmem. */
  1644. X        lp->l_dev = statbuf.st_rdev ;
  1645. X        if ( getkmem ( (long)&Info.i_cdevsw[ major( statbuf.st_rdev ) ]
  1646. X# ifdef SUNOS40
  1647. X            .d_str,
  1648. X# else
  1649. X            .d_ttys,
  1650. X# endif
  1651. X        (char*)&lp->l_addr, sizeof( lp->l_addr ) )
  1652. X        != sizeof( lp->l_addr ) )
  1653. X        {
  1654. X            fprintf( stderr, "sps - Can't read struct tty for %s\n",
  1655. X# ifdef BSD42
  1656. X                dp->d_name ) ;
  1657. X# else
  1658. X                dir.d_name ) ;
  1659. X# endif
  1660. X            continue ;
  1661. X        }
  1662. X# ifndef SUNOS40
  1663. X        lp->l_addr += (int)minor( statbuf.st_rdev ) ;
  1664. X# endif
  1665. X        lp++ ;
  1666. X    }
  1667. X# ifdef BSD42
  1668. X    (void)closedir( dfd ) ;
  1669. X# else
  1670. X    (void)fclose( dfd ) ;
  1671. X# endif
  1672. X}
  1673. ---END-OF-inittty.c---
  1674. LEN=`wc -c < inittty.c`
  1675. if [ $LEN !=  3716 ] ; then
  1676.     echo shar: File "inittty.c" was $LEN, should have been 3716 bytes
  1677. fi
  1678. if [ -f 'main.c' -a "${1}" != "-c" ] ; then
  1679.     echo shar: Won\'t overwrite existing file "main.c"
  1680.     exit 2
  1681. fi
  1682. echo x - main.c
  1683. sed -e 's/^X//' > main.c << '---END-OF-main.c---'
  1684. X# ifndef lint
  1685. Xstatic char SccsId[] =  "@(#)main.c    1.1\t10/1/88" ;
  1686. X# endif
  1687. X
  1688. X# include       "sps.h"
  1689. X# include       "flags.h"
  1690. X# ifdef KVM
  1691. X# include       <kvm.h>
  1692. X# include       <fcntl.h>
  1693. X# endif KVM
  1694. X# ifndef SUNOS40
  1695. X# include       <h/text.h>
  1696. X# endif
  1697. X# include       <sys/stat.h>
  1698. X# include       <stdio.h>
  1699. X
  1700. X
  1701. X/* SPS - Show Process Status */
  1702. X
  1703. X/* J. R. Ward - Hasler AG, Bern, Switzerland         - 24 May 1985 */
  1704. X/*                                  - 26 Nov 1986 */
  1705. X/* J. R. Ward - Olsen & Associates, Zuerich, Switzerland -  1 Oct 1988 */
  1706. X/* <robert@olsen.uucp> */
  1707. X
  1708. X/* NFS additions and SunOS4.0 support by Alexander Dupuy
  1709. X   <dupuy@ncs.columbia.edu> and Charlie Kim <cck@cunixc.cc.columbia.edu>.
  1710. X   Ultrix 2.x support by Rob Lehman at CUCCA. */
  1711. X
  1712. Xmain ( argc,argv )
  1713. X
  1714. Xint                             argc ;
  1715. Xchar                            **argv ;
  1716. X
  1717. X{
  1718. X    register struct process *plist ;
  1719. X    register struct process *process ;
  1720. X# ifndef SUNOS40
  1721. X    register struct text    *text ;
  1722. X# endif
  1723. X    int                     flinfo ;
  1724. X    char            *fileinfo, *filesymbol ;
  1725. X    struct stat        sinfo, ssymbol ;
  1726. X# ifdef WARNPASSWD
  1727. X    struct stat        spasswd ;
  1728. X# endif
  1729. X    extern struct flags     Flg ;
  1730. X    extern struct info      Info ;
  1731. X# ifdef KVM
  1732. X    extern kvm_t           *Flkvm ;
  1733. X# else
  1734. X    extern int              Flmem ;
  1735. X    extern int              Flkmem ;
  1736. X    extern int              Flswap ;
  1737. X# endif
  1738. X    char                    *getcore() ;
  1739. X    struct process          *needed(), *mktree() ;
  1740. X
  1741. X    /* Renice as fast as possible for root only (Suggested by Jeff Mogul,
  1742. X       gregorio!mogul) */
  1743. X    if ( !getuid() )
  1744. X        (void)nice( -40 ) ;
  1745. X    /* Decode the flag arguments */
  1746. X    flagdecode( argc, argv ) ;      
  1747. X    /* Determine the terminal width */
  1748. X    if ( !Flg.flg_w && !Flg.flg_N && !Flg.flg_i )
  1749. X        termwidth() ;
  1750. X    /* Open the cpu physical memory, kernel virtual memory and swap device*/
  1751. X# ifdef KVM
  1752. X    Flkvm = kvm_open( Flg.flg_s, Flg.flg_k, NULL, O_RDONLY, "sps" ) ;
  1753. X# else
  1754. X    if ( Flg.flg_k )
  1755. X    {
  1756. X        Flmem = openfile( Flg.flg_k ) ;
  1757. X        Flkmem = Flmem ;
  1758. X    }
  1759. X    else
  1760. X    {
  1761. X        Flmem = openfile( FILE_MEM ) ;
  1762. X        Flkmem = openfile( FILE_KMEM ) ;
  1763. X        if ( !Flg.flg_o )
  1764. X            Flswap = openfile( FILE_SWAP ) ;
  1765. X    }
  1766. X# endif
  1767. X    if ( Flg.flg_i )
  1768. X    {       /* -i flag for info file initialisation */
  1769. X        initialise() ;          
  1770. X        exit( 0 ) ;
  1771. X    }
  1772. X    /* Check that the information file is newer than the symbol and
  1773. X       password files, suggested by gregorio!mogul */
  1774. X    fileinfo = Flg.flg_j ? Flg.flg_j : FILE_INFO ;
  1775. X    filesymbol = Flg.flg_s ? Flg.flg_s : FILE_SYMBOL ;
  1776. X    flinfo = openfile( fileinfo ) ;
  1777. X    (void)fstat( flinfo, &sinfo ) ;
  1778. X    if ( !stat( filesymbol, &ssymbol ) &&
  1779. X        sinfo.st_mtime < ssymbol.st_mtime )
  1780. X        fprintf( stderr,
  1781. X           "sps - WARNING: Info file `%s' is older than symbol file `%s'\n",
  1782. X            fileinfo, filesymbol ) ;
  1783. X# ifdef WARNPASSWD
  1784. X    if ( !stat( FILE_PASSWD, &spasswd ) &&
  1785. X        sinfo.st_mtime < spasswd.st_mtime )
  1786. X        fprintf( stderr,
  1787. X           "sps - WARNING: Info file `%s' is older than passwd file `%s'\n",
  1788. X            fileinfo, FILE_PASSWD ) ;
  1789. X# endif
  1790. X    /* Read the information file */
  1791. X    if ( read( flinfo, (char*)&Info, sizeof( struct info ) )
  1792. X    != sizeof( struct info ) )
  1793. X    {
  1794. X        fprintf( stderr, "sps - Can't read info file `%s'", fileinfo ) ;
  1795. X        sysperror() ;
  1796. X    }
  1797. X    (void)close( flinfo ) ;
  1798. X    /* Find current tty status */
  1799. X    ttystatus() ;                   
  1800. X    /* Now that we know the available ttys, decode the flags */
  1801. X    flagsetup() ;                   
  1802. X    process = (struct process*)getcore(Info.i_nproc*sizeof(struct process));
  1803. X# ifndef SUNOS40
  1804. X    text = (struct text*)getcore( Info.i_ntext * sizeof( struct text ) ) ;
  1805. X# endif
  1806. X    do
  1807. X    {       /* Read current process status */
  1808. X# ifdef SUNOS40
  1809. X        readstatus( process ) ;
  1810. X        /* Select those processes to be listed */
  1811. X        plist = needed( process ) ;
  1812. X# else
  1813. X        readstatus( process, text ) ;
  1814. X        /* Select those processes to be listed */
  1815. X        plist = needed( process, text ) ;
  1816. X# endif
  1817. X        /* Form a tree of listed processes */
  1818. X        plist = mktree( process, plist ) ;
  1819. X        if ( !Flg.flg_N )
  1820. X        {       /* Print the processes */
  1821. X            prheader() ;
  1822. X            printall( plist, 0 ) ;
  1823. X        }
  1824. X        prsummary() ;
  1825. X        (void)fflush( stdout ) ;
  1826. X        if ( Flg.flg_r )        
  1827. X        {       /* If repeating, again get tty status */
  1828. X            ttystatus() ;
  1829. X            if ( Flg.flg_rdelay )
  1830. X# ifdef BSD42
  1831. X                sleep( Flg.flg_rdelay ) ;
  1832. X# else
  1833. X                sleep( (int)Flg.flg_rdelay ) ;
  1834. X# endif
  1835. X        }
  1836. X    } while ( Flg.flg_r ) ;
  1837. X    exit( 0 ) ;
  1838. X}
  1839. ---END-OF-main.c---
  1840. LEN=`wc -c < main.c`
  1841. if [ $LEN !=  4246 ] ; then
  1842.     echo shar: File "main.c" was $LEN, should have been 4246 bytes
  1843. fi
  1844. if [ -f 'mktree.c' -a "${1}" != "-c" ] ; then
  1845.     echo shar: Won\'t overwrite existing file "mktree.c"
  1846.     exit 2
  1847. fi
  1848. echo x - mktree.c
  1849. sed -e 's/^X//' > mktree.c << '---END-OF-mktree.c---'
  1850. X# ifndef lint
  1851. Xstatic char SccsId[] =  "@(#)mktree.c    1.1\t10/1/88" ;
  1852. X# endif
  1853. X
  1854. X# include       "sps.h"
  1855. X
  1856. X/*
  1857. X** MKTREE - Sort the needed processes by subtree and at the top by user.
  1858. X** This procedure takes a list of processes (as returned by needed())
  1859. X** and returnes a pointer to a sorted list.
  1860. X*/
  1861. Xstruct process  *mktree ( process, plist )
  1862. X
  1863. Xstruct process                  *process ;
  1864. Xstruct process                  *plist ;
  1865. X
  1866. X{
  1867. X    register struct process *p ;
  1868. X    register struct process *pp ;
  1869. X    register struct process *lp ;
  1870. X    struct process          *op ;
  1871. X    struct process          proot ;
  1872. X
  1873. X    proot.pr_sibling = (struct process*)0 ;
  1874. X    for ( p = plist ; p ; p = p->pr_plink )
  1875. X    {
  1876. X        if ( p->pr_pptr > &process[1] )
  1877. X        {
  1878. X            for ( pp = plist ; pp ; pp = pp->pr_plink )
  1879. X            {
  1880. X                if ( pp != p->pr_pptr )
  1881. X                    continue ;
  1882. X                if ( lp = pp->pr_child )
  1883. X                {       /* Does process have children ? */
  1884. X                    op = (struct process*)0 ;
  1885. X                    while (lp &&
  1886. X                    lp->pr_p.p_pid < p->pr_p.p_pid )
  1887. X                    {
  1888. X                        op = lp ;
  1889. X                        lp=lp->pr_sibling ;
  1890. X                    }
  1891. X                    if ( op )
  1892. X                    {
  1893. X                        p->pr_sibling = lp ;
  1894. X                        op->pr_sibling = p ;
  1895. X                        break ;
  1896. X                    }
  1897. X                }       
  1898. X                p->pr_sibling = lp ;
  1899. X                pp->pr_child = p ;
  1900. X                break ;
  1901. X            }
  1902. X            if ( pp )
  1903. X                continue ;
  1904. X        }
  1905. X        /* We have a top level process, sort into top level list.
  1906. X           The top level is sorted firstly by user-id and then
  1907. X           by process-id. */
  1908. X        lp = &proot ;
  1909. X        pp = lp->pr_sibling ;
  1910. X        while ( pp )
  1911. X        {
  1912. X            if ( p->pr_p.p_uid < pp->pr_p.p_uid )
  1913. X                break ;
  1914. X            if ( p->pr_p.p_uid == pp->pr_p.p_uid
  1915. X            && p->pr_p.p_pid < pp->pr_p.p_pid )
  1916. X                break ;
  1917. X            lp = pp, pp = pp->pr_sibling ;
  1918. X        }
  1919. X        p->pr_sibling = lp->pr_sibling ;
  1920. X        lp->pr_sibling = p ;
  1921. X    }
  1922. X    return ( proot.pr_sibling ) ;
  1923. X}
  1924. ---END-OF-mktree.c---
  1925. LEN=`wc -c < mktree.c`
  1926. if [ $LEN !=  1703 ] ; then
  1927.     echo shar: File "mktree.c" was $LEN, should have been 1703 bytes
  1928. fi
  1929. if [ -f 'needed.c' -a "${1}" != "-c" ] ; then
  1930.     echo shar: Won\'t overwrite existing file "needed.c"
  1931.     exit 2
  1932. fi
  1933. echo x - needed.c
  1934. sed -e 's/^X//' > needed.c << '---END-OF-needed.c---'
  1935. X# ifndef lint
  1936. Xstatic char SccsId[] =  "@(#)needed.c    1.3\t7/4/90" ;
  1937. X# endif
  1938. X
  1939. X# include       "sps.h"
  1940. X# include       "flags.h"
  1941. X# ifndef SUNOS40
  1942. X# include       <h/text.h>
  1943. X# endif
  1944. X# include       <stdio.h>
  1945. X
  1946. X/*
  1947. X** NEEDED - Determine which processes are needed for the printout
  1948. X** and add these to a list of needed processes.
  1949. X*/
  1950. X# ifdef SUNOS40
  1951. Xstruct process  *needed ( process )
  1952. X
  1953. Xregister struct process         *process ;
  1954. X
  1955. X# else
  1956. X
  1957. Xstruct process  *needed ( process, text )
  1958. X
  1959. Xregister struct process         *process ;
  1960. Xstruct text                     *text ;
  1961. X
  1962. X# endif
  1963. X{
  1964. X    register struct process *p ;
  1965. X    register struct process *plist ;
  1966. X    struct process          *lastp ;
  1967. X    int                     uid ;
  1968. X    extern struct flags     Flg ;
  1969. X    extern union userstate  User ;
  1970. X    extern struct info      Info ;
  1971. X    extern struct ttyline   Notty ;
  1972. X    struct ttyline          *findtty() ;
  1973. X    char                    *getcmd() ;
  1974. X
  1975. X    plist = (struct process*)0 ;
  1976. X    lastp = &process[ Info.i_nproc ] ;
  1977. X    /* Normalise internal pointers from kernel addresses. For each kmem
  1978. X       address in the `proc' and `text' structures, we convert that
  1979. X       address for our own internal use. */
  1980. X    for ( p = process ; p < lastp ; p++ )
  1981. X    {                               
  1982. X        if ( !p->pr_p.p_stat )  
  1983. X            continue ;
  1984. X# ifndef SUNOS40
  1985. X        /* Normalise internal text pointers */
  1986. X        if ( p->pr_p.p_textp )
  1987. X            p->pr_p.p_textp = &text[p->pr_p.p_textp - Info.i_text0];
  1988. X# endif
  1989. X        /* Normalise internal linked list of processes */
  1990. X        p->pr_plink = p->pr_p.p_link ?
  1991. X            &process[ p->pr_p.p_link  - Info.i_proc0 ] :
  1992. X            (struct process*)0 ;
  1993. X        /* Normalise internal parent pointers */
  1994. X        p->pr_pptr = p->pr_p.p_pptr ?
  1995. X            &process[ p->pr_p.p_pptr - Info.i_proc0 ] :
  1996. X            (struct process*)0 ;
  1997. X        /* Check for valid parent pointers */
  1998. X        if ( !p->pr_pptr )
  1999. X        {
  2000. X            p->pr_pptr = process ;
  2001. X            continue ;
  2002. X        }
  2003. X        if ( p->pr_pptr < process || p->pr_pptr >= lastp )
  2004. X        {
  2005. X            fprintf( stderr, "sps - process %d has bad pptr\n",
  2006. X                p->pr_p.p_pid ) ;
  2007. X            p->pr_pptr = process ;
  2008. X        }
  2009. X    }
  2010. X    /* For each process, see if it is a candidate for selection.
  2011. X       If so, retrieve its command arguments and upage information. */
  2012. X    uid = getuid() ;
  2013. X    for ( p = process ; p < lastp ; p++ )
  2014. X    {                               
  2015. X        if ( !p->pr_p.p_stat )
  2016. X            continue ;
  2017. X        /* Count processes and sizes */
  2018. X        summarise( p ) ;
  2019. X        /* Select the given processes. Bear in mind that selection
  2020. X           of processes based on the `F' and `T' flags must be
  2021. X           postponed until the upage is accessed. */
  2022. X        if ( !Flg.flg_F && !Flg.flg_T && !selectproc( p, process, uid ))
  2023. X            continue ;
  2024. X        /* Try to find the process' command arguments. Accessing the
  2025. X           arguments also involves retrieving the upage. */
  2026. X        p->pr_cmd = getcmd( p ) ;
  2027. X        /* If the upage was found successfully, use this information */
  2028. X        if ( p->pr_upag )       
  2029. X        {
  2030. X# ifdef BSD42
  2031. X            p->pr_rself = User.u_us.u_ru ;
  2032. X            p->pr_rchild = User.u_us.u_cru ;
  2033. X# else
  2034. X            p->pr_vself = User.u_us.u_vm ;
  2035. X            p->pr_vchild = User.u_us.u_cvm ;
  2036. X# endif
  2037. X            p->pr_tty = findtty( p ) ;
  2038. X            p->pr_files = filecount( p ) ;
  2039. X        }
  2040. X        else
  2041. X            p->pr_tty = &Notty ;
  2042. X        /* Select on the basis of the `F' and `T' flags */
  2043. X        if ( Flg.flg_F          
  2044. X        && !(p->pr_p.p_pgrp && p->pr_p.p_pgrp == p->pr_tty->l_pgrp) )
  2045. X            continue ;
  2046. X        if ( Flg.flg_T && !selecttty( p ) )
  2047. X            continue ;
  2048. X        /* Arrive here with a selected process. Add this to the
  2049. X           linked list of needed processes. */
  2050. X        p->pr_plink = plist ;   
  2051. X        plist = p ;
  2052. X        p->pr_child = (struct process*)0 ;
  2053. X        p->pr_sibling = (struct process*)0 ;
  2054. X    }
  2055. X    return ( plist ) ;
  2056. X}
  2057. X
  2058. X/* SUMMARISE - Summarises the given process into the `Summary' structure */
  2059. X/*
  2060. X** SHOULD ACCOUNT HERE FOR THE SIZE OF LOADED PAGE TABLES, BUT WE DON'T REALLY
  2061. X** KNOW THEIR RESIDENT SIZES.
  2062. X*/
  2063. Xsummarise ( p )
  2064. X
  2065. Xregister struct process         *p ;
  2066. X
  2067. X{
  2068. X# ifndef SUNOS40
  2069. X    register struct text    *tp ;
  2070. X# endif
  2071. X    int                     busy ;
  2072. X    extern struct summary   Summary ;
  2073. X
  2074. X    Summary.sm_ntotal++ ;
  2075. X    if ( p->pr_p.p_stat == SZOMB )
  2076. X        return ;
  2077. X    /* Firstly, account for processes */
  2078. X# if defined(OLDSTATS) || !defined(SUNOS40)
  2079. X    Summary.sm_ktotal += p->pr_p.p_dsize + p->pr_p.p_ssize ;
  2080. X# else
  2081. X    seg_count( p ) ;        /* count up process pages */
  2082. X    
  2083. X    Summary.sm_ktotal += p->pr_private + p->pr_shared ;
  2084. X# endif
  2085. X    Summary.sm_kloaded += p->pr_p.p_rssize ;
  2086. X    Summary.sm_kswapped += p->pr_p.p_swrss ;
  2087. X    if ( p->pr_p.p_flag & SLOAD )
  2088. X        Summary.sm_nloaded++ ;
  2089. X    else
  2090. X        Summary.sm_nswapped++ ;
  2091. X    busy = (p->pr_p.p_stat == SRUN) || (p->pr_p.p_stat==SSLEEP
  2092. X         && (p->pr_p.p_pri<PZERO && p->pr_p.p_pid > MSPID) ) ;
  2093. X# ifdef SUNOS40
  2094. X    /* Ignore the idle processes */
  2095. X    if ( p->pr_p.p_pid == 3 || p->pr_p.p_pid == 4 )
  2096. X        busy = 0 ;
  2097. X# endif SUNOS40
  2098. X    if ( busy )
  2099. X    {
  2100. X        Summary.sm_nbusy++ ;
  2101. X# if defined(OLDSTATS) || !defined(SUNOS40)
  2102. X        Summary.sm_kbusy += p->pr_p.p_dsize + p->pr_p.p_ssize ;
  2103. X# else
  2104. X        Summary.sm_kbusy += p->pr_private + p->pr_shared ;
  2105. X# endif
  2106. X    }
  2107. X# ifndef SUNOS40
  2108. X    /* Now account for their texts */
  2109. X    if ( !(tp = p->pr_p.p_textp) || !tp->x_count )
  2110. X        return ;                
  2111. X    Summary.sm_ktotal += tp->x_size ;
  2112. X    Summary.sm_kloaded += tp->x_rssize ;
  2113. X    Summary.sm_kswapped += tp->x_swrss ;
  2114. X    if ( busy )
  2115. X        Summary.sm_kbusy += tp->x_size ;
  2116. X    tp->x_count = 0 ;
  2117. X# endif
  2118. X}
  2119. ---END-OF-needed.c---
  2120. LEN=`wc -c < needed.c`
  2121. if [ $LEN !=  5151 ] ; then
  2122.     echo shar: File "needed.c" was $LEN, should have been 5151 bytes
  2123. fi
  2124. if [ -f 'openfiles.c' -a "${1}" != "-c" ] ; then
  2125.     echo shar: Won\'t overwrite existing file "openfiles.c"
  2126.     exit 2
  2127. fi
  2128. echo x - openfiles.c
  2129. sed -e 's/^X//' > openfiles.c << '---END-OF-openfiles.c---'
  2130. X# ifndef lint
  2131. Xstatic char SccsId[] =  "@(#)openfiles.c    1.1\t10/1/88" ;
  2132. X# endif
  2133. X
  2134. X# include       <stdio.h>
  2135. X# include       "sps.h"
  2136. X# include       "flags.h"
  2137. X# include       <varargs.h>
  2138. X# ifdef KVM
  2139. X# include       <kvm.h>
  2140. X# endif
  2141. X
  2142. X/* Miscellaneous procedures */
  2143. X
  2144. X/* OPENFILE - Opens the named file */
  2145. Xopenfile ( name )
  2146. X
  2147. Xchar                            *name ;
  2148. X
  2149. X{
  2150. X    register int            fd ;
  2151. X
  2152. X    if ( (fd = open( name, 0 )) >= 0 )
  2153. X        return ( fd ) ;
  2154. X    fprintf( stderr, "sps - Can't open %s", name ) ;
  2155. X    sysperror() ;
  2156. X    /* NOTREACHED */
  2157. X}
  2158. X
  2159. X# ifdef KVM
  2160. X
  2161. Xgetkmem ( addr, buf, bufsize )
  2162. X
  2163. Xlong                            addr ;
  2164. Xchar                            *buf ;
  2165. Xint                             bufsize ;
  2166. X{
  2167. X    extern kvm_t            *Flkvm ;
  2168. X
  2169. X    return( kvm_read( Flkvm, (long)addr, buf, bufsize ) ) ;
  2170. X}
  2171. X
  2172. X# else
  2173. X
  2174. Xgetkmem ( addr, buf, bufsize )
  2175. X
  2176. Xlong                            addr ;
  2177. Xchar                            *buf ;
  2178. Xint                             bufsize ;
  2179. X{
  2180. X    extern int              Flkmem ;
  2181. X
  2182. X    memseek( Flkmem, (long)addr ) ;
  2183. X    return( read( Flkmem, buf, bufsize ) ) ;
  2184. X}
  2185. X
  2186. X/* MEMSEEK - Seek on a special file */
  2187. Xmemseek ( fd, pos )
  2188. X
  2189. Xint                             fd ;
  2190. Xlong                            pos ;
  2191. X
  2192. X{
  2193. X    extern int              errno ;
  2194. X    extern struct flags     Flg ;
  2195. X    long                    lseek() ;
  2196. X
  2197. X    errno = 0 ;
  2198. X    if ( Flg.flg_k )
  2199. X# ifdef SUN
  2200. X        pos &= KERNELBASE - 1 ;
  2201. X# else
  2202. X        pos &= 0x7fffffff ;
  2203. X# endif
  2204. X    (void)lseek( fd, pos, 0 ) ;
  2205. X    if ( errno )
  2206. X    {
  2207. X        fprintf( stderr, "sps - Seek failed" ) ;
  2208. X        sysperror() ;
  2209. X    }
  2210. X}
  2211. X
  2212. X/* SWSEEK - Seek on the swap device */
  2213. Xswseek ( pos )
  2214. X
  2215. Xlong                            pos ;
  2216. X
  2217. X{
  2218. X    extern int              Flswap ;
  2219. X    extern int              errno ;
  2220. X    long                    lseek() ;
  2221. X
  2222. X    errno = 0 ;
  2223. X    (void)lseek( Flswap, pos, 0 ) ;
  2224. X    if ( errno )
  2225. X    {
  2226. X        fprintf( stderr, "sps - Seek failed" ) ;
  2227. X        sysperror() ;
  2228. X    }
  2229. X}
  2230. X
  2231. X# endif
  2232. X
  2233. X# ifdef lint
  2234. Xint                             errno ;
  2235. Xint                             sys_nerr ;
  2236. Xchar                            *sys_errlist[] ;
  2237. X# endif
  2238. X
  2239. X/* SYSPERROR - Reports a system defined error msg and then exits gracefully */
  2240. Xsysperror ()
  2241. X{
  2242. X    extern int              errno ;
  2243. X    extern int              sys_nerr ;
  2244. X    extern char             *sys_errlist[] ;
  2245. X
  2246. X    if ( 0 < errno && errno < sys_nerr )
  2247. X        fprintf( stderr, " : %s", sys_errlist[errno] ) ;
  2248. X    (void)fputc( '\n', stderr ) ;
  2249. X    exit( 1 ) ;
  2250. X}
  2251. X
  2252. X/* STRSAVE - Store a string in core for later use. */
  2253. Xchar    *strsave ( cp )
  2254. X
  2255. Xregister char                   *cp ;
  2256. X
  2257. X{
  2258. X    register char           *chp ;
  2259. X    char                    *getcore(), *strcpy() ;
  2260. X
  2261. X    chp = getcore( strlen( cp ) + 1 ) ;
  2262. X    (void)strcpy( chp, cp ) ;
  2263. X    return ( chp ) ;
  2264. X}
  2265. X
  2266. X/* GETCORE - Allocate and return a pointer to the asked for amount of core */
  2267. Xchar    *getcore ( size )
  2268. X
  2269. Xregister int                    size ;
  2270. X
  2271. X{
  2272. X    register char           *chp ;
  2273. X    char                    *malloc() ;
  2274. X
  2275. X    if ( chp = malloc( (unsigned)size ) )
  2276. X        return ( chp ) ;
  2277. X    fprintf( stderr, "sps - Out of core" ) ;
  2278. X    sysperror() ;
  2279. X    /* NOTREACHED */
  2280. X}
  2281. X
  2282. Xunion flaglist  *getflgsp ( argc )
  2283. X
  2284. Xregister int                    argc ;
  2285. X
  2286. X{
  2287. X    char                    *getcore() ;
  2288. X
  2289. X    return ( (union flaglist*)getcore( sizeof( union flaglist )*argc ) ) ;
  2290. X}
  2291. X
  2292. X/* PREXIT - Print an error message and exit */
  2293. X/* VARARGS */
  2294. X/* ARGSUSED */
  2295. Xprexit ( va_alist )
  2296. X
  2297. Xva_dcl
  2298. X
  2299. X{
  2300. X    char                    *fmt ;
  2301. X    va_list                  args ;
  2302. X
  2303. X    va_start( args ) ;
  2304. X    fmt = va_arg( args, char * ) ;
  2305. X
  2306. X    vfprintf( stderr, fmt, args ) ;
  2307. X    exit( 1 ) ;
  2308. X}
  2309. X
  2310. X# ifndef VPRINTF
  2311. X
  2312. Xint vfprintf ( filep, fmt, args )
  2313. X
  2314. XFILE                            *filep ;
  2315. Xchar                            *fmt ;
  2316. Xva_list                          args ;
  2317. X
  2318. X{
  2319. X    _doprnt( fmt, args, filep ) ;    
  2320. X    return( ferror( filep ) ? EOF : 0 ) ;
  2321. X}
  2322. X
  2323. X# endif
  2324. ---END-OF-openfiles.c---
  2325. LEN=`wc -c < openfiles.c`
  2326. if [ $LEN !=  3718 ] ; then
  2327.     echo shar: File "openfiles.c" was $LEN, should have been 3718 bytes
  2328. fi
  2329. exit 0
  2330.