<commit_before>/***************************************************
 * file: userconfig/uc_main.c
 *
 * Copyright (c) 1996 <NAME> (<EMAIL>)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software withough specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * library functions for userconfig library
 *
 * $Id: uc_main.c,v 1.9 1996/10/05 13:30:43 jkh Exp $
 */

#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <paths.h>
#include <sys/mman.h>
#include <nlist.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <tcl.h>

#include "uc_main.h"

extern int	isDebug(void);
extern void	msgDebug(char *fmt, ...);

static struct nlist _nl[] = {
    {"_isa_devtab_bio"},
    {"_isa_devtab_tty"},
    {"_isa_devtab_net"},
    {"_isa_devtab_null"},
    {"_isa_biotab_wdc"},
    {"_isa_biotab_fdc"},
    {"_eisadriver_set"},
    {"_eisa_dev_list"},
    {"_pcidevice_set"},
    {"_device_list"},
    {"_scbusses"},
    {"_scsi_cinit"},
    {"_scsi_dinit"},
    {"_scsi_tinit"},
    {""},
};

struct kernel *
uc_open(char *name){
    int kd, flags, incore;
    struct kernel *kern;
    struct stat sb;
    char kname[80];
    int size, i = 0;
    struct nlist *nl = _nl;

    if (strcmp(name, "-incore") == 0)
	incore = 1;
    else
	incore = 0;
    
    if (incore || (strcmp(name,"-bootfile") == 0))
	strncpy(kname, getbootfile(), 79);
    else
	strncpy(kname, name, 79);

    if (isDebug())
	msgDebug("uc_open: kernel name is %s, incore = %d\n", kname, incore);
    kern = (struct kernel *)malloc(sizeof(struct kernel));

#ifdef KERN_NO_SYMBOLS
    if (incore) {
	FILE *fp;

	fp = fopen("/stand/symbols", "r");
	if (!fp) {
	    msgDebug("Couldn't open /stand/symbols file!  Punting.\n");
	    free(kern);
	    return NULL;
	}
	if (fscanf(fp, "%d\n", &size) != 1) {
	    msgDebug("Unable to get # of name list entries from symbol file.\n");
	    free(kern);
	    return NULL;
	}
	nl = (struct nlist *)malloc((size + 1) * sizeof(struct nlist));
	for (i = 0; i < size; i++) {
	    char name[255];
	    int c1;
	    unsigned int uc1;
	    short d1;
	    unsigned long v1;

	    if (fscanf(fp, "%s %u %d %hd %ld\n", name, &uc1, &c1, &d1, &v1) == 5) {
		nl[i].n_name = strdup(name);
		nl[i].n_type = (unsigned char)uc1;
		nl[i].n_other = (char)c1;
		nl[i].n_desc = d1;
		nl[i].n_value = v1;
		if (isDebug())
		    msgDebug("Entry %d, decoded: \"%s\", %d %d %hd %ld\n", i, nl[i].n_name, nl[i].n_type, nl[i].n_other, nl[i].n_desc, nl[i].n_value);
	    }
	    else {
		nl[i].n_name = "";
		nl[i].n_type = 0;
		nl[i].n_other = 0;
		nl[i].n_desc = 0;
		nl[i].n_value = 0;
	    }
	}
	nl[i].n_name = NULL;
	fclose(fp);
    	kern->nl = nl;
	i = 0;
    }
    else
#endif
	i = nlist(kname, nl);
    if (i == -1) {
	msgDebug("uc_open: kernel %s does not contain symbols.\n", kname);
	free(kern);
	return NULL;
    }
#ifdef KERN_NO_SYMBOLS
    if (!incore) {
#else
    {
#endif
	kern->nl=(struct nlist *)malloc(sizeof(nl));
	bcopy(nl, kern->nl, sizeof(nl));
    }
	
    if (incore) {
	if ((kd=open("/dev/kmem", O_RDONLY)) < 0) {
	    free(kern);
	    kern = (struct kernel *)-3;
	    msgDebug("uc_open: Unable to open /dev/kmem.\n");
	    return kern;
	}
	kern->core = (caddr_t)NULL;
	kern->incore = 1;
	kern->size = 0;
    }
    else {
	if (stat(kname, &sb) < 0) {
	    free(kern);
	    kern = (struct kernel *)-1;
	    msgDebug("uc_open: Unable to stat %s.\n", kname);<commit_msg>Found my mistake:  I should have been copying from _nl in the on-disk case, not nl (size we take its size - d'oh!).<commit_after>/***************************************************
 * file: userconfig/uc_main.c
 *
 * Copyright (c) 1996 <NAME> (<EMAIL>)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software withough specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * library functions for userconfig library
 *
 * $Id: uc_main.c,v 1.10 1996/10/05 16:33:05 jkh Exp $
 */

#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <paths.h>
#include <sys/mman.h>
#include <nlist.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <tcl.h>

#include "uc_main.h"

extern int	isDebug(void);
extern void	msgDebug(char *fmt, ...);

static struct nlist _nl[] = {
    {"_isa_devtab_bio"},
    {"_isa_devtab_tty"},
    {"_isa_devtab_net"},
    {"_isa_devtab_null"},
    {"_isa_biotab_wdc"},
    {"_isa_biotab_fdc"},
    {"_eisadriver_set"},
    {"_eisa_dev_list"},
    {"_pcidevice_set"},
    {"_device_list"},
    {"_scbusses"},
    {"_scsi_cinit"},
    {"_scsi_dinit"},
    {"_scsi_tinit"},
    {""},
};

struct kernel *
uc_open(char *name){
    int kd, flags, incore;
    struct kernel *kern;
    struct stat sb;
    char kname[80];
    int size, i = 0;
    struct nlist *nl = _nl;

    if (strcmp(name, "-incore") == 0)
	incore = 1;
    else
	incore = 0;
    
    if (incore || (strcmp(name,"-bootfile") == 0))
	strncpy(kname, getbootfile(), 79);
    else
	strncpy(kname, name, 79);

    if (isDebug())
	msgDebug("uc_open: kernel name is %s, incore = %d\n", kname, incore);
    kern = (struct kernel *)malloc(sizeof(struct kernel));

#ifdef KERN_NO_SYMBOLS
    if (incore) {
	FILE *fp;

	fp = fopen("/stand/symbols", "r");
	if (!fp) {
	    msgDebug("Couldn't open /stand/symbols file!  Punting.\n");
	    free(kern);
	    return NULL;
	}
	if (fscanf(fp, "%d\n", &size) != 1) {
	    msgDebug("Unable to get # of name list entries from symbol file.\n");
	    free(kern);
	    return NULL;
	}
	kern->nl = nl = (struct nlist *)malloc((size + 1) * sizeof(struct nlist));
	for (i = 0; i < size; i++) {
	    char *cp, name[255];
	    int c1;
	    unsigned int uc1;
	    short d1;
	    unsigned long v1;

	    if (fgets(name, 255, fp) == NULL) {
		msgDebug("Can't get name field for entry %d\n", i);
		free(kern);
		return NULL;
	    }
	    if ((cp = index(name, '\n')) != NULL)
		*cp = '\0';
	    nl[i].n_name = strdup(name);
	    if (fscanf(fp, "%u %d %hd %ld\n", &uc1, &c1, &d1, &v1) == 5) {
		nl[i].n_type = (unsigned char)uc1;
		nl[i].n_other = (char)c1;
		nl[i].n_desc = d1;
		nl[i].n_value = v1;
		if (isDebug())
		    msgDebug("uc_open: for entry %d, decoded: \"%s\", %d %d %hd %ld\n", i, nl[i].n_name, nl[i].n_type, nl[i].n_other, nl[i].n_desc, nl[i].n_value);
	    }
	    else {
		nl[i].n_type = 0;
		nl[i].n_other = 0;
		nl[i].n_desc = 0;
		nl[i].n_value = 0;
	    }
	}
	fclose(fp);
	i = 0;
    }
    else
#endif
	i = nlist(kname, nl);
    if (i == -1) {
	msgDebug("uc_open: kernel %s does not contain symbols.\n", kname);
	free(kern);
	return NULL;
    }
#ifdef KERN_NO_SYMBOLS
    if (!incore) {
#else
    {
#endif
	kern->nl=(struct nlist *)malloc(sizeof(_nl));
	bcopy(_nl, kern->nl, sizeof(_nl));
    }
	
    if (incore) {
	if ((kd=open("/dev/kmem", O_RDONLY)) < 0) {
	    free(kern);
	    kern = (struct kernel *)-3;
	    msgDebug("uc_open: Unable to open /dev/kmem.\n");
	    return kern;
	}
	kern->core = (caddr_t)NULL;
	kern->incore = 1;
	kern->size = 0;
    }
    else {
	if (stat(kname, &sb) < 0) {
	    free(kern);
	    kern = (struct kernel *)-1;
	    msgDebug("uc_open: Unable to stat %s.\n", kname);
{'fraction_non_alphanumeric': 0.1648959236773634, 'fraction_numerical': 0.014418907198612316, 'mean_word_length': 3.260969976905312, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1181094', 'n_tokens_mistral': 3674, 'n_tokens_neox': 3393, 'n_words': 1028}