Ejemplo n.º 1
0
GList *get_list_from_buffer(gchar *buffer, GList *which_list, gboolean is_arraylist) {
	gchar *pos,*nextpos;
	pos = buffer;
	nextpos = strchr(pos, '\n');
	do {
		if (nextpos)
			*nextpos='\0';
		/*g_print("next line: %s\n",pos);*/
		if (is_arraylist) {
			gchar **temparr = string_to_array(pos);
			which_list = g_list_prepend(which_list, temparr);
		} else {
			DEBUG_MSG("get_list, adding string \"%s\" to the stringlist=%p\n", pos, which_list);
			which_list = g_list_prepend(which_list, g_strdup(pos));
		}
		if (nextpos) {
			nextpos++;
			if (*nextpos == '\0')
				pos = NULL;
			else
				pos = nextpos;
		} else 
			pos = NULL;
		if (pos)
			nextpos = strchr(pos, '\n');
	} while (pos);
	return g_list_reverse(which_list);
}
Ejemplo n.º 2
0
NODE* string_to_tree_converter(char *input_array)
{
	int *integer_array = string_to_array(input_array);
	integer_array = Quick_Sort(integer_array,0,array_length(integer_array)-1);
	NODE* output =  array_to_tree_convertor(integer_array,0,array_length(integer_array));
	free(integer_array);
	return output;
}
Ejemplo n.º 3
0
void test_tree_construction()
{
	char inorder[5][36] = { "8,4,9,2,10,5,11,1,12,6,13,3,14,7,15",
							"1,2,3,4",
							"1,2,3",
							"1,2,3,4,5,6,7,8,9,10,11,12,13,14,15",
							"1,2"
							};

	char preorder[5][36] = { "1,2,4,8,9,5,10,11,3,6,12,13,7,14,15",
							 "2,1,3,4",
							 "2,1,3",
							 "8,4,2,1,3,6,5,7,12,10,9,11,14,13,15",
							 "1,2"
						   };

	char expected_tree[5][52] = {"1,2,4,8,$,9,$,5,10,$,11,$,3,6,12,$,13,$,7,14,$,15,$",
		   						 "2,1,$,3,-,4,$",
								 "2,1,$,3,$",
								 "8,4,2,1,$,3,$,6,5,$,7,$,12,10,9,$,11,$,14,13,$,15,$",
								 "1,-,2,$"
								};
	int iter_loop;
	for(iter_loop=0;iter_loop<5;iter_loop++)
	{
		printf("%d-->",iter_loop+1);
		int preorder_index = 0;
		int *inorder_array = string_to_array(inorder[iter_loop]);
		int *preorder_array = string_to_array(preorder[iter_loop]);
		NODE *head  = construct_tree_from_inorder_preorder(inorder_array,preorder_array,0,array_length(inorder_array)-1,&preorder_index);
		NODE *root = (NODE*)malloc(sizeof(NODE));
		int index;
		index = next_element_in_string(expected_tree[iter_loop],0);
		root->data = create_number(expected_tree[iter_loop],index);
		index = next_element_in_string(expected_tree[iter_loop],index);
		construct_tree(root,expected_tree[iter_loop],index);
		(1==tree_comparator(head,root))?printf("ACCEPTED\n"):printf("REJECTED\n");
		delete_tree(head);
		free(inorder_array);
		free(preorder_array);
	}
}
Ejemplo n.º 4
0
//// read a file contents from either a archive of real directory
void b_zzip_read(task *tsk, pntr *argstack)
{
	char *fileName;
	pntr p = argstack[0];
	int badtype;

	CHECK_ARG(0, CELL_CONS);
	if((badtype = array_to_string(p, &fileName)) >= 0){
		set_error(tsk, "error1: argument is not a string (contains non-char: %s)", cell_types[badtype]);
		return;
	}
	
	ZZIP_FILE* fp = zzip_open (fileName, O_RDONLY|O_BINARY);

    if (! fp){
   		perror (fileName);
    }
    
    int bufSize = 2, blockSize = 1024, numBlocks = 1;
    char buf[bufSize];
    int n, counter = 0;
    char *contents = (char *)calloc(blockSize, sizeof(char));
    
    /* read chunks of bufSize bytes into buf and concatenate them into previous string */
    while( (n = (zzip_read(fp, buf, bufSize-1))) > 0){
    	counter++;
    	
    	if(counter == 1){
//    		strcpy(contents, buf);
			strncat(contents, buf, bufSize-1);
			bufSize = 21;
    	}else{
    		int originalSize = strlen(contents);
    		if( ((blockSize*numBlocks) - (originalSize + 1)) < bufSize){
    			numBlocks++;
    			contents = string_mkroom(contents, blockSize*numBlocks);
    		}

    		strncat(contents, buf, bufSize-1);
//    		printf("%s\n\n\n", contents);
    	}
    	buf[n] = '\0';
    }
    
    argstack[0] = string_to_array(tsk, contents);
	
	zzip_close(fp);
}
Ejemplo n.º 5
0
pntr decode_java_response(task *tsk, const char *str, endpointid source)
{
  int len = strlen(str);
  if ((2 <= len) && ('"' == str[0]) && ('"' == str[len-1])) {
    char *sub = substring(str,1,len-1);
    char *unescaped = unescape(sub);
    pntr p = string_to_array(tsk,unescaped);
    free(unescaped);
    free(sub);
    return p;
  }
  else if (!strcmp(str,"nil")) {
    return tsk->globnilpntr;
  }
  else if (!strcmp(str,"false")) {
    return tsk->globnilpntr;
  }
  else if (!strcmp(str,"true")) {
    return tsk->globtruepntr;
  }
  else if ((1 < len) && ('@' == str[0])) {
    int id = atoi(str+1);
    sysobject *so = new_sysobject(tsk,SYSOBJECT_JAVA);
    pntr p;
    so->jid.managerid = source;
    so->jid.jid = id;
    make_pntr(p,so->c);
    return p;
  }
  else if (!strncmp(str,"error: ",7)) {
    set_error(tsk,"jcall: %s",&str[7]);
    return tsk->globnilpntr;
  }
  else {
    char *end = NULL;
    double d = strtod(str,&end);
    if (('\0' != *str) && ('\0' == *end)) {
      pntr p;
      set_pntrdouble(p,d);
      return p;
    }
    else {
      set_error(tsk,"jcall: invalid response");
      return tsk->globnilpntr;
    }
  }
}
Ejemplo n.º 6
0
/*
 * parse a buffer.
 *
 * NOTE: parse() should not be called recusively by any other functions!
 */
void
parse(struct Client *client_p, char *pbuffer, char *bufend)
{
	struct Client *from = client_p;
	char *ch;
	char *s;
	char *end;
	int i;
	int paramcount, mpara = 0;
	char *numeric = 0;
	struct Message *mptr;

	Debug((DEBUG_DEBUG, "Parsing %s:", pbuffer));

	s_assert(!IsDead(client_p));
	s_assert(client_p->localClient->fd >= 0);
	if(IsDead(client_p) || client_p->localClient->fd < 0)
		return;
	s_assert(bufend-pbuffer < 512);

	for (ch = pbuffer; *ch == ' '; ch++)	/* skip spaces */
		/* null statement */ ;

	para[0] = from->name;

	if(*ch == ':')
	{
		ch++;

		/*
		 * Copy the prefix to 'sender' assuming it terminates
		 * with SPACE (or NULL, which is an error, though).
		 */

		sender = ch;

		if((s = strchr(ch, ' ')))
		{
			*s = '\0';
			s++;
			ch = s;
		}

		i = 0;

		if(*sender && IsServer(client_p))
		{
			from = find_client(sender);
			if(from == NULL)
			{
				from = find_server(sender);
			}

			/* Hmm! If the client corresponding to the
			 * prefix is not found--what is the correct
			 * action??? Now, I will ignore the message
			 * (old IRC just let it through as if the
			 * prefix just wasn't there...) --msa
			 */
			if(from == NULL)
			{
				Debug((DEBUG_ERROR, "Unknown prefix (%s)(%s) from (%s)",
				       sender, pbuffer, client_p->name));
				ServerStats->is_unpf++;

				remove_unknown(client_p, sender, pbuffer);

				return;
			}

			para[0] = from->name;

			if(from->from != client_p)
			{
				ServerStats->is_wrdi++;
				Debug((DEBUG_ERROR, "Message (%s) coming from (%s)",
				       pbuffer, client_p->name));

				cancel_clients(client_p, from, pbuffer);
				return;
			}
		}
		while (*ch == ' ')
			ch++;
	}

	if(*ch == '\0')
	{
		ServerStats->is_empt++;
		Debug((DEBUG_NOTICE, "Empty message from host %s:%s", client_p->name, from->name));
		return;
	}

	/*
	 * Extract the command code from the packet.  Point s to the end
	 * of the command code and calculate the length using pointer
	 * arithmetic.  Note: only need length for numerics and *all*
	 * numerics must have parameters and thus a space after the command
	 * code. -avalon
	 */

	/* EOB is 3 chars long but is not a numeric */

	if(*(ch + 3) == ' ' &&	/* ok, lets see if its a possible numeric.. */
	   IsDigit(*ch) && IsDigit(*(ch + 1)) && IsDigit(*(ch + 2)))
	{
		mptr = (struct Message *) NULL;
		numeric = ch;
		paramcount = MAXPARA;
		ServerStats->is_num++;
		s = ch + 3;	/* I know this is ' ' from above if */
		*s++ = '\0';	/* blow away the ' ', and point s to next part */
	}
	else
	{
		int ii = 0;

		if((s = strchr(ch, ' ')))
			*s++ = '\0';

		mptr = hash_parse(ch);

		if(!mptr || !mptr->cmd)
		{
			/*
			 * Note: Give error message *only* to recognized
			 * persons. It's a nightmare situation to have
			 * two programs sending "Unknown command"'s or
			 * equivalent to each other at full blast....
			 * If it has got to person state, it at least
			 * seems to be well behaving. Perhaps this message
			 * should never be generated, though...  --msa
			 * Hm, when is the buffer empty -- if a command
			 * code has been found ?? -Armin
			 */
			if(pbuffer[0] != '\0')
			{
				if(IsPerson(from))
					sendto_one(from,
						   ":%s %d %s %s :Unknown command",
						   me.name, ERR_UNKNOWNCOMMAND, from->name, ch);
				Debug((DEBUG_ERROR, "Unknown (%s) from %s",
				       ch, get_client_name(client_p, HIDE_IP)));
			}
			ServerStats->is_unco++;
			return;
		}

		paramcount = mptr->parameters;
		mpara = mptr->maxpara;

		ii = bufend - ((s) ? s : ch);
		mptr->bytes += ii;
	}

	end = bufend - 1;

	/* XXX this should be done before parse() is called */
	if(*end == '\n')
		*end-- = '\0';
	if(*end == '\r')
		*end = '\0';

	i = 0;

	if(s != NULL)
		i = string_to_array(s, para);

	if(mptr == (struct Message *) NULL)
	{
		do_numeric(numeric, client_p, from, i, para);
		return;
	}

	if(handle_command(mptr, client_p, from, i, para) < -1)
	{
		char *p;
		for (p = pbuffer; p <= end; p += 8)
		{
			/* HACK HACK */
			/* Its expected this nasty code can be removed
			 * or rewritten later if still needed.
			 */
			if((unsigned long) (p + 8) > (unsigned long) end)
			{
				for (; p <= end; p++)
				{
					ilog(L_CRIT, "%02x |%c", p[0], p[0]);
				}
			}
			else
				ilog(L_CRIT,
				     "%02x %02x %02x %02x %02x %02x %02x %02x |%c%c%c%c%c%c%c%c",
				     p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
				     p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
		}
	}
}
Ejemplo n.º 7
0
/*
 * parse a buffer.
 *
 * NOTE: parse() should not be called recusively by any other functions!
 */
void
parse(struct Client *client_p, char *pbuffer, char *bufend)
{
  struct Client *from = client_p;
  char *ch;
  char *s;
  char *numeric = 0;
  unsigned int i = 0;
  struct Message *mptr = NULL;

  if (IsDefunct(client_p))
    return;

  assert(client_p->localClient->fd.flags.open);
  assert((bufend - pbuffer) < 512);

  for (ch = pbuffer; *ch == ' '; ch++) /* skip spaces */
    /* null statement */ ;

  para[0] = from->name;

  if (*ch == ':')
  {
    ch++;

    /* Copy the prefix to 'sender' assuming it terminates
     * with SPACE (or NULL, which is an error, though).
     */
    sender = ch;

    if ((s = strchr(ch, ' ')) != NULL)
    {
      *s = '\0';
      s++;
      ch = s;
    }

    if (*sender && IsServer(client_p))
    {
      /*
       * XXX it could be useful to know which of these occurs most frequently.
       * the ID check should always come first, though, since it is so easy.
       */
      if ((from = find_person(client_p, sender)) == NULL)
      {
        from = find_server(sender);

	if (from == NULL && IsCapable(client_p, CAP_TS6) &&
	  client_p->name[0] == '*' && IsDigit(*sender) && strlen(sender) == 3)
	{
          /* Dirty hack to allow messages from masked SIDs (i.e. the ones
           * hidden by fakename="..."). It shouldn't break anything, since
           * unknown SIDs don't happen during normal ircd work --adx
           */
	  from = client_p;
	}
      }
      
      /* Hmm! If the client corresponding to the
       * prefix is not found--what is the correct
       * action??? Now, I will ignore the message
       * (old IRC just let it through as if the
       * prefix just wasn't there...) --msa
       */
      if (from == NULL)
      {
        ServerStats->is_unpf++;
        remove_unknown(client_p, sender, pbuffer);
        return;
      }

      para[0] = from->name;

      if (from->from != client_p)
      {
        ServerStats->is_wrdi++;
        cancel_clients(client_p, from, pbuffer);
        return;
      }
    }

    while (*ch == ' ')
      ch++;
  }

  if (*ch == '\0')
  {
    ServerStats->is_empt++;
    return;
  }

  /* Extract the command code from the packet.  Point s to the end
   * of the command code and calculate the length using pointer
   * arithmetic.  Note: only need length for numerics and *all*
   * numerics must have parameters and thus a space after the command
   * code. -avalon
   */

  /* EOB is 3 chars long but is not a numeric */
  if (*(ch + 3) == ' ' && /* ok, lets see if its a possible numeric.. */
      IsDigit(*ch) && IsDigit(*(ch + 1)) && IsDigit(*(ch + 2)))
  {
    mptr = NULL;
    numeric = ch;
    ServerStats->is_num++;
    s = ch + 3;  /* I know this is ' ' from above if            */
    *s++ = '\0'; /* blow away the ' ', and point s to next part */
  }
  else
  { 
    int ii = 0;

    if ((s = strchr(ch, ' ')) != NULL)
      *s++ = '\0';

    if ((mptr = find_command(ch)) == NULL)
    {
      /* Note: Give error message *only* to recognized
       * persons. It's a nightmare situation to have
       * two programs sending "Unknown command"'s or
       * equivalent to each other at full blast....
       * If it has got to person state, it at least
       * seems to be well behaving. Perhaps this message
       * should never be generated, though...  --msa
       * Hm, when is the buffer empty -- if a command
       * code has been found ?? -Armin
       */
      if (pbuffer[0] != '\0')
      {
        if (IsClient(from))
          sendto_one(from, form_str(ERR_UNKNOWNCOMMAND),
                     me.name, from->name, ch);
      }

      ServerStats->is_unco++;
      return;
    }

    assert(mptr->cmd != NULL);

    ii = bufend - ((s) ? s : ch);
    mptr->bytes += ii;
  }

  if (s != NULL)
    i = string_to_array(s, para);
  else
  {
    i = 0;
    para[1] = NULL;
  }

  if (mptr == NULL)
    do_numeric(numeric, client_p, from, i, para);
  else
    handle_command(mptr, client_p, from, i, para);
}
Ejemplo n.º 8
0
static gboolean parse_config_file(GList * config_list, gchar * filename)
{
	gboolean retval = FALSE;
	gchar *tmpstring = NULL, *tmpstring2;
	gchar **tmparray;
	GList *rclist, *tmplist, *tmplist2;
	Tconfig_list_item *tmpitem;

	DEBUG_MSG("parse_config_file, started\n");

	rclist = NULL;
	rclist = get_list(filename, rclist,FALSE);
	
	if (rclist == NULL) {
		DEBUG_MSG("no rclist, returning!\n");
		return retval;
	}

	/* empty all variables that have type GList ('l') */
	tmplist = g_list_first(config_list);
	while (tmplist != NULL) {
		tmpitem = (Tconfig_list_item *) tmplist->data;
		DEBUG_MSG("parse_config_file, type=%c, identifier=%s\n", tmpitem->type, tmpitem->identifier);
		if (tmpitem->type == 'l' || tmpitem->type == 'a') {
			DEBUG_MSG("parse_config_file, freeing list before filling it\n");
			free_stringlist((GList *) * (void **) tmpitem->pointer);
			*(void **) tmpitem->pointer = (GList *)NULL;
		}
		DEBUG_MSG("parse_config_file, type=%c, identifier=%s\n", tmpitem->type, tmpitem->identifier);
		tmplist = g_list_next(tmplist);
	}
	DEBUG_MSG("parse_config_file, all the type 'l' and 'a' have been emptied\n");
	DEBUG_MSG("parse_config_file, length rclist=%d\n", g_list_length(rclist));
/* And now for parsing every line in the config file, first check if there is a valid identifier at the start. */
	tmplist = g_list_first(rclist);
	while (tmplist) {
		tmpstring = (gchar *) tmplist->data;

		if (tmpstring != NULL) {
			DEBUG_MSG("parse_config_file, tmpstring=%s\n", tmpstring);
			g_strchug(tmpstring);

			tmplist2 = g_list_first(config_list);
			while (tmplist2) {
				tmpitem = (Tconfig_list_item *) tmplist2->data;
#ifdef DEVELOPMENT
				if (!tmpitem || !tmpitem->identifier || !tmpstring) {
					g_print("WARNING: almost a problem!\n");
				}
#endif
				if (g_strncasecmp(tmpitem->identifier, tmpstring, strlen(tmpitem->identifier)) == 0) {
					/* we have found the correct identifier */
					retval = TRUE;
					DEBUG_MSG("parse_config_file, identifier=%s, string=%s\n", tmpitem->identifier, tmpstring);
					/* move pointer past the identifier */
					tmpstring += strlen(tmpitem->identifier);
					trunc_on_char(tmpstring, '\n');
					g_strstrip(tmpstring);

					switch (tmpitem->type) {
					case 'i':
						*(int *) (void *) tmpitem->pointer = atoi(tmpstring);
						break;
					case 's':
						*(void **) tmpitem->pointer = (char *) realloc((char *) *(void **) tmpitem->pointer, strlen(tmpstring) + 1);
						strcpy((char *) *(void **) tmpitem->pointer, tmpstring);
						break;
					case 'e':
						tmpstring2 = unescape_string(tmpstring, FALSE); /* I wonder if that should be TRUE */
						*(void **) tmpitem->pointer = (char *) realloc((char *) *(void **) tmpitem->pointer, strlen(tmpstring2) + 1);
						strcpy((char *) *(void **) tmpitem->pointer, tmpstring2);
						g_free(tmpstring2);
						break;
					case 'l':
					case 'm':
						tmpstring2 = g_strdup(tmpstring);
						* (void **) tmpitem->pointer = g_list_prepend((GList *) * (void **) tmpitem->pointer, tmpstring2);
						DEBUG_MSG("parse_config_file, *(void **)tmpitem->pointer=%p\n", *(void **) tmpitem->pointer);
						break;
					case 'a':
						tmparray = string_to_array(tmpstring);
						if (tmpitem->len <= 0 || tmpitem->len == count_array(tmparray)) {
							* (void **) tmpitem->pointer = g_list_prepend((GList *) * (void **) tmpitem->pointer, tmparray);
						} else {
							DEBUG_MSG("parse_config_file, not storing array, count_array() != tmpitem->len\n");
							g_strfreev(tmparray);
						}
						DEBUG_MSG("parse_config_file, *(void **)tmpitem->pointer=%p\n", *(void **) tmpitem->pointer);
						break;
					default:
						break;
					}
					tmplist2 = g_list_last(tmplist2);
				}
				tmplist2 = g_list_next(tmplist2);
			}
		}
		tmplist = g_list_next(tmplist);
	}
	DEBUG_MSG("parse_config_file, parsed all entries, freeing list read from file\n");	
	free_stringlist(rclist);
	return retval;
}
Ejemplo n.º 9
0
/* parse()
 *
 * given a raw buffer, parses it and generates parv, parc and sender
 */
void
parse(struct Client *client_p, char *pbuffer, char *bufend)
{
	struct Client *from = client_p;
	char *ch;
	char *s;
	char *end;
	int i = 1;
	char *numeric = 0;
	struct Message *mptr;

	s_assert(MyConnect(client_p));
	s_assert(client_p->localClient->fd >= 0);
	if(IsAnyDead(client_p))
		return;

	for (ch = pbuffer; *ch == ' '; ch++)	/* skip spaces */
		/* null statement */ ;

	para[0] = from->name;

	if(*ch == ':')
	{
		ch++;

		/* point sender to the sender param */
		sender = ch;

		if((s = strchr(ch, ' ')))
		{
			*s = '\0';
			s++;
			ch = s;
		}

		if(*sender && IsServer(client_p))
		{
			from = find_any_client(sender);

			/* didnt find any matching client, issue a kill */
			if(from == NULL)
			{
				ServerStats->is_unpf++;
				remove_unknown(client_p, sender, pbuffer);
				return;
			}

			para[0] = from->name;

			/* fake direction, hmm. */
			if(from->from != client_p)
			{
				ServerStats->is_wrdi++;
				cancel_clients(client_p, from, pbuffer);
				return;
			}
		}
		while (*ch == ' ')
			ch++;
	}

	if(*ch == '\0')
	{
		ServerStats->is_empt++;
		return;
	}

	/* at this point there must be some sort of command parameter */

	/*
	 * Extract the command code from the packet.  Point s to the end
	 * of the command code and calculate the length using pointer
	 * arithmetic.  Note: only need length for numerics and *all*
	 * numerics must have parameters and thus a space after the command
	 * code. -avalon
	 */

	/* EOB is 3 chars long but is not a numeric */

	if(*(ch + 3) == ' ' &&	/* ok, lets see if its a possible numeric.. */
	   IsDigit(*ch) && IsDigit(*(ch + 1)) && IsDigit(*(ch + 2)))
	{
		mptr = NULL;
		numeric = ch;
		ServerStats->is_num++;
		s = ch + 3;	/* I know this is ' ' from above if */
		*s++ = '\0';	/* blow away the ' ', and point s to next part */
	}
	else
	{
		int ii = 0;

		if((s = strchr(ch, ' ')))
			*s++ = '\0';

		mptr = hash_parse(ch);

		/* no command or its encap only, error */
		if(!mptr || !mptr->cmd)
		{
			/*
			 * Note: Give error message *only* to recognized
			 * persons. It's a nightmare situation to have
			 * two programs sending "Unknown command"'s or
			 * equivalent to each other at full blast....
			 * If it has got to person state, it at least
			 * seems to be well behaving. Perhaps this message
			 * should never be generated, though...  --msa
			 * Hm, when is the buffer empty -- if a command
			 * code has been found ?? -Armin
			 */
			if(pbuffer[0] != '\0')
			{
				if(IsPerson(from))
					sendto_one(from, form_str(ERR_UNKNOWNCOMMAND),
						   me.name, from->name, ch);
			}
			ServerStats->is_unco++;
			return;
		}

		ii = bufend - ((s) ? s : ch);
		mptr->bytes += ii;
	}

	end = bufend - 1;

	/* XXX this should be done before parse() is called */
	if(*end == '\n')
		*end-- = '\0';
	if(*end == '\r')
		*end = '\0';

	if(s != NULL)
		i = string_to_array(s, para);

	if(mptr == NULL)
	{
		do_numeric(numeric, client_p, from, i, para);
		return;
	}

	if(handle_command(mptr, client_p, from, i, /* XXX discards const!!! */ (const char **)para) < -1)
	{
		char *p;
		for (p = pbuffer; p <= end; p += 8)
		{
			/* HACK HACK */
			/* Its expected this nasty code can be removed
			 * or rewritten later if still needed.
			 */
			if((unsigned long) (p + 8) > (unsigned long) end)
			{
				for (; p <= end; p++)
				{
					ilog(L_MAIN, "%02x |%c", p[0], p[0]);
				}
			}
			else
				ilog(L_MAIN,
				     "%02x %02x %02x %02x %02x %02x %02x %02x |%c%c%c%c%c%c%c%c",
				     p[0], p[1], p[2], p[3], p[4], p[5],
				     p[6], p[7], p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
		}
	}

}
Ejemplo n.º 10
0
/*    enlarge() */
static void b_enlargeRect1(task *tsk, pntr *argstack)
{
    /* pointers to the parameters */
    pntr val1 = argstack[1]; // originalRect
    pntr val2 = argstack[0]; // times
    int badtype;

    /* the result value to be return */
    Rectangle *rect_return = new_Rectangle();

    Rectangle *originalRect = new_Rectangle();
    int times;

    /* Check validity of each parameter */
    CHECK_ARG(1, CELL_CONS);
    CHECK_ARG(0, CELL_NUMBER);

    /* Initialize all arguments for this method */

    /* Initialize the struct: Rectangle */
    pntr originalRect_val1 = head(tsk, val1);
    originalRect->width = pntrdouble(originalRect_val1);
    pntr originalRect_val2 = head(tsk, tail(tsk, val1));
    if( (badtype = array_to_string(originalRect_val2, &(originalRect->creator) )) > 0) {
        set_error(tsk, "string: argument is not a string (contains non-char: %s)", cell_types[badtype]);
        return;
    }

    pntr originalRect_val3 = head(tsk, tail(tsk, tail(tsk, val1)));
    originalRect->height = pntrdouble(originalRect_val3);

    /* Initialize another struct: Color*/
    Color *originalRect_col = new_Color( );
    originalRect->col = originalRect_col;
    /* new root pntr for struct Color */
    pntr originalRect_col_val = head(tsk, tail(tsk, tail(tsk, tail(tsk, val1))));
    pntr originalRect_col_val1 = head(tsk, originalRect_col_val);
    originalRect_col->red = pntrdouble(originalRect_col_val1);
    pntr originalRect_col_val2 = head(tsk, tail(tsk, originalRect_col_val));
    originalRect_col->blue = pntrdouble(originalRect_col_val2);
    pntr originalRect_col_val3 = head(tsk, tail(tsk, tail(tsk, originalRect_col_val)));
    originalRect_col->green = pntrdouble(originalRect_col_val3);

    /* Initialize another struct: gray*/
    gray *originalRect_col_cg = new_gray( );
    originalRect_col->cg = originalRect_col_cg;
    /* new root pntr for struct gray */
    pntr originalRect_col_cg_val = head(tsk, tail(tsk, tail(tsk, tail(tsk, originalRect_col_val))));
    pntr originalRect_col_cg_val1 = head(tsk, originalRect_col_cg_val);
    if( (badtype = array_to_string(originalRect_col_cg_val1, &(originalRect_col_cg->country) )) > 0) {
        set_error(tsk, "string: argument is not a string (contains non-char: %s)", cell_types[badtype]);
        return;
    }

    pntr originalRect_col_cg_val2 = head(tsk, tail(tsk, originalRect_col_cg_val));
    originalRect_col_cg->grayCode = pntrdouble(originalRect_col_cg_val2);

    /* end Initialization of struct Rectangle */
    times = pntrdouble(val2);
    
    /* Call the method and get the return value */
    rect_return = enlargeRect(originalRect, times);
    
    /* Translate the resultant pntr to be return */

    /* Translate C struct to ELC struct */

    /* pntr for struct gray*/
    pntr p_rect_return_col_cg_country = string_to_array(tsk, rect_return->col->cg->country);
    pntr p_rect_return_col_cg_grayCode;
    set_pntrdouble(p_rect_return_col_cg_grayCode, rect_return->col->cg->grayCode);
    /* the root pntr for struct gray */
    pntr p_rect_return_col_cg = make_cons(tsk, p_rect_return_col_cg_country, make_cons(tsk, p_rect_return_col_cg_grayCode, tsk->globnilpntr));


    /* pntr for struct Color*/
    pntr p_rect_return_col_red;
    set_pntrdouble(p_rect_return_col_red, rect_return->col->red);
    pntr p_rect_return_col_blue;
    set_pntrdouble(p_rect_return_col_blue, rect_return->col->blue);
    pntr p_rect_return_col_green;
    set_pntrdouble(p_rect_return_col_green, rect_return->col->green);
    /* the root pntr for struct Color */
    pntr p_rect_return_col = make_cons(tsk, p_rect_return_col_red, make_cons(tsk, p_rect_return_col_blue, make_cons(tsk, p_rect_return_col_green, make_cons(tsk, p_rect_return_col_cg, tsk->globnilpntr))));


    /* pntr for struct Rectangle*/
    pntr p_rect_return_width;
    set_pntrdouble(p_rect_return_width, rect_return->width);
    pntr p_rect_return_creator = string_to_array(tsk, rect_return->creator);
    pntr p_rect_return_height;
    set_pntrdouble(p_rect_return_height, rect_return->height);
    /* the root pntr for struct Rectangle */
    pntr p_rect_return = make_cons(tsk, p_rect_return_width, make_cons(tsk, p_rect_return_creator, make_cons(tsk, p_rect_return_height, make_cons(tsk, p_rect_return_col, tsk->globnilpntr))));

    /* set the return value */
    argstack[0] = p_rect_return;

    /* Free the return struct */
    free_Rectangle(rect_return);
}
Ejemplo n.º 11
0
//// read the directory entries of given dir/archive
static void b_zzip_read_dirent(task *tsk, pntr *argstack)
{
	char *fileName;
	pntr p = argstack[0];
	int badtype;
	
	CHECK_ARG(0, CELL_CONS);
	if((badtype = array_to_string(p, &fileName)) >= 0){
		set_error(tsk, "error1: argument is not a string (contains non-char: %s)", cell_types[badtype]);
		return;
	}

    ZZIP_DIR * dir;
    ZZIP_DIRENT * d;
  
    dir = zzip_opendir(fileName);
    if (! dir){
    	fprintf (stderr, "did not open %s: ", fileName);
    	set_error(tsk, "error1: could not handle file: %s", fileName);
		return;
    }

    char *singleFileName;
    char *compressionType;
    int fSize = 20;
    char fileSize[fSize];
    char compressedSize[fSize];
    pntr pSingleFileName, pCompressionType, pFileSize, pCompressedSize;
    pntr preList, singleList;
    int counter = 0;
    
	/* read each dir entry, a list for each file */
	while ((d = zzip_readdir (dir))){
		counter++;
		/* orignal size / compression-type / compression-ratio / filename */
		singleFileName = d->d_name;
		pSingleFileName = string_to_array(tsk, singleFileName);	//// convert the string to cons list
		
//		sprintf(compressionType, "%s ", zzip_compr_str(d->d_compr)); //// NOTE: executing this func will change the tsk->steamstack, very weird
																	//// NOTE: overflow caused here
		compressionType = (char *)zzip_compr_str(d->d_compr);
		pCompressionType = string_to_array(tsk, compressionType);
		
//		snprintf(fileSize, 5, "%d ", d->st_size);	//// NOTE: executing this func will change the tsk->steamstack, very weird
		format_double(fileSize, fSize, d->st_size);
		pFileSize = string_to_array(tsk, fileSize);

//		sprintf(compressedSize, "%d ", d->d_csize);
		format_double(compressedSize, fSize, d->d_csize);
		pCompressedSize = string_to_array(tsk, compressedSize);
		
//		printf("cell type: %s \t", cell_type(preList));
		//// link the cons lists to form a new list
//		singleList = connect_lists(tsk, &pSingleFileName, &pCompressionType);
//		singleList = connect_lists(tsk, &singleList, &pFileSize);
//		singleList = connect_lists(tsk, &singleList, &pCompressedSize);
		
		//// make cons from the last element to the beginning element
		singleList = make_cons(tsk, pCompressedSize, tsk->globnilpntr);
		singleList = make_cons(tsk, pFileSize, singleList);
		singleList = make_cons(tsk, pCompressionType, singleList);
		singleList = make_cons(tsk, pSingleFileName, singleList);
		
		
		if(counter == 1){
			preList = make_cons(tsk, singleList, tsk->globnilpntr);
//			printf("cell type: %s \t", cell_type(preList));
		}else{
			preList = make_cons(tsk, singleList, preList);
//			printf("cell type: %s \n", cell_type(preList));
		}
	}

	argstack[0] = preList;
//	printf("cell type: %s \n", cell_type(argstack[0]));
}
Ejemplo n.º 12
0
int main(int argc, char **argv) {
    char * tmpdir = getenv("_FAKEUSER_DIR_");
    char *passwd_file, *group_file;

    name = argv[0];
    int opt, ret = 0;
    int action = 0, uid = 0, gid = 0;
    char *name = NULL, *passwd = NULL, *members = NULL, *shell = NULL, *gecos = NULL, *dir = NULL;
    extern char *optarg;

    while ((opt = getopt(argc, argv, "UGu:g:n:p:m:s:c:d:h")) != -1) {
        switch (opt) {
        case 'U':
            action = 'U';
            break;
        case 'G':
            action = 'G';
            break;
        case 'u':
            uid = atoi(optarg);
            break;
        case 'g':
            gid = atoi(optarg);
            break;
        case 'n':
            name = optarg;
            break;
        case 'p':
            passwd = optarg;
            break;
        case 'm':
            members = optarg;
            break;
        case 's':
            shell = optarg;
            break;
        case 'c':
            gecos = optarg;
            break;
        case 'd':
            dir = optarg;
            break;
        case 'h':
            help();
            exit(EXIT_SUCCESS);
        default: /* '?' */
            usage_fd(stderr);
            exit(EXIT_FAILURE);
        }
    }
    if (action == 0 || name == NULL) {
        usage();
        exit(EXIT_FAILURE);
    }
    // only continue when environment variable with directory found.
    if (!tmpdir) {
        fputs("Error! Not in fakeuser environment\n", stderr);
        exit(EXIT_FAILURE);
    }
    // init file paths
    passwd_file = (char*)malloc(strlen(tmpdir)+10);
    strcpy(passwd_file, tmpdir);
    strcat(passwd_file, "/passwd");

    group_file = (char*)malloc(strlen(tmpdir)+10);
    strcpy(group_file, tmpdir);
    strcat(group_file, "/group");

    // Create directory structure
    mkdir_r(tmpdir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);

    if (action == 'U') {
        // create and append passwd entry
        struct passwd pw;
        pw.pw_name = name;
        pw.pw_passwd = passwd ? passwd : "";
        pw.pw_gecos = gecos ? gecos : "";
        pw.pw_dir = dir ? dir : "";
        pw.pw_shell = shell ? shell : "";
        pw.pw_uid = uid;
        pw.pw_gid = gid;
        // append to file with error handling.
        FILE * pwf = fopen(passwd_file, "a");
        if (pwf) {
            if(putpwent(&pw, pwf))
                ret = EIO;
            if (fclose(pwf))
                ret = EIO;
        } else
            ret = EIO;
    } else if (action == 'G') {
        // create and append group entry
        struct group gr;
        gr.gr_name = name;
        gr.gr_passwd = passwd ? passwd : "";
        gr.gr_gid = gid;
        char *strings;
        gr.gr_mem = members ? string_to_array(members, " ,;", &strings) : (char *[]) {
            NULL
        };
        // append to file with error handling.
        FILE * pwf = fopen(group_file, "a");
        if (pwf) {
            if(putgrent(&gr, pwf))
                ret = EIO;
            if (fclose(pwf))
                ret = EIO;
        } else
            ret = EIO;
    }
    // return 0 on success or the error value
    return ret;
}