Esempio n. 1
0
static struct toptenentry *
read_topten(int fd, int limit)
{
    int i, size;
    struct toptenentry *ttlist;
    char *data, *line;

    lseek(fd, 0, SEEK_SET);
    data = loadfile(fd, &size);
    if (!data)
        /* the only sensible reason for not getting any data is that the record
           file doesn't exist or is empty. If it does have data but is
           unreadable for some other reason, writing will fail later on, so
           pretending it's empty won't hurt anything. */
        return calloc(limit + 1, sizeof (struct toptenentry));

    ttlist = calloc(limit + 1, sizeof (struct toptenentry));
    line = data;
    for (i = 0; i < limit; i++) {
        if (!readentry(line, &ttlist[i]))
            break;
        line = strchr(line, '\n') + 1;
    }

    free(data);
    return ttlist;
}
Esempio n. 2
0
static PathEntry* parseentry(char* buf, PathType pathtype, char*tmp)
{
	PathEntry* pet = NULL;

	if (pathtype == PATHDIR_T)
		readentry(buf, "DIR", tmp);
	else if (pathtype == PATHFILE_T)
		readentry(buf, "FILE", tmp);

	if (strlen(tmp) > 0)
	{
		pet = (PathEntry*)calloc(1l, sizeof(PathEntry));
		pet->pt = pathtype;
		strcpy(pet->path, tmp);
		pet->next = NULL;
	}

	return pet;
}
Esempio n. 3
0
int main(int argc, char * argv[]) {
	
	FILE * edict = fopen("./edict.locale", "r");
	if(!edict) {
		fprintf(stderr, "Could not open the file edict.locale.\n");
		fprintf(stderr, "Exiting.\n");
	}
	
	FILE * kanjidic = fopen("./kanjidic.locale", "r");
	if(!kanjidic) {
		fprintf(stderr, "Could not open the file kanjidic.locale.\n");
		fprintf(stderr, "Exiting.\n");
	} 
	/* The first line is only a comment */
	//skip_line(edict);
	skip_line(kanjidic);
	
	fprintf(stderr, "I have opened both edict.locale and kanjidic.locale.\n");
	/* Read in the kanjidic file. */
	kanji * klist = readkanjidic(kanjidic);
	fprintf(stderr, "Parsed the KANJIDIC file.\n");
	
	/* We'll need a panini monad later (this is used by learnentry_func)
	 */
	pmonad = monad_new();
	monad_rules(pmonad, "english");
	
	/* Read the edict file in line by line */
	int i = 0;
	for(;;) {
		/* Read in the headword. */
		char * headword = readword(edict);
		if(!headword) break;
		if(!strlen(headword)) break;
		
		/* Every 32nd line, print out how far we've come. 
		 * (This test is an optimisation; the terminal is a slow thing to print to.) */
		if(i == (i & ~(017)))
			fprintf(stderr, "EDICT importer: %d %s                            \r", i, headword);
		
		/* Parse the line and try to generate Panini source code */
		readentry(edict, headword, klist);
		
		i++;
	}
	
	/* Print out all the used kanji. */
	compilekanji(klist);
	free_kanji(klist);
	
	return 0;
}
Esempio n. 4
0
/* read the toptenentry appended to the end of a game log */
void
read_log_toptenentry(int fd, struct nh_topten_entry *entry)
{
    struct toptenentry tt;
    int size;
    char *line = loadfile(fd, &size);

    if (!line)
        return;

    readentry(line, &tt);
    fill_nh_score_entry(&tt, entry, -1, FALSE);
    free(line);
}
Esempio n. 5
0
void gwkludge(void)
{
    struct sockaddr_in dst, gate;
    FILE *fp;
    char *type, *dname, *gname, *qual, buf[BUFSIZ];
    struct interface *ifp;
    int metric, n;
    struct rt_entry route;

    fp = fopen(_PATH_GATEWAYS, "r");
    if (fp == NULL)
        return;
    qual = buf;
    dname = buf + 64;
    gname = buf + ((BUFSIZ - 64) / 3);
    type = buf + (((BUFSIZ - 64) * 2) / 3);
    memset(&dst, 0, sizeof (dst));
    memset(&gate, 0, sizeof (gate));
    memset(&route, 0, sizeof(route));
    /* format: {net | host} XX gateway XX metric DD [passive | external]\n */
#define	readentry(fp) \
	fscanf((fp), "%s %s gateway %s metric %d %s\n", \
		type, dname, gname, &metric, qual)
    for (;;) {
        if ((n = readentry(fp)) == EOF)
            break;
        /*
         *	Lusertrap. Vendors should ship the line
         *
         *	CONFIGME CONFIGME gateway CONFIGME metric 1
         *
         */
        if (strcmp(type,"CONFIGME")==0)
        {
            fprintf(stderr,"Not starting gated. Please configure first.\n");
            exit(1);
        }
        if (!getnetorhostname(type, dname, &dst))
            continue;
        if (!gethostnameornumber(gname, &gate))
            continue;
        if (metric == 0)			/* XXX */
            metric = 1;
        if (strcmp(qual, "passive") == 0) {
            /*
             * Passive entries aren't placed in our tables,
             * only the kernel's, so we don't copy all of the
             * external routing information within a net.
             * Internal machines should use the default
             * route to a suitable gateway (like us).
             */
            route.rt_dst = *(struct sockaddr *) &dst;
            route.rt_router = *(struct sockaddr *) &gate;
            route.rt_flags = RTF_UP;
            if (strcmp(type, "host") == 0)
                route.rt_flags |= RTF_HOST;
            if (metric)
                route.rt_flags |= RTF_GATEWAY;
            (void) rtioctl(ADD, &route.rt_rt);
            continue;
        }
        if (strcmp(qual, "external") == 0) {
            /*
             * Entries marked external are handled
             * by other means, e.g. EGP,
             * and are placed in our tables only
             * to prevent overriding them
             * with something else.
             */
            rtadd((struct sockaddr *)&dst,
                  (struct sockaddr *)&gate, metric,
                  RTS_EXTERNAL|RTS_PASSIVE);
            continue;
        }
        /* assume no duplicate entries */
        externalinterfaces++;
        ifp = (struct interface *)malloc(sizeof (*ifp));
        memset(ifp, 0, sizeof (*ifp));
        ifp->int_flags = IFF_REMOTE;
        /* can't identify broadcast capability */
        ifp->int_net = inet_netof_subnet(dst.sin_addr);
        if (strcmp(type, "host") == 0) {
            ifp->int_flags |= IFF_POINTOPOINT;
            ifp->int_dstaddr = *((struct sockaddr *)&dst);
        }
        ifp->int_addr = *((struct sockaddr *)&gate);
        ifp->int_metric = metric;
        ifp->int_next = ifnet;
        ifnet = ifp;
        addrouteforif(ifp);
    }
    fclose(fp);
}
Esempio n. 6
0
_RuneLocale *
_Read_RuneMagi(FILE *fp)
{
	/* file */
	_FileRuneLocale frl;
	/* host data */
	char *hostdata;
	size_t hostdatalen;
	void *lastp;
	_RuneLocale *rl;
	struct stat sb;
	int x;
	uint32_t runetype_nranges, maplower_nranges, mapupper_nranges, var_len;

	if (fstat(fileno(fp), &sb) < 0)
		return NULL;

	if (sb.st_size < sizeof(_FileRuneLocale))
		return NULL;
	/* XXX more validation? */

	/* Someone might have read the magic number once already */
	rewind(fp);

	if (fread(&frl, sizeof(frl), 1, fp) != 1)
		return NULL;
	if (memcmp(frl.frl_magic, _RUNE_MAGIC_1, sizeof(frl.frl_magic)))
		return NULL;

	runetype_nranges = ntohl(frl.frl_runetype_ext.frr_nranges);
	maplower_nranges = ntohl(frl.frl_maplower_ext.frr_nranges);
	mapupper_nranges = ntohl(frl.frl_mapupper_ext.frr_nranges);
	var_len = ntohl((uint32_t)frl.frl_variable_len);

#if SIZE_MAX <= UINT32_MAX
	if (runetype_nranges > SIZE_MAX / sizeof(_RuneEntry) ||
	    maplower_nranges > SIZE_MAX / sizeof(_RuneEntry) ||
	    mapupper_nranges > SIZE_MAX / sizeof(_RuneEntry))
		return NULL;
#endif

	if (var_len > INT32_MAX)
		return NULL;

	hostdatalen = sizeof(*rl);
	SAFE_ADD(hostdatalen, var_len);
	SAFE_ADD(hostdatalen, runetype_nranges * sizeof(_RuneEntry));
	SAFE_ADD(hostdatalen, maplower_nranges * sizeof(_RuneEntry));
	SAFE_ADD(hostdatalen, mapupper_nranges * sizeof(_RuneEntry));

	if ((hostdata = calloc(hostdatalen, 1)) == NULL)
		return NULL;
	lastp = hostdata + hostdatalen;

	rl = (_RuneLocale *)hostdata;
	rl->rl_variable = rl + 1;

	memcpy(rl->rl_magic, frl.frl_magic, sizeof(rl->rl_magic));
	memcpy(rl->rl_encoding, frl.frl_encoding, sizeof(rl->rl_encoding));

	/* XXX assumes rune_t = uint32_t */
	rl->rl_invalid_rune = ntohl((uint32_t)frl.frl_invalid_rune);
	rl->rl_variable_len = ntohl((uint32_t)frl.frl_variable_len);

	for (x = 0; x < _CACHED_RUNES; ++x) {
		rl->rl_runetype[x] = ntohl(frl.frl_runetype[x]);

		/* XXX assumes rune_t = uint32_t */
		rl->rl_maplower[x] = ntohl((uint32_t)frl.frl_maplower[x]);
		rl->rl_mapupper[x] = ntohl((uint32_t)frl.frl_mapupper[x]);
	}

	if (readrange(rl, &rl->rl_runetype_ext, runetype_nranges, lastp, fp) ||
	    readrange(rl, &rl->rl_maplower_ext, maplower_nranges, lastp, fp) ||
	    readrange(rl, &rl->rl_mapupper_ext, mapupper_nranges, lastp, fp))
		goto err;

	if (readentry(&rl->rl_runetype_ext, fp) != 0)
		goto err;

	if ((uint8_t *)rl->rl_variable + rl->rl_variable_len >
	    (uint8_t *)lastp)
		goto rune_err;

	if (rl->rl_variable_len == 0)
		rl->rl_variable = NULL;
	else if (fread(rl->rl_variable, rl->rl_variable_len, 1, fp) != 1)
		goto rune_err;
	if (find_codeset(rl))
		goto rune_err;
	_wctype_init(rl);

	/*
	 * error if we have junk at the tail, 
	 * or if we can't allocate memory.
	 */
	if (ftello(fp) != sb.st_size || __make_ctype_tabs(rl) == -1)
		goto rune_err;

	return(rl);
rune_err:
	_freeentry(&rl->rl_runetype_ext);
err:
	free(hostdata);
	return NULL;
}