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

  1. /*
  2.  * names.c -- lookup names via nlist
  3.  *
  4.  * Copyright (C) 1986, 1990  Philip L. Budne
  5.  *
  6.  * This file is part of "Phil's Finger Program".
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 1, or (at your option)
  11.  * any later version.
  12.  *
  13.  */
  14.  
  15. # ifndef lint
  16. static char *rcsid = "$Id: names.c,v 3.0 90/07/06 13:11:24 budd Rel $";
  17. # endif /* lint not defined */
  18.  
  19. # include "finger.h"
  20.  
  21. # ifdef DEBUGSW
  22. # define IFDEBUG( x ) if( sw_debug ) printf x
  23. # else  /* DEBUGSW not defined */
  24. # define IFDEBUG( x )
  25. # endif /* DEBUGSW not defined */
  26.  
  27. # if Umax != 42                /* the rest of the file.... */
  28.  
  29. # ifdef USG
  30. # include <fcntl.h>
  31. /* TODO: <sys/var.h> check info_proc vs. ((proc *)v.ve_proc) - v.v_proc */
  32. # else  /* USG not defined */
  33. # include <sys/file.h>
  34. # endif /* USG not defined */
  35. # include <sys/types.h>
  36. # include <sys/stat.h>
  37. # ifdef AUX
  38. # include <a.out.h>
  39. # else  /* AUX not defined */
  40. # include <nlist.h>
  41. # endif /* AUX not defined */
  42. # include <stdio.h>
  43.  
  44. # include "args.h"
  45. # include "info.h"            /* after finger.h */
  46. # include "kmem.h"
  47.  
  48. long symdate =
  49. # include "symdate.h"
  50. ;
  51.  
  52. GLOBAL struct info I;
  53.  
  54. extern FTYPE kmem;            /* from kmem.c */
  55.  
  56. extern char longversion[];        /* from version.c */
  57. extern char *sys_errlist[];        /* from library */
  58. extern int errno;            /* from library */
  59.  
  60. struct nlist nl[] = {            /* nlist table */
  61. # ifdef UNDERSCORE_NLIST_NAMES
  62. # define SYM(s,sc,m) { s },
  63. # else  /* UNDERSCORE_NLIST_NAMES not defined */
  64. # define SYM(s,sc,m) { sc },
  65. # endif /* UNDERSCORE_NLIST_NAMES not defined */
  66. # include "syms.h"
  67. # undef SYM
  68.     ""
  69. };
  70. # define LNL ((sizeof(nl)/sizeof(struct nlist))-1)
  71.  
  72. unsigned long *nvptrs[] = {
  73. # define SYM(s,sc,m) CONC(&I.info_,m) ,    /* build table of pointers to values */
  74. # include "syms.h"
  75. # undef SYM
  76. };
  77.  
  78. LOCAL int readnlist(kfile)        /* force read of nlist */
  79. char *kfile;
  80. {
  81.     register int i;
  82.     struct stat kernst;
  83. # ifdef HAVE_VERSION
  84.     char kmem_verstr[ VERSTRLEN+1 ];
  85. # endif /* HAVE_VERSION defined */
  86.  
  87.     IFDEBUG(("getting namelist from %s\n", kfile ));
  88.  
  89.     if( nlist(kfile, nl) < 0 ) {
  90.     fprintf(stderr, "%%Could not read %s namelist\n", kfile );
  91.     return( FALSE );
  92.     }
  93.  
  94.     for( i = 0; i < LNL; i++ ) {
  95.     if(
  96. # ifndef COFF
  97.       nl[i].n_type == N_UNDF &&
  98. # endif /* COFF not defined */
  99.       nl[i].n_value == 0L ) {
  100.         fprintf(stderr, "%%No namelist entry for %s\n", nl[i].n_name );
  101.         return( FALSE );
  102.     } /* empty entry */
  103.     *nvptrs[i] = nl[i].n_value;
  104.     } /* for i */
  105.  
  106.     if( stat( kfile, &kernst ) == 0 ) {
  107.     I.info_kerneldate = kernst.st_mtime;
  108.     I.info_kernelsize = kernst.st_size;
  109.     I.info_symdate = symdate;
  110.     } /* stat */
  111.  
  112.     strncpy( I.info_fingerversion, longversion, sizeof( I.info_fingerversion));
  113. # ifdef HAVE_VERSION
  114.     bzero( I.info_verstr, sizeof( I.info_verstr ) );
  115.     bzero( kmem_verstr,  sizeof( kmem_verstr )  );
  116.  
  117.     /* save _version from /vmunix in I.info_verstr */
  118.     if( read_vmunix( KERNEL_FILE, I.info_version,
  119.             I.info_verstr, VERSTRLEN ) ) {
  120.     I.info_verstr[VERSTRLEN] = '\0';
  121.  
  122. # ifdef DEBUGSW
  123.     if( sw_debug ) {
  124.         printf("info_version %#x\n", I.info_version );
  125.         printf("%s:\n", kfile );
  126.         printf("%*s\n", -VERSTRLEN, I.info_verstr );
  127.     }
  128. # endif /* DEBUGSW defined */
  129.  
  130. # ifdef PICKY
  131.     /*
  132.      * be picky.... insist that running kernel
  133.      * contain same string at same location
  134.      */
  135.  
  136.     if( KMEMREAD(I.info_version, kmem_verstr,  VERSTRLEN ) ) {
  137.         kmem_verstr[VERSTRLEN] = '\0';
  138.  
  139. # ifdef DEBUGSW
  140.         if( sw_debug ) {
  141.         printf("kmem @ %#x:\n", I.info_version );
  142.         printf("%*s\n", -VERSTRLEN, kmem_verstr );
  143.         }
  144. # endif /* DEBUGSW defined */
  145.  
  146.         /* if match, we win */
  147.         if( strncmp( kmem_verstr, I.info_verstr, VERSTRLEN ) == 0 )
  148.         return( TRUE );
  149.     } /* kread ok */
  150. # endif /* PICKY defined */
  151.     } /* got version from /vmunix file */
  152. # ifdef PICKY
  153.     return( FALSE );
  154. # else  /* PICKY not defined */
  155.     return( TRUE );
  156. # endif /* PICKY not defined */
  157. # else  /* HAVE_VERSION not defined */
  158.     return( TRUE );
  159. # endif /* HAVE_VERSION not defined */
  160. } /* readnlist */
  161.  
  162. LOCAL int writenfile( kfile )
  163. char *kfile;
  164. {
  165.     int nfile;
  166.  
  167.     if( !readnlist( kfile ) )
  168.     return( FALSE );
  169.  
  170.     if( sw_nosave )            /* write prohibited? */
  171.     return( TRUE );            /* be nice */
  172.  
  173.     IFDEBUG(("writing %s\n", SAVED_NLIST ));
  174.     if( (nfile=open( SAVED_NLIST, O_TRUNC|O_CREAT|O_WRONLY,
  175.             NLIST_MODE)) >= 0 ) {
  176.     write( nfile, &I, sizeof( I ) );
  177. # ifndef USG
  178.     fchown( nfile, geteuid(), getegid() );
  179.     fchmod( nfile, NLIST_MODE );
  180. # else  /* USG defined */
  181.     chown( SAVED_NLIST, geteuid(), getegid() );
  182.     chmod( SAVED_NLIST, NLIST_MODE );
  183. # endif /* USG defined */
  184.     close( nfile );
  185.     } /* open for write */
  186.     else
  187.     fprintf( stderr, "%%Could not write %s (%s)\n",
  188.         SAVED_NLIST, sys_errlist[ errno ] );
  189.     return( TRUE );
  190. } /* writenfile */
  191.  
  192. GLOBAL int readnames() {    /* get nlist from save file or /vmunix */
  193. # ifdef SAVED_NLIST
  194.     int nfile, oknfile, oknlist;
  195.     struct stat savst, kernst;
  196.     char kmem_verstr[ VERSTRLEN+1 ];
  197.  
  198.     oknfile = oknlist = FALSE;
  199.  
  200.     /* check if SAVED_NLIST exists, is right size */
  201.     /* built from same copy of syms.h */
  202.     if( !sw_read ) {                /* not getting new save */
  203.     IFDEBUG(("%s: ", SAVED_NLIST ));
  204.     if( stat( SAVED_NLIST, &savst ) == 0 &&    /* got status of save file */
  205.        savst.st_size == sizeof( I ) ) {    /* data file is right size */
  206.         IFDEBUG(("size ok "));
  207.  
  208.         if( (nfile = open( SAVED_NLIST, O_RDONLY )) >= 0 ) { /* opened */
  209.         if( read( nfile, &I, sizeof( I ) ) == sizeof( I ) ) {
  210.             IFDEBUG(("read ok "));
  211.             if( I.info_symdate == symdate ) {/* same copy of syms.h? */
  212.             oknfile = TRUE;        /* doing fine */
  213.             IFDEBUG(("symdate ok "));
  214.             } /* same copy of syms.h */
  215. # ifdef DEBUGSW
  216.             else if( sw_debug )
  217.             printf("*symdate mismatch*");
  218. # endif /* DEBUGSW defined */
  219.  
  220.         }
  221.         close( nfile );
  222.         } /* open ok */
  223.     } /* stat + size ok */
  224.     IFDEBUG(("\n"));
  225.     } /* not sw_read */
  226.  
  227.     if( sw_read || !oknfile )        /* if no good fall back on /vmunix */
  228.     return( writenfile( KERNEL_FILE ) );
  229.  
  230. # ifdef AUTONLIST
  231.     /*
  232.      *    does version string in running kernel match that of
  233.      *  the kernel that was running when the nlist file was created?
  234.      */
  235. # ifdef HAVE_VERSION
  236.     if( I.info_version != 0L ) {        /* have addr for verstr */
  237.     bzero( kmem_verstr, sizeof( kmem_verstr ) );
  238.     if( KMEMREAD( I.info_version, kmem_verstr, VERSTRLEN ) ) {
  239. # ifdef DEBUGSW
  240.         if( sw_debug ) {
  241.         printf("kmem @ %#x:\n", I.info_version );
  242.         printf("%*s\n", -VERSTRLEN, kmem_verstr );
  243.         printf("info_verstr:\n");
  244.         printf("%*s\n", -VERSTRLEN, I.info_verstr );
  245.         } /* sw_debug */
  246. # endif /* DEBUGSW defined */
  247.         if( strncmp( I.info_verstr, kmem_verstr, VERSTRLEN ) == 0  )
  248.         oknlist = TRUE;            /* matches!! */
  249.     } /* kread ok */
  250.     } /* version != 0 */
  251.     else
  252. # endif /* HAVE_VERSION defined */
  253.     if( stat( KERNEL_FILE, &kernst ) == 0 &&   /* get kernel date/size */
  254.         savst.st_mtime > kernst.st_mtime &&      /* more recent than kernel */
  255.         I.info_kerneldate == kernst.st_mtime && /* same /vmunix */
  256.         I.info_kernelsize == kernst.st_size ) {
  257. # ifdef DEBUGSW
  258.     if( sw_debug )
  259.         printf("%s and %s kernel size and date match\n",
  260.            SAVED_NLIST, KERNEL_FILE );
  261. # endif /* DEBUGSW defined */
  262.     oknlist = TRUE;
  263.     } /* stat ok... */
  264.     if( !oknlist )
  265.     return( writenfile( KERNEL_FILE ) );
  266. # else  /* AUTONLIST not defined */
  267.     if( !oknlist )
  268.     return( readnlist( KERNEL_FILE ) );
  269. # endif /* AUTONLIST not defined */
  270.     return( TRUE );
  271. # else  /* SAVED_NLIST not defined */
  272.     return( readnlist( KERNEL_FILE ) );
  273. # endif /* SAVED_NLIST not defined */
  274. } /* readnames */
  275.  
  276. # endif /* Umax != 42 */
  277.  
  278. /*
  279.  * Local variables:
  280.  * comment-column: 40
  281.  * End:
  282.  */
  283.