globle void GetSeekCurBinary( void *theEnv, long offset) { #if WIN_BTC lseek(SystemDependentData(theEnv)->BinaryFileHandle,offset,SEEK_CUR); #endif #if WIN_MVC _lseek(SystemDependentData(theEnv)->BinaryFileHandle,offset,SEEK_CUR); #endif #if (! WIN_BTC) && (! WIN_MVC) fseek(SystemDependentData(theEnv)->BinaryFP,offset,SEEK_CUR); #endif }
globle void GenTellBinary( void *theEnv, long *offset) { #if WIN_BTC *offset = lseek(SystemDependentData(theEnv)->BinaryFileHandle,0,SEEK_CUR); #endif #if WIN_MVC *offset = _lseek(SystemDependentData(theEnv)->BinaryFileHandle,0,SEEK_CUR); #endif #if (! WIN_BTC) && (! WIN_MVC) *offset = ftell(SystemDependentData(theEnv)->BinaryFP); #endif }
const RC AllocatePage(PF_FileHandle *fileHandle,PF_PageHandle *pageHandle) { PF_PageHandle *pPageHandle=pageHandle; RC tmp; int i,byte,bit; fileHandle->pHdrFrame->bDirty=true; if((fileHandle->pFileSubHeader->nAllocatedPages)<=(fileHandle->pFileSubHeader->pageCount)){ for(i=0;i<=fileHandle->pFileSubHeader->pageCount;i++){ byte=i/8; bit=i%8; if(((fileHandle->pBitmap[byte])&(1<<bit))==0){ (fileHandle->pFileSubHeader->nAllocatedPages)++; fileHandle->pBitmap[byte]|=(1<<bit); break; } } if(i<=fileHandle->pFileSubHeader->pageCount) return GetThisPage(fileHandle,i,pageHandle); } fileHandle->pFileSubHeader->nAllocatedPages++; fileHandle->pFileSubHeader->pageCount++; byte=fileHandle->pFileSubHeader->pageCount/8; bit=fileHandle->pFileSubHeader->pageCount%8; fileHandle->pBitmap[byte]|=(1<<bit); if((tmp=AllocateBlock(&(pPageHandle->pFrame)))!=SUCCESS){ return tmp; } pPageHandle->pFrame->bDirty=false; pPageHandle->pFrame->fileDesc=fileHandle->fileDesc; pPageHandle->pFrame->fileName=fileHandle->fileName; pPageHandle->pFrame->pinCount=1; pPageHandle->pFrame->accTime=clock(); memset(&(pPageHandle->pFrame->page),0,sizeof(Page)); pPageHandle->pFrame->page.pageNum=fileHandle->pFileSubHeader->pageCount; if(_lseek(fileHandle->fileDesc,0,SEEK_END)==-1){ bf_manager.allocated[pPageHandle->pFrame-bf_manager.frame]=false; return PF_FILEERR; } if(_write(fileHandle->fileDesc,&(pPageHandle->pFrame->page),sizeof(Page))!=sizeof(Page)){ bf_manager.allocated[pPageHandle->pFrame-bf_manager.frame]=false; return PF_FILEERR; } return SUCCESS; }
int __cdecl _fseek_nolock( FILE* str, long offset, int whence ) { REG1 FILE* stream; /* Init stream pointer */ stream = str; if (!inuse(stream)) { errno = EINVAL; return (-1); } /* Clear EOF flag */ stream->_flag &= ~_IOEOF; /* If seeking relative to current location, then convert to a seek relative to beginning of file. This accounts for buffering, etc. by letting fseek() tell us where we are. */ if (whence == SEEK_CUR) { offset += _ftell_nolock(stream); whence = SEEK_SET; } /* Flush buffer as necessary */ _flush(stream); /* If file opened for read/write, clear flags since we don't know what the user is going to do next. If the file was opened for read access only, decrease _bufsiz so that the next _filbuf won't cost quite so much */ if (stream->_flag & _IORW) { stream->_flag &= ~(_IOWRT | _IOREAD); } else if ((stream->_flag & _IOREAD) && (stream->_flag & _IOMYBUF) && !(stream->_flag & _IOSETVBUF)) { stream->_bufsiz = _SMALL_BUFSIZ; } /* Seek to the desired locale and return. */ return (_lseek(_fileno(stream), offset, whence) == -1L ? -1 : 0); }
static int dfs_win32_write(struct dfs_fd *file, const void *buf, rt_size_t len) { int fd; int char_write; fd = (int)(file->data); char_write = _write(fd, buf, len); if (char_write < 0) return win32_result_to_dfs(GetLastError()); /* update position */ file->pos = _lseek(fd, 0, SEEK_CUR); return char_write; }
void write(rfs_fpos_t pos, size_t len, const void *in) { if (!writeok) base.throwError(EACCES,"invalid mode for write"); if (tempfile==-1) tempfile = createTempFile(base,tempfilename); long ret = (long)_lseek(tempfile,(long)pos,SEEK_SET); if (ret!=pos) base.throwError(errno,"write.1"); int wr = _write(tempfile,in,len); if (wr==-1) base.throwError(errno,"write.2"); if (wr!=len) // disk full base.throwError(ENOSPC,"write.3"); if (pos+wr>savesize) savesize = pos+wr; }
Long FileHandler::Seek(FileDescriptor fd, Long offset, Int origin) { Long pos; pos = _lseek(fd.fd, offset, origin); if(pos == -1L) { ///////////////////////////////////////// //Error ///////////////////////////////////////// return EOF; } else { return pos; } }
/*@C PetscBinarySeek - Moves the file pointer on a PETSc binary file. Not Collective Input Parameters: + fd - the file . off - number of bytes to move. Use PETSC_BINARY_INT_SIZE, PETSC_BINARY_SCALAR_SIZE, etc. in your calculation rather than sizeof() to compute byte lengths. - whence - if PETSC_BINARY_SEEK_SET then off is an absolute location in the file if PETSC_BINARY_SEEK_CUR then off is an offset from the current location if PETSC_BINARY_SEEK_END then off is an offset from the end of file Output Parameter: . offset - new offset in file Level: developer Notes: Integers are stored on the file as 32 long, regardless of whether they are stored in the machine as 32 or 64, this means the same binary file may be read on any machine. Hence you CANNOT use sizeof() to determine the offset or location. Concepts: files^binary seeking Concepts: binary files^seeking .seealso: PetscBinaryRead(), PetscBinaryWrite(), PetscBinaryOpen(), PetscBinarySynchronizedWrite(), PetscBinarySynchronizedRead(), PetscBinarySynchronizedSeek() @*/ PetscErrorCode PetscBinarySeek(int fd,off_t off,PetscBinarySeekType whence,off_t *offset) { int iwhence = 0; PetscFunctionBegin; if (whence == PETSC_BINARY_SEEK_SET) iwhence = SEEK_SET; else if (whence == PETSC_BINARY_SEEK_CUR) iwhence = SEEK_CUR; else if (whence == PETSC_BINARY_SEEK_END) iwhence = SEEK_END; else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown seek location"); #if defined(PETSC_HAVE_LSEEK) *offset = lseek(fd,off,iwhence); #elif defined(PETSC_HAVE__LSEEK) *offset = _lseek(fd,(long)off,iwhence); #else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"System does not have a way of seeking on a file"); #endif PetscFunctionReturn(0); }
char *LoadShader (char* fileName) //, char* Shadersource) { FILE *fp; char *bufP = NULL; int fSize; #ifdef _WIN32 int f; #endif char fn [FILENAME_LEN]; if (!(fileName && *fileName)) return NULL; // no fileName sprintf (fn, "%s%s%s", gameFolders.szShaderDir, *gameFolders.szShaderDir ? "/" : "", fileName); #ifdef _WIN32 if (0 > (f = _open (fn, _O_RDONLY))) return NULL; // couldn't open file fSize = _lseek (f, 0, SEEK_END); _close (f); if (fSize <= 0) return NULL; // empty file or seek error #endif if (!(fp = fopen (fn, "rt"))) return NULL; // couldn't open file #ifndef _WIN32 fseek (fp, 0, SEEK_END); fSize = ftell (fp); if (fSize <= 0) { fclose (fp); return NULL; // empty file or seek error } #endif if (!(bufP = new char [fSize + 1])) { fclose (fp); return NULL; // out of memory } fSize = (int) fread (bufP, sizeof (char), fSize, fp); bufP [fSize] = '\0'; fclose (fp); return bufP; }
int _write(int file, char *ptr, int len) { if (len == 0) return 0; if (file == 0 || file == 1) { usbSend(ptr, len); return len; } else if (file < 10) { unsigned int port = UART_FD[file]; if (port) { int totalSent = 0; while (len > 0) { int sent = sendUART(port, ptr, len); len -= sent; ptr += sent; totalSent += sent; } return totalSent; } else { return -1; } } else { int fd = file-10; if (fd < fatFilesOpen) { unsigned int res; if (fatFiles[fd].append) { _lseek(file, 0, SEEK_END); } FRESULT rr = f_write(&fatFiles[fd].fil, ptr, len, &res); if (rr) { fiprintf(stderr, "Failed to write FAT file %d (bytes: %d): %d\n\r", fd, len, rr); errno = EINVAL; return -1; } else { errno = 0; return res; } } } errno = EINVAL; return -1; }
int LoadMUI(char* filename) { int res = 0; int v1=0; char* fn = filename?filename:MCFilePath(mclg_deffile); int f; if (fn && (f=_open(fn,A_ReadOnly+A_BIN,P_READ,&err))!=-1) { MCLG_hdr hdr; if (_read(f, &hdr, sizeof(hdr), &err)==sizeof(hdr)) { if (hdr.sig==mclg_sig) { _lseek(f,hdr.offset,S_SET,&err,&err); int tblcn=(hdr.size & 0xffff); int tblsz=tblcn*sizeof(MC_LG); int bufsz=hdr.size>>16; FreeMUI(); muibuff=malloc(bufsz); muibuffsz=bufsz; MC_LG* tbl; MC_LG* lg; lg=tbl=malloc(tblsz); res = (_read(f, tbl, tblsz, &err)==tblsz) && (_read(f, muibuff, bufsz, &err)==bufsz); if (res) { LockSched(); for(int cc=0;cc<tblcn;cc++) { int ind=getmuiind(lg->id); mui_ld[ind]=(char*)((int)muibuff + lg->ofs); lg=(MC_LG*)((int)lg+sizeof(MC_LG)); } UnlockSched(); } else mfree(muibuff); mfree(tbl); } else v1=1;
void __cdecl rewind ( FILE *str ) { REG1 FILE *stream; REG2 int fd; _VALIDATE_RETURN_VOID( (str != NULL), EINVAL); /* Init stream pointer */ stream = str; fd = _fileno(stream); /* Lock the file */ _lock_str(stream); __try { /* Flush the stream */ _flush(stream); /* Clear errors */ stream->_flag &= ~(_IOERR|_IOEOF); _osfile_safe(fd) &= ~(FEOFLAG); /* Set flags */ /* [note: _flush set _cnt=0 and _ptr=_base] */ if (stream->_flag & _IORW) stream->_flag &= ~(_IOREAD|_IOWRT); /* Position to beginning of file */ if(_lseek(fd,0L,0)==-1) { stream->_flag |= _IOERR; } } __finally { /* unlock stream */ _unlock_str(stream); } }
static int dfs_win32_seek(struct dfs_fd *file, rt_off_t offset) { int result; /* set offset as current offset */ if (file->type == FT_DIRECTORY) { return -DFS_STATUS_ENOSYS; } else if (file->type == FT_REGULAR) { result = _lseek((int)(file->data), offset, SEEK_SET); if (result >= 0) return offset; } return win32_result_to_dfs(GetLastError()); }
static int shaderSize(char *fileName, EShaderType shaderType) { int fd; char name[100]; int count = -1; strcpy(name, fileName); switch (shaderType) { case EVertexShader: strcat(name, ".vert"); break; case EFragmentShader: strcat(name, ".frag"); break; default: printf("ERROR: unknown shader file type\n"); exit(1); break; } // // Open the file, seek to the end to find its length // #ifdef WIN32 /*[*/ fd = _open(name, _O_RDONLY); if (fd != -1) { count = _lseek(fd, 0, SEEK_END) + 1; _close(fd); } #else /*][*/ fd = open(name, O_RDONLY); if (fd != -1) { count = lseek(fd, 0, SEEK_END) + 1; close(fd); } #endif /*]*/ return count; }
const RC ForceAllPages(PF_FileHandle *fileHandle) { int i,offset; for(i=0;i<PF_BUFFER_SIZE;i++){ if(bf_manager.allocated[i]==false) continue; if(strcmp(bf_manager.frame[i].fileName,fileHandle->fileName)!=0) continue; if(bf_manager.frame[i].bDirty==true){ offset=(bf_manager.frame[i].page.pageNum)*sizeof(Page); if(_lseek(fileHandle->fileDesc,offset,SEEK_SET)==offset-1) return PF_FILEERR; if(_write(fileHandle->fileDesc,&(bf_manager.frame[i].page),sizeof(Page))!=sizeof(Page)) return PF_FILEERR; } bf_manager.allocated[i]=false; } return SUCCESS; }
const RC GetThisPage(PF_FileHandle *fileHandle,PageNum pageNum,PF_PageHandle *pageHandle) { int i,nread,offset; RC tmp; PF_PageHandle *pPageHandle=pageHandle; if(pageNum>fileHandle->pFileSubHeader->pageCount) return PF_INVALIDPAGENUM; if((fileHandle->pBitmap[pageNum/8]&(1<<(pageNum%8)))==0) return PF_INVALIDPAGENUM; pPageHandle->bOpen=true; for(i=0;i<PF_BUFFER_SIZE;i++){ if(bf_manager.allocated[i]==false) continue; if(strcmp(bf_manager.frame[i].fileName,fileHandle->fileName)!=0) continue; if(bf_manager.frame[i].page.pageNum==pageNum){ pPageHandle->pFrame=bf_manager.frame+i; pPageHandle->pFrame->pinCount++; pPageHandle->pFrame->accTime=clock(); return SUCCESS; } } if((tmp=AllocateBlock(&(pPageHandle->pFrame)))!=SUCCESS){ return tmp; } pPageHandle->pFrame->bDirty=false; pPageHandle->pFrame->fileDesc=fileHandle->fileDesc; pPageHandle->pFrame->fileName=fileHandle->fileName; pPageHandle->pFrame->pinCount=1; pPageHandle->pFrame->accTime=clock(); offset=pageNum*sizeof(Page); if(_lseek(fileHandle->fileDesc,offset,SEEK_SET)==offset-1){ bf_manager.allocated[pPageHandle->pFrame-bf_manager.frame]=false; return PF_FILEERR; } if((nread=read(fileHandle->fileDesc,&(pPageHandle->pFrame->page),sizeof(Page)))!=sizeof(Page)){ bf_manager.allocated[pPageHandle->pFrame-bf_manager.frame]=false; return PF_FILEERR; } return SUCCESS; }
int fseek(FILE *f, int pos, int how) { int adjust = 0; if (how != SEEK_SET && how != SEEK_CUR && how != SEEK_END) { errno = EINVAL; return -1; } if (SEEK_CUR == how && (f->mode & _IOACC) != _IONBF) { adjust = f->end - f->ptr; if (_FREAD == f->last) { adjust = -adjust; if (f->ch != EOF) adjust--; } } if (fflush(f) < 0) return -1; if ((pos = _lseek(f->fd, pos + adjust, how)) < 0) { errno = EIO; return -1; } return 0; }
off_t ftello(FILE *stream) { long result; int adjust = 0; if (io_testflag(stream,_IOREADING)) adjust = -stream->_count; else if (io_testflag(stream,_IOWRITING) && stream->_buf && !io_testflag(stream,_IONBF)) adjust = stream->_ptr - stream->_buf; else adjust = 0; result = _lseek(fileno(stream), (off_t)0, SEEK_CUR); if ( result == -1 ) return result; result += (long) adjust; return result; }
static int alock_grab_lock ( int fd, int slot ) { int res; #if defined( HAVE_LOCKF ) res = lseek (fd, (off_t) (ALOCK_SLOT_SIZE * slot), SEEK_SET); if (res == -1) return -1; res = lockf (fd, F_LOCK, (off_t) ALOCK_SLOT_SIZE); #elif defined( HAVE_FCNTL ) struct flock lock_info; (void) memset ((void *) &lock_info, 0, sizeof (struct flock)); lock_info.l_type = F_WRLCK; lock_info.l_whence = SEEK_SET; lock_info.l_start = (off_t) (ALOCK_SLOT_SIZE * slot); lock_info.l_len = (off_t) ALOCK_SLOT_SIZE; res = fcntl (fd, F_SETLKW, &lock_info); #elif defined( _WIN32 ) if( _lseek( fd, (ALOCK_SLOT_SIZE * slot), SEEK_SET ) < 0 ) return -1; /* * _lock will try for the lock once per second, returning EDEADLOCK * after ten tries. We just loop until we either get the lock * or some other error is returned. */ while((res = _locking( fd, _LK_LOCK, ALOCK_SLOT_SIZE )) < 0 ) { if( errno != EDEADLOCK ) break; } #else # error alock needs lockf, fcntl, or _locking #endif if (res == -1) { assert (errno != EDEADLK); return -1; } return 0; }
signed int ResourceManager::pointToFile( int id) { char found; int j; int i; found = 0; for ( i = 0; ; ++i ) { if ( i < NUM_AGG_FILES ) { if ( this->aggContentInfo[i] ) { for ( j = 0; this->numberOfFilesInAGG[i] > j; ++j ) { if ( this->aggContentInfo[i][j].id == id ) { found = 1; this->curHandleIdx = i; break; } } } if ( !found ) continue; } break; } if ( !found ) { sprintf( globBuf, "ResMgr::PointToFile failure! ThisFileId:%d LastFileId:%d LastFileName:%s", id, this->fileID, this->resourceToLoad); terminate(globBuf); } return _lseek(this->fileDescriptors[this->curHandleIdx], this->aggContentInfo[this->curHandleIdx][j].off, SEEK_SET); }
HANDLE GGPROTO::dccfileallow(HANDLE hTransfer, const PROTOCHAR* szPath) { struct gg_dcc *dcc = (struct gg_dcc *) hTransfer; char fileName[MAX_PATH], *path = mir_t2a(szPath); strncpy(fileName, path, sizeof(fileName)); strncat(fileName, (char*)dcc->file_info.filename, sizeof(fileName) - strlen(fileName)); dcc->folder = _strdup((char *) path); dcc->tick = 0; mir_free(path); // Remove transfer from waiting list gg_EnterCriticalSection(&ft_mutex, "dccfileallow", 38, "ft_mutex", 1); list_remove(&transfers, dcc, 0); gg_LeaveCriticalSection(&ft_mutex, "dccfileallow", 38, 1, "ft_mutex", 1); // Open file for appending and check if ok if ((dcc->file_fd = _open(fileName, _O_WRONLY | _O_APPEND | _O_BINARY | _O_CREAT, _S_IREAD | _S_IWRITE)) == -1) { debugLogA("dccfileallow(): Failed to create file \"%s\". errno=%d: %s", fileName, errno, strerror(errno)); TCHAR error[512]; mir_sntprintf(error, SIZEOF(error), TranslateT("Cannot create transfer file. ERROR: %d: %s (dcc)\n%s"), errno, _tcserror(errno), szPath); showpopup(m_tszUserName, error, GG_POPUP_ERROR); ProtoBroadcastAck(dcc->contact, ACKTYPE_FILE, ACKRESULT_FAILED, dcc, 0); // Free transfer gg_free_dcc(dcc); return 0; } // Put an offset to the file dcc->offset = _lseek(dcc->file_fd, 0, SEEK_END); // Add to watches and start transfer gg_EnterCriticalSection(&ft_mutex, "dccfileallow", 39, "ft_mutex", 1); list_add(&watches, dcc, 0); gg_LeaveCriticalSection(&ft_mutex, "dccfileallow", 39, 1, "ft_mutex", 1); debugLogA("dccfileallow(): Receiving file \"%s\" from %d.", dcc->file_info.filename, dcc->peer_uin); return hTransfer; }
DWORD LoadRawFromDataFile(CompressedFile *TheFileNode, BYTE *UncompressedBuffer, int FileNum, BYTE *CompressedBuffer) { //Load up temporary data if not passed in bool TempBuffer=false, TempFile=false; const int TenMegs=10*1024*1024; if(!CompressedBuffer) { TempBuffer=true; CompressedBuffer=new BYTE[TenMegs]; } if(!FileNum) { TempFile=true; FileNum=_open(DataFileName, _O_RDONLY|_O_BINARY); } //Read in compressed data _lseek(FileNum, TheFileNode->FileStart, SEEK_SET); _read(FileNum, CompressedBuffer, TheFileNode->FileSize); //Close temporary file if(TempFile) _close(FileNum); //Uncompress data z_stream TheStream={CompressedBuffer, TheFileNode->FileSize, 0, UncompressedBuffer, TenMegs, NULL, NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL}; if(inflateInit2_(&TheStream, -MAX_WBITS, (const char*)"1.2.1", sizeof(TheStream)) || inflate(&TheStream, Z_FINISH)!=1) { if(CompressedBuffer) delete[] CompressedBuffer; return 0; } inflateEnd(&TheStream); //Delete temporary buffer if(TempBuffer) delete[] CompressedBuffer; return TheStream.total_out; //Return uncompressed data size }
int CFileTar::UnTar(char *TarFile, int index, char *fpath) { TarIndex ti; if(GetFileInfo(TarFile,&ti,index)) return 1; int fdin,fdout; if((fdin=_open(TarFile,_O_BINARY|_O_RDONLY))<0) { _close(fdin); return 1; } if((fdout=_open(fpath,_O_CREAT|_O_WRONLY|_O_BINARY|_O_TRUNC, _S_IWRITE))<0) { _close(fdin); _close(fdout); return 1; } _lseek(fdin,ti.Start,SEEK_SET); long rem; char buff[NBUFFSIZE]; rem=ti.Size; while(rem > NBUFFSIZE) { _read(fdin,buff,NBUFFSIZE); _write(fdout,buff,NBUFFSIZE); rem -= NBUFFSIZE; } if(rem>0) { _read(fdin,buff,rem); _write(fdout,buff,rem); } _close(fdin); _close(fdout); return 0; }
const RC ForcePage(PF_FileHandle *fileHandle,PageNum pageNum) { int i; for(i=0;i<PF_BUFFER_SIZE;i++){ if(bf_manager.allocated[i]==false) continue; if(strcmp(bf_manager.frame[i].fileName,fileHandle->fileName)!=0) continue; if(bf_manager.frame[i].page.pageNum==pageNum){ if(bf_manager.frame[i].pinCount!=0) return PF_PAGEPINNED; if(bf_manager.frame[i].bDirty==true){ if(_lseek(fileHandle->fileDesc,pageNum*PF_PAGESIZE,SEEK_SET)<0) return PF_FILEERR; if(_write(fileHandle->fileDesc,&(bf_manager.frame[i].page),PF_PAGESIZE)<0) return PF_FILEERR; } bf_manager.allocated[i]=false; } } return SUCCESS; }
//------------------------------------------------------------------------------ // // SendFile() - stands-in for system provided sendfile // //------------------------------------------------------------------------------ int32 CSimpleSocket::SendFile(int32 nOutFd, int32 nInFd, off_t *pOffset, int32 nCount) { int32 nOutCount = CSimpleSocket::SocketError; #ifdef WIN32 static char szData[SOCKET_SENDFILE_BLOCKSIZE]; int32 nInCount = 0; if (_lseek(nInFd, *pOffset, SEEK_SET) == -1) { return -1; } while (nOutCount < nCount) { nInCount = (nCount - nOutCount) < SOCKET_SENDFILE_BLOCKSIZE ? (nCount - nOutCount) : SOCKET_SENDFILE_BLOCKSIZE; if ((_read(nInFd, szData, nInCount)) != (int32)nInCount) { return -1; } if ((SEND(nOutFd, szData, nInCount, 0)) != (int32)nInCount) { return -1; } nOutCount += nInCount; } *pOffset += nOutCount; #else nOutCount = sendfile(nOutFd, nInFd, pOffset, (size_t)nCount); #endif TranslateSocketError(); return nOutCount; }
void fs__sendfile(uv_fs_t* req, uv_file out_file, uv_file in_file, off_t in_offset, size_t length) { const size_t max_buf_size = 65536; size_t buf_size = length < max_buf_size ? length : max_buf_size; int n, result = 0; char* buf = (char*)malloc(buf_size); if (!buf) { uv_fatal_error(ERROR_OUTOFMEMORY, "malloc"); } if (in_offset != -1) { result = _lseek(in_file, in_offset, SEEK_SET); } if (result != -1) { while (length > 0) { n = _read(in_file, buf, length < buf_size ? length : buf_size); if (n == 0) { break; } else if (n == -1) { result = -1; break; } length -= n; n = _write(out_file, buf, n); if (n == -1) { result = -1; break; } result += n; } } SET_REQ_RESULT(req, result); }
void GetCount(const char *pcCountName, char *pcCount) { pcCount[0] = '\0'; // Try to open the file int iFile = _open(pcCountName, _O_RDWR); if (iFile == -1) return ; // Read in the data char pcData[STR_SIZE]; memset(pcData, 0, STR_SIZE); int iLen = _read(iFile, pcData, STR_SIZE); // Format out the data if (iLen > 0) { char pcFormat[STR_SIZE] = ""; char *pcIn = pcData; char *pcOut = pcFormat; unsigned int uValue = 0; while (*pcIn && ((*pcIn < '1') || (*pcIn > '9'))) pcIn++; while (*pcIn && (*pcIn >= '0') && (*pcIn <= '9')) { uValue *= 10; uValue += *pcIn - '0'; *pcOut++ = *pcIn++; } // Write out the new value uValue++; sprintf(pcFormat, "%u", uValue); if (_lseek(iFile, 0, SEEK_SET) != -1) { _write(iFile, pcFormat, strlen(pcFormat)); } // Copy out the string strcpy(pcCount, pcFormat); } // Close the file _close(iFile); return; } // GetCount
const RC AllocateBlock(Frame **buffer) { int i,min,offset; bool flag; clock_t mintime; for(i=0;i<PF_BUFFER_SIZE;i++) if(bf_manager.allocated[i]==false){ bf_manager.allocated[i]=true; *buffer=bf_manager.frame+i; return SUCCESS; } flag=false; for(i=0;i<PF_BUFFER_SIZE;i++){ if(bf_manager.frame[i].pinCount!=0) continue; if(flag==false){ flag=true; min=i; mintime=bf_manager.frame[i].accTime; } if(bf_manager.frame[i].accTime<mintime){ min=i; mintime=bf_manager.frame[i].accTime; } } if(flag==false) return PF_NOBUF; if(bf_manager.frame[min].bDirty==true){ offset=(bf_manager.frame[min].page.pageNum)*sizeof(Page); if(_lseek(bf_manager.frame[min].fileDesc,offset,SEEK_SET)==offset-1) return PF_FILEERR; if(_write(bf_manager.frame[min].fileDesc,&(bf_manager.frame[min].page),sizeof(Page))!=sizeof(Page)) return PF_FILEERR; } *buffer=bf_manager.frame+min; return SUCCESS; }
/* * os_open -- open abstraction layer */ int os_open(const char *pathname, int flags, ...) { wchar_t *path = util_toUTF16(pathname); if (path == NULL) return -1; int ret; if (flags & O_CREAT) { va_list arg; va_start(arg, flags); mode_t mode = va_arg(arg, mode_t); va_end(arg); ret = _wopen(path, flags, mode); } else { ret = _wopen(path, flags); } util_free_UTF16(path); /* BOM skipping should not modify errno */ int orig_errno = errno; /* * text files on windows can contain BOM. As we open files * in binary mode we have to detect bom and skip it */ if (ret != -1) { char bom[3]; if (_read(ret, bom, sizeof(bom)) != 3 || memcmp(bom, UTF8_BOM, 3) != 0) { /* UTF-8 bom not found - reset file to the beginning */ _lseek(ret, 0, SEEK_SET); } } errno = orig_errno; return ret; }
DWORD LstSrcTxt (short sSrcHdl, LPSTR lpWrkBuf, WORD usBufSiz, DWORD ulTxtOff, IDXERRPRC lpMsgDsp) { unsigned uiErrCod; WORD usTxtLen; /********************************************************************/ /* Copy Text portion of file */ /********************************************************************/ if (ulTxtOff && (-1L != _lseek (sSrcHdl, ulTxtOff, SEEK_SET)) && (-1L != (ulTxtOff = _tell (sSrcHdl))) && (-1 != (usTxtLen = Rd_FilFwd (sSrcHdl, lpWrkBuf, min (usBufSiz - 1, VBSTXTMAX - 1), FIOENCNON, &uiErrCod)))) { lpWrkBuf[usTxtLen] = '\0'; lpMsgDsp ("%Fs\n", (LPSTR) lpWrkBuf); } else lpMsgDsp ("Txt Inactive\n"); /********************************************************************/ /********************************************************************/ return (0L); }