void Print::println(const char* text) {
	int i;
	for(i = 0; text[i] != 0; i++) {
		write(text[i]);
	}
	write('\n');
}
Example #2
0
bool ICACHE_FLASH_ATTR pca9685_setPWM(uint8_t num, uint16_t val)
{
	uint8_t buf[4];

	if(num > 15)
		return false;

	if(num & 1)
	{
		buf[0] = 0;
		buf[1] = 0;
		buf[2] = val;
		buf[3] = val>>8;
		if(chOff & (1<<num))
		{
			bool ret = write(PCA9685_LED0_ON_L + 4*num, buf, 4);
			if(ret == true)
				chOff &= ~(1<<num);
			return ret;
		}
		else
		{
			return write(PCA9685_LED0_ON_L + 4*num + 2, &buf[2], 2);
		}
	}
Example #3
0
int main(int argc, char **argv)
{
    int id = geteuid();
    if (id != 0)
    {
        printf("You must run this program as root\n");
        return -1;
    }
    if (argc != 2)
    {
        printf("You must specify the rpm for the fan, value must be between 2000 and 6200\n");
        return -1;
    }
    int parameter = atoi(argv[1]);
    
    if (parameter >= 2000 && parameter <= 6200)
    {
        int file;
        file = open("/sys/devices/platform/applesmc.768/fan1_manual", O_WRONLY | O_CREAT);
        write(file, "1", (ssize_t)1);
        close(file);
        
        file = open("/sys/devices/platform/applesmc.768/fan1_output", O_WRONLY | O_CREAT);
        write(file, argv[1], (ssize_t)4);
        close(file);
        printf("Fan RPM changed to %s\n", argv[1]);
    }
    else
    {
        printf("Value must be between 2000 and 6200\n");
    }
    return 0;
}
static void
sgi_write_table(void)
{
	sgilabel->csum = 0;
	sgilabel->csum = SGI_SSWAP32(two_s_complement_32bit_sum(
			(unsigned int*)sgilabel, sizeof(*sgilabel)));
	assert(two_s_complement_32bit_sum(
		(unsigned int*)sgilabel, sizeof(*sgilabel)) == 0);

	if (lseek(fd, 0, SEEK_SET) < 0)
		fdisk_fatal(unable_to_seek);
	if (write(fd, sgilabel, SECTOR_SIZE) != SECTOR_SIZE)
		fdisk_fatal(unable_to_write);
	if (!strncmp((char*)sgilabel->directory[0].vol_file_name, "sgilabel", 8)) {
		/*
		 * keep this habit of first writing the "sgilabel".
		 * I never tested whether it works without (AN 981002).
		 */
		sgiinfo *info = fill_sgiinfo();
		int infostartblock = SGI_SSWAP32(sgilabel->directory[0].vol_file_start);
		if (lseek(fd, infostartblock*SECTOR_SIZE, SEEK_SET) < 0)
			fdisk_fatal(unable_to_seek);
		if (write(fd, info, SECTOR_SIZE) != SECTOR_SIZE)
			fdisk_fatal(unable_to_write);
		free(info);
	}
}
Example #5
0
/*
 * unbuffered output: writes values of matrix to file
 */
void writeMatrix(float **m, int dim1, int dim2, int fd) {
   int length, size, ndx1, ndx2, max_dbl_chars = 4 + DBL_MANT_DIG - DBL_MIN_EXP;
   char str_bf[DFLT_STR_SIZE + 1],
    *num_bf = (char*)alloca(sizeof(char) * max_dbl_chars);

   str_bf[0] = num_bf[0] = '\0';
   size = 0;

   for (ndx1 = 0; ndx1 < dim1; ++ndx1) {
      for (ndx2 = 0; ndx2 < dim2; ++ndx2) {
         length = snprintf(num_bf, max_dbl_chars, "%.2lf%c", m[ndx1][ndx2],
          (ndx2 < dim2 - 1 ? ' ' : '\n'));

         strncpy((char*)(str_bf + size), num_bf, DFLT_STR_SIZE - size + 1);
         if (size + length > DFLT_STR_SIZE) {
            write(fd, str_bf, size);
            strncpy(str_bf, (num_bf + (DFLT_STR_SIZE - size)),
             (size = (length - (DFLT_STR_SIZE - size))) + 1);
         }
         else size += length;
      }
   }

   write(fd, str_bf, size);
}
Example #6
0
static int
main(string arg)
{
    object ply, wpn, usr;
    if (!objectp(ply = this_player()) ||
        !objectp(wpn = ART_C_WEAPON->get_instance(ply)) ||
        wpn->is_suspended())
    {
        write("You have no physical form.\n");
        return 1;
    }
    if (objectp(usr = wpn->query_user()) &&
        objectp(usr->query_attack()))
    {
        return notify_fail("You can not slip while fighting.");
    }
    if (!objectp(usr = wpn->query_owner())) {
        return notify_fail("Noone is carrying you.");
    }
    write("You attempt to slip from " + usr->query_name() + "'s hands.\n");
    if (!usr->drop_one_item(wpn)) {
        return notify_fail("You fail.");
    }
    return 1;
}
Example #7
0
void CUtils::common_pipe_write(int fd, const char* buffer, int32_t buffer_size)
{
	int ret = 0;
	int32_t size = buffer_size;

	// 第一个while循环写入大小
	while (true)
	{
		ret = write(fd, &size, sizeof(size));
		if ((-1 == ret) && (EINTR == errno))
			continue;
		if (-1 == ret)
		    THROW_SYSCALL_EXCEPTION(NULL, errno, "write");

		break;
	}

	const char* bufferp = buffer;

	// 第二个while循环根据大小写入内容
	while (size > 0)
	{
		ret = write(fd, bufferp, size);
		if ((-1 == ret) && (EINTR == errno))
			continue;
		if (-1 == ret)
		    THROW_SYSCALL_EXCEPTION(NULL, errno, "write");

		size -= ret;
		bufferp += ret;
	}
}
Example #8
0
static int write_bytes(const char *path, unsigned char *buf, int len)
{
    int fd = -1;
    int ret = -1;
    int size;
    unsigned char temp[1];
    
    fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
    if (fd == -1) {
        goto end;
    }
    
    temp[0] = (char)len;
    size = write(fd, temp, 1);
    if (size <= 0)
        goto end;
    
    size = write(fd, buf, len);
    if (size != len)
        goto end;
    
    ret = size;
    
end:
    if (fd != -1)
        close(fd);
    
    return ret;
}
Example #9
0
void	ft_putstr(char const *s)
{
	if (!s)
		write(1, "(null)", 6);
	else
		write(1, s, ft_strlen(s));
}
Example #10
0
bool 
NRF24L01P::begin(const void* config)
{
  // Setup hardware features, channel, bitrate, retransmission, dynamic payload
  write(FEATURE, (_BV(EN_DPL) | _BV(EN_ACK_PAY) | _BV(EN_DYN_ACK)));
  write(RF_CH, m_channel);
  write(RF_SETUP, (RF_DR_2MBPS | RF_PWR_0DBM));
  write(SETUP_RETR, ((2 << ARD) | (15 << ARC)));
  write(DYNPD, DPL_PA);
  write(SETUP_AW, AW_3BYTES);

  // Setup hardware receive pipes (0:ack, 1:device, 2:broadcast)
  addr_t rx_addr = m_addr;
  write(RX_ADDR_P1, &rx_addr, sizeof(rx_addr));
  write(RX_ADDR_P2, 0);
  write(EN_RXADDR, (_BV(ERX_P2) | _BV(ERX_P1) | _BV(ERX_P0)));
  
  // Auto-acknowledgement on device pipe
  write(EN_AA, (_BV(ENAA_P2) | _BV(ENAA_P1) | _BV(ENAA_P0)));

  // Ready to go
  powerup();
  spi.attach(this);
  m_irq.enable();
  return (true);
}
Example #11
0
int
NRF24L01P::recv(uint8_t& src, uint8_t& port, 
		void* buf, size_t size, 
		uint32_t ms)
{
  // Run in receiver mode
  set_receiver_mode();

  // Check if there is data available on any pipe
  uint32_t start = RTC::millis();
  while (!available()) {
    if ((ms != 0) && (RTC::since(start) > ms)) return (-2);
    Power::sleep(m_mode);
  } 
  m_dest = (m_status.rx_p_no == 1 ? m_addr.device : 0);
  write(STATUS, _BV(RX_DR));
  
  // Check for payload error from device (Tab. 20, pp. 51, R_RX_PL_WID)
  uint8_t count = read(R_RX_PL_WID) - 2;
  if ((count > PAYLOAD_MAX) || (count > size)) {
    write(FLUSH_RX);
    return (-1);
  }

  // Read the source address, port and payload
  spi.begin(this);
  m_status = spi.transfer(R_RX_PAYLOAD);
  src = spi.transfer(0);
  port = spi.transfer(0);
  spi.read(buf, count);
  spi.end();
  return (count);
}
int32_t EffectBassBoost::process(audio_buffer_t* in, audio_buffer_t* out)
{
    for (uint32_t i = 0; i < in->frameCount; i ++) {
        int32_t dryL = read(in, i * 2);
        int32_t dryR = read(in, i * 2 + 1);

        /* Original LVM effect was far more involved than this one.
         * This effect is mostly a placeholder until I port that, or
         * something else. LVM process diagram was as follows:
         *
         * in -> [ HPF ] -+-> [ mono mix ] -> [ BPF ] -> [ compressor ] -> out
         *                `-->------------------------------>--'
         *
         * High-pass filter was optional, and seemed to be
         * tuned at 55 Hz and upwards. BPF is probably always tuned
         * at the same frequency, as this would make sense.
         *
         * Additionally, a compressor element was used to limit the
         * mixing of the boost (only!) to avoid clipping.
         */
        int32_t boost = mBoost.process(dryL + dryR);

        write(out, i * 2, dryL + boost);
        write(out, i * 2 + 1, dryR + boost);
    }

    return mEnable ? 0 : -ENODATA;
    }
Example #13
0
void			kick_clients(t_server *server, int fd, char *str)
{
  int			i;
  char			buff[4096];

  (void) server;
  i = 7;
  bzero(buff, 4096);
  while ((str[i] == ' ' || str[i] == '\t') && str[i])
    i++;
  i += 5;
  while ((str[i] == ' ' || str[i] == '\t') && str[i])
    i++;
  if (!kill(atoi(&str[i]), SIGUSR2))
    write(fd, "Client successfully kicked !",
	  strlen("Client successfully kicked !"));
  else
    {
      sprintf(buff, "Error while kicking : %s", strerror(errno));
      write(fd, buff, strlen(buff));
      return;
    }
  sprintf(buff, "Monit : Client instanciation %s kicked successfully", &str[i]);
  mysql_notif_monit(server, "times", buff);
}
Example #14
0
	void MTcpSocket::sendPacketInternal(const Mara::MPacket *pPacket)
	{
		QByteArray data;
		bool compressed = pPacket->compressed();

		if(compressed)
		{
			QByteArray uncompressed(*(pPacket->serialize()));
			data = qCompress(uncompressed);
			qDebug("Compression Ratio: %d/%d", data.size(), uncompressed.size());
		}
		else
		{
			data = *(pPacket->serialize());
		}

		{
			QMutexLocker lock(&_mutex);

			write(_packetHeaderBytes);
			_socketStream << data.length();
			_socketStream << pPacket->type();

			write(data);

			_socketStream << qChecksum(data.constData(), data.length());
			_socketStream << compressed;
		}

		if(pPacket->deleteOnSend()) delete pPacket;
	}
Example #15
0
sFile *
sopen(const char *path, int flags, mode_t mode, char *key)
{

    char buf[32];
    int res;
    sFile *sf=malloc(sizeof(sFile));
	time_t t;
   	srand((unsigned) time(&t)); //Intializes random number generator
   	
    AES_set_encrypt_key(key,AESKeyLength,&(sf->key));
    AES_set_decrypt_key(key,AESKeyLength,&(sf->dkey));
    sf->ctr=malloc(sizeCTR);

    sf->fd=open(path,O_RDWR|(flags&O_TRUNC),mode);
    
    read(sf->fd,sf->ctr,sizeCTR);
    res=read(sf->fd,(char *)&(sf->rlen),sizeRLEN);
    if (res!=sizeRLEN){ /* new or truncated file */
        sf->rlen=0;
        {int i;
            //for(i=0;i<sizeCTR;i++)sf->ctr[i]=i;
            for(i=0;i<sizeCTR;i++)sf->ctr[i]=rand();
        }
        if(write(sf->fd,sf->ctr,sizeCTR)!=sizeCTR){
            return (sFile *)0;
        }
        if(write(sf->fd,(char *)&(sf->rlen),sizeRLEN)!=sizeRLEN){
            return (sFile *)0;
        }
    }
    sf->offset=sFileHeaderSize;
    lseek(sf->fd,sf->offset,SEEK_SET);
    return sf;
}
Example #16
0
/*
 * windowSwap - swap a windows data
 */
void static windowSwap( wind *w )
{
    int         i, size;
    long        pos;

    pos = (long)w->id * buffSize();
    FileSeek( swapHandle, pos );
    size = w->width * w->height;
    i = write( swapHandle, w->overlap, size );
    if( i != size ) {
        return;
    }
    i = write( swapHandle, w->whooverlapping, size );
    if( i != size ) {
        return;
    }
    i = write( swapHandle, w->text, size * sizeof( char_info ) );
    if( i != sizeof( char_info ) * size ) {
        return;
    }
    MemFree2( &w->text );
    MemFree2( &w->whooverlapping );
    MemFree2( &w->overlap );
    w->isswapped = TRUE;

} /* windowSwap */
Example #17
0
static void				echo_op_n(char **msh_av, char **env)
{
	int		i;
	char	*str;
	int		len_char;

	i = 2;
	len_char = 0;
	while (msh_av[i] != NULL)
	{
		len_char += ft_strlen(msh_av[i]);
		i++;
	}
	len_char += 1;
	if (!(str = (char *)malloc(sizeof(char) * len_char)))
		return ;
	i = 2;
	while (msh_av[i] != NULL)
	{
		str = ft_strjoin_glue(str, msh_av[i], ' ');
		i++;
	}
	str[len_char + i] = '\0';
	str++;
	write(3, str, ft_strlen(str));
	write(3, "\n", 1);
	(void)env;
}
static enum VCLI_status_e
varnish_ask_cli(const struct varnish *v, const char *cmd, char **repl)
{
	int i;
	unsigned retval;
	char *r;

	if (cmd != NULL) {
		vtc_dump(v->vl, 4, "CLI TX", cmd, -1);
		i = write(v->cli_fd, cmd, strlen(cmd));
		assert(i == strlen(cmd));
		i = write(v->cli_fd, "\n", 1);
		assert(i == 1);
	}
	i = VCLI_ReadResult(v->cli_fd, &retval, &r, 20.0);
	if (i != 0) {
		vtc_log(v->vl, 0, "CLI failed (%s) = %d %u %s",
		    cmd, i, retval, r);
		return ((enum VCLI_status_e)retval);
	}
	assert(i == 0);
	vtc_log(v->vl, 3, "CLI RX  %u", retval);
	vtc_dump(v->vl, 4, "CLI RX", r, -1);
	if (repl != NULL)
		*repl = r;
	else
		free(r);
	return ((enum VCLI_status_e)retval);
}
Example #19
0
uint32_t putData(int sessionfd, char* src, uint32_t buffLen)
{
  bitPacket   streamLength;
  uint32_t    byteCounter;
  char        terminationChar;

  streamLength.data = buffLen;

  byteCounter = 0;
  while (byteCounter < 4) {
    byteCounter += write(sessionfd, &(streamLength.chr[byteCounter]), 4-byteCounter);
  }

  byteCounter = 0;
  while (byteCounter < streamLength.data) {
    byteCounter += write(sessionfd, &(src[byteCounter]), streamLength.data-byteCounter);
  }

  #ifdef DEBUG
  printf("Payload Sent\n");
  #endif /* DEBUG */

  terminationChar = '\n';

  byteCounter = 0;
  while (byteCounter < 1) {
    byteCounter += write(sessionfd, &terminationChar, 1-byteCounter);
  }

  return streamLength.data;

}
int main(int argc, char* argv[]) {
    char buffer[BUFSIZE]; // buffer for reading and writing
    if (argc != 3) { // this is the only valid structure for arguments
        printf("usage: %s source dest\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    // infile is only for reading
    int inFile = open(argv[1], O_RDONLY);
    // outfile is for writing - or overwriting if it already exists
    int outFile = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR, S_IWUSR);

    // number of bytes read, potentially negative (for error condition)
    ssize_t numRead;
    // how many nulls we have in a row - always positive (unsigned)
    size_t holeSize = 0;

    // read in up to BUFSIZE
    while ((numRead = read(inFile, buffer, BUFSIZE)) > 0) {
        // for each byte of buffer
        for (int i = 0; i < numRead; i++) {
            // if the byte is a null, increment our counter of nulls
            if (buffer[i] == '\0') {
                holeSize += 1;
            } else if (holeSize > 0) {
                // if the byte is not a null, and is the first byte after some nulls, we should seek
                // the number of null bytes past. so if we have 0 0 0 0 y, we would have a null counter
                // of four, and then would seek right past them to y
                lseek(outFile, holeSize, SEEK_CUR);
                // now write just one byte - just our new non-null byte to the first location for y in
                // the new file
                write(outFile, &buffer[i], 1);
                // we've seeked past the nulls, so clear our counter
                holeSize = 0;
            } else {
                // in this case, we are a non-null after a non-null (or the first byte of the file
                // which happens to be non-null). so we just write one byte and keep moving
                write(outFile, &buffer[i], 1);
            }
        }
    }

    // error reading (e.g. perms)
    if (numRead == -1) {
        perror("reading");
        exit(EXIT_FAILURE);
    }

    // error closing (but who cares, we're exiting anyway)
    if (close(inFile) == -1) {
        perror("close infile");
        exit(EXIT_FAILURE);
    }

    if (close(outFile) == -1) {
        perror("close outfile");
        exit(EXIT_FAILURE);
    }

    return 0;
}
static off_t
cat_fd(unsigned char * prepend, size_t count, off_t *gsizep, int fd)
{
	char buf[BUFLEN];
	off_t in_tot;
	ssize_t w;

	in_tot = count;
	w = write(STDOUT_FILENO, prepend, count);
	if (w == -1 || (size_t)w != count) {
		maybe_warn("write to stdout");
		return -1;
	}
	for (;;) {
		ssize_t rv;

		rv = read(fd, buf, sizeof buf);
		if (rv == 0)
			break;
		if (rv < 0) {
			maybe_warn("read from fd %d", fd);
			break;
		}

		if (write(STDOUT_FILENO, buf, rv) != rv) {
			maybe_warn("write to stdout");
			break;
		}
		in_tot += rv;
	}

	if (gsizep)
		*gsizep = in_tot;
	return (in_tot);
}
Example #22
0
int main(int argc, char **argv)
{
	struct sockaddr_in serv_addr;
	int cfd; int n;
	char buf[BUFSIZ];

	cfd = socket(AF_INET, SOCK_STREAM, 0);
	if (cfd == -1)
		error_sys("socket");

	memset(&serv_addr, 0, sizeof(serv_addr)); //用sizeof可使更通用
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(SERV_PORT);
	inet_pton(AF_INET, SERV_ADDR, &serv_addr.sin_addr.s_addr);

	n = connect(cfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
	if (n == -1)
		error_sys("connect");

	while (1) {
		n = read(STDIN_FILENO, buf, BUFSIZ);
		if (n == 0) //手动产生文件结束符 ctrl + d
			break;
		n = write(cfd, buf, n);//将数据放到发送缓冲区中,此时write立即返回,不管是否已经发出去了,若对方已关闭链接,则无法发送,并将置RST标志位,表示对方已经关闭链接,当下一次调用write时,write启动另一个内核函数去检查到此标志位,然后向此客户端程序发送SIGPIPE信号,默认是终止进程,若是服务器,需将此信号的处理方式改为捕捉,不致服务器出现崩溃
		n = read(cfd, buf, BUFSIZ);
		n = write(STDOUT_FILENO, buf, n);
	}
	close(cfd);
	return 0;
}
Example #23
0
int write_long (int fd, unsigned long v)
{
	unsigned char v_byte;
	unsigned short v_short;

	if (v < 0xF8)
	{
		v_byte = v;
		write (fd, &v_byte, sizeof (v_byte));
	}
	else if (v < 0xFFFF)
	{
		v_byte = 0xFE;
		write (fd, &v_byte, sizeof (v_byte));
		v_short = v;
		write (fd, &v_short, sizeof (v_short));
	}
	else
	{
		v_byte = 0xFF;
		write (fd, &v_byte, sizeof (v_byte));
		write (fd, &v, sizeof (v));
	}
	return 0;
}
Example #24
0
void Ant::send(AntRequest &request) {
    // checksum is XOR of all bytes
    uint8_t checksum = 0;

    // the new new deal
    checksum ^= ANT_START_BYTE;
    write(ANT_START_BYTE);

    // send length
    uint8_t len = (request.getDataLength());
    checksum ^= len;
    write(len);

    // Msg id
    write(request.getMsgId());
    checksum ^= request.getMsgId();

    for (int i = 0; i < request.getDataLength(); i++) {
        write(request.getData(i));
        checksum ^= request.getData(i);
    }

    // send checksum
    write(checksum);
}
Example #25
0
static int write_wav_header(snd_pcm_t *pcm)
{
	snd_pcm_file_t *file = pcm->private_data;
	static char header[] = {
		'R', 'I', 'F', 'F',
		0x24, 0, 0, 0,
		'W', 'A', 'V', 'E',
		'f', 'm', 't', ' ',
		0x10, 0, 0, 0,
	};
	static char header2[] = {
		'd', 'a', 't', 'a',
		0, 0, 0, 0
	};
	
	setup_wav_header(pcm, &file->wav_header);

	if (write(file->fd, header, sizeof(header)) != sizeof(header) ||
	    write(file->fd, &file->wav_header, sizeof(file->wav_header)) !=
	    sizeof(file->wav_header) ||
	    write(file->fd, header2, sizeof(header2)) != sizeof(header2)) {
		int err = errno;
		SYSERR("Write error.\n");
		return -err;
	}
	return 0;
}
Example #26
0
static void setup(void)
{
	int ret;

	TEST_PAUSE;

	tst_tmpdir();

	in_fd = creat(in_file, 0700);
	if (in_fd == -1)
		tst_brkm(TBROK | TERRNO, cleanup, "Create %s failed", in_file);

	ret = write(in_fd, TEST_MSG_IN, strlen(TEST_MSG_IN));
	if (ret == -1)
		tst_brkm(TBROK | TERRNO, cleanup, "Write %s failed", in_file);
	close(in_fd);

	in_fd = open(in_file, O_RDONLY);
	if (in_fd == -1)
		tst_brkm(TBROK | TERRNO, cleanup, "Open %s failed", in_file);

	out_fd = open(out_file, O_TRUNC | O_CREAT | O_RDWR, 0777);
	if (out_fd == -1)
		tst_brkm(TBROK | TERRNO, cleanup, "Open %s failed", out_file);
	ret = write(out_fd, TEST_MSG_OUT, strlen(TEST_MSG_OUT));
	if (ret == -1)
		tst_brkm(TBROK | TERRNO, cleanup, "Write %s failed", out_file);
}
Example #27
0
void	ft_putaddr_fd(void *str, int fd)
{
	long int			div;
	char				temp;
	unsigned long int	n;

	n = (unsigned long int)str;
	div = 16;
	write(fd, "0x", 2);
	while (n / div >= 16)
		div *= 16;
	while (div > 0)
	{
		temp = '0' + n / div;
		if (temp > '9')
		{
			temp += 39;
			write(fd, &temp, 1);
		}
		else
			write(fd, &temp, 1);
		n %= div;
		div /= 16;
	}
}
Example #28
0
// copy the bytes from 'in_fd' to 'out_fd', stop after finding
// 'n' chars 'sep'
int first_n_fields(int out_fd, int in_fd, char sep, int n) {

    int r;
    char c;

    for ( ;; ) {

        r = read(in_fd, &c, 1);
        if (r <= 0) {
            break;
        }

        if (c == sep) {
            n--;
        }

        if (n <= 0) {
            write(out_fd, "\n", 1);
            break;
        }

        write(out_fd, &c, 1);
    }

    return 1;
}
Example #29
0
void sendAck(int client, int ack) {
  if (ack == SUCCESS) {
    write(client,"SUCCESS",8);
  } else {
    write(client,"FAILURE",8);
  }
}
Example #30
0
const char* verr_msg(error_t err) {
  static __thread char buf[200];
  int n = 0;

  void write(const char* fmt, ...) {
    if (n < sizeof(buf)) {
      va_list va;
      va_start(va, fmt);
      n += vsnprintf(buf+n, sizeof(buf)-n, fmt, va);
      va_end(va);
    }
  }

  write("[%x:", err);

  int provider = VERR_PROVIDER(err);
  void* ptr = hashtable_get(providers, &provider);
  if (!ptr) {
    write("unknown]");
  } else {
    ErrorProvider* ep = *(ErrorProvider**)ptr;
    write("%s]", ep->name);
    const char* msg = ep->get_msg(err);
    if (msg) {
      write(" %s", msg);
    }
  }

  return buf;
}