2

I am trying to replicate WIN32 code for MacOSX, however I am unable to replicate a part of code

Here is the code.

main_int64 pos; OP_ASSERT(sizeof(pos)==sizeof(fpos_t)); /*Translate the seek to an absolute one.*/ if(_whence==SEEK_CUR) { int ret; ret=fgetpos((FILE *)_stream,(fpos_t *)&pos); if(ret)return ret; } else if(_whence==SEEK_END)pos=_filelengthi64(_fileno((FILE *)_stream)); else if(_whence==SEEK_SET)pos=0; else return -1; /*Check for errors or overflow.*/ if(pos<0||_offset<-pos||_offset>OP_INT64_MAX-pos)return -1; pos+=_offset; int seeko = fsetpos((FILE *)_stream,(fpos_t *)&pos); fprintf("BaseTool", "SEEKO VALUE %d \n", seeko); return seeko; 

I am stuck at

else if(_whence==SEEK_END)pos=_filelengthi64(_fileno((FILE *)_stream));

I am not sure how can I replace these functions in mac based system as these are WIN based functions

1
  • MacOSX is not Linux, but both are (nearly) POSIX. See this answer to a nearly duplicate question Commented May 11, 2018 at 9:42

1 Answer 1

1

from the code I understand you are trying to get the length of the file, in linux you can do this way,

 int fp = open("check5.c", O_RDONLY); off_t lengthOfFile = lseek(fp, 0, SEEK_END); close(fp); printf("%d",lengthOfFile); 

So replace it with "lseek(fileno,0, SEEK_END)" should give length of the file.

Sign up to request clarification or add additional context in comments.

3 Comments

I would recommend using stat(2) instead.
Yeah stat should work as well. But for this lseek also should work
It won't work if check5.c was not a plain file, but e.g. a fifo... And your lseek trick would give strange results if check5.c is written at the same time by some other process.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.