replace function getwd -> getcwd

This commit is contained in:
6543 2019-03-22 00:48:06 +01:00
parent fac5131e82
commit f0ee3af392
1 changed files with 21 additions and 21 deletions

View File

@ -59,13 +59,13 @@ BTFILE* btFiles::_new_bfnode()
int btFiles::_btf_open(BTFILE *pbf) int btFiles::_btf_open(BTFILE *pbf)
{ {
char fn[MAXPATHLEN]; char fn[MAXPATHLEN];

if(m_flag_automanage && (m_total_opened >= MAX_OPEN_FILES)){ // close any files. if(m_flag_automanage && (m_total_opened >= MAX_OPEN_FILES)){ // close any files.
BTFILE *pbf_n,*pbf_close; BTFILE *pbf_n,*pbf_close;
pbf_close = (BTFILE *) 0; pbf_close = (BTFILE *) 0;
for(pbf_n = m_btfhead; pbf_n ; pbf_n = pbf_n->bf_next){ for(pbf_n = m_btfhead; pbf_n ; pbf_n = pbf_n->bf_next){
if(!pbf_n->bf_flag_opened) continue; // file not been opened. if(!pbf_n->bf_flag_opened) continue; // file not been opened.
if( !pbf_close || pbf_n->bf_last_timestamp < pbf_close->bf_last_timestamp) if( !pbf_close || pbf_n->bf_last_timestamp < pbf_close->bf_last_timestamp)
pbf_close = pbf_n; pbf_close = pbf_n;
} }
if(!pbf_close || fclose(pbf_close->bf_fp) < 0) return -1; if(!pbf_close || fclose(pbf_close->bf_fp) < 0) return -1;
@ -79,7 +79,7 @@ int btFiles::_btf_open(BTFILE *pbf)
}else{ }else{
strcpy(fn, pbf->bf_filename); strcpy(fn, pbf->bf_filename);
} }

if( !(pbf->bf_fp = fopen(fn,"r+")) ) return -1; if( !(pbf->bf_fp = fopen(fn,"r+")) ) return -1;


pbf->bf_flag_opened = 1; pbf->bf_flag_opened = 1;
@ -169,21 +169,21 @@ int btFiles::_btf_recurses_directory(const char *cur_path, BTFILE* lastnode)
DIR *dp; DIR *dp;
BTFILE *pbf; BTFILE *pbf;


if( !getwd(full_cur) ) return -1; if( !getcwd(full_cur, MAXPATHLEN) ) return -1;


if( cur_path ){ if( cur_path ){
strcpy(fn, full_cur); strcpy(fn, full_cur);
if( MAXPATHLEN <= snprintf(full_cur, MAXPATHLEN, "%s%c%s", fn, PATH_SP, cur_path)) if( MAXPATHLEN <= snprintf(full_cur, MAXPATHLEN, "%s%c%s", fn, PATH_SP, cur_path))
return -1; return -1;
} }

if( (DIR*) 0 == (dp = opendir(full_cur))){ if( (DIR*) 0 == (dp = opendir(full_cur))){
fprintf(stderr,"error, open directory %s failed, %s\n",cur_path,strerror(errno)); fprintf(stderr,"error, open directory %s failed, %s\n",cur_path,strerror(errno));
return -1; return -1;
} }


while( (struct dirent*) 0 != (dirp = readdir(dp)) ){ while( (struct dirent*) 0 != (dirp = readdir(dp)) ){

if( 0 == strcmp(dirp->d_name, ".") || if( 0 == strcmp(dirp->d_name, ".") ||
0 == strcmp(dirp->d_name, "..") ) continue; 0 == strcmp(dirp->d_name, "..") ) continue;


@ -202,7 +202,7 @@ int btFiles::_btf_recurses_directory(const char *cur_path, BTFILE* lastnode)
} }


if( S_IFREG & sb.st_mode ){ if( S_IFREG & sb.st_mode ){

pbf = _new_bfnode(); pbf = _new_bfnode();
#ifndef WINDOWS #ifndef WINDOWS
if( !pbf ) return -1; if( !pbf ) return -1;
@ -212,12 +212,12 @@ int btFiles::_btf_recurses_directory(const char *cur_path, BTFILE* lastnode)
if( !pbf->bf_filename ){ closedir(dp); return -1;} if( !pbf->bf_filename ){ closedir(dp); return -1;}
#endif #endif
strcpy(pbf->bf_filename, fn); strcpy(pbf->bf_filename, fn);

pbf->bf_length = sb.st_size; pbf->bf_length = sb.st_size;
m_total_files_length += sb.st_size; m_total_files_length += sb.st_size;


if( lastnode ) lastnode->bf_next = pbf; else m_btfhead = pbf; if( lastnode ) lastnode->bf_next = pbf; else m_btfhead = pbf;

lastnode = pbf; lastnode = pbf;


}else if( S_IFDIR & sb.st_mode ){ }else if( S_IFDIR & sb.st_mode ){
@ -293,13 +293,13 @@ int btFiles::BuildFromFS(const char *pathname)
m_btfhead = pbf; m_btfhead = pbf;
}else if( S_IFDIR & sb.st_mode ){ }else if( S_IFDIR & sb.st_mode ){
char wd[MAXPATHLEN]; char wd[MAXPATHLEN];
if( !getwd(wd) ) return -1; if( !getcwd(wd, MAXPATHLEN) ) return -1;
m_directory = new char[strlen(pathname) + 1]; m_directory = new char[strlen(pathname) + 1];
#ifndef WINDOWS #ifndef WINDOWS
if( !m_directory ) return -1; if( !m_directory ) return -1;
#endif #endif
strcpy(m_directory, pathname); strcpy(m_directory, pathname);

if(chdir(m_directory) < 0){ if(chdir(m_directory) < 0){
fprintf(stderr,"error, change work directory to %s failed, %s",m_directory, strerror(errno)); fprintf(stderr,"error, change work directory to %s failed, %s",m_directory, strerror(errno));
return -1; return -1;
@ -328,7 +328,7 @@ int btFiles::BuildFromMI(const char *metabuf, const size_t metabuf_len, const ch
r = decode_query(metabuf,metabuf_len,"info|files",(const char**) 0, &q,QUERY_POS); r = decode_query(metabuf,metabuf_len,"info|files",(const char**) 0, &q,QUERY_POS);


if( r ){ if( r ){
BTFILE *pbf_last = (BTFILE*) 0; BTFILE *pbf_last = (BTFILE*) 0;
BTFILE *pbf = (BTFILE*) 0; BTFILE *pbf = (BTFILE*) 0;
size_t dl; size_t dl;
if( decode_query(metabuf,metabuf_len,"info|length", if( decode_query(metabuf,metabuf_len,"info|length",
@ -350,7 +350,7 @@ int btFiles::BuildFromMI(const char *metabuf, const size_t metabuf_len, const ch
} }


/* now r saved the pos of files list. q saved list length */ /* now r saved the pos of files list. q saved list length */
p = metabuf + r + 1; p = metabuf + r + 1;
q--; q--;
for(; q && 'e' != *p; p += dl, q -= dl){ for(; q && 'e' != *p; p += dl, q -= dl){
if(!(dl = decode_dict(p, q, (const char*) 0)) ) return -1; if(!(dl = decode_dict(p, q, (const char*) 0)) ) return -1;
@ -413,7 +413,7 @@ int btFiles::CreateFiles()
}else{ }else{
strcpy(fn, pbt->bf_filename); strcpy(fn, pbt->bf_filename);
} }

if(stat(fn ,&sb) < 0){ if(stat(fn ,&sb) < 0){
if(ENOENT == errno){ if(ENOENT == errno){
if( !_btf_creat_by_path(fn,pbt->bf_length)){ if( !_btf_creat_by_path(fn,pbt->bf_length)){
@ -461,28 +461,28 @@ size_t btFiles::FillMetaInfo(FILE* fp)
if( bencode_str("files", fp) != 1 ) return 0; if( bencode_str("files", fp) != 1 ) return 0;


if( bencode_begin_list(fp) != 1) return 0; if( bencode_begin_list(fp) != 1) return 0;

for( p = m_btfhead; p; p = p->bf_next){ for( p = m_btfhead; p; p = p->bf_next){
if( bencode_begin_dict(fp) != 1) return 0; if( bencode_begin_dict(fp) != 1) return 0;

if( bencode_str("length", fp) != 1 ) return 0; if( bencode_str("length", fp) != 1 ) return 0;
if( bencode_int(p->bf_length, fp) != 1) return 0; if( bencode_int(p->bf_length, fp) != 1) return 0;


if( bencode_str("path", fp) != 1) return 0; if( bencode_str("path", fp) != 1) return 0;
if( bencode_path2list(p->bf_filename, fp) != 1 ) return 0; if( bencode_path2list(p->bf_filename, fp) != 1 ) return 0;

if( bencode_end_dict_list(fp) != 1) return 0; if( bencode_end_dict_list(fp) != 1) return 0;
} }

if(bencode_end_dict_list(fp) != 1 ) return 0; if(bencode_end_dict_list(fp) != 1 ) return 0;

if(bencode_str("name", fp) != 1) return 0; if(bencode_str("name", fp) != 1) return 0;
return bencode_str(m_directory, fp); return bencode_str(m_directory, fp);

}else{ }else{
if( bencode_str("length", fp) != 1 ) return 0; if( bencode_str("length", fp) != 1 ) return 0;
if( bencode_int(m_btfhead->bf_length, fp) != 1) return 0; if( bencode_int(m_btfhead->bf_length, fp) != 1) return 0;

if( bencode_str("name", fp) != 1 ) return 0; if( bencode_str("name", fp) != 1 ) return 0;
return bencode_str(m_btfhead->bf_filename, fp); return bencode_str(m_btfhead->bf_filename, fp);
} }