Example #1
0
/*
** 'fileio_getstring' reads a string from from a file and returns
** the length of the string read. The string is stored at address
** 'p'. It assumes that there is enough room to store the string.
** The function can handle string in both Acorn format and this
** interpreter's. In Acorn's format, strings can be up to 255
** characters long. They are stored in the file in reverse order,
** that is, the last character of the string is first.
*/
int32 fileio_getstring(int32 handle, char *p) {
  FILE *stream;
  int32 marker, length = 0, n;
  handle = map_handle(handle);
  if (fileinfo[handle].eofstatus!=OKAY) {	/* If EOF is pending, flag an error */
    fileinfo[handle].eofstatus = ATEOF;
    error(ERR_HITEOF);
  }
  if (fileinfo[handle].lastwaswrite) {		/* Ensure everything has been written to disk first */
    fflush(fileinfo[handle].stream);
    fileinfo[handle].lastwaswrite = FALSE;
  }
  stream = fileinfo[handle].stream;
  marker = read(stream);
  switch (marker) {
  case PRINT_SHORTSTR:	/* Reading short string in 'Acorn' format */
    length = read(stream);
    for (n=1; n<=length; n++) p[length-n] = read(stream);
    break;
  case PRINT_LONGSTR:	/* Reading long string */
    length = 0;		/* Start by reading the string length (four bytes, little endian) */
    for (n=0; n<sizeof(int32); n++) length+=read(stream)<<(n*BYTESHIFT);
    for (n=0; n<length; n++) p[n] = read(stream);
    break;
  default:
    error(ERR_TYPESTR);
  }
  return length;
}
Example #2
0
unsigned long read_marker_and_time (int fd, int *setp, int *errorp)
{
	unsigned char v_byte;
	unsigned short v_short;
	unsigned long v_long;

	*errorp = *setp = 0;
read_long:
	*errorp = read (fd, &v_byte, sizeof (v_byte));
	if (*errorp <= 0)
		return 0;

	switch (v_byte)
	{
		case 0xf8:
			*setp = 1;
			goto read_long;
		case 0xfe:
			*errorp = read (fd, &v_short, sizeof (v_short));
			return v_short;	
		case 0xff:
			*errorp = read (fd, &v_long, sizeof (v_long));
			return v_long;	
		default:
			return v_byte;
	}
}
Example #3
0
int main(){
  unsigned char ask[66];
  unsigned char bsk[66];
  unsigned char A[132];
  unsigned char B[132];
  unsigned char KAB[132];
  unsigned char KBA[132];
  int outer = 0;
  int fd = open("/dev/urandom", O_RDONLY);
  
  for(int i=0; i<1; i++){
    read(fd, ask, 66);
    read(fd, bsk, 66);
    ask[65]=0;
    bsk[65]=0;
    p521_32_scalarmult_base(A, ask);
    p521_32_scalarmult_base(B, bsk);
    p521_32_scalarmult(KAB, bsk, A);
    p521_32_scalarmult(KBA, ask, B);
    int flag = 0;
    for(int i=0; i<132; i++){
      flag |= KAB[i]^KBA[i];
    }
    if(flag){
      printf("print 'DH', False\n");
      outer = 1;
    }
  }
  if(outer){
    exit(1);
  }else{
    exit(0);
  }
}
Example #4
0
void write_client_msg(Client* client, int write_fd)
{
    rb_red_blk_node* transfer = RBExactQuery(client->transfers, &write_fd);
    File_msg* file_msg = (File_msg*)(transfer->info);
    /*it is Encryption of a request or a transfer + or (data + Encryption)*/
    int valwrite = write(write_fd, file_msg->msg.buffer+file_msg->msg.offset, BUFFER_SIZE-file_msg->msg.offset);
    file_msg->msg.offset += valwrite;
    if(file_msg->msg.offset == BUFFER_SIZE) {
        logger("<Client><write_client_msg> got full message to write\n");
        if((file_msg->msg.total_len != 0) && (file_msg->msg.offset < file_msg->msg.total_len)) { /*fetch next part of the file*/
            if(ENCRYPTION_ENABLED) {
                char file_read_buffer[BUFFER_SIZE];
                for(int i=0; i<BUFFER_SIZE; ++i)
                    file_read_buffer[i] = file_msg->msg.buffer[i];
                file_msg->msg.offset += read(file_msg->file_fd, file_read_buffer, BUFFER_SIZE);
                AES128_ECB_encrypt(file_read_buffer, file_msg->aes_key, file_msg->msg.buffer);
                logger("<Client><write_client_msg>read full chunck from file and encrypted it\n");
            } else {
                read(file_msg->file_fd, file_msg->msg.buffer, BUFFER_SIZE);
                logger("<Client><write_client_msg>read full chunck from file and encrypted it\n");
            }
        } else if((file_msg->msg.total_len != 0)) { //write finished
            logger("<Client><write_client_msg>writing to the other end finished\n");
            FD_CLR(write_fd, &(client->fd_write_set));
            close(file_msg->file_fd);
        } else {
            logger("<Client><write_client_msg>transfer message\n");
            char file_read_buffer[BUFFER_SIZE];
            for(int i=0; i<BUFFER_SIZE; ++i)
                file_read_buffer[i] = file_msg->msg.buffer[i];
            AES128_ECB_encrypt(file_read_buffer, file_msg->aes_key, file_msg->msg.buffer);
            FD_CLR(write_fd, &(client->fd_read_set));
        }
    }
}
Example #5
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 #6
0
static int read_bytes(char *path, char *buf, int len)
{
    int fd = -1;
    int ret = -1;
    int size;
    unsigned char temp[1];
    
    fd = open(path, O_RDONLY);
    if (fd == -1) {
        return ret;
    }
    
    size = read(fd, temp, 1);
    if (size <= 0)
        goto end;
    
    size = read(fd, buf, len);
    if (size != temp[0])
        goto end;
    
    ret = size;
    
end:
    if (fd != -1)
        close(fd);
    
    return ret;
}
Example #7
0
int pobox_getcands(char *yomi, char *candstr, int len, int mode)
{
	char r;
	int i;
	int ret;

	if (mode) {
		fprintf(gWrServ, "1%s \n", yomi);
	} else {
		fprintf(gWrServ, "1%s\n", yomi);
	}
	ret = fflush(gWrServ);
	if (ret < 0) {
		pobox_disconnect();
		pobox_connect();
		if (mode) {
			fprintf(gWrServ, "1%s \n", yomi);
		} else {
			fprintf(gWrServ, "1%s\n", yomi);
		}
		fflush(gWrServ);
	}
	read(gServSock, &r, 1);
	if (r == '1') {		/* succeeded */
		i = read(gServSock, candstr, len - 1);
		candstr[i] = '\0';
		//printf("pobox_getcands recv =  '%s'\n",candstr);
	} else {
		while (read(gServSock, &r, 1) > 0 && r != '\n');
		return 1;
	}
	return 0;
}
Example #8
0
convpalette()
{
	long i, fil3;
	short danumshades;

	i = 0;
	while (strnicmp(wadata[i],"PLAYPAL",7) != 0) i++;
	lseek(fil1,wadplc[i],SEEK_SET);
	read(fil1,pal,768);
	outp(0x3c8,0);
	for(i=0;i<768;i++)
	{
		pal[i] >>= 2;
		outp(0x3c9,pal[i]);
	}

	i = 0;
	while (strnicmp(wadata[i],"COLORMAP",8) != 0) i++;
	lseek(fil1,wadplc[i],SEEK_SET);
	read(fil1,palookup,8192);

	if ((fil3 = open("palette.dat",O_BINARY|O_TRUNC|O_CREAT|O_WRONLY,S_IWRITE)) == -1)
		{ printf("Cannot save palette.dat\n"); exit(0); }
	write(fil3,pal,768);
	danumshades = 32;
	write(fil3,&danumshades,2);
	write(fil3,palookup,8192);
	close(fil3);
}
  bool canRxWait( unsigned timeout_ms ) {
    int16_t rc;
    fd_set rfds;
    struct timeval s_timeout;
    static uint8_t ui8_buf[16];

    FD_ZERO( &rfds );
    FD_SET( __HAL::msqDataClient.i32_pipeHandle, &rfds );
#ifdef USE_MUTUAL_EXCLUSION
    FD_SET( __HAL::breakWaitPipeFd[0], &rfds );
#endif

    s_timeout.tv_sec = 0;
    s_timeout.tv_usec = timeout_ms * 1000;

    rc = select( __HAL::msqDataClient.i32_pipeHandle+1, &rfds, NULL, NULL, &s_timeout );

#ifdef USE_MUTUAL_EXCLUSION
    if( FD_ISSET( __HAL::breakWaitPipeFd[0], &rfds ) ) {
      static char buff[256];
      while( 0 < read( __HAL::breakWaitPipeFd[0], buff, 256 ) ) { }
    }
#endif

    const bool r = ( rc > 0 );

    if( r > 0 && FD_ISSET( __HAL::msqDataClient.i32_pipeHandle, &rfds ) > 0 ) {
      for ( rc = read( __HAL::msqDataClient.i32_pipeHandle, &ui8_buf, 16 ); rc == 16 ;
            rc = read( __HAL::msqDataClient.i32_pipeHandle, &ui8_buf, 16 ) ) {}
    }

    return r;
  }
Example #10
0
int main (void)
{
   
   unsigned int length = 0;
   unsigned long long int sumcheck = 0;
   unsigned long long int sum = 0;
   char *mem = (char*)BASE_ADRESS;
   
   printf(" (c) Alexis Marquet\n");
   printf("Bootloader ready, code destination = 0x%X\n",(unsigned int)mem);
   printf("Asking for boot file....... \005 \n");
   
   
   int l = read(0,&length,sizeof(length));
   int m = read(0,mem,length);
   int s = read(0,&sum,sizeof(sum));
   
   for(uint32_t i = 0; i < length; i++)
   {
      sumcheck += mem[i];
   }
   if(sum!=sumcheck)
   {
      fprintf(stderr,"Error when checking SUM, %llx != %llx\n",sum, sumcheck);
      while(1);
   }
   printf("about to branch...\n");
   BRANCHTO(BASE_ADRESS);
   return 0;
}
Example #11
0
static size_t
SpellCustomMapDict(FcitxSpell *spell, SpellCustomDict *dict, const char *lang)
{
    int fd;
    struct stat stat_buf;
    size_t flen = 0;
    size_t total_len;
    char magic_buff[strlen(DICT_BIN_MAGIC)];
    fd = SpellCustomGetSysDictFile(spell, lang);

    if (fd == -1)
        return 0;
    if (fstat(fd, &stat_buf) == -1 ||
            (size_t)stat_buf.st_size <= sizeof(uint32_t) + strlen(DICT_BIN_MAGIC))
        goto out;
    if (read(fd, magic_buff, strlen(DICT_BIN_MAGIC)) <= 0)
        goto out;
    if (memcmp(DICT_BIN_MAGIC, magic_buff, strlen(DICT_BIN_MAGIC)))
        goto out;
    total_len = stat_buf.st_size - strlen(DICT_BIN_MAGIC);
    dict->map = malloc(total_len + 1);
    if (!dict->map)
        goto out;
    do {
        int c;
        c = read(fd, dict->map, total_len - flen);
        if (c <= 0)
            break;
        flen += c;
    } while (flen < total_len);
    dict->map[flen] = '\0';
out:
    close(fd);
    return flen;
}
Example #12
0
File: 25.c Project: mkazantsev/os
int main(void) {
	int fd[2];
	int status;
	pid_t child;
	char buf;
	char buf2[10];

	pipe(fd);

	child = fork();
	if (child == 0) {
		char *s = "hElLo wOrLd!";
		//close(fd[0]);
		write(fd[0], "hello", 5);
		write(fd[1], s, strlen(s));
		return 0;
	}
	
	//close(fd[1]);
	read(fd[1], buf2, 10);
	
	write(1, buf2, 10);
	
	close(fd[1]);
	
	while ((read(fd[0], &buf, 1)) != 0) {
		buf = toupper(buf);
		write(1, &buf, 1);
	}
	write(1, "\n", 1);
	wait(&status);
	return 0;
}
Example #13
0
int main(){
	shmid = shmget(123,MAX_SIZE,0777|IPC_CREAT);
	ch = shmat(shmid,NULL,0);
	char myfifo[MAX_SIZE] = "/home/niks/my_codes/comp_network/chat_server/myfifo1";
	char msg[MAX_SIZE];
	int fd;
	while(1){
		if(*ch == 1){
			fd = open(myfifo,O_RDWR);
			printf("Enter your msg..\n");
			scanf("%s",msg);
			write(fd,msg,MAX_SIZE);
			read(fd,msg,MAX_SIZE);
			printf("%s",msg);
			close(fd);
		}
		
		else{
			sleep(5);
			fd = open(myfifo,O_RDWR);
			read(fd,msg,MAX_SIZE);
			printf("client 1 received a message : %s",msg);
			close(fd);
		}
		
		
	}
	
	return 0;
}
int Serializable::readUTF(std::string &str, DataStream *stream, bool widen) {
  uint32_t utflen = 0;
  int ret = 1;
  if (!widen) {
    uint16_t shortLength = 0;
    ret = read(shortLength, stream);
    utflen = shortLength;

    if (ret <= 0)
      return ret;
  } else {
    uint32_t len;
    ret = read(len, stream);
    if (ret <= 0)
      return ret;
    utflen = len;
  }

  if (utflen == 0) {
    str = "";
    return 1;
  }

  std::vector<uint8_t> buf;
  ret = stream->readData(buf, utflen);

  // The number of chars produced may be less than utflen
  str = std::string((const char*) &buf[0], utflen);
  return utflen;
}
Example #15
0
int main(int argc,char *argv[])
{
	int fd1,fd2;
	char buff1[1024];
	char buff2[1024];
	int m,n;
	fd1=open(argv[1],O_RDONLY);
	if(fd1<0)
	{
		printf("open the first file failed!\n");
		return -1;
	}
	fd2=open(argv[2],O_RDONLY);
	if(fd2<0)
	{
		printf("open the second file failed!\n");
		return -1;
	}
	while(1)
	{	
		n=read(fd1,buff1,1024);
		m=read(fd2,buff2,1024);
		if(strcmp(buff1,buff2)!=0 || n!=m )
		{
			printf("not the same!\n");
			return -1;
		}
		if(n==0)
			break;		
	}
	printf("the two files are the same!\n");
	return 0;
}
Example #16
0
int main() {
    int pipeAB[2];
    int pipeBC[2];
    int pipeCA[2];
    pipe(pipeAB);
    pipe(pipeBC);
    pipe(pipeCA);
    int buf;
    int r = 1;

    // parent is process A
    // first child is process is B

    if (fork()  == 0) {
        close(pipeCA[0]);
        close(pipeCA[1]);
        close(pipeAB[1]);       // can only read from this pipe
        close(pipeBC[0]);       // can only write to this pipe
        while (r) {
            r = read(pipeAB[0], &buf, 1);
            if (r) {
                printf("Process B recieved: %c\n", buf);
            }
            write(pipeBC[1], &buf, r);
        }
        close(pipeAB[0]);
        close(pipeBC[1]);
        _exit(0);
    }

    if (fork()  == 0) {
        close(pipeAB[0]);
        close(pipeAB[1]);
        close(pipeBC[1]);       // can only read from this pipe
        close(pipeCA[0]);       // can only write to this pipe
        while (r) {
            r = read(pipeBC[0], &buf, 1);
            if (r) {
                printf("Process C recieved: %c\n", buf);
            }
            write(pipeCA[1], &buf, r);
        } while (r);
        close(pipeBC[0]);
        close(pipeCA[1]);
        _exit(0);
    }
    
    int out = 'A';
    int in = 0;
    close(pipeBC[0]);
    close(pipeBC[1]);
    close(pipeAB[0]);           // "A" can only write into this pipe
    close(pipeCA[1]);           // "A" can only read from this pipe
    write(pipeAB[1], &out, 1);
    read(pipeCA[0], &in, 1);
    printf("Wrote: %c, Read: %c\n", out, in);
    
    close(pipeAB[1]);           // causes B to read 0
    close(pipeCA[0]);           // causes A to not longer want to read
}
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;
    }
int main(int argc, char **argv)
{

    int sockfd, len, result;
    struct sockaddr_in address;
    char ch[128];
    int read_len = 0 ;

    /*クライアント用ソケット作成*/
    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    /*サーバ側と同じ名前でソケットの名前を指定*/
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr("127.0.0.1");
    address.sin_port = htons(1192);
    len = sizeof(address);

    /*クライアントのソケットとサーバのソケットの接続*/
    result = connect(sockfd, (struct sockaddr *)&address, len);
    if(result == -1) ERROR("oops : client1");

    /*sockfdを介して読み書きができるようにする*/
    len = read(sockfd, &ch, sizeof(ch));
    printf("char from 1 server = %s,read_len=%d,bufsize=%ld\n", ch,len,sizeof(ch));
    memset(ch, 0 , sizeof(ch));

    printf("sleep 1 sec\n");
    sleep(1);

    len = read(sockfd, &ch, sizeof(ch));
    printf("char from 2 server = %s,read_len=%d,bufsize=%ld\n", ch,len,sizeof(ch));
    close(sockfd);
    exit(0);
}
Example #19
0
int main(int argc , char *argv[])
{
	int fds1,fds2,a;
	char ch;
	if (argc !=3 )
		{
			printf("exactly 2 arguments expected");
			return 0;
		}
	else
		{
			fds1=open(argv[1],O_RDONLY);
			fds2=open(argv[2],O_WRONLY | O_CREAT | O_TRUNC);
			a=read(fds1,&ch,1);
			while(a!=0 && a!=-1)
			{
				printf("%c",ch);

				a=write(fds2,&ch,1);
				if(a==-1) continue;
				a=read(fds1,&ch,1);
			}
		}
			
}
Example #20
0
//!
//! diffs two files: 0=same, -N=different, N=error
//!
//! @param[in] path1
//! @param[in] path2
//!
//! @return 0=same, -N=different, N=error
//!
int diff(const char *path1, const char *path2)
{
    int fd1 = 0;
    int fd2 = 0;
    int read1 = 0;
    int read2 = 0;
    char buf1[BUFSIZE] = "";
    char buf2[BUFSIZE] = "";

    if ((fd1 = open(path1, O_RDONLY)) < 0) {
        LOGERROR("failed to open %s\n", path1);
    } else if ((fd2 = open(path2, O_RDONLY)) < 0) {
        LOGERROR("failed to open %s\n", path2);
        close(fd1);
    } else {
        do {
            read1 = read(fd1, buf1, BUFSIZE);
            read2 = read(fd2, buf2, BUFSIZE);
            if (read1 != read2)
                break;

            if (read1 && memcmp(buf1, buf2, read1))
                break;
        } while (read1);

        close(fd1);
        close(fd2);
        return (-(read1 + read2));     // both should be 0s if files are equal
    }
    return EUCA_ERROR;
}
Example #21
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 #22
0
int
ksyms_aout_parse()
{
	struct exec eh;
	uint32_t size;

	if (lseek(ksyms, 0, SEEK_SET) == -1)
		return -1;

	if (read(ksyms, &eh, sizeof eh) != sizeof eh)
		return -1;

	if (N_BADMAG(eh))
		return -1;

	ksymoffs = (off_t)N_STROFF(eh);
	if (lseek(ksyms, ksymoffs, SEEK_SET) == -1)
		return -1;

	if (read(ksyms, &size, sizeof size) != sizeof size)
		return -1;
	ksymoffs += sizeof size;
	if (size <= sizeof size)
		return -1;
	ksymsize = (off_t)size - sizeof size;

	return 0;
}
Example #23
0
/*
 * Merged and calculates the lux values how its dicribed in the datasheet
 * @return lux value
 */
float Light::getLuxValue() {
	// Read and check for errors.
#ifdef PROTOCOL_BINARY
#else
	if(read() == -1)
		PRINTF("Light sensor read error\r\n");
	//PRINTF("Ch0: %d   Ch1: %d \r\n", ch0, ch1);
#endif
	if(read() == -1)
			PRINTF("Light sensor read error\r\n");

	if(ch0 <= 0 || ch1 <= 0)
		return 500;
	float coeff = (ch1/(float)ch0);

	if (coeff <= 0.52){
		return (0.0315 * ch0 - 0.0593 * ch0 * (pow(coeff, 1.4)));
	}else if (coeff <= 0.65){
		return (0.0229 * ch0 - 0.0291 * ch1);
	}else if (coeff <= 0.80){
		return (0.0157 * ch0 - 0.0180 * ch1);
	}else if (coeff <= 1.30){
		return (0.00338 * ch0 - 0.00260 * ch1);
	}
	return 0;
}
Example #24
0
int main ()
{
    int p2c[2], c2p[2];
    int pid;
    char buf[100];
    pipe(p2c);
    pipe(c2p);
    pid = fork();
    if(pid > 0)
    {
        close(c2p[0]);
        close(p2c[1]);
        write(c2p[1], parent, sizeof(parent));
        close(c2p[1]);
        read(p2c[0], buf, 100);
        printf("%s\n", buf);
        close(p2c[0]);
    }
    else if(pid == 0)
    {
        close(p2c[0]);
        close(c2p[1]);
        read(c2p[0], buf, 100);
        printf("%s\n", buf);
        write(p2c[1], child, sizeof(child));
        close(c2p[0]);
        close(p2c[0]);
    }
    return 0;
}
Example #25
0
void play(){
	if(pause==1){
		return;
	}
	read();
	if(ret!=0){
		calljava(1);
		return;
	}
	if(packet->stream_index==audioIndex){
		ret1=avcodec_decode_audio4(pCodecCtx,pFrame,&finish,packet);
		packet->size-=ret1;
		if(finish){
    		r=swr_convert(au_convert_ctx,&out_buffer, MAX_AUDIO_FRAME_SIZE,(const uint8_t **)pFrame->data , pFrame->nb_samples);
    		out_buffer_size=av_samples_get_buffer_size(NULL,out_channels ,r,out_sample_fmt, 1);
			AudioWrite(out_buffer,out_buffer_size);
			//av_log(NULL,AV_LOG_INFO,"tni:%d packet size:%6d ret:%6d",tni,packet->size,ret);
		}else{
			if(ret==0){
				read();
				play();
			}
		}
		calljava(2);
	}else{
		packet->size=0;
		read();
		play();
	}

}
int main() {
  Sales_data total;
  if (read(std::cin, total)) {
  //double price;
  //if (std::cin >> total.bookNo >> total.units_sold >> price) {
  //  total.revenue = total.units_sold * price;
    Sales_data trans;
    while (read(std::cin, trans)) {
    //while (std::cin >> trans.bookNo >> trans.units_sold >> price) {
    //  trans.revenue = trans.units_sold * price;
      if (total.isbn() == trans.isbn()) {
        total.combine(trans);
      } else {
        print(std::cout, total) << std::endl;
        //std::cout << total.bookNo << " "
        //          << total.units_sold << " "
        //          << total.revenue << std::endl;
        total = trans;  // Use default copy constructor
      }
    }
    print(std::cout, total) << std::endl;
    //std::cout << total.bookNo << " "
    //          << total.units_sold << " "
    //          << total.revenue << std::endl;
  } else {
    std::cerr << "No data!" << std::endl;
    return -1;
  }

  return 0;
}
Example #27
0
File: fs.c Project: rbkloss/dcc_fs
struct superblock * fs_open(const char *fname) {
    int fd = open(fname, O_RDWR);
    if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
        errno = EBUSY;
        return NULL;
    }
    struct superblock* sb = (struct superblock*) malloc(sizeof (struct superblock));
    read(fd, sb, sizeof (struct superblock));

    /*
     * If =fname does not contain a
     * 0xdcc605fs, then errno is set to EBADF.
     */
    if (sb->magic != 0xdcc605f5) {
        errno = EBADF;
        close(fd);
        free(sb);
        return NULL;
    }
    int blocksz = sb->blksz;
    free(sb);
    sb = (struct superblock*) malloc(blocksz);
    lseek(fd, 0, SEEK_SET);
    read(fd, sb, blocksz);
    return sb;
}
Example #28
0
// read_to_taskbuf(fd, t)
//	Reads data from 'fd' into 't->buf', t's bounded buffer, either until
//	't's bounded buffer fills up, or no more data from 't' is available,
//	whichever comes first.  Return values are TBUF_ constants, above;
//	generally a return value of TBUF_AGAIN means 'try again later'.
//	The task buffer is capped at TASKBUFSIZ.
taskbufresult_t read_to_taskbuf(int fd, task_t *t)
{
    unsigned headpos = (t->head % TASKBUFSIZ);
    unsigned tailpos = (t->tail % TASKBUFSIZ);
    ssize_t amt;

    if (t->head == t->tail || headpos < tailpos)
    {
        amt = read(fd, &t->buf[tailpos], TASKBUFSIZ - tailpos);
    }
    else
    {
        amt = read(fd, &t->buf[tailpos], headpos - tailpos);
    }

    if (amt == -1 && (errno == EINTR || errno == EAGAIN
                || errno == EWOULDBLOCK))
        return TBUF_AGAIN;
    else if (amt == -1)
        return TBUF_ERROR;
    else if (amt == 0)
        return TBUF_END;
    else {
        t->tail += amt;
        return TBUF_OK;
    }
}
Example #29
0
int read_dim(int file, long int *dim)
{
	unsigned char byte1, byte2, byte3, byte4;
	int n, res;
	double skip = 0.0;

	if ((n = read(file, &byte1, 1)) != 1) {
		ERRORLOGG("Errore durante la lettura della dimensione del tag ID3  al byte1");
		return ERR_PARSE;
	}
	res = calculate_skip((int) byte1, &skip, 24);
	if ((n = read(file, &byte2, 1)) != 1) {
		ERRORLOGG("Errore durante la lettura della dimensione del tag ID3 al byte2");
		return ERR_PARSE;
	}
	res = calculate_skip((int) byte2, &skip, 16);
	if ((n = read(file, &byte3, 1)) != 1) {
		ERRORLOGG("Errore durante la lettura della dimensione del tag ID3 al byte3");
		return ERR_PARSE;
	}
	res = calculate_skip((int) byte3, &skip, 8);

	if ((n = read(file, &byte4, 1)) != 1) {
		ERRORLOGG("Errore durante la lettura della dimensione del tag ID3 al byte4");
		return ERR_PARSE;
	}
	res = calculate_skip((int) byte4, &skip, 0);
	*dim = (long int) skip;

	return ERR_NOERROR;
}
//****************************************************************************
//
//! Set operating frequency of the PWM module
//!
//! \param freq: in Hz, between 24Hz to 1526Hz
//!
//! \return void
//
//****************************************************************************
void setPWMFreq(float freq)
{
	freq = (freq * 9)/10; // Correct for overshoot in the frequency setting
    float prescaleval = 25000000;
    prescaleval /= 4096;
    prescaleval /= freq;
    prescaleval -= 1;
    //print out prescale value if wanted
    #ifdef PWM_DEBUG
    UART_PRINT("Estimated pre-scale: "); 
    UART_PRINT("%4.2f\n\r",prescaleval);
    #endif
    uint8_t prescale = floor(prescaleval + 0.5);
    #ifdef PWM_DEBUG
    UART_PRINT("Final pre-scale: "); 
    UART_PRINT("%d\n\r",prescale);
    #endif
    PWM_reset();
    uint8_t oldmode = read(PCA9685_MODE1);
    uint8_t newmode = (oldmode&0x7F) | 0x10; // sleep
    write(PCA9685_MODE1, newmode); // go to sleep
    write(PCA9685_PRESCALE, prescale); // set the prescaler
    write(PCA9685_MODE1, oldmode);
    delay(5);
    write(PCA9685_MODE1, oldmode | 0xa1);  // This sets the MODE1 register 
                                            // to turn on auto increment.
    oldmode = read(PCA9685_MODE1);
}