How to change permissions on an existing IFS Object using RPGLE |
What is chmod() API?
chmod() API known as Change Mode API that allows us to change the permissions/authorities of an existing IFS object.
Prototype for the chmod() API in C Language
int chmod(const char *path, mode_t mode)
Prototype for the chmod() API in RPG
D chmod PR 10I 0 ExtProc('chmod') D ifspath * Value options(*string) D changemode 10U 0 Value
The chmod() function shall change the file permission bits of the file named by the pathname pointed to by the path to the corresponding bits in the mode. The mode parameter works exactly the same as the mode parameter on the open() api.
RPGLE program for change permissions on an existing IFS Object
HDFTACTGRP(*NO) D chmod PR 10I 0 ExtProc('chmod') D ifspath * Value options(*string) D changemode 10U 0 Value D errorifs PR * ExtProc('__errno') D strerror PR * ExtProc('strerror') D error_num 10I 0 value * owner,group,other (RWX) * owner authority D M_readowner C 256 D M_writeowner C 128 D M_executeowner C 64 * group authority D M_readgroup C 32 D M_writegroup C 16 D M_executegroup C 8 * other people D M_readother C 4 D M_writeother C 2 D M_executeother C 1 * * Difspath s 512a Dreturn_chmod s 10i 0 inz Derror_ptr S * Derror_num S 10I 0 based(error_ptr) Derrormsg_ptr S * Derror_msg S 500a based(errormsg_ptr) C EVAL ifspath = '/home/easyclass/helloworld' C EVAL return_chmod = chmod(%trim(ifspath): C M_readowner + C M_writeowner + C M_executeowner ) C IF return_chmod < 0 C EVAL error_ptr = errorIFS() C EVAL errormsg_ptr = strerror(error_num) C ENDIF C EVAL *inlr = *on C RETURN
HDFTACTGRP(*NO) D chmod PR 10I 0 ExtProc('chmod') D ifspath * Value options(*string) D changemode 10U 0 Value D errorifs PR * ExtProc('__errno') D strerror PR * ExtProc('strerror') D error_num 10I 0 value * owner,group,other (RWX) * owner authority D M_readowner C 256 D M_writeowner C 128 D M_executeowner C 64 * group authority D M_readgroup C 32 D M_writegroup C 16 D M_executegroup C 8 * other people D M_readother C 4 D M_writeother C 2 D M_executeother C 1 * * Difspath s 512a Dreturn_chmod s 10i 0 inz Derror_ptr S * Derror_num S 10I 0 based(error_ptr) Derrormsg_ptr S * Derror_msg S 500a based(errormsg_ptr) /free ifspath = '/home/easyclass/helloworld'; return_chmod = chmod(%trim(ifspath): M_readowner + M_writeowner + M_executeowner ); if return_chmod < 0; error_ptr = errorIFS(); errormsg_ptr = strerror(error_num); endif; *inlr = *on; return; /end-free
**FREE CTL-OPT DFTACTGRP(*NO); DCL-PR chmod int(10) EXTPROC('chmod'); ifspath pointer VALUE options(*string); changemode int(10) VALUE; END-PR; DCL-PR errorifs pointer EXTPROC('__errno'); END-PR; DCL-PR strerror pointer EXTPROC('strerror'); error_num int(10) VALUE; END-PR; // * <-----mode----> // * owner,group,other (RWX) // * owner authority DCL-C M_readowner 256; DCL-C M_writeowner 128; DCL-C M_executeowner 64; // * group authority DCL-C M_readgroup 32; DCL-C M_writegroup 16; DCL-C M_executegroup 8; // * other people DCL-C M_readother 4; DCL-C M_writeother 2; DCL-C M_executeother 1; DCL-S ifspath CHAR(512); DCL-S return_chmod int(10); DCL-S error_ptr pointer; DCL-S error_num int(10) based(error_ptr); DCL-S errormsg_ptr pointer; DCL-S error_msg char(500) based(errormsg_ptr); ifspath = '/home/easyclass/helloworld'; return_chmod = chmod(%trim(ifspath): M_readowner + M_writeowner + M_executeowner ); if return_chmod < 0; error_ptr = errorIFS(); errormsg_ptr = strerror(error_num); endif; *inlr = *on; return;
In the above source code, we tried to change the permissions(Read, write and execute authority) of the file named helloworld on the user home directory. If the chmod() api returns 0 that means permissions have been changed and if it returns -1 then that means failed to change file permissions and in case of error we do call error handling apis to return error number and corresponding error message.