Removing directories in IFS using RPGLE |
What is RMDIR() API/ Removing directories?
RMDIR is a command which will remove an empty directory on various operating systems. RMDIR() API is used to delete or remove any directory. On success zero is returned. On error, -1 is returned and errno is set to indicate the error. We cannot delete the directory having that has files within it.
Prototype of RMDIR() api in C Language
int rmdir(const char *path)
Prototype of RMDIR() api in RPGLE
D rmdir PR 10I 0 ExtProc('rmdir') D dirpath * Value options(*string)
RPGLE Program for Removing directories.
RPG Code in Fixed format for Removing directories on IFS in RPGLE.
HDFTACTGRP(*NO) D rmdir PR 10I 0 ExtProc('rmdir') D dirpath * Value options(*string) * D errorifs PR * ExtProc('__errno') * D strerror PR * ExtProc('strerror') D error_num 10I 0 value * Dreturn_rmdir s 10i 0 inz D dirpath s 512a inz Derror_ptr S * Derror_num S 10I 0 based(error_ptr) Derrormsg_ptr S * Derror_msg S 50a based(errormsg_ptr) C EVAL dirpath = '/home/easyclass/dir1' C EVAL return_rmdir = rmdir(%trim(dirpath)) C IF return_rmdir < 0 C EVAL error_ptr = errorIFS() C EVAL errormsg_ptr = strerror(error_num) C error_msg DSPLY C RETURN C ELSE C 'DIR1 REMOVED'DSPLY C RETURN C ENDIF
RPG Code in /Free and /End-Free format for Removing directories on IFS in RPGLE.
HDFTACTGRP(*NO) D rmdir PR 10I 0 ExtProc('rmdir') D dirpath * Value options(*string) * D errorifs PR * ExtProc('__errno') * D strerror PR * ExtProc('strerror') D error_num 10I 0 value * Dreturn_rmdir s 10i 0 inz D dirpath s 512a inz Derror_ptr S * Derror_num S 10I 0 based(error_ptr) Derrormsg_ptr S * Derror_msg S 50a based(errormsg_ptr) /Free dirpath = '/home/easyclass/dir1'; return_rmdir = rmdir(%trim(dirpath)); if return_rmdir < 0; error_ptr = errorIFS(); errormsg_ptr = strerror(error_num); DSPLY error_msg; return; else; DSPLY 'DIR1 REMOVED'; endif; /End-Free
RPG Code in Fully Free format Removing directories on IFS in 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 rmdir int(10) EXTPROC('rmdir'); dirpath pointer VALUE options(*string); END-PR; DCL-S return_rmdir int(10) inz; DCL-S dirpath CHAR(512); 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); dirpath = '/home/easyclass/dir1'; return_rmdir = rmdir(%trim(dirpath)); if return_rmdir < 0; error_ptr = errorIFS(); errormsg_ptr = strerror(error_num); DSPLY error_msg; return; else; DSPLY 'DIR1 REMOVED'; endif;
The above code removes the sub-directory in the user home directory by passing the directory path to the rmdir() api. In case remove directory failed we do called the error handling api's to get the error number and the corresponding error message otherwise the directory gets removed and we display the message "DIR1 REMOVED".