int main(void)
{
	struct node *list = NULL;
	int number;
	
	printf("Please enter random number, and press '0' to quit\n");
	printf("Input number: ");
	scanf("%d", &number);
	
	while (number != 0) {
		list = add_to_list(list, number);
		printf("Input number: ");
		scanf("%d", &number);
	}
	
	printf("Please enter the number which you want to search and count: ");
	scanf("%d", &number);
	printf("The number %d is stored in the list %d times.\n", number, count_occurrences(list, number));
	
	free(list);
	
	return 0;
}
Example #2
0
static bool
find_inc (bool first_try)
{
  rtx insn;
  basic_block bb = BLOCK_FOR_INSN (mem_insn.insn);
  rtx other_insn;
  df_ref *def_rec;

  /* Make sure this reg appears only once in this insn.  */
  if (count_occurrences (PATTERN (mem_insn.insn), mem_insn.reg0, 1) != 1)
    {
      if (dump_file)
	fprintf (dump_file, "mem count failure\n");
      return false;
    }

  if (dump_file)
    dump_mem_insn (dump_file);

  /* Find the next use that is an inc.  */
  insn = get_next_ref (REGNO (mem_insn.reg0),
		       BLOCK_FOR_INSN (mem_insn.insn),
		       reg_next_inc_use);
  if (!insn)
    return false;

  /* Even though we know the next use is an add or inc because it came
     from the reg_next_inc_use, we must still reparse.  */
  if (!parse_add_or_inc (insn, false))
    {
      /* Next use was not an add.  Look for one extra case. It could be
	 that we have:

	 *(a + b)
	 ...= a;
	 ...= b + a

	 if we reverse the operands in the mem ref we would
	 find this.  Only try it once though.  */
      if (first_try && !mem_insn.reg1_is_const)
	{
	  reverse_mem ();
	  return find_inc (false);
	}
      else
	return false;
    }

  /* Need to assure that none of the operands of the inc instruction are
     assigned to by the mem insn.  */
  for (def_rec = DF_INSN_DEFS (mem_insn.insn); *def_rec; def_rec++)
    {
      df_ref def = *def_rec;
      unsigned int regno = DF_REF_REGNO (def);
      if ((regno == REGNO (inc_insn.reg0))
	  || (regno == REGNO (inc_insn.reg_res)))
	{
	  if (dump_file)
	    fprintf (dump_file, "inc conflicts with store failure.\n");
	  return false;
	}
      if (!inc_insn.reg1_is_const && (regno == REGNO (inc_insn.reg1)))
	{
	  if (dump_file)
	    fprintf (dump_file, "inc conflicts with store failure.\n");
	  return false;
	}
    }

  if (dump_file)
    dump_inc_insn (dump_file);

  if (inc_insn.form == FORM_POST_ADD)
    {
      /* Make sure that there is no insn that assigns to inc_insn.res
	 between the mem_insn and the inc_insn.  */
      rtx other_insn = get_next_ref (REGNO (inc_insn.reg_res),
				     BLOCK_FOR_INSN (mem_insn.insn),
				     reg_next_def);
      if (other_insn != inc_insn.insn)
	{
	  if (dump_file)
	    fprintf (dump_file,
		     "result of add is assigned to between mem and inc insns.\n");
	  return false;
	}

      other_insn = get_next_ref (REGNO (inc_insn.reg_res),
				 BLOCK_FOR_INSN (mem_insn.insn),
				 reg_next_use);
      if (other_insn
	  && (other_insn != inc_insn.insn)
	  && (DF_INSN_LUID (inc_insn.insn) > DF_INSN_LUID (other_insn)))
	{
	  if (dump_file)
	    fprintf (dump_file,
		     "result of add is used between mem and inc insns.\n");
	  return false;
	}

      /* For the post_add to work, the result_reg of the inc must not be
	 used in the mem insn since this will become the new index
	 register.  */
      if (reg_overlap_mentioned_p (inc_insn.reg_res, PATTERN (mem_insn.insn)))
	{
	  if (dump_file)
	    fprintf (dump_file, "base reg replacement failure.\n");
	  return false;
	}
    }

  if (mem_insn.reg1_is_const)
    {
      if (mem_insn.reg1_val == 0)
	{
	  if (!inc_insn.reg1_is_const)
	    {
	      /* The mem looks like *r0 and the rhs of the add has two
		 registers.  */
	      int luid = DF_INSN_LUID (inc_insn.insn);
	      if (inc_insn.form == FORM_POST_ADD)
		{
		  /* The trick is that we are not going to increment r0,
		     we are going to increment the result of the add insn.
		     For this trick to be correct, the result reg of
		     the inc must be a valid addressing reg.  */
		  addr_space_t as = MEM_ADDR_SPACE (*mem_insn.mem_loc);
		  if (GET_MODE (inc_insn.reg_res)
		      != targetm.addr_space.address_mode (as))
		    {
		      if (dump_file)
			fprintf (dump_file, "base reg mode failure.\n");
		      return false;
		    }

		  /* We also need to make sure that the next use of
		     inc result is after the inc.  */
		  other_insn
		    = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_use);
		  if (other_insn && luid > DF_INSN_LUID (other_insn))
		    return false;

		  if (!rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
		    reverse_inc ();
		}

	      other_insn
		= get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
	      if (other_insn && luid > DF_INSN_LUID (other_insn))
		return false;
	    }
	}
      /* Both the inc/add and the mem have a constant.  Need to check
	 that the constants are ok. */
      else if ((mem_insn.reg1_val != inc_insn.reg1_val)
	       && (mem_insn.reg1_val != -inc_insn.reg1_val))
	return false;
    }
  else
    {
      /* The mem insn is of the form *(a + b) where a and b are both
	 regs.  It may be that in order to match the add or inc we
	 need to treat it as if it was *(b + a).  It may also be that
	 the add is of the form a + c where c does not match b and
	 then we just abandon this.  */

      int luid = DF_INSN_LUID (inc_insn.insn);
      rtx other_insn;

      /* Make sure this reg appears only once in this insn.  */
      if (count_occurrences (PATTERN (mem_insn.insn), mem_insn.reg1, 1) != 1)
	return false;

      if (inc_insn.form == FORM_POST_ADD)
	{
	  /* For this trick to be correct, the result reg of the inc
	     must be a valid addressing reg.  */
	  addr_space_t as = MEM_ADDR_SPACE (*mem_insn.mem_loc);
	  if (GET_MODE (inc_insn.reg_res)
	      != targetm.addr_space.address_mode (as))
	    {
	      if (dump_file)
		fprintf (dump_file, "base reg mode failure.\n");
	      return false;
	    }

	  if (rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
	    {
	      if (!rtx_equal_p (mem_insn.reg1, inc_insn.reg1))
		{
		  /* See comment above on find_inc (false) call.  */
		  if (first_try)
		    {
		      reverse_mem ();
		      return find_inc (false);
		    }
		  else
		    return false;
		}

	      /* Need to check that there are no assignments to b
		 before the add insn.  */
	      other_insn
		= get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
	      if (other_insn && luid > DF_INSN_LUID (other_insn))
		return false;
	      /* All ok for the next step.  */
	    }
	  else
	    {
	      /* We know that mem_insn.reg0 must equal inc_insn.reg1
		 or else we would not have found the inc insn.  */
	      reverse_mem ();
	      if (!rtx_equal_p (mem_insn.reg0, inc_insn.reg0))
		{
		  /* See comment above on find_inc (false) call.  */
		  if (first_try)
		    return find_inc (false);
		  else
		    return false;
		}
	      /* To have gotten here know that.
	       *(b + a)

	       ... = (b + a)

	       We also know that the lhs of the inc is not b or a.  We
	       need to make sure that there are no assignments to b
	       between the mem ref and the inc.  */

	      other_insn
		= get_next_ref (REGNO (inc_insn.reg0), bb, reg_next_def);
	      if (other_insn && luid > DF_INSN_LUID (other_insn))
		return false;
	    }

	  /* Need to check that the next use of the add result is later than
	     add insn since this will be the reg incremented.  */
	  other_insn
	    = get_next_ref (REGNO (inc_insn.reg_res), bb, reg_next_use);
	  if (other_insn && luid > DF_INSN_LUID (other_insn))
	    return false;
	}
      else /* FORM_POST_INC.  There is less to check here because we
	      know that operands must line up.  */
	{
	  if (!rtx_equal_p (mem_insn.reg1, inc_insn.reg1))
	    /* See comment above on find_inc (false) call.  */
	    {
	      if (first_try)
		{
		  reverse_mem ();
		  return find_inc (false);
		}
	      else
		return false;
	    }

	  /* To have gotten here know that.
	   *(a + b)

	   ... = (a + b)

	   We also know that the lhs of the inc is not b.  We need to make
	   sure that there are no assignments to b between the mem ref and
	   the inc.  */
	  other_insn
	    = get_next_ref (REGNO (inc_insn.reg1), bb, reg_next_def);
	  if (other_insn && luid > DF_INSN_LUID (other_insn))
	    return false;
	}
    }

  if (inc_insn.form == FORM_POST_INC)
    {
      other_insn
	= get_next_ref (REGNO (inc_insn.reg0), bb, reg_next_use);
      /* When we found inc_insn, we were looking for the
	 next add or inc, not the next insn that used the
	 reg.  Because we are going to increment the reg
	 in this form, we need to make sure that there
	 were no intervening uses of reg.  */
      if (inc_insn.insn != other_insn)
	return false;
    }

  return try_merge ();
}
Example #3
0
/* render a list of agents if available */
void
load_agent_list (WINDOW * main_win, char *addr)
{
   if (!conf.list_agents)
      return;

   char *ptr_value;
   GAgents *agents = NULL;
   gpointer value_ptr = NULL;
   int c, quit = 1, delims = 0;
   int i, n = 0, alloc = 0;
   int y, x, list_h, list_w, menu_w, menu_h;

   getmaxyx (stdscr, y, x);
   list_h = y / 2;              /* list window - height */
   list_w = x - 4;              /* list window - width */
   menu_h = list_h - AGENTS_MENU_Y - 1; /* menu window - height */
   menu_w = list_w - AGENTS_MENU_X - AGENTS_MENU_X;     /* menu window - width */

   value_ptr = g_hash_table_lookup (ht_hosts_agents, addr);
   if (value_ptr != NULL) {
      ptr_value = (char *) value_ptr;
      delims = count_occurrences (ptr_value, '|');

      n = ((strlen (ptr_value) + menu_w - 1) / menu_w) + delims + 1;
      agents = new_gagents (n);
      alloc = split_agent_str (ptr_value, agents, menu_w);
   }

   WINDOW *win = newwin (list_h, list_w, (y - list_h) / 2, (x - list_w) / 2);
   keypad (win, TRUE);
   wborder (win, '|', '|', '-', '-', '+', '+', '+', '+');

   /* create a new instance of GMenu and make it selectable */
   GMenu *menu = new_gmenu (win, menu_h, menu_w, AGENTS_MENU_Y, AGENTS_MENU_X);

   /* add items to GMenu */
   menu->items = (GItem *) xcalloc (alloc, sizeof (GItem));
   for (i = 0; i < alloc; ++i) {
      menu->items[i].name = alloc_string (agents[i].agents);
      menu->items[i].checked = 0;
      menu->size++;
   }
   post_gmenu (menu);

   char buf[256];
   snprintf (buf, sizeof buf, "User Agents for %s", addr);
   draw_header (win, buf, 1, 1, list_w - 2, 1);
   mvwprintw (win, 2, 2, "[UP/DOWN] to scroll - [q] to close window");

   wrefresh (win);
   while (quit) {
      c = wgetch (stdscr);
      switch (c) {
       case KEY_DOWN:
          gmenu_driver (menu, REQ_DOWN);
          draw_header (win, "", 2, 3, CONF_MENU_W, 0);
          break;
       case KEY_UP:
          gmenu_driver (menu, REQ_UP);
          draw_header (win, "", 2, 3, CONF_MENU_W, 0);
          break;
       case KEY_RESIZE:
       case 'q':
          quit = 0;
          break;
      }
      wrefresh (win);
   }
   /* clean stuff up */
   for (i = 0; i < alloc; ++i)
      free (menu->items[i].name);
   free (menu->items);
   free (menu);

   for (i = 0; i < alloc; ++i)
      free (agents[i].agents);
   if (agents)
      free (agents);

   touchwin (main_win);
   close_win (win);
   wrefresh (main_win);
}
Example #4
0
//aux to call stats functions
void statistics(char stat)
{
    if(stopApp==1)main();
    char tmp[1024]= {0x0};
    int fldcnt=0;
    char arr[MAXFLDS][MAXFLDSIZE]= {0x0};
    int recordcnt=0;

    if(stat=='a') strcpy(filename,"");

    if (!strcmp(filename,""))
    {
        printf("Enter key [d] to assume defaults [logcsv.csv],\n");
        printf("or insert the name of the CSV file [file.csv]: ");
        scanf("%s", filename);
    }
    if (!strcmp(filename,"d"))
        strcpy(filename,"logcsv.csv");

    in = fopen(filename,"r");/* open file on command line */

    if(checkFileIn()==-1)
    {
        optionSaveStats='3';
        printf("Error loading file...[%s]\n\n", filename);
        statistics('a');
    }
    if(checkFileIn()==0)
    {
        if(stat=='a')printf("Loading file...[%s]\n\n", filename);
        fgets(tmp,sizeof(tmp),in); /*jump over header*/
        while(fgets(tmp,sizeof(tmp),in)!=0) /* read a record */
        {
            i=0;
            j=0;
            recordcnt++;
            parse(tmp,",",arr,&fldcnt);    /* whack record into fields */
            for(i; i<fldcnt; i++)
            {
                //copy all records to strs
                strcpy(strs[recordcnt-1][i],arr[i]);
            }

        }
        if(stat=='a')printf("Done!!!\n", filename);
        if(stat=='a')PressEnterToReturn();
    }

    if (stat=='b') {
        system("clear");
        print_all_macip(recordcnt, 2,"Source devices:\n"); //list of all source devices - print mac and ip
        PressEnterToReturn();
        fclose(in);
        optionSaveStats='3';
        system("clear");
        statsMenu();
    }
    if (stat=='c') {
        system("clear");
        print_all(recordcnt, 8, "Source ports:\n"); //list of all source ports
        PressEnterToReturn();
        fclose(in);
        optionSaveStats='3';
        system("clear");
        statsMenu();
    }
    if (stat=='d') {
        system("clear");
        print_all_macip(recordcnt, 3,"\nDestination devices:\n"); //list of all destination devices - print mac and ip
        PressEnterToReturn();
        fclose(in);
        optionSaveStats='3';
        system("clear");
        statsMenu();
    }
    if (stat=='e') {
        system("clear");
        print_all(recordcnt, 9, "Destination ports:\n"); //list of all destination ports
        PressEnterToReturn();
        fclose(in);
        optionSaveStats='3';
        system("clear");
        statsMenu();
    }
    if (stat=='f') {
        system("clear");
        rep_count(recordcnt, 12, "0", "\nNº of Queries: "); //count number of queries, verifying the flag response=o
        PressEnterToReturn();
        fclose(in);
        optionSaveStats='3';
        system("clear");
        statsMenu();
    }
    if (stat=='g') {
        system("clear");
        rep_count(recordcnt, 12, "1", "\nNº of Responses: "); //count number of responses, verifying the flag response=1
        PressEnterToReturn();
        fclose(in);
        optionSaveStats='3';
        system("clear");
        statsMenu();
    }
    if (stat=='h') {
        system("clear");
        rep_count(recordcnt, 13, "3", "\nNº of Rcodes: "); //count number of Rcodes errors
        PressEnterToReturn();
        fclose(in);
        optionSaveStats='3';
        system("clear");
        statsMenu();
    }
    if (stat=='i') {
        system("clear");
        check_times(recordcnt, 10); //verify delay between question and answer
        PressEnterToReturn();
        fclose(in);
        optionSaveStats='3';
        system("clear");
        statsMenu();
    }
    if (stat=='j') {
        system("clear");
        print_all_stats(recordcnt, 2, "\nMAC source:"); //stats MAC Source
        PressEnterToReturn();
        fclose(in);
        optionSaveStats='3';
        system("clear");
        statsMenu();
    }
    if (stat=='k') {
        system("clear");
        print_all_stats(recordcnt, 3, "\nMAC destination:");//stats MAC destination
        PressEnterToReturn();
        fclose(in);
        optionSaveStats='3';
        system("clear");
        statsMenu();
    }
    if (stat=='l') {
        system("clear");
        print_all_stats(recordcnt, 18, "\nNames:");//stats Name
        PressEnterToReturn();
        fclose(in);
        optionSaveStats='3';
        system("clear");
        statsMenu();
    }
    if (stat=='m') {
        system("clear");
        string keyword;
        printf("Insert the keyword to search in all queries: ");
        scanf("%s", keyword);
        count_occurrences(recordcnt, 18, "\nThe selected keyword:", keyword);
        PressEnterToReturn();
        fclose(in);
        optionSaveStats='3';
        system("clear");
        statsMenu();
    }
    if (stat=='n') {
        system("clear");
        check_blacklisted(recordcnt, 21, "\nCheck queries responses:\n");//Check queries responses
        PressEnterToReturn();
        fclose(in);
        optionSaveStats='3';
        system("clear");
        statsMenu();
    }

    statsMenu();
}
Example #5
0
static void
print_html_hosts (FILE * fp, GHolder * h, int process)
{
  GAgents *agents;

  char *data, *bandwidth, *usecs, *ag, *ptr_value, *host;
  float percent, l;
  int hits;
  int i, j, max, until = 0, delims = 0, colspan = 6;
  size_t alloc = 0;

#ifdef HAVE_LIBGEOIP
  const char *location = NULL;
  colspan++;
#endif

  if (h->idx == 0)
    return;

  print_html_h2 (fp, HOSTS_HEAD, HOSTS_ID);
  print_p (fp, HOSTS_DESC);
  print_html_begin_table (fp);
  print_html_begin_thead (fp);

  fprintf (fp, "<tr>");
  fprintf (fp, "<th></th>");
  fprintf (fp, "<th>Hits</th>");
  fprintf (fp, "<th>%%</th>");
  fprintf (fp, "<th>Bandwidth</th>");
  if (conf.serve_usecs) {
    colspan++;
    fprintf (fp, "<th>Time&nbsp;served</th>");
  }
  fprintf (fp, "<th>IP</th>");
#ifdef HAVE_LIBGEOIP
  fprintf (fp, "<th>Country</th>");
#endif
  if (conf.enable_html_resolver) {
    colspan++;
    fprintf (fp, "<th>Hostname</th>");
  }

  fprintf (fp, "<th style=\"width:100%%;text-align:right;\">");
  fprintf (fp, "<span class=\"r\" onclick=\"t(this)\">◀</span>");
  fprintf (fp, "</th>");
  fprintf (fp, "</tr>");

  print_html_end_thead (fp);
  print_html_begin_tbody (fp);

  until = h->idx < MAX_CHOICES ? h->idx : MAX_CHOICES;
  max = 0;
  for (i = 0; i < until; i++) {
    if (h->items[i].hits > max)
      max = h->items[i].hits;
  }

  for (i = 0; i < until; i++) {
    hits = h->items[i].hits;
    data = h->items[i].data;
    percent = get_percentage (process, hits);
    percent = percent < 0 ? 0 : percent;
    bandwidth = filesize_str (h->items[i].bw);
    l = get_percentage (max, hits);
    l = l < 1 ? 1 : l;

#ifdef HAVE_LIBTOKYOCABINET
    ag = tc_db_get_str (ht_hosts_agents, data);
#else
    ag = g_hash_table_lookup (ht_hosts_agents, data);
#endif

    print_html_begin_tr (fp, i > OUTPUT_N ? 1 : 0);
    fprintf (fp, "<td>");
    if (ag != NULL)
      fprintf (fp, "<span class=\"s\" onclick=\"a(this)\">▶</span>");
    else
      fprintf (fp, "<span class=\"s\">-</span>");
    fprintf (fp, "</td>");

    fprintf (fp, "<td>%d</td>", hits);
    fprintf (fp, "<td>%4.2f%%</td>", percent);

    fprintf (fp, "<td>");
    clean_output (fp, bandwidth);
    fprintf (fp, "</td>");

    /* usecs */
    if (conf.serve_usecs) {
      usecs = usecs_to_str (h->items[i].usecs);
      fprintf (fp, "<td>");
      clean_output (fp, usecs);
      fprintf (fp, "</td>");
      free (usecs);
    }

    fprintf (fp, "<td>%s</td>", data);

#ifdef HAVE_LIBGEOIP
    location = get_geoip_data (data);
    fprintf (fp, "<td style=\"white-space:nowrap;\">%s</td>", location);
#endif

    if (conf.enable_html_resolver) {
      host = reverse_ip (data);
      fprintf (fp, "<td style=\"white-space:nowrap;\">%s</td>", host);
      free (host);
    }

    fprintf (fp, "<td class=\"graph\">");
    fprintf (fp, "<div class=\"bar\" style=\"width:%f%%\"></div>", l);
    fprintf (fp, "</td>");
    print_html_end_tr (fp);

    /* render agents for each host */
    if (ag != NULL) {
      ptr_value = (char *) ag;

      delims = count_occurrences (ptr_value, '|');
      /* round-up + padding */
      alloc = ((strlen (ptr_value) + 300 - 1) / 300) + delims + 1;
      agents = xmalloc (alloc * sizeof (GAgents));
      memset (agents, 0, alloc * sizeof (GAgents));

      /* split agents into struct */
      split_agent_str (ptr_value, agents, 300);

      fprintf (fp, "<tr class=\"agent-hide\">\n");
      fprintf (fp, "<td colspan=\"%d\">\n", colspan);
      fprintf (fp, "<div>");
      fprintf (fp, "<table class=\"pure-table-striped\">");

      /* output agents from struct */
      for (j = 0; (j < 10) && (agents[j].agents != NULL); j++) {
        print_html_begin_tr (fp, 0);
        fprintf (fp, "<td>");
        clean_output (fp, agents[j].agents);
        fprintf (fp, "</td>");
        print_html_end_tr (fp);
      }

      fprintf (fp, "</table>\n");
      fprintf (fp, "</div>\n");
      fprintf (fp, "</td>\n");
      print_html_end_tr (fp);

      for (j = 0; (agents[j].agents != NULL); j++)
        free (agents[j].agents);
      free (agents);
#ifdef HAVE_LIBTOKYOCABINET
      if (ag)
        free (ag);
#endif
    }

    free (bandwidth);
  }

  print_html_end_tbody (fp);
  print_html_end_table (fp);
}