/** * @brief Close the file. * * The frame length in the header part is overwritten by the actual value * before file close. * * @param fp [in] file pointer to close, previously opened by wrwav_open(). * * @return TRUE on success, FALSE on failure. */ boolean wrwav_close(FILE *fp) { unsigned int i; /* overwrite data length after recording */ /* at 5-8(len+36), 41-44 (len) */ if (fseek(fp, 40, SEEK_SET) != 0) { /* error */ jlog("Error: wrwav: failed to seek for header\n"); return(FALSE); } i = totallen * sizeof(SP16); if (!mywrite(&i, 4, 1, fp)) { jlog("Error: wrwav: failed to re-write header\n"); return(FALSE); } if (fseek(fp, 4, SEEK_SET) != 0) { /* error */ jlog("Error: wrwav: failed to seek for header\n"); return(FALSE); } i = totallen * sizeof(SP16) + 36; if (!mywrite(&i, 4, 1, fp)) { jlog("Error: wrwav: failed to re-write header\n"); return(FALSE); } /* close file */ fclose_writefile(fp); return(TRUE); }
/* process_line * * Process the request (a filename) from the client. Send back the * response and then the content of the requested file through * mysocket layer. * * Returns * 0 on success * -1 on failure */ static int process_line(int sd, char *line) { char resp[5000]; int fd = -1, length; if (!*line || access(line, R_OK) < 0) { sprintf(resp, "%s,-1,File does not exist or access denied\r\n", line); } else { if ((fd = open(line, O_RDONLY)) < 0) { sprintf(resp, "%s,-1,File could not be opened\r\n", line); } else { sprintf(resp, "%s,%lu,Ok\r\n", line, lseek(fd, 0, SEEK_END)); lseek(fd, 0, SEEK_SET); } } /** fprintf(stderr, "sending to client: %s of length %d bytes\n", resp, strlen(resp)); **/ /* Return the response to the client */ if (mywrite(sd, resp, strlen(resp)) < 0) { if (fd != -1) close(fd); return -1; } if (fd == -1) return 0; for (;;) { length = read(fd, resp, sizeof(resp)); if (length == 0) break; if (length == -1) { perror("read"); close(fd); return -1; } /* fwrite(resp, length, 1, stdout); */ if (mywrite(sd, resp, length) < 0) { close(fd); return -1; } } close(fd); return 0; }
static void output_file(char*buf, int size) { int i; char linebuf[64]; uint32_t *buf32 = (uint32_t*)buf; switch (output_type) { case OFMT_BIN : mywrite(buf, size); break; case OFMT_STR_BIN : linebuf[0] = linebuf[33] = '"'; linebuf[34] = ','; linebuf[35] = '\n'; for (i=0; i<size/4; i++) { set_bin(linebuf+1, buf32[i]); mywrite(linebuf, 36); } for (i=i; i<output_line_min; i++) { mywrite("\"00000000000000000000000000000000\",\n", 36); } break; case OFMT_STR_HEX : linebuf[0]='x'; linebuf[1] = linebuf[10] = '"'; linebuf[11] = ','; linebuf[12] = '\n'; for (i=0; i<size/4; i++) { set_hex(linebuf+2, buf32[i]); if (i == size/4 -1) { mywrite(linebuf, 11); } else { mywrite(linebuf, 13); } } for (i=i; i<output_line_min; i++) { mywrite(",\nx\"00000000\"", 13); } break; case OFMT_COE : mywrite("memory_initialization_radix=16;\n", 32); mywrite("memory_initialization_vector=\n", 30); linebuf[8] = ','; linebuf[9] = '\n'; for (i=0; i<size/4; i++) { if (i == size/4-1) { linebuf[8]=';'; } set_hex(linebuf, buf32[i]); mywrite(linebuf, 10); } break; case OFMT_EX_MNE : default : warning("Unexpected case @ output_file\n"); break; } }
extern "C" void PySys_WriteStderr(const char* format, ...) noexcept { va_list va; va_start(va, format); mywrite("stderr", stderr, format, va); va_end(va); }
/*! blocking I/R write. *! return number of bytes written, or negative for error. */ int lnp_logical_write(const void *data, size_t length) { // With Win32 we are using Blocking Write by default #ifndef NQC_RCXLIB #if !defined(_WIN32) fd_set fds; // wait for transmission // do { FD_ZERO(&fds); FD_SET(rcxFD(),&fds); } while(select(rcxFD()+1,NULL,&fds,NULL,NULL)<1); #endif // transmit // #if defined(LINUX) || defined(linux) if (tty_usb == 0) #endif keepaliveRenew(); #endif /* !NQC_RCXLIB */ return mywrite(rcxFD(), data, length)!=length; }
void* twrite(void* arg) { int sock = *(int*)arg; int ret =-1; fd_set fset; FD_ZERO(&fset); FD_SET(sock,&fset); struct timeval tm; tm.tv_sec = 3; tm.tv_usec = 0; while(1) { ret = select(sock+1,NULL,&fset,NULL,&tm); if ( ret == -1 ) { printf("select fail\n"); } else if ( ret == 0) { printf("time out\n"); } if ( FD_ISSET(sock,&fset) ) { char* buf = "hello"; int len = strlen(buf); if ( len == mywrite(sock,buf,len) ) { printf("mywrite len=%d\n",len); } } } }
int main () { int fd, cnt; char buf[12]; fd = myopen ("/tmp/demo/1.txt", O_CREAT | O_RDWR); if (fd == -1) { perror ("open: "); return -1; } cnt = myread (fd, buf, 10); if (cnt > 0) { mywrite (STDIN_FILENO, buf, cnt); } else { perror ("write: "); } printf ("\nexiting...\n"); sleep (1); return 0; }
static void send_reset() { mywrite(all_reset, ALL_RESET_SZ, 0); usleep(60000); myflush(); usleep(70000); return; }
// -------------------------------------------------------------------- // main // -------------------------------------------------------------------- int main(int argc, char**argv){ if (!myopen()) { printf("Cannot open port %s\n", DEV); return -1; } Byte buf[10]; unsigned int l=0; // write l=1; buf[0]=0xAA; if (!mywrite(buf, l)) { printf("Cannot write on port\n"); } // read l=1; if (!myread(buf, l, 10)) { printf("Cannot read on port\n"); } else { printf("read:0x%02x\n", buf[0]); } myclose(); return 0; }
void write_file() { int fd; int nbytes; char buf[BLOCK_SIZE]; bzero(buf, BLOCK_SIZE); if (1 == strlen(pathName) && pathName[0] >= '0' && pathName[0] <= '9') { // 1) get fd fd = atoi(pathName); // 2) verify fd is indeed opened for W or RW or APPEND mode if ((0 != running->fd[fd]) && (0 != running->fd[fd]->refCount) && (1 == running->fd[fd]->mode || 2 == running->fd[fd]->mode || 3 == running->fd[fd]->mode)) { // 3) copy text string into a buf[] and get it's length as nbytes nbytes = strlen(parameter); strncpy(buf, parameter, nbytes + 1); printf("%s\n", buf); mywrite(fd, buf, nbytes); } else { printf("write : fd = %d is NOT FOR WRITE\n", fd); } } else { printf("write : ensure correct fd is provided\n"); } }
int method2() { int fd; //char * myfifo = "/tmp/myfifo"; char * myfifo = "myfifo"; /* create the FIFO (named pipe) */ printf("mkfifo\n"); mkfifo(myfifo, 0666); printf("system call\n"); system("gnuplot < myfifo"); printf("open\n"); fd = open(myfifo, O_WRONLY); printf("write\n"); mywrite(fd, "plot [-3:3] sin(x)"); printf("close\n"); close(fd); /* remove the FIFO */ printf("unlink\n"); unlink(myfifo); printf("done\n"); return 0; }
int method1() { int fd; //char * myfifo = "/tmp/myfifo"; char * myfile = "myfifo"; /* create the FIFO (named pipe) */ //printf("mkfifo\n"); //mkfifo(myfifo, 0666); printf("open\n"); fd = open(myfile, O_WRONLY); printf("system call\n"); int rc = system("gnuplot -p < myfifo"); printf("system rc: %i\n", rc); printf("write\n"); mywrite(fd, "plot [-3:3] sin(x)\n"); printf("close\n"); close(fd); printf("done\n"); return 0; }
/** * Write speech samples. * * @param fp [in] file descriptor * @param buf [in] speech data to be written * @param len [in] length of above * * @return actual number of written samples. */ boolean wrwav_data(FILE *fp, SP16 *buf, int len) { boolean ret; ret = mywrite(buf, sizeof(SP16), len, fp); if (ret) totallen += len; return(ret); }
static int echoSilently(int from, int to){ char buff[BUFFSZ]; int bytesIn = 0, bytesOut = 0; bytesIn = read(from, buff, BUFFSZ); if(bytesIn <= 0){ return bytesIn; } bytesOut = mywrite(to, buff, bytesIn, 0); return bytesOut; }
static void ConnRequest( char request ) { int bytes; NameBuff[0] = request; bytes = mywrite( ConnHdl, NameBuff, strlen( NameBuff ) + 1 ); bytes = myread( ConnHdl, NameBuff, 1 ); }
int main(void) { struct sigaction sigsegv; char c; // This fails due to a bad fd (at one point I was not handling failing // mmap() calls, and would have got a seg fault). char* res1 = mmap(0, 0, PROT_READ, MAP_PRIVATE, -1, 0 ); // This succeeds but is meaningless. Important thing is that the size is // zero, so Annelid should not subtract one from the size when doing any // range calculations. (It did at one point, giving 0xffffffff, which // screwed everything up.) char* res2 = mmap(0, 0, PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0 ); // This succeeds and is useful. char* res3 = mmap(0, getpagesize(), PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); assert(MAP_FAILED == res1); assert(NULL == res2); assert(MAP_FAILED != res3 && NULL != res3); // Intall SEGV handler sigsegv.sa_handler = SEGV_handler; sigsegv.sa_flags = 0; assert( 0 == sigemptyset( &sigsegv.sa_mask ) ); assert( 0 == sigaction(SIGSEGV, &sigsegv, NULL) ); #define TTT(i) \ if (__builtin_setjmp(TTT_jmpbuf) == 0) { c = res3[i]; } TTT(0); TTT(-1); mywrite(res3, 5); mywrite(res3-1, 5); assert( 0 == munmap(res3, getpagesize()) ); TTT(0); TTT(-1); mywrite(res3, 5); mywrite(res3-1, 5); return 0; }
boolean outprob_cache_output(FILE *fp, HMMWork *wrk, int framenum) { int s,t; boolean needswap; #ifdef WORDS_BIGENDIAN needswap = FALSE; #else /* LITTLE ENDIAN */ needswap = TRUE; #endif needswap = TRUE; if (wrk->outprob_allocframenum < framenum) { jlog("Error: outprob_cache_output: framenum > allocated (%d > %d)\n", framenum, wrk->outprob_allocframenum); return FALSE; } { unsigned int ui; unsigned short us; short st; float f; jlog("Stat: outprob_cache_output: %d states, %d samples\n", wrk->statenum, framenum); ui = framenum; if (!mywrite((char *)&ui, sizeof(unsigned int), 1, fp, needswap)) return FALSE; ui = wrk->OP_param->header.wshift; if (!mywrite((char *)&ui, sizeof(unsigned int), 1, fp, needswap)) return FALSE; us = wrk->statenum * sizeof(float); if (!mywrite((char *)&us, sizeof(unsigned short), 1, fp, needswap)) return FALSE; st = F_USER; if (!mywrite((char *)&st, sizeof(short), 1, fp, needswap)) return FALSE; for (t = 0; t < framenum; t++) { for (s = 0; s < wrk->statenum; s++) { f = wrk->outprob_cache[t][s]; if (!mywrite((char *)&f, sizeof(float), 1, fp, needswap)) return FALSE; } } } return TRUE; }
int __get_squid_info(char *squidoption, char *squidcmd, int port, int index) { char buf[LEN_4096]; char *hostname = "localhost"; int len, conn, bytesWritten, fsize = 0; struct p_squid_info psi = {&s_st_squid[index].sc, &s_st_squid[index].si}; if ((conn = socket(PF_INET, SOCK_STREAM, 0)) < 0) { close(conn); return -1; } if (client_comm_connect(conn, hostname, port, NULL) < 0) { close(conn); return -1; } int flags; /* set socket fd noblock */ if ((flags = fcntl(conn, F_GETFL, 0)) < 0) { close(conn); return -1; } if (fcntl(conn, F_SETFL, flags & ~O_NONBLOCK) < 0) { close(conn); return -1; } struct timeval timeout = {10, 0}; setsockopt(conn, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(struct timeval)); setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval)); bytesWritten = mywrite(conn, squidcmd, strlen(squidcmd)); if (bytesWritten < 0) { close(conn); return -2; } else if (bytesWritten != strlen(squidcmd)) { close(conn); return -3; } while ((len = myread(conn, buf, sizeof(buf) - fsize -1)) > 0) { fsize += len; } buf[fsize] = '\0'; /* read error */ if (fsize < 1000) { close(conn); return -1; } if (parse_squid_info(buf, squidoption, &psi) < 0) { close(conn); return -1; } close(conn); return 0; }
int main() { int i=0; int j=5; mywrite(&i); assert(i == 0); // UNKNOWN! assert(glob == 5); assert(j == 5); return 0; }
int write_file() { char fd_str[64]; char text[128]; int fd; //char buf[1024]; OFT* oftp = 0; //1. Get the fd and text string to write. //Copy the filename and the text to write over. (using input parameters here) strcpy(fd_str, pathname); strcpy(text, parameter); //get the fd. sscanf(fd_str, "%d", &fd); oftp = running->fd[fd]; /* //Assume the file is already opened and is residing in the fd[]. //trying to find the fd: int i =0; for(i = 0; i < NFD;i++) { if(running->fd[i] != 0) { //the entry is guarantee for having an OFT entry oftp = running->fd[i]; if(!strcmp(oftp->minodeptr->name, filename)) { //the name is matched. oftp = running->fd[i]; break; } } } */ if(oftp == 0) { //reached the end of the fd array, no match. printf ("File is not opened"); return -1; } //if reached here, mean the file was found between the fd array of current running proc. if(oftp->mode != 1 && oftp->mode != 2 && oftp-> mode != 3) { //file was not open for WR, RD or APPEND. printf ("File was not open for WR, RD, or APPEND\n"); return -1; } //if reached here, mean the file was open for valid mode. return mywrite(fd, text, strlen(text)); }
void PySys_WriteStdout(const char *format, ...) { va_list va; va_start(va, format); mywrite("stdout", stdout, format, va); va_end(va); }
int module_stop(void) { unsigned char c = 1; DBG("cicero: stop()\n"); cicero_stop = 1; mywrite(fd2[1], &c, 1); return 0; }
int writeHeader(int fd, int rows, int cols, unsigned int start) { unsigned int fileSize; unsigned int headerSize; unsigned int paddedCols; BITMAPFILEHEADER bmfh; BITMAPINFOHEADER bmih; memset (&bmfh, 0, sizeof(bmfh)); memset (&bmih, 0, sizeof(bmih)); paddedCols = ((cols/4)*4 !=cols ? ((cols+4)/4)*4 : cols); headerSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); fileSize = rows*paddedCols*sizeof(PIXEL)+headerSize; bmfh.bfType = 0x4D42; bmfh.bfSize = fileSize; bmfh.bfReserved1 = 0; bmfh.bfReserved2 = 0; bmfh.bfOffBits = start; bmih.biSize = 40; bmih.biWidth = cols; bmih.biHeight = rows; bmih.biPlanes = 1; bmih.biBitCount = 24; bmih.biCompression = 0; bmih.biSizeImage = 0; bmih.biXPelsPerMeter = 0; bmih.biYPelsPerMeter = 0; bmih.biClrUsed = 0; bmih.biClrImportant = 0; if(mywrite(fd, ((char*)&bmfh)+2, sizeof(bmfh)-2) < 0) { perror("Can't write BITMAPFILEHEADER"); return -2; } if(mywrite(fd, (char*)&bmih, sizeof(bmih)) < 0) { perror("Can't write BITMAPINFOHEADER"); return -3; } return 0; }
trap_retval RemotePut( byte *data, trap_elen length ) { trap_elen bytes_written; trap_elen real_length; real_length = length; if( length == 0 ) length = 1; /* Can't write zero bytes */ bytes_written = mywrite( WriteHdl, data, length ); if( bytes_written != length ) return( REQUEST_FAILED ); if( length == 1 ) { /* Send true length through */ bytes_written = mywrite( WriteHdl, &real_length, sizeof( trap_elen ) ); if( bytes_written != sizeof( trap_elen ) ) { return( REQUEST_FAILED ); } } return( length ); }
void draw_askbox(LISTBOX *l) { draw_box(l->title, l->x, l->y, l->w, l->h, TRUE); draw_contents(l); hi_vid(); goto1(l->x+4, l->y+2); mywrite((char *)l->parent); pch('?'); n_vid(); }
int writeBits(int fd, int rows, int cols, PIXEL* bitmap, unsigned int start) { int row; char padding[3]; int padAmount; char useless[DEFAULT_BITMAP_OFFSET]; padAmount = ((cols * sizeof(PIXEL)) % 4) ? (4 - ((cols * sizeof(PIXEL)) % 4)) : 0; memset(padding, 0, 3); start -= sizeof(BITMAPFILEHEADER)-2+sizeof(BITMAPINFOHEADER); if(start > 0) { memset(useless, 0, start); if(mywrite(fd, useless, start) < 0) { perror("Can't lseek to bitmap"); return -6; } } /* if(lseek (fd, start, SEEK_SET) < 0) { perror("Can't lseek to bitmap"); return -6; } */ for (row=0; row < rows; row++) { if(mywrite(fd, (char*)(bitmap+(row*cols)), cols*sizeof(PIXEL)) < 0) { perror("Can't write bitmap"); return -7; } if(padAmount > 0) { if(mywrite(fd, padding, padAmount) < 0) { perror("Can't write bitmap"); return -8; } } } return 0; }
int main(void) { char *buf = malloc(sizeof(char)*6), *buf2 = malloc(sizeof(char)*6); struct itimerval* itval = malloc(sizeof(struct itimerval) - 1); int diff = buf2 - buf; buf[0] = 'h'; buf[1] = 'e'; buf[2] = 'l'; buf[3] = 'l'; buf[4] = 'o'; buf[5] = 'x'; // error (read) (will fail due to -1, as we want -- don't want any // unpredictable output to foul up the test) mywrite(buf+3, 5); // error (read) mywrite(buf-1, 5); // error (read) mywrite(buf+1, diff); // error (read) myopen(buf+3, 0x0); // error (read_asciiz) mygetitimer(0, itval); // error (write) //---- free(buf); mywrite(buf, 5); // error mywrite(buf+3, 5); // error mywrite(buf+1, diff); // error (read) return 0; }
void myread(int file, char *cmd, char *buffer) { debug_message("READ1\n"); int len; mywrite(file, cmd); len = read(file, buffer, BUFFER_SIZE); if (len >= 0) buffer[len] = 0; else buffer[0] = 0; debug_message("BUFFER: %s\n", buffer); debug_message("READ2\n"); }
void init_power() { debug_message("INIT1\n"); int k; char fname[100]; char err[256]; int h; gettimeofday(&st, NULL); for(k=0; k < NUM_PWR_CHANNELS; k++) { sprintf(fname, "/dev/usbtmc%d", k); if ((h = open(fname, O_RDWR)) >= 0) { fprintf(stderr, "- POWER: preparing meter %s\n", fname); myfile[k] = h; mywrite(myfile[k], "*RST"); //Minimum (mV) range, default resolution. printf controls //how much resolution prints -- see data_collector.c if (k == 0) mywrite(myfile[k], "CONF:VOLT:DC MIN,DEF"); else mywrite(myfile[k], "CONF:VOLT:AC MIN"); mywrite(myfile[k], "TRIG:SOUR IMM"); //mywrite(myfile[k], "TRIG:COUN MIN"); //mywrite(myfile[k], "SAMP:COUN MAX"); //mywrite(myfile[k], "SAMP:SOUR TIM"); //mywrite(myfile[k], "SAMP:TIM 1 ms"); //mywrite(myfile[k], "INIT"); //do //{ myread(myfile[k], "SYST:ERR?", err); error_message("- POWER: meter %d initialized with error status %s", k, err); //} while(strncmp(err,"+0",2) != 0); } } error_message("- detected %d power meters\n", k); debug_message("INIT2\n"); }
static char *OpenRequest( void ) { trap_elen bytes; BindHdl = PipeOpen( BINDERY ); if( BindHdl == -1 ) return( TRP_ERR_NMPBIND_not_found ); NameBuff[0] = OPEN_REQUEST; bytes = mywrite( BindHdl, NameBuff, strlen( NameBuff ) + 1 ); if( bytes == 0 ) return( TRP_ERR_NMPBIND_not_found ); bytes = myread( BindHdl, NameBuff, 1 ); if( bytes == 0 ) return( TRP_ERR_NMPBIND_not_found ); myclose( BindHdl ); return( NULL ); }