Creating directories in IFS using RPGLE |
How can we create directory using RPGLE?
The MKDIR() function shall create a new directory with name path. The file permission bits of the new directory shall be initialized from mode. MKDIR() return zero on success. On error, -1 is returned and errno is set to indicate the error.
Prototype of MKDIR() in C Language
int mkdir(const char *path, mode_t mode)
Prototype of MKDIR() in RPGLE
D mkdir PR 10I 0 ExtProc('mkdir') D dirpath * Value options(*string) D mode 10U 0 Value
RPGLE Program for creating directories MKDIR
RPG Code in Fixed format for Creating directories in IFS using RPGLE.
HDFTACTGRP(*NO) D mkdir PR 10I 0 ExtProc('mkdir') D dirpath * Value options(*string) D mode 10U 0 Value * D errorifs PR * ExtProc('__errno') * D strerror PR * ExtProc('strerror') D error_num 10I 0 value * Dreturn_mkdir s 10i 0 inz D dirpath s 512a inz D mode s 10U 0 inz Derror_ptr S * Derror_num S 10I 0 based(error_ptr) Derrormsg_ptr S * Derror_msg S 50a based(errormsg_ptr) * <-----mode----> * 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 * C EVAL dirpath = '/home/easyclass/dir1' C EVAL mode = M_readowner + C M_writeowner C EVAL return_mkdir = mkdir(%trim(dirpath):mode) C IF return_mkdir < 0 C EVAL error_ptr = errorIFS() C EVAL errormsg_ptr = strerror(error_num) C error_msg DSPLY C RETURN C ELSE C 'DIR1 CREATED'DSPLY C ENDIF
RPG Code in /Free and /End-Free format for Creating directories in IFS using RPGLE.
HDFTACTGRP(*NO) D mkdir PR 10I 0 ExtProc('mkdir') D dirpath * Value options(*string) D mode 10U 0 Value * D errorifs PR * ExtProc('__errno') * D strerror PR * ExtProc('strerror') D error_num 10I 0 value * Dreturn_mkdir s 10i 0 inz D dirpath s 512a inz D mode s 10U 0 inz Derror_ptr S * Derror_num S 10I 0 based(error_ptr) Derrormsg_ptr S * Derror_msg S 50a based(errormsg_ptr) * <-----mode----> * 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 * /Free dirpath = '/home/easyclass/dir1'; mode = M_readowner + M_writeowner ; return_mkdir = mkdir(%trim(dirpath):mode); if return_mkdir < 0; error_ptr = errorIFS(); errormsg_ptr = strerror(error_num); DSPLY error_msg; return; else; DSPLY 'DIR1 CREATED'; endif; /End-Free
RPG Code in Fully Free format for Creating directories in IFS using RPGLE.
**FREE CTL-OPT DFTACTGRP(*NO); DCL-PR errorifs pointer EXTPROC('__errno'); END-PR; DCL-PR strerror pointer EXTPROC('strerror'); error_num int(10) VALUE; END-PR; DCL-PR mkdir int(10) EXTPROC('mkdir'); dirpath pointer VALUE options(*string); mode uns(10) VALUE; END-PR; DCL-S return_mkdir int(10) inz; DCL-S dirpath CHAR(512); DCL-S mode uns(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(50) based(errormsg_ptr); // * <-----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; dirpath = '/home/easyclass/dir1'; mode = M_readowner + M_writeowner ; return_mkdir = mkdir(%trim(dirpath):mode); if return_mkdir < 0; error_ptr = errorIFS(); errormsg_ptr = strerror(error_num); DSPLY error_msg; return; else; DSPLY 'DIR1 CREATED'; endif;
The above code creates the sub-directory in the user home directory by providing only read and write authority using the mkdir() api. In case directory creation failed we do called the error handling api's to get the error number and the corresponding error message otherwise the directory gets created and we display the message "DIR1 CREATED".