示例#1
0
文件: launch.c 项目: Sorikairo/42sh
char	*get_value(char *name, char **env)
{
  int	k;
  int	i;
  int	j;
  char	*ret;

  k = 0;
  while (env[k] != NULL)
    {
      if (strncmp(name, env[k], utl_strlen(name)) == 0)
	{
	  if ((ret = malloc_str(sizeof(*ret) *
				(utl_strlen(env[k]) + 1))) == NULL)
	    return (name);
	  i = 0;
	  j = 0;
	  while (i <= utl_strlen(name) && env[k][i] != 0)
	    i++;
	  while (env[k][i] != 0)
	    ret[j++] = env[k][i++];
	  ret[j] = 0;
	  return (ret);
	}
      k++;
    }
  return (name);
}
示例#2
0
文件: launch.c 项目: Sorikairo/42sh
void	get_env_var(t_cmd *cmd, t_system *sys)
{
  char	*tp;
  int	k;
  int	i;
  int	j;

  k = 0;
  while (cmd->args[k] != NULL)
    {
      if (cmd->args[k][0] == '$' && cmd->args[k][1] != 0)
	{
	  if ((tp = malloc_str(sizeof(*tp) *
			       (utl_strlen(cmd->args[k]) + 1))) == NULL)
	    return;
	  i = 0;
	  j = 1;
	  while (cmd->args[k][j] != 0)
	    tp[i++] = cmd->args[k][j++];
	  tp[i] = 0;
	  args_modifier(cmd, tp, sys, k);
	  free(tp);
	}
      k++;
    }
}
示例#3
0
 struct node *putloop(struct node 
void testcases()
{
	struct node *res1,*ptr1,*ptr2,*res;
	char *ip1,*op;
	int i,check;
	for(i=0;i<2;i++)
	{
		res1=NULL;
		ip1=NULL;
		ip1=NULL;
		ip1=malloc_str(testDB[i].input1);
		ip1=valid_str(ip1);
		if(ip1!=NULL)
		{
			ip1=rem_space(ip1);
			res1=create_list(ip1);
			res1=putloop(res1);
			res1=removeloop(res1);

			
		}
		
		
		
		display(res);
		 
		free(op);
		free(ip1);
		free(res);
		free(output);

	}

}
示例#4
0
char * splitkey(char * key, int maxkey, char * val, int maxvalue, const char ** pend, const char * buf)
{
	const char * pv;
	char *backupstr;

	if(!buf && *buf)
		return 0;

	backupstr = malloc_str(buf, -1);
	*key = 0;
	*val = 0;
	
	pv = strchr_s(backupstr, '=');
	if(pv != NULL)
	{
		backupstr[pv-backupstr] = ' '; //将key和alue使用空格分割
		getkey(key, maxkey, 0, backupstr);
		getkey(val, maxvalue, pend, pv);
	}
	else
	{
		getkey(key, maxkey, pend, backupstr);
	}

	free(backupstr);
	
	if(*key == 0)
		return 0;

	return key;
}
示例#5
0
int		get_next_line(const int fd, char **line)
{
	char			buff[BUFF_SIZE + 1];
	int				ret;
	static t_stock	*list = NULL;
	char			**str;

	str = multi_fd(fd, &list);
	if (fd < 0 || !line || read(fd, buff, 0) < 0)
		return (-1);
	if (*str == NULL)
	{
		*str = ft_strdup("\0");
	}
	while ((ret = read(fd, buff, BUFF_SIZE)) > 0)
	{
		*str = (ft_concat(buff, ret, *str));
		if (ft_strstr(*str, "\n") != NULL)
			break ;
	}
	*str = malloc_str(*str, line);
	if (*str == NULL && ft_strcmp(*line, "\0") == 0)
		return (0);
	return (1);
}
示例#6
0
void testcases()
{
	struct node *res,*output;
	char *ip,*op;
	int i,check;
	for(i=0;i<9;i++)
	{
		res=output=NULL;
		ip=op=NULL;
		ip=malloc_str(testDB[i].input);
		
		ip=valid_str(ip);
		//if(ip==NULL)
			//printf("hai\n");
		if(ip!=NULL)
		{
			ip=rem_space(ip);
			res=create_list(ip);
			//display(res);
			res=swap_adj_nodes(res);
		}
		else
		{
			res=NULL;
		}
		op=malloc_str(testDB[i].output);
		output=create_list(op);
		//if(res==NULL)
			
		check=list_cmp(res,output);
		display(res);
		display(output);
		if(check==0)
			printf("passed\n");
		else 
			printf("failed\n");
		free(op);
		free(ip);
		free(res);
		free(output);

	}

}
示例#7
0
void anlizetextbtw(const char * buf, callback deal, void * parm)
{
	char * texttw;
	const char * pstart = strchr_s(buf, '<');
	
	if(pstart == 0 || pstart-buf == 0)
		return ;
	
	texttw = malloc_str(buf, pstart-buf);
	deal(MARK_BTW, texttw, pstart-buf, parm);
	free(texttw);
}
示例#8
0
static void compile(route *route, char *path) {
  int status, pos = 0;
  regex_t *result = malloc(sizeof(regex_t));
  regmatch_t matches[2];
  char *named_param_re_src = "([^/]*)";
  char *matcher_re_src = malloc_str(strlen(path) * 2);

  // capture named params and build regex
  while ((status = regexec(configuration_get()->param_match_regex, path + pos, 2, matches, 0)) == 0) {
    int match_length = matches[1].rm_eo - matches[1].rm_so;

    // get everething before parameter in url - step 1: /foo/(:bar)/test -> /foo/
    memcpy(matcher_re_src + strlen(matcher_re_src), path + pos, matches[0].rm_so);
    // put parameter regex - step 2: /foo/(:bar)/test -> /foo/([^/]*)
    memcpy(matcher_re_src + strlen(matcher_re_src), named_param_re_src, strlen(named_param_re_src));
    // capture named parameter name to use later during match
    char *parameter = malloc_str(match_length);
    memcpy(parameter, path + pos + matches[1].rm_so, matches[1].rm_eo - matches[1].rm_so);

    route->named_params[route->named_params_count] = parameter;
    route->named_params_count++;

    // advance
    pos += matches[0].rm_eo;
  }

  // remaining url part - step 3: /foo/(:bar)/test -> /foo/([^/]*)/test
  memcpy(matcher_re_src + strlen(matcher_re_src), path + pos, strlen(path) - pos);

  char *regex = malloc_str(strlen("^") + strlen(matcher_re_src) + strlen("$"));
  sprintf(regex, "^%s$", matcher_re_src);
  if(regcomp(result, regex, REG_EXTENDED) != 0) {
    die("Cannot compile dynamic matcher for route: %s", path);
  }
  free(regex);
  free(matcher_re_src);

  route->matcher = result;
}
示例#9
0
bool route_match(route *route, const char *path, params_map *params) {
  regmatch_t matches[route->named_params_count + 1];

  int status = regexec(route->matcher, path, route->named_params_count + 1, matches, 0);
  bool match = (status == 0);

  if (match && route->named_params_count > 0) {
    int i = 0;
    for (i = 0; i < route->named_params_count; i++) {
      int length = matches[i + 1].rm_eo - matches[i + 1].rm_so;
      char *param_value = malloc_str(length);

      memcpy(param_value, path + matches[i + 1].rm_so, length);
      params_map_add_str(params, route->named_params[i], param_value);

      free(param_value);
    }
  }

  return match;
}
示例#10
0
void testcases()
{
	struct node *res;
	char *ip;
	int i,check;
	float res1;
	for(i=0;i<10;i++)
	{
		res=NULL;
		ip=NULL;
		ip=malloc_str(testDB[i].input);
		
		ip=valid_str(ip);
		if(ip!=NULL)
		{
			ip=rem_space(ip);
			res=create_list(ip);
			res=mergesort(res);
			res1=get_median(res);
		}
		else
		{
			res1=0;
		}
			
		check=val_cmp(res1,testDB[i].value);
		//printf("%f\t%f\n",res1,testDB[i].value);
		
		if(check==0)
			printf("passed\n");
		else 
			printf("failed\n");
		
		free(ip);
		free(res);
		

	}

}
示例#11
0
char *params_map_serialize(params_map *p_map) {
  dictIterator *dict_iterator;
  dictEntry *dict_entry;
  param_entry *param;
  char *line, *result = NULL;

  dict_iterator = dictGetIterator(p_map);

  while((dict_entry = dictNext(dict_iterator)) != NULL) {
    param = (param_entry *)dictGetEntryVal(dict_entry);

    line = malloc_str(strlen("[") + strlen(param->name) + strlen(":") + strlen(param->val) + strlen("]"));
    sprintf(line, "[%s:%s]", param->name, param->val);

    result = str_concat(result, line);

    free(line);
  }

  free(dict_iterator);

  if (result == NULL) result = strdup("Params hash is empty");
  return result;
}
示例#12
0
static char *copy_chunk_from_buffer(const char *buf, size_t len) {
  char *field = malloc_str(len);
  strncat(field, buf, len);

  return field;
}
示例#13
0
char *build_header_header(http_response *response) {
  char *result = malloc_str(3 + strlen("HTTP/1.1 XXX ") + strlen(response->header_summary) + strlen("\n"));
  sprintf(result, "HTTP/1.1 %d %s\n", response->code, response->header_summary);

  return result;
}
示例#14
0
const char * anlizetext(const char * mark, const char * buf, callback deal, void * parm)
{
	char  * marklow = (char *)malloc(strlen(mark)+1);
	strtolower(marklow, mark);

	if(marklow[0] != '/')
	{
		int count = 1;
		char bname[1024];
		char nameend[1024];
		char tmpmark[1024];
		const char * bufbp = buf;
		const char * next = buf;

		strcpy(nameend, "/");
		strcat(nameend,  marklow);

		if(!strcmp(marklow, "script")) //   跳过关于script内部的内容
		{
			char * tw = 0;
			strcpy(nameend, "</");
			strcat(nameend, marklow);
			
			buf = getmark(nameend, buf);
			if(buf == 0)
			{
				free(marklow);
				return bufbp;
			}
			tw = malloc_str(bufbp, buf-bufbp);
			if(tw)
			{
				deal(MARK_BTW, tw, buf-bufbp, parm);
				free(tw);
			}
			count = 0;
		}

		while(count)
		{
			buf = next;
			if(!anlizemark(tmpmark, 1024, &next, buf))
				break ;

			if(isnote(tmpmark))
				continue;

			if(!getkey(bname, 1024, 0, tmpmark))
				break;

			strtolower(bname, bname);
			if(!strcmp(bname, marklow))
				count++;
			if(!strcmp(bname, nameend))
				count--;
			buf = strchr_s(buf, '<');
		}

		while(count == 0)
		{
			char * tmpbuf = malloc_str(bufbp, buf-bufbp);
			if(!tmpbuf)
				break;
				
			deal(MARK_TXT, tmpbuf, buf-bufbp+1, parm);
			free(tmpbuf);
			break;
		}
	}
	free(marklow);
	return buf;
}