Ejemplo n.º 1
0
void Test_split_string(CuTest* tc) {
	int n = 255;	/* Maximum n = 255 */
	int t = 254;	/* t <= n, we choose less than that so we have two tests */

	char * phrase = "This is a test of Bücher and Später.";

	int count = 10;
	int i;

	for (i = 0; i < count; ++i)
	{
		char ** result = split_string(phrase, n, t);

		/* Extract secret using first t shares */
		char * answer = join_strings(result, t);
		CuAssertStrEquals(tc, phrase, answer);
		free(answer);

		/* Extract secret using all shares */
		answer = join_strings(result, n);
		CuAssertStrEquals(tc, phrase, answer);
		free(answer);

		free_string_shares(result, n);
	}
}
Ejemplo n.º 2
0
Archivo: shell.c Proyecto: vshan/dash
int dash_eval(char *line, char *std_input, char *origin)
{
  if (line == NULL)
    return 1;

  int num_tokens;
  char **tokens = str_split(line, " ", &num_tokens);
  char *token;
  int i;
  int remote_pipe_pos = -1;

  for (i = 0; i < num_tokens; i++) {
    if (strcmp(tokens[i], REMOTE_PIPE) == 0) {
      remote_pipe_pos = i;
      break;
    }
  }

  if (remote_pipe_pos == -1) // No remote pipes
  {
    char *msg_data = dash_exec_scmd(tokens, remote_pipe_pos + 1, num_tokens, std_input);
    if (std_input == NULL) {
      printf("%s", msg_data);
      return 1;
    }
    else { // Send to origin
      char *fio_msg = dashp_fio(msg_data);
      send_to_host(fio_msg, origin);
      return 1;
    }
  }
  else // Remote pipe at remote_pipe_pos
  {
    char *subcommand = join_strings(tokens, " ", remote_pipe_pos+1, num_tokens);
    char *remotehost = extract_host(subcommand);
    char *maincommand = join_strings(tokens, " ", 0, remote_pipe_pos);
    char *msg_data = dash_exec_scmd(tokens, 0, remote_pipe_pos, std_input);
    char *send_msg;
    if (std_input == NULL) {
      // TODO: MAKE IP ADDRESS FUNCTION WORK!
      char *fin_remote_host = (char *) malloc (strlen(remotehost) + 1);
      strcat(fin_remote_host, remotehost);
      strcat(fin_remote_host, ":");
      remove_substring(subcommand, fin_remote_host);
      free (fin_remote_host);
      subcommand[strlen(subcommand)-1] = '\0';
      send_msg = dashp_pip(msg_data, "127.0.0.1", subcommand);
    }
    else {
      send_msg = dashp_pip(msg_data, origin, subcommand);
    }
    send_to_host(send_msg, remotehost);
    printf("Sent");
    return 2;
  }
}
Ejemplo n.º 3
0
GRID *read_grid(char *keyname,short print) {
	/*!
	 *\author Emanuele Cordano
	 *\date May 2008
	 *
	 */
	GRID *grid;
	long j,c;

	grid=(GRID *)malloc(sizeof(GRID));
	if (!grid) t_error("Grid in read_grid was not allocated");

	grid->file_resume_lines=copy_string(join_strings(keyname,LINE_SUFFIX));
	grid->file_resume_polygons=copy_string(join_strings(keyname,POLYGON_SUFFIX));
	grid->file_resume_connections=copy_string(join_strings(keyname,CONNECTION_SUFFIX));

	grid->lines=read_linevector(grid->file_resume_lines,print);
	grid->polygons=read_polygonvector(grid->file_resume_polygons,print);
	grid->links=read_connection_attribute_array(grid->file_resume_connections,print);

	/* check the boudaries */

	if (grid->polygons->nh!=grid->links->nh) printf ("Error in read_grid inconstancy betenn numbers of polygons %ld and of connection attributes %ld \n",grid->lines->nh,grid->links->nh);


	//j=link->nl;
	//while ((cf==0) || (df==0)) {
	for (j=grid->links->nl;j<=grid->links->nh;j++) {
		if (grid->links->element[j]->connections->nh!=grid->polygons->element[j]->edge_indices->nh)  printf ("Error in read_grid inconstancy between numbers of edge %ld and of connections  %ld at the polygon  %ld \n",grid->polygons->element[j]->edge_indices->nh,grid->links->element[j]->connections->nh,j);

		for (c=grid->links->element[j]->connections->nl;c<=grid->links->element[j]->connections->nh;c++) {
			if (grid->links->element[j]->connections->element[c]<0) grid->boundary_indicator=(long)grid->links->element[j]->connections->element[c];
		}
	}
	//grid->boundary_indicator=-10;
	if (print==1) printf("\n Function read_grid was successfully executed (number of polygons: %ld - number of lines: %ld - boundary indactor : %ld) \n",grid->polygons->nh,grid->lines->nh,grid->boundary_indicator);


	//}



//	char *file_resume_lines_fine=join_strings(resume_filenames,"_lines_fine.txt");
//	char *file_resume_polygons_fine=join_strings(resume_filenames,"_polygons_fine.txt");
//	char *file_resume_connections_fine=join_strings(resume_filenames,"_connections_fine.txt");
//	char *file_resume_c_polygon=join_strings(resume_filenames,"_c_polygon.txt");
//	char *file_resume_c_line=join_strings(resume_filenames,"_c_line.txt");

	return grid;
}
Ejemplo n.º 4
0
DOUBLESQUARE_GRID *read_doublesquare_grid (DOUBLERASTER_MAP *draster, char *keyname,long (*index_pixel_from_a_bin_coarse)(long r, long c,LONGVECTOR *s_index),long (*index_pixel_from_a_bin_fine)(long r, long c,LONGVECTOR *s_index),short print){
	/*
	 *
	 * \author Emanuele Cordano
	 * \date May 2009
	 *
	 *\param - DOUBLERASTER_MAP *draster
	 *\param - (char *) root neme of textfiles containing the struct information.
	 *\param long (*index_pixel_from_a_bin_coarse)(long r, long c,LONGVECTOR *s_index) - equation of the filling curve for the pixel of a coarse grid
	 *\param long (*index_pixel_from_a_bin_fine)(long r, long c,LONGVECTOR *s_index),long d_coarse,long d_fine,short print) - equation of the filling curve for the pixel of a fine grid

	 *\param (short) print
	 *
	 */

	DOUBLESQUARE_GRID *dsq;

	char *keyname_coarse=join_strings(keyname,"__coarse");
	char *keyname_fine=join_strings(keyname,"__fine");

	char *filename_c_polygon=join_strings(keyname,"_c_polygon.txt");
	char *filename_c_line=join_strings(keyname,"_c_line.txt");

	dsq=(DOUBLESQUARE_GRID *)malloc(sizeof(DOUBLESQUARE_GRID));
	if (!dsq) t_error("Double Square Grid dsq in read_doublesquare_grid was not allocated");

	dsq->big=read_square_grid(keyname_coarse,draster->coarse->layer[draster->coarse->reference_index_map],index_pixel_from_a_bin_coarse,draster->coarse->UV->V,draster->coarse->check_novalues,print);
	dsq->fine=read_square_grid(keyname_fine,draster->fine->layer[draster->fine->reference_index_map],index_pixel_from_a_bin_fine,draster->fine->UV->V,draster->fine->check_novalues,print);

	dsq->file_resume_c_line=copy_string(filename_c_line);
	dsq->file_resume_c_polygon=copy_string(filename_c_polygon);

	dsq->small_content_line=read_line_indices(dsq->file_resume_c_line,print);
	dsq->small_content_polygon=read_fine_indices(dsq->file_resume_c_polygon,print);

	if (dsq->small_content_line->index->nh!=dsq->big->grid->lines->nh) {
		printf("Error in read_doublesquare_grid inconstancy with number of (big) lines %ld and %ld \n",dsq->small_content_line->index->nh,dsq->big->grid->lines->nh);
		stop_execution();
	}
	if (dsq->small_content_polygon->nh!=dsq->big->grid->polygons->nh) {
		printf("Error in read_doublesquare_grid inconstancy with number of (big) lines %ld and %ld \n",dsq->small_content_polygon->nh,dsq->big->grid->polygons->nh);
		stop_execution();
	}

	if (print==1) printf("Function read_doublesquare_grid was successfully executed! \n ");

	return dsq;
}
Ejemplo n.º 5
0
/* This function performs what I'm calling the "gluing" together of strings.
 * For example, if you type
 *
 *   $ echo "a"'b'
 *
 * at the shell, the double-quoted string "a" will be glued together with the
 * single-quoted string 'b' because they are not separated by any whitespace.
 *
 * This transformation occurs after parameter expansion and word splitting, but
 * before filename expansion.  So, for example, "a"'b'* would be equivalent to
 * ab*, and ${a}b would be equivalent to "a" "cb" if the shell variable $a is
 * set to "a c".
 *
 * The input is a list @string_list that gives a list of strings passed to the
 * shell.  Each string is glued to any adjacent, succeeding strings that have
 * the flag STRING_FLAG_PRECEDING_WHITESPACE set, which indicates that they
 * should be glued to the preceding string.  The resulting list of "glued"
 * strings replaces the input list.
 *
 * This function also has a special responsibility where it looks for "glued"
 * strings that are constructed from an unquoted string that matches the regular
 * expression [A-Za-z_][A-Za-z_0-9]*=.*, followed by zero or more unquoted or
 * quoted strings.  So, for example,
 *   $ a="b"
 *   $ a=b
 *   $ a="b"'c'
 *   $ a=           # this is legal; it unsets the variable a
 * The glued strings of this form are interpreted as variable assignments, so
 * the STRING_FLAG_VAR_ASSIGNMENT flag is set on these glued strings.
 * */
static int glue_strings(struct list_head *string_list)
{
	struct string *s, *tmp;
	LIST_HEAD(new_list);
	while (!list_empty(string_list)) {
		struct list_head *first = string_list->next;
		int flags;
		/* Glue one string */
		LIST_HEAD(glue_list);
		list_move_tail(first, &glue_list);
		list_for_each_entry_safe(s, tmp, string_list, list) {
			if (s->flags & STRING_FLAG_PRECEDING_WHITESPACE)
				break;
			else
				list_move_tail(&s->list, &glue_list);
		}
		/* Detect variable assignments
		 * TODO: Somehow make it so that unquoted strings where
		 * parameter expansion has occurred on the left side of the
		 * equals sign are not considered variable assignments. */
		s = list_entry(first, struct string, list);
		if (s->flags & STRING_FLAG_UNQUOTED &&
		    string_matches_param_assignment(s))
			flags = STRING_FLAG_VAR_ASSIGNMENT;
		else
			flags = 0;
		s = join_strings(&glue_list);
		s->flags = flags;
		list_add_tail(&s->list, &new_list);
	}
	/* Replace @string_list with the list of glued strings */
	list_splice_tail(&new_list, string_list);
	return 0;
}
Ejemplo n.º 6
0
int update(int socket, t_env *e)
{
    t_client* tmp;
    int         alive;
    
    alive = 0;
    tmp = e->clients;
    while (e->clients != NULL)
    {
        if (e->clients->socket == socket)
        {
            alive = 1;
            break;
        }
        e->clients = e->clients->next;
    }
    e->clients = tmp;
    if (alive == 0)
    {
        my_send(socket, "ko");
        return -1;
    }
    char *map = join_strings(e->map, "\n", e->map_size[0] + 1);
    //puts(map);
    my_send(socket, map);
    return 0;
}
Ejemplo n.º 7
0
 std::string join_strings(
     const std::vector<std::string>& in,
     const std::string& separator
 ) {
     std::string result ;
     join_strings(in, separator, result) ;
     return result ;
 }
Ejemplo n.º 8
0
static void
var_set_tag(DCVariable *var, int argc, char **argv)
{
    char *new_value = join_strings(argv+1, argc-1, ' ');

    if (strpbrk(new_value, "$|") != NULL) {
        warn(_("Tag may not contain `$' or `|' characters.\n"));
	free(new_value);
    } else {
        free(my_tag);
        my_tag = new_value;
    }
}
Ejemplo n.º 9
0
char * extract_secret_from_share_strings(const char * string) {
	char ** shares = malloc(sizeof(char *) * 255);

	char * share;
	char * saveptr = NULL;
	int i = 0;

	/* strtok_rr modifies the string we are looking at, so make a temp copy */
	char * temp_string = strdup(string);

	/* Parse the string by line, remove trailing whitespace */
	share = strtok_rr(temp_string, "\n", &saveptr);

	shares[i] = strdup(share);
	trim_trailing_whitespace(shares[i]);

	while ( (share = strtok_rr(NULL, "\n", &saveptr))) {
		i++;

		shares[i] = strdup(share);

		trim_trailing_whitespace(shares[i]);

		if ((shares[i] != NULL) && (strlen(shares[i]) == 0)) {
			/* Ignore blank lines */
			free(shares[i]);
			i--;
		}
	}

	i++;

	char * secret = join_strings(shares, i);


/*
	fprintf(stdout, "count: %d\n", i);

	for (int j = 0; j < i; ++j)
	{
		fprintf(stderr, "'%s'\n", shares[j]);
	}
*/

	free_string_shares(shares, i);

	return secret;
}
Ejemplo n.º 10
0
static void
var_set_description(DCVariable *var, int argc, char **argv)
{
    char *new_value = join_strings(argv+1, argc-1, ' ');

    if (strpbrk(new_value, "$|") != NULL) {
        warn(_("Description may not contain `$' or `|' characters.\n"));
	free(new_value);
    } else if (strnlen(new_value, 36) >= 36) {
        warn(_("Description is too long - max length is 35 characters.\n"));
	free(new_value);
    } else {
	free(my_description);
	my_description = new_value;
    }
    
}
Ejemplo n.º 11
0
void write_esriascii(char *name, short type, DOUBLEMATRIX *DTM, T_INIT *UV, long novalue){

//	type=0  floating point
//	type=1  integer

	FILE *f;
	long r,c;
	char *temp;

	if(UV->U->co[1]!=UV->U->co[2]){
		printf("\nCannot export in esriascii, grid not square, Dx=%f Dy=%f \n",UV->U->co[2],UV->U->co[1]);
		t_error("Fatal error");
	}

	temp = join_strings(name,ascii_esri);
	f=fopen(temp,"w");
	
	fprintf(f,"ncols         %ld\n",DTM->nch);
	fprintf(f,"nrows         %ld\n",DTM->nrh);
	fprintf(f,"xllcorner     %f\n",UV->U->co[4]);
	fprintf(f,"yllcorner     %f\n",UV->U->co[3]);
	fprintf(f,"cellsize      %f\n",UV->U->co[1]);
	fprintf(f,"NODATA_value  %ld.0\n",novalue);
	
	for(r=1;r<=DTM->nrh;r++){
		for(c=1;c<=DTM->nch;c++){
			if((long)DTM->co[r][c]==novalue){
				fprintf(f,"%ld.0",novalue);
			}else{
				if(type==1){
					fprintf(f,"%ld",(long)(DTM->co[r][c]));
				}else{
					fprintf(f,"%f",DTM->co[r][c]);
				}
			}
			if(c<DTM->nch) fprintf(f," ");
		}
		if(r<DTM->nrh) fprintf(f,"\n");
	}
	
	fclose(f);
	free(temp);
}
Ejemplo n.º 12
0
static char* mid_stops_string(qgs_t *qgs)
{
  int n = qgs->n;

  if (n < 3) return NULL;

  char *stops[n-2];

  for (int i = 0 ; i < n-2 ; i++)
    {
      if ((stops[i] = mid_stop(qgs->entries + i + 1)) == NULL)
	return NULL;
    }

  char *buffer = join_strings(stops, n-2, ':');

  for (int i = 0 ; i < n-2 ; i++)
    free(stops[i]);

  return buffer;
}
Ejemplo n.º 13
0
void write_grassascii_vector(char *name, short type, DOUBLEVECTOR *DTM, long **j, long nr, long nc, T_INIT *UV, long novalue){
	
	//	type=0  floating point
	//	type=1  integer
	
	FILE *f;
	char *temp;
	long r,c;
	
	temp = join_strings(name,ascii_grass);
	f=fopen(temp,"w");
	
	fprintf(f,"north:%f\n",UV->U->co[3]+nr*UV->U->co[1]);
	fprintf(f,"south:%f\n",UV->U->co[3]);
	fprintf(f,"east:%f\n",UV->U->co[4]+nc*UV->U->co[2]);
	fprintf(f,"west:%f\n",UV->U->co[4]);
	fprintf(f,"rows:%ld\n",nr);
	fprintf(f,"cols:%ld\n",nc);

	for(r=1;r<=nr;r++){
		for(c=1;c<=nc;c++){
			if (j[r][c] > 0) {
				if(type==1){
					fprintf(f,"%ld",(long)(DTM->co[j[r][c]]));
				}else{
					fprintf(f,"%f",DTM->co[j[r][c]]);
				}
			}else {
				fprintf(f,"%ld.0",novalue);
			}
			if(c<nc) fprintf(f," ");
		}
		if(r<nr) fprintf(f,"\n");
	}

	fclose(f);
	free(temp);

}
Ejemplo n.º 14
0
void write_grassascii(char *name, short type, DOUBLEMATRIX *DTM, T_INIT *UV, long novalue){

//	type=0  floating point
//	type=1  integer

	FILE *f;
	char *temp;
	long r,c;

	temp = join_strings(name,ascii_grass);
	f=fopen(temp,"w");	
	
	fprintf(f,"north:%f\n",UV->U->co[3]+DTM->nrh*UV->U->co[1]);
	fprintf(f,"south:%f\n",UV->U->co[3]);
	fprintf(f,"east:%f\n",UV->U->co[4]+DTM->nch*UV->U->co[2]);
	fprintf(f,"west:%f\n",UV->U->co[4]);
	fprintf(f,"rows:%ld\n",DTM->nrh);
	fprintf(f,"cols:%ld\n",DTM->nch);
	
	for(r=1;r<=DTM->nrh;r++){
		for(c=1;c<=DTM->nch;c++){
			if((long)DTM->co[r][c]==(long)novalue){
				fprintf(f,"*");
			}else{
				if(type==1){
					fprintf(f,"%ld",(long)(DTM->co[r][c]));
				}else{
					fprintf(f,"%f",DTM->co[r][c]);
				}
			}
			if(c<DTM->nch) fprintf(f," ");
		}
		if(r<DTM->nrh) fprintf(f,"\n");
	}
	
	fclose(f);
	free(temp);

}
Ejemplo n.º 15
0
char *
concat_strings (char *const *argv)
{
  return join_strings ("", argv);
}