In the last chapter, we covered the basics of how stream files work. It's all down hill from here!
One question that I've been asked many times is: "I use the CHKOBJ command in my CL programs. How can I do the same thing with a file in the IFS?"
The answer is the access() API. Access() can be used to check two things: whether the file exists, and whether it's accessible for reading, writing or execution.
The C-language prototype for the access() API looks like this:
int access(const char *path, int amode);
The prototype is quite simple, and I think by now you're already getting the hang of it, so without further ado, here's the RPG version:
D access PR 10I 0 ExtProc('access')
D Path * Value Options(*string)
D amode 10I 0 Value
Please add this to the IFSIO_H copy member, if you're typing it in yourself.
Like most UNIX-type APIs, we can check errno after calling access to find out why the file wasn't accessible.
The amode parameter uses the rightmost 3 bits of the parameter to determine which access we want to check. If the right-most bit is on, access() checks for execute authority. The next bit to the left checks for write access, and the 3rd bit from the right checks for read access.
If none of the bits in the amode parameter are set, the API will only check to see if the object exists.
Just like we did for the other bit-flags that we've used, we will define named constants to both to make our code easier to follow, and also to match the constants that are already defined for the C programmers.
********************************************************************** * Access mode flags for access() * * F_OK = File Exists * R_OK = Read Access * W_OK = Write Access * X_OK = Execute or Search ********************************************************************** D F_OK C 0 D R_OK C 4 D W_OK C 2 D X_OK C 1
Here's a sample of calling access() in an RPG program:
c if access(%trimr(myfile): F_OK) < 0 c eval err = errno c if err = ENOENT c callp die('Errrm... can''t find that file!') c else c callp die(%str(strerror(err))) c endif c endif c if access(%trimr(myfile): R_OK) < 0 c eval err = errno c if err = EACCES c callp die('It''s there, but YOU can''t read ' + c 'it! Nyaahh! Nyaahh!') c else c callp die(%str(strerror(err))) c endif c endif