当前位置: 技术问答>linux和unix
请一段代码从UNIX移到LINUX,函数crypt的问题?
来源: 互联网 发布时间:2015-10-03
本文导语: 这个函数是标准函数吗?为什么在LINUX下编译的时候不能通过? 代码如下: char szSalt[3]; char *crypt(); char *pEncryptPwd; if ( szUserName == (char*)0 ){ fprintf(stderr,"Invalid usernamen"); return(-1); } if ( szPasswd == (char*)0 ){ f...
这个函数是标准函数吗?为什么在LINUX下编译的时候不能通过?
代码如下:
char szSalt[3];
char *crypt();
char *pEncryptPwd;
if ( szUserName == (char*)0 ){
fprintf(stderr,"Invalid usernamen");
return(-1);
}
if ( szPasswd == (char*)0 ){
fprintf(stderr,"Invalid Password n");
return(-1);
}
if ( szUserName[0] == '') {
fprintf(stderr,"Invalid usernamen");
return(-1);
}
if ( szPasswd[0] == '') {
fprintf(stderr,"Invalid Password n");
return(-1);
}
if ( ( *pwd = getpwnam(szUserName)) == (struct passwd *)0 ){
fprintf(stderr,"there is no the user:%sn",szUserName);
return(-1);
}
szSalt[0] = (*pwd)->pw_passwd[0];
szSalt[1] = (*pwd)->pw_passwd[1];
szSalt[2] = '';
pEncryptPwd = crypt(szPasswd , szSalt );
//this
if( 0 == strcmp(pEncryptPwd,(*pwd)->pw_passwd) ){
return(1);
}else{
fprintf(stderr,"passwd is incorrect n");
return(-1);
}
如何解决
代码如下:
char szSalt[3];
char *crypt();
char *pEncryptPwd;
if ( szUserName == (char*)0 ){
fprintf(stderr,"Invalid usernamen");
return(-1);
}
if ( szPasswd == (char*)0 ){
fprintf(stderr,"Invalid Password n");
return(-1);
}
if ( szUserName[0] == '') {
fprintf(stderr,"Invalid usernamen");
return(-1);
}
if ( szPasswd[0] == '') {
fprintf(stderr,"Invalid Password n");
return(-1);
}
if ( ( *pwd = getpwnam(szUserName)) == (struct passwd *)0 ){
fprintf(stderr,"there is no the user:%sn",szUserName);
return(-1);
}
szSalt[0] = (*pwd)->pw_passwd[0];
szSalt[1] = (*pwd)->pw_passwd[1];
szSalt[2] = '';
pEncryptPwd = crypt(szPasswd , szSalt );
//this
if( 0 == strcmp(pEncryptPwd,(*pwd)->pw_passwd) ){
return(1);
}else{
fprintf(stderr,"passwd is incorrect n");
return(-1);
}
如何解决
|
定义struct spwd *spw;或spw = getspnam(username);的地方就应该出错
---
只要你写上一行class forward declaration
struct spwd;
这时就不会出错,但是你想访问成员变量时就会出你那种错误
---
只要你写上一行class forward declaration
struct spwd;
这时就不会出错,但是你想访问成员变量时就会出你那种错误