Return to the API Tips
Checking User's Authority to Files in Root File System
"Gordon R. Robinson IV"
Does anyone know a way, particularly an API, to check if a user has authority to a file? I found an API to check the user's authority to
an object in the QSYS.LIB file system, but I need to be able to check the user's authority to a file in the Root file system. I'm writing a
program that will only show certain options if the users has access the authority to use those files.
Sure. Use the access() API... Here's an example:
[report a broken link by clicking here]
D**********************************************************************
D* Access mode flags for access()
D*
D* F_OK = File Exists
D* R_OK = Read Access
D* W_OK = Write Access
D* X_OK = Execute or Search
D**********************************************************************
D F_OK C 0
D R_OK C 4
D W_OK C 2
D X_OK C 1
D*--------------------------------------------------------------------
D* Determine file accessibility
D*
D* int access(const char *path, int amode)
D*
D*--------------------------------------------------------------------
D access PR 10I 0 ExtProc('access')
D Path * Value options(*string)
D amode 10I 0 Value
D FileFound S 1N Inz(*OFF)
D ReadOk S 1N Inz(*OFF)
D WriteOk S 1N Inz(*OFF)
D SearchOk S 1N Inz(*OFF)
D Msg S 52A
C* Check if file found:
c if access('/test/foo.bar': F_OK) = 0
c eval FileFound = *On
c endif
C* Check if user has read access:
c if FileFound and
c access('/test/foo.bar': R_OK) = 0
c eval ReadOk = *On
c endif
C* Check if user has write access:
c if FileFound and
c access('/test/foo.bar': W_OK) = 0
c eval WriteOk = *On
c endif
C* Check if user has execute/search access:
c if FileFound and
c access('/test/foo.bar': X_OK) = 0
c eval SearchOk = *On
c endif
C* Show results:
c if FileFound
c eval Msg = 'Read Access: ' + ReadOk
c Msg dsply
c eval Msg = 'Write Access: ' + WriteOk
c Msg dsply
c eval Msg = 'Search Access: ' + SearchOk
c dsply Msg
c else
c eval Msg = 'File Not Found!'
c dsply Msg
c endif
c eval *inlr = *on