コード例 #1
0
ファイル: strfile.c プロジェクト: coyizumi/cs111
/*
 * main:
 *	Drive the sucker.  There are two main modes -- either we store
 *	the seek pointers, if the table is to be sorted or randomized,
 *	or we write the pointer directly to the file, if we are to stay
 *	in file order.  If the former, we allocate and re-allocate in
 *	CHUNKSIZE blocks; if the latter, we just write each pointer,
 *	and then seek back to the beginning to write in the table.
 */
int
main(int ac, char *av[])
{
	char *sp, *nsp, dc;
	FILE *inf, *outf;
	off_t last_off, pos, *p;
	size_t length;
	int first;
	uint32_t cnt;
	STR *fp;
	static char string[257];

	setlocale(LC_ALL, "");

	getargs(ac, av);		/* evalute arguments */
	dc = Delimch;
	if ((inf = fopen(Infile, "r")) == NULL) {
		perror(Infile);
		exit(1);
	}

	if ((outf = fopen(Outfile, "w")) == NULL) {
		perror(Outfile);
		exit(1);
	}
	if (!STORING_PTRS)
		fseek(outf, (long)sizeof(Tbl), SEEK_SET);

	/*
	 * Write the strings onto the file
	 */

	Tbl.str_longlen = 0;
	Tbl.str_shortlen = 0xffffffff;
	Tbl.str_delim = dc;
	Tbl.str_version = VERSION;
	first = Oflag;
	add_offset(outf, ftello(inf));
	last_off = 0;
	do {
		sp = fgets(string, 256, inf);
		if (sp == NULL || (sp[0] == dc && sp[1] == '\n')) {
			pos = ftello(inf);
			length = (size_t)(pos - last_off) -
			    (sp != NULL ? strlen(sp) : 0);
			last_off = pos;
			if (length == 0)
				continue;
			add_offset(outf, pos);
			if ((size_t)Tbl.str_longlen < length)
				Tbl.str_longlen = length;
			if ((size_t)Tbl.str_shortlen > length)
				Tbl.str_shortlen = length;
			first = Oflag;
		}
		else if (first) {
			for (nsp = sp; !isalnum((unsigned char)*nsp); nsp++)
				continue;
			ALLOC(Firstch, Num_pts);
			fp = &Firstch[Num_pts - 1];
			if (Iflag && isupper((unsigned char)*nsp))
				fp->first = tolower((unsigned char)*nsp);
			else
				fp->first = *nsp;
			fp->pos = Seekpts[Num_pts - 1];
			first = false;
		}
	} while (sp != NULL);

	/*
	 * write the tables in
	 */

	fclose(inf);
	Tbl.str_numstr = Num_pts - 1;

	if (Cflag)
		Tbl.str_flags |= STR_COMMENTS;

	if (Oflag)
		do_order();
	else if (Rflag)
		randomize();

	if (Xflag)
		Tbl.str_flags |= STR_ROTATED;

	if (!Sflag) {
		printf("\"%s\" created\n", Outfile);
		if (Num_pts == 2)
			puts("There was 1 string");
		else
			printf("There were %u strings\n", Num_pts - 1);
		printf("Longest string: %u byte%s\n", Tbl.str_longlen,
		       Tbl.str_longlen == 1 ? "" : "s");
		printf("Shortest string: %u byte%s\n", Tbl.str_shortlen,
		       Tbl.str_shortlen == 1 ? "" : "s");
	}

	rewind(outf);
	Tbl.str_version = htobe32(Tbl.str_version);
	Tbl.str_numstr = htobe32(Tbl.str_numstr);
	Tbl.str_longlen = htobe32(Tbl.str_longlen);
	Tbl.str_shortlen = htobe32(Tbl.str_shortlen);
	Tbl.str_flags = htobe32(Tbl.str_flags);
	fwrite((char *)&Tbl, sizeof(Tbl), 1, outf);
	if (STORING_PTRS) {
		for (p = Seekpts, cnt = Num_pts; cnt--; ++p)
			*p = htobe64(*p);
		fwrite(Seekpts, sizeof(*Seekpts), (size_t)Num_pts, outf);
	}
	fclose(outf);
	exit(0);
}
コード例 #2
0
ファイル: vlog.c プロジェクト: cleberjsantos/vagent2
static unsigned int vlog_reply(struct httpd_request *request, void *data)
{
	struct vlog_priv_t *vlog;
	int ret;
	char *limit = NULL;
	char *p;
	char *tag = NULL;
	char *itag = NULL;
	struct agent_core_t *core = data;
	GET_PRIV(data,vlog);
	p = next_slash(request->url + 1);

	assert(vlog->tag==NULL);
	assert(vlog->answer == NULL);

	if (p) {
		limit = strdup(p);
		assert(limit);
		char *tmp2 = index(limit,'/');
		if (tmp2 && *tmp2) *tmp2 = '\0';

		if(!(atoi(limit) > 0)) {
			free(limit);
			send_response_fail(request->connection,"Not a number");
			return 0;
		}
		p = next_slash(p);
	}
	if (p) {
		tag = strdup(p);
		char *tmp2 = index(tag,'/');
		if (tmp2 && *tmp2) *tmp2 = '\0';
		p = next_slash(p);
	}
	if (p) {
		itag = strdup(p);
		char *tmp2 = index(itag,'/');
		if (tmp2 && *tmp2) *tmp2 = '\0';
		p = next_slash(p);
	}
	vlog->answer = VSB_new_auto();
	assert(vlog->answer != NULL);
	vlog->vd = VSM_New();
	assert(VSL_Arg(vlog->vd, 'n', core->config->n_arg));
	VSL_Setup(vlog->vd);
	VSL_Arg(vlog->vd, 'd', "");
	if (tag) {
		VSL_Arg(vlog->vd, 'i', tag);
		if (itag)
			VSL_Arg(vlog->vd,'I',itag);
	} else {
		VSL_Arg(vlog->vd, 'k', limit ? limit : "10");
	}

	if (limit)
		free(limit);
	VSB_printf(vlog->answer, "{ \"log\": [");
	ret = VSL_Open(vlog->vd, 1);
	assert(!ret);

	if (tag == NULL) {
		do_order(vlog);
	} else {
		do_unorder(vlog);
	}
	if (tag)
		free(tag);
	if (itag)
		free(itag);

	VSB_printf(vlog->answer, "\n] }\n");
	assert(VSB_finish(vlog->answer) == 0);
	if (VSB_len(vlog->answer) > 1) {
		send_response(request->connection, 200, VSB_data(vlog->answer), VSB_len(vlog->answer));
	} else {
		send_response_fail(request->connection, "FAIL");
	}
	VSB_clear(vlog->answer);
	VSM_Close(vlog->vd);
	vlog->answer = NULL;
	vlog->entries = 0;
	return 0;
}
コード例 #3
0
ファイル: strfile.c プロジェクト: lattera/openbsd
/*
 * main:
 *	Drive the sucker.  There are two main modes -- either we store
 *	the seek pointers, if the table is to be sorted or randomized,
 *	or we write the pointer directly to the file, if we are to stay
 *	in file order.  If the former, we allocate and re-allocate in
 *	CHUNKSIZE blocks; if the latter, we just write each pointer,
 *	and then seek back to the beginning to write in the table.
 */
int
main(int ac, char *av[])
{
	char		*sp, dc;
	FILE		*inf, *outf;
	int32_t		last_off, length, pos;
	int32_t		*p;
	int		first, cnt;
	char		*nsp;
	STR		*fp;
	static char	string[257];

	getargs(ac, av);		/* evalute arguments */
	dc = Delimch;
	if ((inf = fopen(Infile, "r")) == NULL)
		err(1, "%s", Infile);

	if ((outf = fopen(Outfile, "w")) == NULL)
		err(1, "%s", Outfile);
	if (!STORING_PTRS)
		(void) fseek(outf, sizeof Tbl, SEEK_SET);

	/*
	 * Write the strings onto the file
	 */

	Tbl.str_longlen = 0;
	Tbl.str_shortlen = (unsigned int) 0xffffffff;
	Tbl.str_delim = dc;
	Tbl.str_version = VERSION;
	first = Oflag;
	add_offset(outf, ftell(inf));
	last_off = 0;
	do {
		sp = fgets(string, sizeof(string), inf);
		if (sp == NULL || (sp[0] == dc && sp[1] == '\n')) {
			pos = ftell(inf);
			length = pos - last_off - (sp ? strlen(sp) : 0);
			last_off = pos;
			if (!length)
				continue;
			add_offset(outf, pos);
			if (Tbl.str_longlen < (u_int32_t)length)
				Tbl.str_longlen = length;
			if (Tbl.str_shortlen > (u_int32_t)length)
				Tbl.str_shortlen = length;
			first = Oflag;
		}
		else if (first) {
			for (nsp = sp; !isalnum(*nsp); nsp++)
				continue;
			ALLOC(Firstch, Num_pts);
			fp = &Firstch[Num_pts - 1];
			if (Iflag && isupper(*nsp))
				fp->first = tolower(*nsp);
			else
				fp->first = *nsp;
			fp->pos = Seekpts[Num_pts - 1];
			first = FALSE;
		}
	} while (sp != NULL);

	/*
	 * write the tables in
	 */

	(void) fclose(inf);
	Tbl.str_numstr = Num_pts - 1;
	if (Tbl.str_numstr == 0)
		Tbl.str_shortlen = 0;

	if (Oflag)
		do_order();
	else if (Rflag)
		randomize();

	if (Xflag)
		Tbl.str_flags |= STR_ROTATED;

	if (!Sflag) {
		printf("\"%s\" created\n", Outfile);
		if (Tbl.str_numstr == 1)
			puts("There was 1 string");
		else
			printf("There were %u strings\n", Tbl.str_numstr);
		printf("Longest string: %lu byte%s\n",
			  (unsigned long) Tbl.str_longlen,
		       Tbl.str_longlen == 1 ? "" : "s");
		printf("Shortest string: %lu byte%s\n",
			  (unsigned long) Tbl.str_shortlen,
		       Tbl.str_shortlen == 1 ? "" : "s");
	}

	(void) fseek(outf, 0, SEEK_SET);
	Tbl.str_version = htonl(Tbl.str_version);
	Tbl.str_numstr = htonl(Tbl.str_numstr);
	Tbl.str_longlen = htonl(Tbl.str_longlen);
	Tbl.str_shortlen = htonl(Tbl.str_shortlen);
	Tbl.str_flags = htonl(Tbl.str_flags);
	(void) fwrite(&Tbl.str_version,  sizeof(Tbl.str_version),  1, outf);
	(void) fwrite(&Tbl.str_numstr,   sizeof(Tbl.str_numstr),   1, outf);
	(void) fwrite(&Tbl.str_longlen,  sizeof(Tbl.str_longlen),  1, outf);
	(void) fwrite(&Tbl.str_shortlen, sizeof(Tbl.str_shortlen), 1, outf);
	(void) fwrite(&Tbl.str_flags,    sizeof(Tbl.str_flags),    1, outf);
	(void) fwrite( Tbl.stuff,	 sizeof(Tbl.stuff),	   1, outf);
	if (STORING_PTRS)
		for (p = Seekpts, cnt = Num_pts; cnt--; ++p) {
			*p = htonl(*p);
			(void) fwrite(p, sizeof(*p), 1, outf);
		}
	if (fclose(outf))
		err(1, "fclose `%s'", Outfile);
	exit(0);
}
コード例 #4
0
ファイル: ede-tip-compiler.c プロジェクト: edeproject/svn
/*
 * main:
 *      Drive the sucker.  There are two main modes -- either we store
 *      the seek pointers, if the table is to be sorted or randomized,
 *      or we write the pointer directly to the file, if we are to stay
 *      in file order.  If the former, we allocate and re-allocate in
 *      CHUNKSIZE blocks; if the latter, we just write each pointer,
 *      and then seek back to the beginning to write in the table.
 */
int main(int ac, char **av)
{
    register unsigned char *sp;
    register FILE *inf, *outf;
    register int32_t last_off, length, pos, *p;
    register int first, cnt;
    register char *nsp;
    register STR *fp;
    static char string[257];

    getargs(ac, av);		/* evalute arguments */
    if ((inf = fopen(Infile, "r")) == NULL)
    {
	perror(Infile);
	exit(1);
    }

    if ((outf = fopen(Outfile, "w")) == NULL)
    {
	perror(Outfile);
	exit(1);
    }
    if (!STORING_PTRS)
	(void) fseek(outf, sizeof Tbl, 0);

    /*
     * Write the strings onto the file
     */

    Tbl.str_longlen = 0;
    Tbl.str_shortlen = (unsigned int) 0xffffffff;
    Tbl.str_delim = Delimch;
    Tbl.str_version = VERSION;
    first = Oflag;
    add_offset(outf, ftell(inf));
    last_off = 0;
    do
    {
	sp = (unsigned char*)fgets(string, 256, inf);
	if (sp == NULL || STR_ENDSTRING(sp, Tbl))
	{
	    pos = ftell(inf);
	    length = pos - last_off - (sp ? strlen((const char*)sp) : 0);
	    if (!length)
		/* Here's where we go back and fix things, if the
		 * 'fortune' just read was the null string.
		 * We had to make the assignment of last_off slightly
		 * redundant to achieve this.
		 */
	    {
		if (pos - last_off == 2)
		    fix_last_offset(outf, pos);
		last_off = pos;
		continue;
	    }
	    last_off = pos;
	    add_offset(outf, pos);
	    if (Tbl.str_longlen < length)
		Tbl.str_longlen = length;
	    if (Tbl.str_shortlen > length)
		Tbl.str_shortlen = length;
	    first = Oflag;
	}
	else if (first)
	{
	    for (nsp = (char*)sp; !isalnum(*nsp); nsp++)
		continue;
	    ALLOC(Firstch, Num_pts);
	    fp = &Firstch[Num_pts - 1];
	    if (Iflag && isupper(*nsp))
		fp->first = tolower(*nsp);
	    else
		fp->first = *nsp;
	    fp->pos = Seekpts[Num_pts - 1];
	    first = FALSE;
	}
    }
    while (sp != NULL);

    /*
     * write the tables in
     */

    fclose(inf);

    if (Oflag)
	do_order();
    else if (Rflag)
	randomize();

    if (Xflag)
	Tbl.str_flags |= STR_ROTATED;

    if (!Sflag)
    {
	printf("\"%s\" created\n", Outfile);
	if (Num_pts == 1)
	    puts("There was no string");
	else
	{
	    if (Num_pts == 2)
		puts("There was 1 string");
	    else
		printf("There were %ld strings\n", Num_pts - 1);
	    printf("Longest string: %u byte%s\n", Tbl.str_longlen, 
		Tbl.str_longlen == 1 ? "" : "s");
	    printf("Shortest string: %u byte%s\n", Tbl.str_shortlen,
		Tbl.str_shortlen == 1 ? "" : "s");
	}
    }

    fseek(outf, (off_t) 0, 0);
    Tbl.str_version = htonl(Tbl.str_version);
    Tbl.str_numstr = htonl(Num_pts - 1);
    /* Look, Ma!  After using the variable three times, let's store
     * something in it!
     */
    Tbl.str_longlen = htonl(Tbl.str_longlen);
    Tbl.str_shortlen = htonl(Tbl.str_shortlen);
    Tbl.str_flags = htonl(Tbl.str_flags);
    fwrite(&Tbl.str_version,  sizeof Tbl.str_version,  1, outf);
    fwrite(&Tbl.str_numstr,   sizeof Tbl.str_numstr,   1, outf);
    fwrite(&Tbl.str_longlen,  sizeof Tbl.str_longlen,  1, outf);
    fwrite(&Tbl.str_shortlen, sizeof Tbl.str_shortlen, 1, outf);
    fwrite(&Tbl.str_flags,    sizeof Tbl.str_flags,    1, outf);
    fwrite( Tbl.stuff,        sizeof Tbl.stuff,        1, outf);
    if (STORING_PTRS)
    {
	for (p = Seekpts, cnt = Num_pts; cnt--; ++p)
	{
	    *p = htonl(*p);
	    fwrite(p, sizeof *p, 1, outf);
	}
    }
    fclose(outf);
    exit(0);
}
コード例 #5
0
ファイル: sort.c プロジェクト: ComposersDesktop/CDP7
int do_file_sorting(dataptr dz)
{
	int exit_status;
	int  n;
	int a_srate = 0;
	double sum;
	int  infilecnt = dz->all_words-1;
	int  fileno = 0, namestoresize = 0;
	char *filename = dz->wordstor[infilecnt];	/* name of input list file */
	char *monofile=NULL,     *stereofile = NULL, *analfile = NULL, *quadfile = NULL;
	char *pitchfile = NULL,  *transfile = NULL,  *formantfile = NULL;
	char *envfile = NULL,    *otherfile  = NULL, *namestore  = NULL;
	int  *posstore   = NULL;
	int  *lcnt       = NULL;
	char ***lstore   = NULL;
 	double *lenstore = NULL;
 	double *sortlens = NULL;
	char *file48 = ENDOFSTR, *file44 = ENDOFSTR, *file32 = ENDOFSTR;
	char *file24 = ENDOFSTR, *file22 = ENDOFSTR, *file16 = ENDOFSTR;
	int is_file48=FALSE, is_file44=FALSE, is_file32=FALSE, is_file24=FALSE, is_file22=FALSE, is_file16=FALSE;
	int is_mono_list=FALSE,  is_stereo_list=FALSE,is_quad_list=FALSE, is_anal_list=FALSE,is_pitch_list=FALSE;
	int is_trans_list=FALSE, is_fmnt_list=FALSE,  is_env_list=FALSE, is_other_list=FALSE;
	FILE *fp48 = NULL, *fp44 = NULL, *fp32 = NULL, *fp24 = NULL, *fp22 = NULL, *fp16 = NULL, 
	    *fpm = NULL, *fps = NULL, *fpa = NULL, *fpp = NULL, *fpt = NULL, *fpf = NULL, *fpe = NULL, *fpo = NULL, *fpq = NULL;
	char *p;
	int done_errmsg = 0;

	switch(dz->mode) {
	case(BY_DURATION):	
	case(BY_LOG_DUR):	
		if((lcnt = (int *)malloc((dz->iparam[SORT_LENCNT]+1) * sizeof(int)))==NULL) {
			sprintf(errstr,"INSUFFICIENT MEMORY for lcnt store.\n");
			return(MEMORY_ERROR);
		}
		if((sortlens  = (double *)malloc(dz->iparam[SORT_LENCNT] * sizeof(double)))==NULL) {
			sprintf(errstr,"INSUFFICIENT MEMORY for lens store.\n");
			return(MEMORY_ERROR);
		}
		sum = dz->param[SORT_SMALL];
		for(n=0;n<dz->iparam[SORT_LENCNT];n++) {
			lcnt[n] = 0;				
			sortlens[n] = sum;
			if(dz->mode==BY_LOG_DUR)
				sum *= dz->param[SORT_STEP];
			else
				sum += dz->param[SORT_STEP];
		}
		lcnt[dz->iparam[SORT_LENCNT]] = 0;
		sortlens[dz->iparam[SORT_LENCNT]-1] = dz->param[SORT_LARGE];
		dz->iparam[SORT_LENCNT]++;
		/* fall thro */
	case(IN_DUR_ORDER):	
	   	if((lstore = (char ***)malloc((dz->iparam[SORT_LENCNT]+1) * sizeof(char **)))==NULL) {
			sprintf(errstr,"INSUFFICIENT MEMORY for length store.\n");
			return(MEMORY_ERROR);
		}
		for(n=0;n<dz->iparam[SORT_LENCNT]+1;n++) {
// AVOID realloc
			if((lstore[n] = (char **)malloc(infilecnt * sizeof(char *)))==NULL) {
				sprintf(errstr,"INSUFFICIENT MEMORY for length store %d.\n",n+1);
				return(MEMORY_ERROR);
			}
		}
		break;
	}
	strip_extension(filename);
	if(sloom && (dz->mode == BY_FILETYPE || dz->mode == BY_SRATE)) {
		p = filename + strlen(filename) - 1;		/* Strip trailing zero from generic tempfilename */
		*p = ENDOFSTR;
	}
	for(n=0;n<infilecnt;n++) {
		if(!strcmp(dz->wordstor[infilecnt],dz->wordstor[n])) {
			sprintf(errstr,"The name of the listfile cannot be included in the listing!!\n");
			return(DATA_ERROR);
		}
	}
// AVOID realloc
	for(n=0;n<infilecnt;n++)
		namestoresize += strlen(dz->wordstor[n]) + 1;
	if((namestore = (char *)malloc(namestoresize * sizeof(char)))==NULL) {
		sprintf(errstr,"INSUFFICIENT MEMORY for name store.\n");
		return(MEMORY_ERROR);
	}
	namestoresize = 0;
	for(n=0;n<infilecnt;n++) {
	    if((dz->ifd[0] = sndopenEx(dz->wordstor[n],0,CDP_OPEN_RDONLY)) < 0) 	{
			if(dz->mode!=BY_FILETYPE)  {
				if(!done_errmsg) {
					sprintf(errstr,"Some files are NOT soundfiles.\n");
					print_outmessage_flush(errstr);
					done_errmsg = 1;
				}
			}
			dz->ifd[0] = -1;
			continue;
		} else if((dz->mode!=BY_FILETYPE) && filename_extension_is_not_sound(dz->wordstor[n])) {
			if(!done_errmsg) {
				sprintf(errstr,"Some files are NOT soundfiles.\n");
				print_outmessage_flush(errstr);
				done_errmsg = 1;
			}
			continue;
		}
		switch(dz->mode) {
		case(BY_SRATE):		
			if((exit_status = do_srates(filename,dz->wordstor[n],&file48,&file44,&file32,&file24,&file22,&file16,
			&is_file48,&is_file44,&is_file32,&is_file24,&is_file22,&is_file16,
			&fp48,&fp44,&fp32,&fp24,&fp22,&fp16,dz))<0)
				return(exit_status);
			break;
		case(BY_DURATION):	
		case(BY_LOG_DUR):	
			if((exit_status = do_lenths(filename,dz->wordstor[n],&namestore,lstore,&namestoresize,lcnt,sortlens,dz))<0)
				return(exit_status);
			break;
		case(IN_DUR_ORDER):	
			if((exit_status = do_order
			(filename,dz->wordstor[n],&namestore,&lenstore,&posstore,&lstore,&fileno,&namestoresize,dz))<0)
				return(exit_status);
			break;
		case(BY_FILETYPE):	
			if((exit_status = do_types(filename,dz->wordstor[n],
			&monofile,&stereofile,&quadfile,&analfile,&pitchfile,&transfile,&formantfile,&envfile,&otherfile,
			&is_mono_list,&is_stereo_list,&is_quad_list,&is_anal_list,
			&is_pitch_list,&is_trans_list,&is_fmnt_list,&is_env_list,&is_other_list,
			&fpm,&fps,&fpa,&fpp,&fpt,&fpf,&fpe,&fpo,&fpq,dz))<0)
				return(exit_status);
			break;
		case(FIND_ROGUES):	
			do_rogues(dz->wordstor[n],&a_srate,dz);
			break;
		}

		if(dz->ifd[0]!=-1 && sndcloseEx(dz->ifd[0])<0)
			fprintf(stdout,"WARNING: Failed to close sndfile %s.\n",dz->wordstor[n]);
		dz->ifd[0] = -1;
	}
	switch(dz->mode) {
	case(BY_LOG_DUR):
	case(BY_DURATION):	
		if((exit_status = output_lenths(filename,&otherfile,lstore,sortlens,lcnt,dz))<0)
			return(exit_status);
		break;
 	case(BY_SRATE):		
 		output_srates(is_file48,is_file44,is_file32,is_file24,is_file22,is_file16,
		file48,	file44,file32,file24,file22,file16);						
		break;
 	case(IN_DUR_ORDER):	
 		if((exit_status = output_order(filename,&otherfile,posstore,lenstore,lstore,fileno,dz))<0)
			return(exit_status);
 		break;
	case(BY_FILETYPE):	
		output_types(filename,is_mono_list,is_stereo_list,is_quad_list,
		is_anal_list,is_pitch_list,is_trans_list,is_fmnt_list,is_env_list,
		is_other_list,monofile,stereofile,quadfile,analfile,pitchfile,
		transfile,formantfile,envfile,otherfile);							
		break;

	}
	fflush(stdout);
	return(FINISHED);
}