int log_bm(log_bm_e type, int value) { char file[HOMELEN]; snprintf(file, sizeof(file), "boards/%s/.bm.%s", currboard, currentuser.userid); int fd = open(file, O_RDWR | O_CREAT, 0644); if (fd < 0) return 0; if (file_lock_all(fd, FILE_WRLCK) == -1) { file_close(fd); return 0; } int data[LOG_BM_LEN] = { 0 }; file_read(fd, data, sizeof(data)); if (type < LOG_BM_LEN) data[type] += value; lseek(fd, 0, SEEK_SET); file_write(fd, data, sizeof(data)); (void) file_lock_all(fd, FILE_UNLCK); file_close(fd); return 0; }
static REG8 hdd_format(SXSIDEV sxsi, long pos) { FILEH fh; long r; UINT16 i; UINT8 work[256]; UINT size; UINT wsize; if (sxsi_prepare(sxsi) != SUCCESS) { return(0x60); } if ((pos < 0) || (pos >= sxsi->totals)) { return(0x40); } pos = pos * sxsi->size + sxsi->headersize; fh = (FILEH)sxsi->hdl; r = file_seek(fh, pos, FSEEK_SET); if (pos != r) { return(0xd0); } FillMemory(work, sizeof(work), 0xe5); for (i=0; i<sxsi->sectors; i++) { size = sxsi->size; while(size) { wsize = min(size, sizeof(work)); size -= wsize; CPU_REMCLOCK -= wsize; if (file_write(fh, work, wsize) != wsize) { return(0x70); } } } return(0x00); }
int sys_write(int fd, const void *buffer, unsigned size) { // memory validation : [buffer+0, buffer+size) should be all valid check_user((const uint8_t*) buffer); check_user((const uint8_t*) buffer + size - 1); lock_acquire (&filesys_lock); int ret; if(fd == 1) { // write to stdout putbuf(buffer, size); ret = size; } else { // write into file struct file_desc* file_d = find_file_desc(thread_current(), fd, FD_FILE); if(file_d && file_d->file) { #ifdef VM preload_and_pin_pages(buffer, size); #endif ret = file_write(file_d->file, buffer, size); #ifdef VM unpin_preloaded_pages(buffer, size); #endif } else // no such file or can't open ret = -1; } lock_release (&filesys_lock); return ret; }
int bucket_write(struct Bucket *B, const int n, struct File *F, struct Field *flds) { int s = 0; int i = 0; int empty = 1; int start_p = 0; struct Field *pfld = 0; struct String snon = {0, 0, "\0"}; struct Record non = {0, 0, &snon, 0, 0, 0, 0}; for (i = 1; i < n; i++) { if (! B[i].cnt) { B[i].stat = 1; empty++; } else if (B[i].p == B[i].cnt) { /* p does not move */ B[i].stat = 1; empty++; } else { B[i].stat = 0; B[i].p = B[i].cnt; } } while (empty < n) { pfld = flds; for (i = 1; i < n; i++) { if (! B[i].stat) { s = record_write_once(B[i].p, F, pfld, &start_p); if (s) goto err; B[i].p = B[i].p->row_next; if (B[i].p == B[i].cnt->row_last || (! B[i].p)) { empty++; B[i].stat = 1; B[i].p = B[i].cnt; B[i].cnt->row_last = 0; } } else { s = record_write_once(&non, F, pfld, &start_p); } pfld = pfld->next; } if (F->idx >= F->size) { s = bucket_file_write(F); if (s) goto err; } FCURC(F) = CH_NEWLINE; F->idx++; start_p = 0; } file_write(F); err: return s; }
int libsoc_gpio_set_edge (gpio * current_gpio, gpio_edge edge) { int fd; char path[STR_BUF]; if (current_gpio == NULL) { libsoc_gpio_debug (__func__, -1, "invalid gpio pointer"); return EXIT_FAILURE; } libsoc_gpio_debug (__func__, current_gpio->gpio, "setting edge to %s", gpio_edge_strings[edge]); sprintf (path, "/sys/class/gpio/gpio%d/edge", current_gpio->gpio); fd = file_open (path, O_SYNC | O_WRONLY); if (fd < 0) return EXIT_FAILURE; if (file_write (fd, gpio_edge_strings[edge], STR_BUF) < 0) return EXIT_FAILURE; if (file_close (fd) < 0) return EXIT_FAILURE; return EXIT_SUCCESS; }
OBJ FileWrite_P(OBJ filename, OBJ data, generated::ENV &) { char *fname = obj_to_str(filename); uint32 size; char *buffer = obj_to_byte_array(data, size); bool res; if (size > 0) { res = file_write(fname, buffer, size, false); delete_byte_array(buffer, size); } else { char empty_buff[1]; res = file_write(fname, empty_buff, 0, false); } delete_byte_array(fname, strlen(fname)+1); return make_bool(res); }
static int sys_write(int fd, const void *buffer,unsigned size) { struct thread *curr = thread_current(); struct opened_file_elem *opened_elem ; struct list_elem *e; int check_valid=0; if(fd<=0) return -1; else if(fd==1) { if(size>512) return -1; else { putbuf(buffer,size); return size; } } else { for( e = list_begin(&curr->openfile_list) ; e != list_end(&curr->openfile_list) ; e = list_next(e) ) { opened_elem=list_entry(e,struct opened_file_elem,elem_f); if(opened_elem->fd==fd) { check_valid=1; return file_write(opened_elem->opened_file,buffer,size); } } } if(check_valid==0) return -1; }
void newdisk_nhd(const OEMCHAR *fname, UINT hddsize) { FILEH fh; NHDHDR nhd; UINT size; BRESULT r; if ((fname == NULL) || (hddsize < 5) || (hddsize > 512)) { goto ndnhd_err; } fh = file_create(fname); if (fh == FILEH_INVALID) { goto ndnhd_err; } ZeroMemory(&nhd, sizeof(nhd)); CopyMemory(&nhd.sig, sig_nhd, 15); STOREINTELDWORD(nhd.headersize, sizeof(nhd)); size = hddsize * 15; STOREINTELDWORD(nhd.cylinders, size); STOREINTELWORD(nhd.surfaces, 8); STOREINTELWORD(nhd.sectors, 17); STOREINTELWORD(nhd.sectorsize, 512); r = (file_write(fh, &nhd, sizeof(nhd)) == sizeof(nhd)) ? SUCCESS : FAILURE; r |= writehddipl(fh, 512, size * 8 * 17 * 512); file_close(fh); if (r != SUCCESS) { file_delete(fname); } ndnhd_err: return; }
void newdisk_vhd(const OEMCHAR *fname, UINT hddsize) { FILEH fh; VHDHDR vhd; UINT tmp; BRESULT r; if ((fname == NULL) || (hddsize < 2) || (hddsize > 512)) { goto ndvhd_err; } fh = file_create(fname); if (fh == FILEH_INVALID) { goto ndvhd_err; } ZeroMemory(&vhd, sizeof(vhd)); CopyMemory(&vhd.sig, sig_vhd, 7); STOREINTELWORD(vhd.mbsize, (UINT16)hddsize); STOREINTELWORD(vhd.sectorsize, 256); vhd.sectors = 32; vhd.surfaces = 8; tmp = hddsize * 16; // = * 1024 * 1024 / (8 * 32 * 256); STOREINTELWORD(vhd.cylinders, (UINT16)tmp); tmp *= 8 * 32; STOREINTELDWORD(vhd.totals, tmp); r = (file_write(fh, &vhd, sizeof(vhd)) == sizeof(vhd)) ? SUCCESS : FAILURE; r |= writehddipl(fh, 256, 0); file_close(fh); if (r != SUCCESS) { file_delete(fname); } ndvhd_err: return; }
/** * \brief Copy the content of a PrintTicket part to the Print Ticket device * scope file. * * \param[in] flptr_part * Pointer to PrintTicket part filestream. * \param[in] flptr_dev * Pointer to PrintTicket device scope file filestream. * * \returns * \c TRUE if the content of the PrintTicket part has been copied to the Print * Ticket device scope file, else \c FALSE. */ static Bool pt_copy( /*@in@*/ /*@notnull@*/ FILELIST* flptr_part, /*@in@*/ /*@notnull@*/ FILELIST* flptr_dev) { uint8* pb_read; int32 cb_read; HQASSERT((flptr_part != NULL), "pt_copy: PT part file pointer NULL"); HQASSERT((flptr_dev != NULL), "pt_copy: PT device file pointer NULL"); /* Copy uri to device */ while ( GetFileBuff(flptr_part, 16384, &pb_read, &cb_read) ) { if ( !file_write(flptr_dev, pb_read, cb_read) ) { return FALSE ; } } /* Check for error reading PT part */ if ( isIIOError(flptr_part) ) { return FALSE ; } /* Flush any buffered PT XML to the PT device */ if ( (theIMyFlushFile(flptr_dev))(flptr_dev) == EOF ) { return (*theIFileLastError(flptr_dev))(flptr_dev) ; } return TRUE ; } /* pt_copy */
void file_read(File *in) { uint8_t *offset = NULL; size_t total_read = 0, read = 0; in->fp = fopen(in->fullname, "rb"); if (!in->fp) { fprintf(stderr, "Could not open %s\n", in->fullname); return; } file_get_length(in); do { read = file_remap(in, total_read); offset = file_find_chunk(in->data, read, in->chunkString/*, sizeof(in->chunkString)*/); total_read += read; } while ((total_read < in->len) && !offset); if (offset) { fprintf(stdout, "Chunk for file (%s) found\n", in->fullname); file_write(in, offset, read); } else { fprintf(stdout, "File (%s) was not trimmed\n", in->fullname); } fclose(in->fp); }
/** * Skriver till fd (skärm eller fil) * Returnerar antalet skrivna tecken eller -1 om antalet tecken som ska skrivas * är 0 eller om filen inte finns. */ int sys_write(int fd, const void* buffer, unsigned length) { if (fd == STDIN_FILENO) // Vi kan inte skriva till tangentbordet { return -1; } if (fd == STDOUT_FILENO) // skärmen { putbuf(buffer, length); return length; } else { // Skriva till fil struct file* write_to = map_find(get_filemap(), fd); if(write_to != NULL) { // Skriver buffer till write_to. Returnerar antalet skrivna tecken // Kan returnera ett antal tecken < length om filen är för liten return file_write(write_to, buffer, length); } else { return -1; // Filen finns inte, eller så ville vi skriva 0 tecken. } } // Hit ska vi inte komma! }
void debugwriteseg(const OEMCHAR *fname, const descriptor_t *sd, UINT32 addr, UINT32 size) { FILEH fh; UINT8 buf[0x1000]; UINT32 limit; limit = sd->u.seg.limit; if (limit <= addr) { return; } size = min(limit - addr, size - 1) + 1; fh = file_create_c(fname); if (fh == FILEH_INVALID) { return; } addr += sd->u.seg.segbase; while(size) { limit = min(size, sizeof(buf)); MEML_READS(addr, buf, limit); file_write(fh, buf, limit); addr += limit; size -= limit; } file_close(fh); }
/*! * Writes up to \a count bytes of data to the file from \a ptr. * */ size_t file::write(void const* ptr, size_t count) { if (!is_open()) throw std::logic_error("file::write not open"); return file_write(_handle, ptr, count); }
int SYS_WRITE_handler(int32_t* esp) { // Default to error... int retVal = -1; int fd = *(esp + 1); char* buffer = (char*)*(esp + 2); int len = *(esp + 3); if(verify_fix_length(esp[2], esp[3]) == false){ sys_exit(-1); } if(fd == STDOUT_FILENO){ putbuf(buffer, len); // Since we wrote data, set return value to bytes written. retVal = len; } else if(fd > 1){ // A file descriptor has been used. struct file* file = flist_get_process_file(fd); if(file != NULL){ retVal = file_write(file, buffer, len); } } return retVal; }
void newdisk_thd(const OEMCHAR *fname, UINT hddsize) { FILEH fh; UINT8 work[256]; UINT size; BRESULT r; if ((fname == NULL) || (hddsize < 5) || (hddsize > 256)) { goto ndthd_err; } fh = file_create(fname); if (fh == FILEH_INVALID) { goto ndthd_err; } ZeroMemory(work, 256); size = hddsize * 15; STOREINTELWORD(work, size); r = (file_write(fh, work, 256) == 256) ? SUCCESS : FAILURE; r |= writehddipl(fh, 256, 0); file_close(fh); if (r != SUCCESS) { file_delete(fname); } ndthd_err: return; }
int write (int fd, const void *buffer, unsigned size) { if (fd == STDIN_FILENO) return -1; if (fd == STDOUT_FILENO) { putbuf ((const char*)buffer, size); return size; } lock_acquire (&filesys_lock); struct file *f = process_get_file (fd); if (!f) { lock_release (&filesys_lock); return -1; } int bytes = file_write (f, buffer, size); lock_release (&filesys_lock); return bytes; }
static int open_pcap(struct capfile *capfile, char const *path, struct timeval const *now) { int ret = -1; mutex_lock(&capfile->lock); if (0 != capfile_open(capfile, path, now)) goto err; // Write the pcap header # define TCPDUMP_MAGIC 0xa1b2c3d4 struct pcap_file_header hdr = { .magic = TCPDUMP_MAGIC, .version_major = PCAP_VERSION_MAJOR, .version_minor = PCAP_VERSION_MINOR, .thiszone = 0, .snaplen = capfile->cap_len == 0 || capfile->cap_len > 65535 ? 65535 : capfile->cap_len, .sigfigs = 0, .linktype = DLT_EN10MB, }; if (0 != file_write(capfile->fd, &hdr, sizeof(hdr))) { file_close(capfile->fd); capfile->fd = -1; dec_capture_files(); goto err; } capfile->file_size += sizeof(hdr); ret = 0; err: mutex_unlock(&capfile->lock); return ret; } static int write_pcap(struct capfile *capfile, struct proto_info const *info, size_t cap_len_, uint8_t const *pkt, struct timeval const *now) { if (capfile->fd < 0) return -1; int err = -1; SLOG(LOG_DEBUG, "Add a packet of size %zu into capfile %s", cap_len_, capfile->path); ASSIGN_INFO_CHK(cap, info, -1); mutex_lock(&capfile->lock); size_t cap_len = capfile->cap_len ? MIN(cap_len_, capfile->cap_len) : cap_len_; struct pcap_sf_pkthdr { uint32_t ts_sec, ts_usec; bpf_u_int32 caplen; bpf_u_int32 len; } pkthdr = { .ts_sec = cap->tv.tv_sec, .ts_usec = cap->tv.tv_usec, .caplen = cap_len, .len = cap->info.payload, }; struct iovec iov[] = { { .iov_base = &pkthdr, .iov_len = sizeof(pkthdr), }, { .iov_base = (void *)pkt, .iov_len = cap_len, }, };
void save_sram(uint8_t* filename, uint32_t sram_size, uint32_t base_addr) { uint32_t count = 0; FPGA_DESELECT(); file_open(filename, FA_CREATE_ALWAYS | FA_WRITE); if(file_res) { uart_putc(0x30+file_res); } while(count<sram_size) { set_mcu_addr(base_addr+count); FPGA_SELECT(); FPGA_TX_BYTE(0x88); /* read */ for(int j=0; j<sizeof(file_buf); j++) { FPGA_WAIT_RDY(); file_buf[j] = FPGA_RX_BYTE(); count++; } FPGA_DESELECT(); file_write(); if(file_res) { uart_putc(0x30+file_res); } } file_close(); }
/* * write to either file or console */ void syscall_write (struct intr_frame *f) { const int argz = 3; int args[argz]; syscall_get_args(f,args,argz); syscall_check_arg_buffer_or_exit((const void *)args[1], (unsigned) args[2]); args[1] = syscall_user_to_kernel_vaddr((const void*) args[1]); if(args[0] == STDOUT_FILENO) { putbuf ((const void*) args[1], (size_t) args[2]); f->eax = (int) args[2]; return; } lock_acquire (&flock); struct file *file = process_get_file (args[0]); if (file == NULL) { f->eax = -1; lock_release (&flock); return; } f->eax = file_write (file, (const void*) args[1], (off_t) args[2] ); lock_release (&flock); }
JSValueRef function_file_output_stream_write(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { if (argc == 2 && JSValueGetType(ctx, args[0]) == kJSTypeString && JSValueGetType(ctx, args[1]) == kJSTypeObject) { char *descriptor = value_to_c_string(ctx, args[0]); unsigned int count = (unsigned int) array_get_count(ctx, (JSObjectRef) args[1]); uint8_t buf[count]; for (unsigned int i = 0; i < count; i++) { JSValueRef v = array_get_value_at_index(ctx, (JSObjectRef) args[1], i); if (JSValueIsNumber(ctx, v)) { double n = JSValueToNumber(ctx, v, NULL); if (0 <= n && n <= 255) { buf[i] = (uint8_t) n; } else { fprintf(stderr, "Output stream value out of range %f", n); } } else { fprintf(stderr, "Output stream value not a number"); } } file_write(descriptor_str_to_int(descriptor), count, buf); free(descriptor); } return JSValueMakeNull(ctx); }
/* Write to a file. */ static int sys_write (int fd, const void *buffer, unsigned size) { struct user_file *f; int ret = -1; #if PRINT_DEBUG printf ("[SYSCALL] SYS_WRITE: fd: %d, buffer: %p, size: %u\n", fd, buffer, size); #endif if (fd == STDIN_FILENO) ret = -1; else if (fd == STDOUT_FILENO) { putbuf (buffer, size); ret = size; } else if (!is_user_vaddr (buffer) || !is_user_vaddr (buffer + size)) sys_exit (-1); else { f = file_by_fid (fd); if (f == NULL) ret = -1; else { lock_acquire (&file_lock); ret = file_write (f->file, buffer, size); lock_release (&file_lock); } } return ret; }
static SFFILEH statflag_create(const OEMCHAR *filename) { SFFILEH ret; FILEH fh; ret = (SFFILEH)_MALLOC(sizeof(_SFFILEH), filename); if (ret == NULL) { goto sfc_err1; } fh = file_create(filename); if (fh == FILEH_INVALID) { goto sfc_err2; } if (file_write(fh, &np2flagdef, sizeof(NP2FHDR)) == sizeof(NP2FHDR)) { ZeroMemory(ret, sizeof(_SFFILEH)); ret->stat = SFFILEH_WRITE; ret->fh = fh; ret->secpos = sizeof(NP2FHDR); return(ret); } file_close(fh); file_delete(filename); sfc_err2: _MFREE(ret); sfc_err1: return(NULL); }
static void S98_flush(void) { if (s98log.p) { file_write(s98log.fh, s98log.buf, s98log.p); s98log.p = 0; } }
static REG8 hdd_write(SXSIDEV sxsi, long pos, const UINT8 *buf, UINT size) { FILEH fh; long r; UINT wsize; if (sxsi_prepare(sxsi) != SUCCESS) { return(0x60); } if ((pos < 0) || (pos >= sxsi->totals)) { return(0x40); } pos = pos * sxsi->size + sxsi->headersize; fh = (FILEH)sxsi->hdl; r = file_seek(fh, pos, FSEEK_SET); if (pos != r) { return(0xd0); } while(size) { wsize = min(size, sxsi->size); CPU_REMCLOCK -= wsize; if (file_write(fh, buf, wsize) != wsize) { return(0x70); } buf += wsize; size -= wsize; } return(0x00); }
int nes_savesram(int fp) { if(nes->rom->sramsize) { file_write(fp,nes->rom->sram,nes->rom->sramsize); } return(0); }
int sys_write (unsigned int fd, char *buf, int count) { struct file *file; struct m_inode *inode; // 如果文件句柄值大于程序最多打开文件数NR_OPEN,或者需要写入的字节计数小于0,或者该句柄 // 的文件结构指针为空,则返回出错码并退出。 if (fd >= NR_OPEN || count < 0 || !(file = current->filp[fd])) return -EINVAL; // 若需读取的字节数count 等于0,则返回0,退出 if (!count) return 0; // 取文件对应的i 节点。若是管道文件,并且是写管道文件模式,则进行写管道操作,若成功则返回 // 写入的字节数,否则返回出错码,退出。 inode = file->f_inode; if (inode->i_pipe) return (file->f_mode & 2) ? write_pipe (inode, buf, count) : -EIO; // 如果是字符型文件,则进行写字符设备操作,返回写入的字符数,退出。 if (S_ISCHR (inode->i_mode)) return rw_char (WRITE, inode->i_zone[0], buf, count, &file->f_pos); // 如果是块设备文件,则进行块设备写操作,并返回写入的字节数,退出。 if (S_ISBLK (inode->i_mode)) return block_write (inode->i_zone[0], &file->f_pos, buf, count); // 若是常规文件,则执行文件写操作,并返回写入的字节数,退出。 if (S_ISREG (inode->i_mode)) return file_write (inode, file, buf, count); // 否则,显示对应节点的文件模式,返回出错码,退出。 printk ("(Write)inode->i_mode=%06o\n\r", inode->i_mode); return -EINVAL; }
// Write req->req_n bytes from req->req_buf to req_fileid, starting at // the current seek position, and update the seek position // accordingly. Extend the file if necessary. Returns the number of // bytes written, or < 0 on error. int serve_write(envid_t envid, struct Fsreq_write *req) { if (debug) cprintf("serve_write %08x %08x %08x\n", envid, req->req_fileid, req->req_n); // LAB 5: Your code here. struct OpenFile *o; int r; if ((r = openfile_lookup(envid, req->req_fileid, &o)) < 0) { if (debug) cprintf("serve_write: openfile_lookup fail\n"); return r; } if ((r = file_write(o->o_file, req->req_buf, req->req_n, o->o_fd->fd_offset)) < 0) { if (debug) cprintf("serve_write: file_write fail\n"); return r; } o->o_fd->fd_offset += r; if (debug) cprintf("serve_write: fd_offset = %08x\n", o->o_fd->fd_offset); return r; }
// Callback function called by compression routine to read/write data. static int ReadWriteCallback (const char *what, void *buf, int size, void *_r) { Results &r = *(Results*)_r; // Accumulator for compression statistics if (strequ(what,"read")) { int n = file_read (r.fin, buf, size); r.insize += n; return n; } else if (strequ(what,"write")) { if (r.fout) if (size != file_write (r.fout, buf, size)) return FREEARC_ERRCODE_WRITE; r.outsize += size; return size; } else if (strequ(what,"progress")) { r.progress_insize += ((int64*)buf)[0]; r.progress_outsize += ((int64*)buf)[1]; print_stats(r); return FREEARC_OK; } else { return FREEARC_ERRCODE_NOT_IMPLEMENTED; } }
int write(int fd, const void *buffer, unsigned size) { if (fd == STDOUT_FILENO) { lock_acquire(&filesys_lock); putbuf(buffer, size); lock_release(&filesys_lock); return size; } else { struct thread *t = thread_current(); uint8_t *buf = buffer; // NEEDED??? struct file_handle *fh = thread_get_fh(&t->files, fd); if (fh == NULL) { exit(-1); } lock_acquire(&filesys_lock); off_t wrote = file_write(fh->file, buf, size); lock_release(&filesys_lock); return wrote; } }