Beispiel #1
0
int _cp(char *src, char *dest)
{
  int fd, gd, ino, p_ino, dev = running->cwd->dev, temp;
  char src_path[INODE_NAME], dest_path[INODE_NAME], parent_path[INODE_NAME*2];
  MINODE *pip;

  strcpy(src_path, src);
  strcpy(dest_path, dest);
  strcpy(parent_path, dirname(dest));

  if (dest[0] == '/')
  {
    dev = root->dev;
  }
  temp = dev;

  ino = get_inode(dest_path, &temp, TRUE);
  if (ino < 0)                                    // Must first create the destination file
  {
    p_ino = get_inode(parent_path, &dev, FALSE);
    if (p_ino < 0)
    {
      return p_ino;
    }
    pip = iget(dev, p_ino);
    __creat(pip, basename(dest_path), REG_FILE);
    iput(pip);
  }

  fd = __open(src_path, 0); // open the source file for read
  if (fd < 0)
  {
    return fd;
  }
  gd = __open(dest_path, 1); // open the dest file for write
  if (gd < 0)
  {
    return gd;
  }

  int n = 0, counter = 0;
  char cp_buf[BLKSIZE];
  bzero(cp_buf, BLKSIZE);

  while ( (n = __read(fd, cp_buf, BLKSIZE)) > 0 ) // Read from the source file
  {
    if (n < 0)
      break;

    __write(gd, cp_buf, n);                       // Write to the destination file

    bzero(cp_buf, BLKSIZE);
    counter++;
  }

  __close(fd);
  __close(gd);

  return 0;
}
int global_cached_io::handle_pending_requests()
{
	int tot = 0;
	std::vector<thread_safe_page *> dirty_pages;
	while (!pending_requests.is_empty()) {
		page_req_pair reqs[MAX_FETCH_REQS];
		int num = pending_requests.fetch(reqs, MAX_FETCH_REQS);
		for (int i = 0; i < num; i++) {
			// It may be the head of a request list. All requests
			// in the list should point to the same page.
			original_io_request *req = reqs[i].second;
			thread_safe_page *p = reqs[i].first;
			assert(!p->is_old_dirty());
			/**
			 * Right now all pending requests are writes.
			 * All writes are single-buf requests.
			 */
			assert(req->get_next_req_on_page(p) == NULL);
			assert(req->get_io() == this);
			if (req->get_access_method() == WRITE)
				__write(req, p, dirty_pages);
			else
				__read(req, p);
		}
		tot += num;
	}
	// It's not very likely we can get dirty pages here because this is
	// the place where we just finish writing old dirty pages to the disk.
	// The only possible reason is that we happen to overwrite the entire
	// page.
	get_global_cache()->mark_dirty_pages(dirty_pages.data(),
			dirty_pages.size(), underlying);
	return tot;
}
Beispiel #3
0
/*******************************************************************************
* Function Name  : __io_getcharNonBlocking
* Description    : Non blocking read 
* Input          : none
* Output         : dataByte: buffer containing the read byte if any
* Return         : TRUE if there is a data, FALSE otherwise
*******************************************************************************/
boolean __io_getcharNonBlocking(int8u *data)
{
  if (__read(_LLIO_STDIN,data,1))
    return TRUE;
  else
    return FALSE;
}/* end serialReadByte() */
Beispiel #4
0
_WCRTLINK int read( int handle, void *buffer, unsigned len )
{
    unsigned    total = 0;
    unsigned    readamt;
    int         rc;

    __handle_check( handle, -1 );

    while( len > 0 ) {
        if( len > MAXBUFF ) {
            readamt = MAXBUFF;
        } else {
            readamt = len;
        }
        rc = __read( handle, buffer, readamt );
        if( rc == -1 )
            return( rc );
        total += (unsigned)rc;
        if( rc != readamt )
            return( total );

        len -= readamt;
        buffer = ((char *)buffer) + readamt;
    }
    return( total );

}
Beispiel #5
0
static void
_read (struct spu2_dsp *_this, u32 * addr, u32 * data, u32 size)
{
	int i;

	for (i = 0; i < size; i++)
		data[i] = __read (_this, (u32) addr + (u32) (i * 4));
}
Beispiel #6
0
ssize_t read(int fd, void * buf, size_t count)
{
	do_setup();
	if(!isatty(fd))
	{
		io_stats.read_total += count;
		io_stats.read_calls++;
	}
	return __read(fd, buf, count);
}
Beispiel #7
0
bool address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
                      int len, bool is_write)
{
    bool result;

    // TODO: investigate impact of treating reads as producing
    // tainted data, with __coverity_tainted_data_argument__(buf).
    if (is_write) __write(buf, len); else __read(buf, len);

    return result;
}
Beispiel #8
0
ssize_t
__read_chk (int fd, void *buf, size_t nbytes, size_t buflen)
{
  if (nbytes > buflen)
    __chk_fail ();

#ifdef HAVE_INLINED_SYSCALLS
  return INLINE_SYSCALL (read, 3, fd, buf, nbytes);
#else
  return __read (fd, buf, nbytes);
#endif
}
Beispiel #9
0
void __read_one(T &dst, const string &name, xmlpp::Node *element)
{
    debug::mark(name);

    xmlpp::Node::NodeList children = element->get_children(name);
    if (children.empty())
        return;

    xmlpp::Node *child = children.front();
    if (child)
        __read(dst, child);
}
Beispiel #10
0
v8::StringInLanguage &__read(v8::StringInLanguage &dst, xmlpp::Node *element)
{
    v8::String lang;
    v8::String content;

    xmlpp::Node::PrefixNsMap ns;
    ns["xmlns"] = v8::xmlns;

    xmlpp::NodeSet nodes;
    nodes = element->find("xmlns:lang", ns);
    if (!nodes.empty()) {
        dst.setLang(__read(lang, nodes.at(0)));
    }

    nodes = element->find("xmlns:content", ns);
    if (!nodes.empty()) {
        dst.setContent(__read(content, nodes.at(0)));
    }

    return dst;
}
Beispiel #11
0
/* Read data from file descriptor FD, and put the result in the
   buffers described by VECTOR, which is a vector of COUNT 'struct iovec's.
   The buffers are filled in the order specified.
   Operates just like 'read' (see <unistd.h>) except that data are
   put in VECTOR instead of a contiguous buffer.  */
ssize_t
__readv (int fd, const struct iovec *vector, int count)
{
  /* Find the total number of bytes to be read.  */
  size_t bytes = 0;
  for (int i = 0; i < count; ++i)
    {
      /* Check for ssize_t overflow.  */
      if (SSIZE_MAX - bytes < vector[i].iov_len)
	{
	  __set_errno (EINVAL);
	  return -1;
	}
      bytes += vector[i].iov_len;
    }

  /* Allocate a temporary buffer to hold the data.  We should normally
     use alloca since it's faster and does not require synchronization
     with other threads.  But we cannot if the amount of memory
     required is too large.  */
  char *buffer;
  char *malloced_buffer __attribute__ ((__cleanup__ (ifree))) = NULL;
  if (__libc_use_alloca (bytes))
    buffer = (char *) __alloca (bytes);
  else
    {
      malloced_buffer = buffer = (char *) malloc (bytes);
      if (buffer == NULL)
	return -1;
    }

  /* Read the data.  */
  ssize_t bytes_read = __read (fd, buffer, bytes);
  if (bytes_read < 0)
    return -1;

  /* Copy the data from BUFFER into the memory specified by VECTOR.  */
  bytes = bytes_read;
  for (int i = 0; i < count; ++i)
    {
      size_t copy = MIN (vector[i].iov_len, bytes);

      (void) memcpy ((void *) vector[i].iov_base, (void *) buffer, copy);

      buffer += copy;
      bytes -= copy;
      if (bytes == 0)
	break;
    }

  return bytes_read;
}
Beispiel #12
0
// Wav data: 8bit is uint8_t; 16bit is int16
int wav_reader_read_int8(int num_samples, int8_t * data){
    if (!wav_reader_fd) return 1;
    int i;
    int bytes_read = 0;  

    for (i=0; i<num_samples; i++){
        if (bytes_per_sample == 2){
            uint8_t buf[2];
            bytes_read +=__read(wav_reader_fd, &buf, 2);
            data[i] = buf[1];    
        } else {
            uint8_t buf[1];
            bytes_read +=__read(wav_reader_fd, &buf, 1);
            data[i] = buf[0] + 128;
        }
    }
    if (bytes_read == num_samples*bytes_per_sample) {
        return 0;
    } else {
        return 1;
    }
}
int main()
{
	char *buff = __read();
	/* The heap addresses should match */
	printf("\n%s was read at %p\n", buff, buff);

	int nlines = readlines(buffer, MAXLINE);
	int i = 0;
	while(i < nlines) {
		printf("%d : %s\n", i+1, buffer[i]);
		i++;
	}
}
Beispiel #14
0
static hp_timing_t
__get_clockfreq_via_cpuinfo (void)
{
  hp_timing_t result;
  int fd;

  result = 0;

  fd = __open ("/proc/cpuinfo", O_RDONLY);
  if (fd != -1)
    {
      char buf[8192];
      ssize_t n;

      n = __read (fd, buf, sizeof buf);
      if (n > 0)
	{
	  char *mhz = memmem (buf, n, "Cpu0ClkTck", 7);

	  if (mhz != NULL)
	    {
	      char *endp = buf + n;

	      /* Search for the beginning of the string.  */
	      while (mhz < endp
		     && (*mhz < '0' || *mhz > '9')
		     && (*mhz < 'a' || *mhz > 'f')
		     && *mhz != '\n')
		++mhz;

	      while (mhz < endp && *mhz != '\n')
		{
		  if ((*mhz >= '0' && *mhz <= '9') ||
		      (*mhz >= 'a' && *mhz <= 'f'))
		    {
		      result <<= 4;
		      if (*mhz >= '0' && *mhz <= '9')
			result += *mhz - '0';
		      else
			result += (*mhz - 'a') + 10;
		    }
		  ++mhz;
		}
	    }
	}

      __close (fd);
    }

  return result;
}
Beispiel #15
0
//-----------------------------------------------------------------------------
void read_notifications( void ) {
  notification_t note;
  int fd;
  std::string err;

  if(__isset(FD_TIMER_TO_COORDINATOR_READ_CHANNEL, pending_fds)) {
    fd = FD_TIMER_TO_COORDINATOR_READ_CHANNEL;
    if( __read( fd, &note, sizeof(notification_t) ) == -1 ) {
      err = "(coordinator.cpp) read_messages() failed calling __read(FD_TIMER_TO_COORDINATOR_READ_CHANNEL,...)";
      // TODO: write to error log
    } 
  } else if(__isset(FD_PREYCONTROLLER_TO_COORDINATOR_READ_CHANNEL, pending_fds)) {
    fd = FD_PREYCONTROLLER_TO_COORDINATOR_READ_CHANNEL;
    if( __read( fd, &note, sizeof(notification_t) ) == -1 ) {
      err = "(coordinator.cpp) read_messages() failed calling __read(FD_PREYCONTROLLER_TO_COORDINATOR_READ_CHANNEL,...)";
      // TODO: write to error log
    } 
  } else if(__isset(FD_PREDPLANNER_TO_COORDINATOR_READ_CHANNEL, pending_fds)) {
    fd = FD_PREDPLANNER_TO_COORDINATOR_READ_CHANNEL;
    if( __read( fd, &note, sizeof(notification_t) ) == -1 ) {
      err = "(coordinator.cpp) read_messages() failed calling __read(FD_PREDPLANNER_TO_COORDINATOR_READ_CHANNEL,...)";
      // TODO: write to error log
    } 
  } else if(__isset(FD_PREDCONTROLLER_TO_COORDINATOR_READ_CHANNEL, pending_fds)) {
    fd = FD_PREDCONTROLLER_TO_COORDINATOR_READ_CHANNEL;
    if( __read( fd, &note, sizeof(notification_t) ) == -1 ) {
      err = "(coordinator.cpp) read_messages() failed calling __read(FD_PREDCONTROLLER_TO_COORDINATOR_READ_CHANNEL,...)";
      // TODO: write to error log
    } 
  } else if(__isset(FD_WAKEUP_TO_COORDINATOR_READ_CHANNEL, pending_fds)) {
    fd = FD_WAKEUP_TO_COORDINATOR_READ_CHANNEL;
    if( __read( fd, &note, sizeof(notification_t) ) == -1 ) {
      err = "(coordinator.cpp) read_messages() failed calling __read(FD_WAKEUP_TO_COORDINATOR_READ_CHANNEL,...)";
      // TODO: write to error log
    } 
  }
}
Beispiel #16
0
int wav_reader_read_int16(int num_samples, int16_t * data){
    if (!wav_reader_fd) return 1;
    int i;
    int bytes_read = 0;  
    for (i=0; i<num_samples; i++){
        uint8_t buf[2];
        bytes_read +=__read(wav_reader_fd, &buf, 2);
        data[i] = little_endian_read_16(buf, 0);  
    }
    if (bytes_read == num_samples*bytes_per_sample) {
        return 0;
    } else {
        return 1;
    }
}
ssize_t
__readall (int fd, void *buf, size_t len)
{
  size_t n = len;
  ssize_t ret;
  do
    {
      ret = TEMP_FAILURE_RETRY (__read (fd, buf, n));
      if (ret <= 0)
	break;
      buf = (char *) buf + ret;
      n -= ret;
    }
  while (n > 0);
  return ret < 0 ? ret : len - n;
}
Beispiel #18
0
char *_debug_getstr(int *len)
{
    volatile int n = 0;;

    _debug_printf_flush();

    for(n=0;n<DEBUG_INPUT_BUFFER_SIZE;n++)
        debug_read_buf[n] = 0;

    if(ISDEBUGACTIVE())
        __read(0, debug_read_buf, DEBUG_INPUT_BUFFER_SIZE-1);

    if(len)
        *len = small_strlen(debug_read_buf);
    return debug_read_buf;
}
Beispiel #19
0
/*
 * Interface between xdr serializer and tcp connection.
 * Behaves like the system calls, read & write, but keeps some error state
 * around for the rpc level.
 */
static int
readtcp (char *ctptr, char *buf, int len)
{
  struct ct_data *ct = (struct ct_data *)ctptr;
  struct pollfd fd;
  int milliseconds = (ct->ct_wait.tv_sec * 1000) +
    (ct->ct_wait.tv_usec / 1000);

  if (len == 0)
    return 0;

  fd.fd = ct->ct_sock;
  fd.events = POLLIN;
  while (TRUE)
    {
      switch (__poll(&fd, 1, milliseconds))
	{
	case 0:
	  ct->ct_error.re_status = RPC_TIMEDOUT;
	  return -1;

	case -1:
	  if (errno == EINTR)
	    continue;
	  ct->ct_error.re_status = RPC_CANTRECV;
	  ct->ct_error.re_errno = errno;
	  return -1;
	}
      break;
    }
  switch (len = __read (ct->ct_sock, buf, len))
    {

    case 0:
      /* premature eof */
      ct->ct_error.re_errno = ECONNRESET;
      ct->ct_error.re_status = RPC_CANTRECV;
      len = -1;			/* it's really an error */
      break;

    case -1:
      ct->ct_error.re_errno = errno;
      ct->ct_error.re_status = RPC_CANTRECV;
      break;
    }
  return len;
}
Beispiel #20
0
v8::StringInDifferentLanguages &
__read(v8::StringInDifferentLanguages &dst, xmlpp::Node *element)
{
    xmlpp::Node::PrefixNsMap ns;
    ns["xmlns"] = v8::xmlns;

    xmlpp::NodeSet nodes;
    nodes = element->find("xmlns:item", ns);

    dst.clear();

    for (auto node : nodes) {
        v8::StringInLanguage item;
        dst.push_back(__read(item, node));
    }

    return dst;
}
Beispiel #21
0
int wav_reader_open(const char * filepath){
    wav_reader_fd = open(filepath, O_RDONLY); 
    if (!wav_reader_fd) {
        log_error("Can't open file %s", filepath);
        return 1;
    }

    uint8_t buf[40];
    __read(wav_reader_fd, buf, sizeof(buf));

    int num_channels = little_endian_read_16(buf, 22);
    int block_align = little_endian_read_16(buf, 32);
    if (num_channels != 1 && num_channels != 2) {
        log_error("Unexpected num channels %d", num_channels);
        return 1;
    }
    bytes_per_sample = block_align/num_channels;
    if (bytes_per_sample > 2){
        bytes_per_sample = bytes_per_sample/8;
    }
    return 0;
}
/* Create a socket connected to a name. */
int
__nscd_open_socket (const char *key, size_t keylen, request_type type,
		    void *response, size_t responselen)
{
  int saved_errno = errno;

  int sock = open_socket ();
  if (sock >= 0)
    {
      request_header req;
      req.version = NSCD_VERSION;
      req.type = type;
      req.key_len = keylen;

      struct iovec vec[2];
      vec[0].iov_base = &req;
      vec[0].iov_len = sizeof (request_header);
      vec[1].iov_base = (void *) key;
      vec[1].iov_len = keylen;

      ssize_t nbytes = TEMP_FAILURE_RETRY (__writev (sock, vec, 2));
      if (nbytes == (ssize_t) (sizeof (request_header) + keylen)
	  /* Wait for data.  */
	  && wait_on_socket (sock) > 0)
	{
	  nbytes = TEMP_FAILURE_RETRY (__read (sock, response, responselen));
	  if (nbytes == (ssize_t) responselen)
	    return sock;
	}

      close_not_cancel_no_status (sock);
    }

  __set_errno (saved_errno);

  return -1;
}
Beispiel #23
0
char *_debug_getstr(int *len)
{
    volatile int n = 0;;

#if CONFIG_DRIVER_PRINTF_REDLIBV2!=1
    _debug_printf_flush();
#endif

    for(n=0;n<DEBUG_INPUT_BUFFER_SIZE;n++)
        debug_read_buf[n] = 0;

    if(ISDEBUGACTIVE())
    {
#if CONFIG_DRIVER_PRINTF_REDLIBV2!=1
    	__read(0, debug_read_buf, DEBUG_INPUT_BUFFER_SIZE-1);
#else
    	__sys_read(0, debug_read_buf, DEBUG_INPUT_BUFFER_SIZE-1);
#endif
    }

    if(len)
        *len = small_strlen(debug_read_buf);
    return debug_read_buf;
}
Beispiel #24
0
/* Test whether the machine has more than one processor.  This is not the
   best test but good enough.  More complicated tests would require `malloc'
   which is not available at that time.  */
static int
is_smp_system (void)
{
  static const int sysctl_args[] = { CTL_KERN, KERN_VERSION };
  char buf[512];
  size_t reslen = sizeof (buf);

  /* Try reading the number using `sysctl' first.  */
  if (__sysctl ((int *) sysctl_args,
		sizeof (sysctl_args) / sizeof (sysctl_args[0]),
		buf, &reslen, NULL, 0) < 0)
    {
      /* This was not successful.  Now try reading the /proc filesystem.  */
      int fd = __open ("/proc/sys/kernel/version", O_RDONLY);
      if (__builtin_expect (fd, 0) == -1
	  || (reslen = __read (fd, buf, sizeof (buf))) <= 0)
	/* This also didn't work.  We give up and say it's a UP machine.  */
	buf[0] = '\0';

      __close (fd);
    }

  return strstr (buf, "SMP") != NULL;
}
/*
 * reads data from the tcp connection.
 * any error is fatal and the connection is closed.
 * (And a read of zero bytes is a half closed stream => error.)
 */
static int
readtcp (char *xprtptr, char *buf, int len)
{
  SVCXPRT *xprt = (SVCXPRT *)xprtptr;
  int sock = xprt->xp_sock;
  int milliseconds = 35 * 1000;
  struct pollfd pollfd;

  do
    {
      pollfd.fd = sock;
      pollfd.events = POLLIN;
      switch (__poll (&pollfd, 1, milliseconds))
	{
	case -1:
	  if (errno == EINTR)
	    continue;
	  /*FALLTHROUGH*/
	case 0:
	  goto fatal_err;
	default:
          if ((pollfd.revents & POLLERR) || (pollfd.revents & POLLHUP)
              || (pollfd.revents & POLLNVAL))
            goto fatal_err;
	  break;
	}
    }
  while ((pollfd.revents & POLLIN) == 0);

  if ((len = __read (sock, buf, len)) > 0)
    return len;

 fatal_err:
  ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
  return -1;
}
Beispiel #26
0
char getc() {
	char c;
	void *p = &c;
	__read(STDIN, p, 1);
	return *(char*) p;
}
Beispiel #27
0
static inline uint8_t _nokia1100_char_designer(const col_t i, const row_t j, const uint8_t k)
{
	register uint8_t send, tmp;
	
	/* design the char */
	send = font_simple_get(nokia1100_buffer[i][j].data, k);
	
	if(vt100_rowprop[i].double_height)
	{
		if(vt100_rowprop[i].double_height_pos)
		{
			tmp = send;
			send = 0x00;
			if(__read(tmp,BIT3))
			{
				__high(send, BIT7|BIT6);
			}
			
			if(__read(tmp,BIT2))
			{
				__high(send, BIT5|BIT4);
			}
			
			if(__read(tmp,BIT1))
			{
				__high(send, BIT3|BIT2);
			}
			
			if(__read(tmp,BIT0))
			{
				__high(send, BIT1|BIT0);
			}
		}
		else
		{
			tmp = send;
			send = 0x00;
			
			if(__read(tmp,BIT4))
			{
				__high(send, BIT1|BIT0);
			}
			
			if(__read(tmp,BIT5))
			{
				__high(send, BIT3|BIT2);
			}
			
			if(__read(tmp,BIT6))
			{
				__high(send, BIT5|BIT4);
			}
			
			if(__read(tmp,BIT7))
			{
				__high(send, BIT7|BIT6);
			}
		}
	}
	
	if(nokia1100_buffer[i][j].prop.underline)
	{
		send |= 0x80;
	}
	
	if(nokia1100_buffer[i][j].prop.invert)
	{
		send ^= 0xFF;
	}
	
	if(j == vt100_cursor.col
		&& setting.bits.CURSOR_STATE 
		&& i == vt100_cursor.row)
	{
		send ^= parm_setting.bits.CURSOR ? 0xFF : 0x80;
	}
	
	return send;
}
Beispiel #28
0
int Signer::LoadKeys()
{
    bool bKeysReaded = false, bNotOldFmt = false;
    int nReaden;
    int errLoadKey;
    int fh = -1;
    int st_size = 0;
    const int nMaxBufLen = 164;
    char *pBufRead = new char[nMaxBufLen];   // Here Keys must be
    m_siErrorCode = 0;
    KeyFromCL = FALSE;

    if( (!isIgnoreKeyFile) && (Key64Flag == FALSE) ) {
#ifdef O_BINARY
        fh = __open( m_szKeyFileName, O_RDONLY | O_BINARY);
#else
        fh = __open( m_szKeyFileName, O_RDONLY);
#endif

        if( fh == -1 )
        {
            m_siErrorCode = 2;//errno;
            return false;
        }

        st_size = lseek(fh, 0, SEEK_END);
        lseek(fh, 0, SEEK_SET);
        if (st_size == lMinKeyFileSize)
        {
            // load 164 bytes from "small" keys file
            nReaden = __read( fh, pBufRead, nMaxBufLen );
            bKeysReaded = (nReaden == lMinKeyFileSize);
        }
        __close( fh );
    }
    else {
        bKeysReaded = true;
        nReaden = lMinKeyFileSize;
        memcpy( pBufRead, szKeyData, lMinKeyFileSize);
    }

    //*************************************************************************

    if(bKeysReaded)
    {
        SecureKeyByIDPWHalf(pBufRead, lMinKeyFileSize);
        WORD old_SignFlag;
        old_SignFlag = ((KeyFileFormat *)pBufRead)->wSignFlag;
        ((KeyFileFormat *)pBufRead)->wSignFlag = 0;
        errLoadKey = keys.LoadFromBuffer( pBufRead, lMinKeyFileSize );
        if(errLoadKey)
        {
            // Restore for correct Loading (CRC) !
            ((KeyFileFormat *)pBufRead)->wSignFlag = old_SignFlag;
            SecureKeyByIDPWHalf(pBufRead, lMinKeyFileSize); // restore buffer

            SecureKeyByIDPW(pBufRead, lMinKeyFileSize);

            ((KeyFileFormat *)pBufRead)->wSignFlag = 0;
            errLoadKey = keys.LoadFromBuffer( pBufRead, lMinKeyFileSize );
        }

        delete[] pBufRead;
        if( !errLoadKey )
            bKeysReaded = true;
        else
        {
            Keys flushKey;
            keys = flushKey;
            m_siErrorCode = -3;
        }
    }

    return bKeysReaded;
}
Beispiel #29
0
hp_timing_t
__get_clockfreq (void)
{
  /* We read the information from the /proc filesystem.  It contains at
     least one line like
	itc MHz    : 733.390988
     We search for this line and convert the number in an integer.  */
  static hp_timing_t result;
  int fd;

  /* If this function was called before, we know the result.  */
  if (result != 0)
    return result;

  fd = __open ("/proc/cpuinfo", O_RDONLY);
  if (__builtin_expect (fd != -1, 1))
    {
      /* XXX AFAIK the /proc filesystem can generate "files" only up
         to a size of 4096 bytes.  */
      char buf[4096];
      ssize_t n;

      n = __read (fd, buf, sizeof buf);
      if (__builtin_expect (n, 1) > 0)
	{
	  char *mhz = memmem (buf, n, "itc MHz", 7);

	  if (__builtin_expect (mhz != NULL, 1))
	    {
	      char *endp = buf + n;
	      int seen_decpoint = 0;
	      int ndigits = 0;

	      /* Search for the beginning of the string.  */
	      while (mhz < endp && (*mhz < '0' || *mhz > '9') && *mhz != '\n')
		++mhz;

	      while (mhz < endp && *mhz != '\n')
		{
		  if (*mhz >= '0' && *mhz <= '9')
		    {
		      result *= 10;
		      result += *mhz - '0';
		      if (seen_decpoint)
			++ndigits;
		    }
		  else if (*mhz == '.')
		    seen_decpoint = 1;

		  ++mhz;
		}

	      /* Compensate for missing digits at the end.  */
	      while (ndigits++ < 6)
		result *= 10;
	    }
	}

      __close (fd);
    }

  return result;
}
Beispiel #30
0
int fgetc(FILE *f) {
	int c;
	int nbchar;
  nbchar = __read(_LLIO_STDIN, (unsigned char *)&c, 1);
	return ((nbchar==0)?-1:c);
}