Ejemplo n.º 1
0
static int	ft_out_prec(char type, t_flags fl, int len)
{
	int	w;

	w = 0;
	if (fl.prec)
	{
		while (len + w < fl.prec && is_pnumeric(type) && type != 'f')
		{
			ft_putchar('0');
			w++;
		}
		return (w);
	}
	return (0);
}
Ejemplo n.º 2
0
int			ft_out(char type, char *out, t_flags fl)
{
	int		len;
	int		w;

	len = ft_strlen(out);
	w = 0;
	w += ft_prefix(type, out, fl);
	w += ft_out_prec(type, fl, len);
	if (fl.prec > len && is_pnumeric(type))
		len = fl.prec;
	if (type == 's' && fl.prec)
		len = fl.prec;
	else if (type == 's' && fl.prec == 0 && fl.ip == 0)
		fl.prec = len;
	if (fl.padding)
		w += ft_padstr(type, out, fl, len);
	else
		w += ft_out_sprec(type, out, fl);
	return (w);
}
Ejemplo n.º 3
0
/* ========================================================================= */
struct pitem *procstat_enablepitem(struct qparts *qp)
{
   char hdr_temp[MAX_QPART_LEN]; /* Temp holder of the header */
   char *hdr_ptr;                /* Holds the location of the header info we need */
   int cpu_cnt;
   int requested_cpu;

   void *data_ptr;
   struct cpu_data_bin *dstruct;
   struct pitem *this_pitem;
   struct pitem *base_pitem; /* Used to return a list */
   struct pitem *last_pitem; /* Used to build the returned list */
   int dtype;

   /* Make sure we were called with the right quad (provider) name */
   if ( 0 != strcmp(qp->pname, "proc.stat") )
      return(NULL);

   /* Discussion:
      I am going to go ahead and set up the provider to collect data.
      Therotically this should be done after we establish that the input 
      from the user is good. (Such as - they did not ask for data on a CPU
      that does not exist.) But, since I know that the code will exit on
      any failure, then I will just do the work and bail if it comes to 
      that. Furthermore... I need to know how many CPUs there are in order
      to properly judge the input anyways.
   */

   /* Flag update for the provider */
   this_provider->update_required = 1;

   /* Setup the read buffer - if not already done. */
   if ( NULL == this_buf )
   {
      if ( NULL == (this_buf = InitReadBuff("/proc/stat", 25)) )
      {
         error_msg("ERROR: Unable to setup /proc/stat buffer.");
         return(NULL);
      }
   }

   if ( NULL == this_data )
   {
      if ( 1 > (cpu_cnt = cpus_in_buff(this_buf)) )
      {
         error_msg("ERROR: Unable to enumerate CPUs.");
         return(NULL);
      }

      if ( NULL == (this_data = new_stat_struct(cpu_cnt)) )
      {
         error_msg("ERROR: Unable to allocate memory for CPU stats.");
         return(NULL);
      }


      if(update_stat_data(this_buf, this_data))
      {
         error_msg("ERROR: Unable to retrieve data from /proc/stat.");
         return(NULL);
      }

      /* What is going on here?!
         bzero() the data. Do this so that the first iteration will not
         be all 0s. NULL the data so that we are looking at all 0s
         for the first set of data - and the resulting data will be the
         CPU times since boot. bzero() also insures that the values are
         all 0, and not random data that may make for some interesting
         values.
      */
      bzero(this_data->thistotal, sizeof(struct cpudata));
      bzero(this_data->lasttotal, sizeof(struct cpudata));

      bzero(this_data->thiscpuX, cpu_cnt * sizeof(struct cpudata));
      bzero(this_data->lastcpuX, cpu_cnt * sizeof(struct cpudata));
      
      /* NOTE - Additional items can go here (later) */
   }

   /* Now start parsing the quad. */

   /* ============================== */
   /* Total CPU stat */
   if ( qp->iname == StrStr(qp->iname, "cputotal.") )
   {
      /* This is a total CPU stat */

      if ( qp->pargs[0] != 0 )
      {
         /* A provider arg was used - with an item that does not support it! */
         error_msg("ERROR: The proc.stat cputotal option does not support a CPU argument.");
         return(NULL);
      }

      /* Break down into raw and pct - then parse individual items */

      if ( is_raw_stat(qp->iname) )
      {
         this_pitem = NULL;

         CMPEN_CPU_RAW(user);
         CMPEN_CPU_RAW(nice);
         CMPEN_CPU_RAW(sys);
         CMPEN_CPU_RAW(idle);
         CMPEN_CPU_RAW(iowait);
         CMPEN_CPU_RAW(irq);
         CMPEN_CPU_RAW(sirq);
         CMPEN_CPU_RAW(steal);
         CMPEN_CPU_RAW(guest);
         CMPEN_CPU_RAW(gnice);
         
         if ( this_pitem )
         {
            if ( NULL == (dstruct = malloc(sizeof(struct cpu_data_bin))) )
               return(NULL);
            dstruct->dtype = DTYPE_RAW;
            dstruct->thisd = this_data->thistotal;
            dstruct->lastd = this_data->lasttotal;
            this_pitem->dstruct = (void *)dstruct;

            this_pitem->next_ui = this_provider->ui_list;
            this_provider->ui_list = this_pitem;

            return(this_pitem);
         }

         /* This is a fall-through to return(NULL) */
      }

      if ( is_pct_stat(qp->iname) )
      {
         if ( ( ShouldDiff(qp) ) || ( ShouldPSAvg(qp) ) )
         {
            error_msg("ERROR: diff and/or psavg is not supported with an explicit pct.");
            return(NULL);
         }

         CMPEN_CPU_PCT(user);
         CMPEN_CPU_PCT(nice);
         CMPEN_CPU_PCT(sys);
         CMPEN_CPU_PCT(idle);
         CMPEN_CPU_PCT(iowait);
         CMPEN_CPU_PCT(irq);
         CMPEN_CPU_PCT(sirq);
         CMPEN_CPU_PCT(steal);
         CMPEN_CPU_PCT(guest);
         CMPEN_CPU_PCT(gnice);

         if ( this_pitem )
         {
            if ( NULL == (dstruct = malloc(sizeof(struct cpu_data_bin))) )
               return(NULL);

            dstruct->dtype = DTYPE_PCT;
            dstruct->thisd = this_data->thistotal;
            dstruct->lastd = this_data->lasttotal;

            this_pitem->dstruct = (void *)dstruct;

            this_pitem->next_ui = this_provider->ui_list;
            this_provider->ui_list = this_pitem;

            return(this_pitem);
         }

         /* This is a fall-through to return(NULL) */
      }

      /* If we are here, then the input was not parsed ---> ERROR CONDITION */
   }


   /* ============================== */
   /* Per-CPU (either specific or all) */
   if ( qp->iname == StrStr(qp->iname, "cpu.") )
   {
      /* This is a per-CPU stat */

      if ( qp->pargs[0] == 0 )
      {
         /* A provider arg was not used - with an item that requires it! */
         error_msg("ERROR: The per-cpu option requires a CPU argument.");
         return(NULL);
      }

      /* ============================== */
      /* For all CPUs (wildcard) */
      if (( qp->pargs[0] == '*' ) && ( qp->pargs[1] == 0 ))
      {
         dtype = DTYPE_PCT;

         if(is_raw_stat(qp->iname))
            dtype = DTYPE_RAW;

         requested_cpu = 0;
         base_pitem = NULL;
         last_pitem = NULL; /* About a compiler warning */
         while ( requested_cpu < this_data->cpucnt )
         {
            this_pitem = NULL;

            if ( DTYPE_RAW == dtype )
            {
               CMPEN_CPU_RAW(user);
               CMPEN_CPU_RAW(nice);
               CMPEN_CPU_RAW(sys);
               CMPEN_CPU_RAW(idle);
               CMPEN_CPU_RAW(iowait);
               CMPEN_CPU_RAW(irq);
               CMPEN_CPU_RAW(sirq);
               CMPEN_CPU_RAW(steal);
               CMPEN_CPU_RAW(guest);
               CMPEN_CPU_RAW(gnice);
            }
            else
            {
               CMPEN_CPU_PCT(user);
               CMPEN_CPU_PCT(nice);
               CMPEN_CPU_PCT(sys);
               CMPEN_CPU_PCT(idle);
               CMPEN_CPU_PCT(iowait);
               CMPEN_CPU_PCT(irq);
               CMPEN_CPU_PCT(sirq);
               CMPEN_CPU_PCT(steal);
               CMPEN_CPU_PCT(guest);
               CMPEN_CPU_PCT(gnice);
            }

            
            if ( NULL == this_pitem )
               return(NULL);

            if ( NULL == (dstruct = malloc(sizeof(struct cpu_data_bin))) )
                  return(NULL);

            dstruct->dtype = dtype;

            dstruct->thisd = this_data->thiscpuX + requested_cpu;
            dstruct->lastd = this_data->lastcpuX + requested_cpu;

            this_pitem->dstruct = (void *)dstruct;

            /* Linked list magic for the output item */
            if ( NULL == base_pitem )
            {
               base_pitem = this_pitem;
            }
            else
            {
               /* last_pitem is set at the bottom of the loop. It is not set when base_pitem is NULL */
               last_pitem->next_opi = this_pitem;
            }

            /* Set the header - if none was requested */
            if ( NULL == qp->cfpn->header )
            {
               strcpy(hdr_temp, this_pitem->header); /* Copy the existing header */
               hdr_ptr = hdr_temp + 3; /* We *know* that the string begins with "cpu." */
               sprintf(this_pitem->header, "%d%s", requested_cpu, hdr_ptr);
            }

            /* Put this in the update item list */
            this_pitem->next_ui = this_provider->ui_list;
            this_provider->ui_list = this_pitem;

            last_pitem = this_pitem;

            requested_cpu++;
         }

         /* Return the top of the list */
         return(base_pitem);
      }

      /* ============================== */
      /* Per-CPU for a single CPU */
      if ( is_pnumeric(qp->pargs) )
      {
         requested_cpu = atoi(qp->pargs);

         if ( requested_cpu >= this_data->cpucnt ) 
         {
            error_msg("ERROR: The requested proc.stat CPU (%d) is not on this system.", requested_cpu);
            return(NULL);
         }

         this_pitem = NULL;

         if (is_raw_stat(qp->iname))
         {
            dtype = DTYPE_RAW;

            CMPEN_CPU_RAW(user);
            CMPEN_CPU_RAW(nice);
            CMPEN_CPU_RAW(sys);
            CMPEN_CPU_RAW(idle);
            CMPEN_CPU_RAW(iowait);
            CMPEN_CPU_RAW(irq);
            CMPEN_CPU_RAW(sirq);
            CMPEN_CPU_RAW(steal);
            CMPEN_CPU_RAW(guest);
            CMPEN_CPU_RAW(gnice);
         }
         else
         {
            dtype = DTYPE_PCT;

            CMPEN_CPU_PCT(user);
            CMPEN_CPU_PCT(nice);
            CMPEN_CPU_PCT(sys);
            CMPEN_CPU_PCT(idle);
            CMPEN_CPU_PCT(iowait);
            CMPEN_CPU_PCT(irq);
            CMPEN_CPU_PCT(sirq);
            CMPEN_CPU_PCT(steal);
            CMPEN_CPU_PCT(guest);
            CMPEN_CPU_PCT(gnice);
         }
         
         if ( this_pitem )
         {
            if ( NULL == (dstruct = malloc(sizeof(struct cpu_data_bin))) )
               return(NULL);
            dstruct->dtype = dtype;

            dstruct->thisd = this_data->thiscpuX + requested_cpu;
            dstruct->lastd = this_data->lastcpuX + requested_cpu;

            this_pitem->dstruct = (void *)dstruct;

            this_pitem->next_ui = this_provider->ui_list;
            this_provider->ui_list = this_pitem;

            return(this_pitem);
         }
      }

      /* If we are here, then the input was not parsed (or error) ---> ERROR CONDITION */
   }


   return(NULL);
}