星期三, 11月 08, 2023

2012-03-19 如何擷取使用者的預設 home 目錄(home directory) ?(getpwnam API or QSYRUSRI API)




如何擷取使用者的預設 home 目錄(home directory) ?
有二種方法:
1. 使用 getpwnam() API
2. 使用 RETRIEVE USER INFORMATION (QSYRUSRI) API format USRI0300,
   由於此 API 所擷取的是 UCS-2 內碼,所以需要 CDRCVRT API 將 UCS-2 轉換為
   EBCDIC


1. 使用 getpwnam() API
File  : QCLSRC

Member: RTVUSRHOMC

Type  : CLLE

Usage : CRTCLPGM yourlib/RTVUSRHOMC
        
OS    : V5R4
Note  : 此範例來自 Scott Klement

PGM  PARM(&USRPRF)
 
     DCL VAR(&USRPRF)  TYPE(*CHAR) LEN(10)
     DCL VAR(&NULL)    TYPE(*CHAR) LEN(1 ) VALUE(x'00')
     DCL VAR(&USRNULL) TYPE(*CHAR) LEN(11)
     DCL VAR(&NULLPTR) TYPE(*PTR)
     DCL VAR(&RESULT)  TYPE(*PTR)
 
     DCL VAR(&PASSWD) TYPE(*CHAR) LEN(64) +
           STG(*BASED) BASPTR(&RESULT)
     DCL VAR(&PW_DIR) TYPE(*PTR) +
           STG(*DEFINED) DEFVAR(&PASSWD 33)
 
     DCL VAR(&BUFPTR) TYPE(*PTR)
     DCL VAR(&BUFFER) TYPE(*CHAR) LEN(5000) +
           STG(*BASED) BASPTR(&BUFPTR)
     DCL VAR(&BUFLEN) TYPE(*UINT) LEN(4)
 
     DCL VAR(&HOMEDIR) TYPE(*CHAR) LEN(5000)
 
     CHGVAR VAR(&NULLPTR) VALUE(*NULL)
 
 
 
     /* Call the getpwnam() API to get a pointer to the Unix +
        'passwd' structure, which contains the home directory */
 
     CHGVAR VAR(&USRNULL) VALUE(&USRPRF *TCAT &NULL)
 
     CALLPRC PRC('getpwnam') +
             PARM(&USRNULL) +
             RTNVAL(&RESULT)
 
     IF (&RESULT *EQ &NULLPTR) DO
       /* ack */
     ENDDO
 
 
 
     /* The &PW_DIR variable should now point to storage  +
        that contains a null-terminated home directory.   +
                                                          +
        The strlen() API will provide the length of that  +
        home directory. I've limited this length to 5000  +
        chars so it fits in the &HOMEDIR variable.        +
                                                          +
        Finally, copy it from the memory buffer into the  +
        &HOMEDIR variable.                                */
 
     CHGVAR VAR(&BUFPTR) VALUE(&PW_DIR)
 
     CALLPRC PRC('strlen') +
             PARM((&BUFPTR *BYVAL)) +
             RTNVAL(&BUFLEN)
 
     IF (&BUFLEN *GT 5000) DO
        CHGVAR VAR(&BUFLEN) VALUE(5000)
     ENDDO
 
     CHGVAR VAR(&HOMEDIR) VALUE(%SST(&BUFFER 1 &BUFLEN))
 
 
 
     /* Now, &HOMEDIR has the home directory that was needed.  +
                                                               +
        Just to prove it works, I'll send it as a *COMP msg.   */
 
     SNDPGMMSG MSGID(CPF9897) MSGF(QCPFMSG) MSGTYPE(*COMP) +
               MSGDTA(&HOMEDIR)
 
ENDPGM




2. 使用 RETRIEVE USER INFORMATION (QSYRUSRI) API format USRI0300,
   由於此 API 所擷取的是 UCS-2 內碼,所以需要 CDRCVRT API 將 UCS-2 轉換為
   EBCDIC
File  : QCLSRC

Member: RTVUSRHOME

Type  : CLP

Usage : CRTCLPGM yourlib/RTVUSRHOMC

OS    : ALL
Note  : 此範例來自 RTVUSRHOME

RTVUSRHOME: PGM        PARM(&USRPRF &HOMEDIRN)                                           
                                                                                          
             DCL        VAR(&USRPRF) TYPE(*CHAR) LEN(10) /**/                             
             DCL        VAR(&RCV) TYPE(*CHAR) LEN(9999) /**/                              
             DCL        VAR(&RCVLEN) TYPE(*CHAR) LEN(4) /**/                              
                                                                                          
             DCL        VAR(&ERR) TYPE(*CHAR) LEN(100) /**/                               
             DCL        VAR(&FORMAT) TYPE(*CHAR) LEN(8) +                                 
                          VALUE('USRI0300') /**/                                          
                                                                                          
             DCL        VAR(&OFSHOME) TYPE(*CHAR) LEN(4) /**/                             
             DCL        VAR(&OFSHOMED) TYPE(*DEC) LEN(9) /**/                             
                                                                                          
             DCL        VAR(&HOMEDIR) TYPE(*CHAR) LEN(512) /*IN UCS*2*/                   
             DCL        VAR(&CCSID) TYPE(*CHAR) LEN(4) /**/                               
             DCL        VAR(&LOHOME) TYPE(*CHAR) LEN(4) /**/                              
                                                                                          
             DCL        VAR(&ST1) TYPE(*CHAR) LEN(4) /**/                                 
             DCL        VAR(&L1) TYPE(*CHAR) LEN(4) /**/                                  
             DCL        VAR(&CCSIDN) TYPE(*CHAR) LEN(4) /**/                                         
             DCL        VAR(&CCSIDNN) TYPE(*DEC) LEN(5 0) /**/                                       
             DCL        VAR(&ST2) TYPE(*CHAR) LEN(4) /**/                                            
             DCL        VAR(&GCCASN) TYPE(*CHAR) LEN(4) /**/                                         
             DCL        VAR(&L2) TYPE(*CHAR) LEN(4) /**/                                             
             DCL        VAR(&HOMEDIRN) TYPE(*CHAR) LEN(256) /*IN EBCDIC*/                            
             DCL        VAR(&L3) TYPE(*CHAR) LEN(4) /**/                                             
             DCL        VAR(&L4) TYPE(*CHAR) LEN(4) /**/                                             
                                                                                                      
             CHGVAR     VAR(%BIN(&RCVLEN)) VALUE(9999)                                               
                                                                                                      
             IF         COND(&USRPRF = '*CURRENT  ') THEN(RTVJOBA +                                  
                          CURUSER(&USRPRF) DFTCCSID(&CCSIDNN))                                       
                                                                                                      
 /* RETRIEVE USER INFORMATION (QSYRUSRI) API */                                                       
                                                                                                      
             CALL       PGM(QSYRUSRI) PARM(&RCV &RCVLEN &FORMAT +                                    
                           &USRPRF &ERR)                                                              
             CHGVAR     VAR(&OFSHOME) VALUE(%SST(&RCV 601 4))                                        
             /* OFFSET TO HOMEDIR-BLOCK */
             CHGVAR     VAR(&OFSHOMED) VALUE(%BIN(&OFSHOME))            
                                                                                                              
             CHGVAR     VAR(&OFSHOMED) VALUE(&OFSHOMED + 1)                                                   
             /* CCSID OF HOMEDIR IS 61952 UCS-2 */
             CHGVAR     VAR(&CCSID) VALUE(%SST(&RCV &OFSHOMED 4))        
             CHGVAR     VAR(&OFSHOMED) VALUE(&OFSHOMED +4+2+3+3+4)                                            
             /* NUMBER OF BYTES HOMEDIR UCS*2 */
             CHGVAR     VAR(&LOHOME) VALUE(%SST(&RCV &OFSHOMED 4))         
             CHGVAR     VAR(&OFSHOMED) VALUE(&OFSHOMED +4+2+10)                                               
             /* HOMEDIR IN UCS*2 */
             CHGVAR     VAR(&HOMEDIR) VALUE(%SST(&RCV &OFSHOMED 512))                   
                                                                                                              
             CHGVAR     VAR(%BIN(&ST1)) VALUE(0)                                                              
             /* NUMBER OF BYTES INPUT STRING */
             CHGVAR     VAR(&L1) VALUE(&LOHOME)                             
             /* CONVERT IN DFT JOB CCSID */
             RTVJOBA    DFTCCSID(&CCSIDNN)                                      
             CHGVAR     VAR(%BIN(&CCSIDN)) VALUE(&CCSIDNN)                                                    
             /* 2 = SPACE PADDED, SO L2 = L3 */
             CHGVAR     VAR(%BIN(&ST2)) VALUE(2)                            
             CHGVAR     VAR(%BIN(&GCCASN)) VALUE(0)                                                           
             /* ALLOCATED OUTPUT LENGTH IN BYTES */
             CHGVAR     VAR(%BIN(&L2)) VALUE(256)                       
                                                                                                              
/* CONVERT A GRAPHIC CHARACTER STRING (CDRCVRT) API */                                                        
             CALL       PGM(CDRCVRT) PARM(&CCSID &ST1 &HOMEDIR &L1 +                                          
                          &CCSIDN &ST2 &GCCASN &L2 &HOMEDIRN &L3 +                                            
                          &L4 &ERR)                                                                           
                                                      
             SNDPGMMSG  MSG(&HOMEDIRN)              
                                                      
ENDPGM



詳細資訊參照:
getpwnam()--Get User Information for User Name
Retrieve User Information (QSYRUSRI) API 




沒有留言: