Esempio n. 1
0
    void
md5_file(char* md5, char* filepath)
{
    /* md5には33バイト以上必要。'\0'が付加される。 */
    FILE *fp;

    if (!(fp = fopen(filepath, "rb")))
	*md5 = '\0';
    else
    {
	ftoMD5(fp, md5);
	fclose(fp);
    }
}
Esempio n. 2
0
static int ls1(PCStr(file),LsArg *lsa)
{	const char *dir = lsa->l_dir;
	const char *fmt = lsa->l_fmt;
	const char *oline = lsa->l_buf;
	const char *sp;
	char sc;
	FileStat st;
	int rcode;
	int st_ok;
	int width;
	CStr(path,1024);
	CStr(name,1024);
	CStr(buf,1024);
	refQStr(op,lsa->l_buf); /**/
	const char *tail;
	/*
	char *tfmt = 0;
	*/
	const char *tfmt = lsa->l_tfmt;

	((char*)oline)[0] = 0;
	lsa->l_ikey = 0;
	if( file[0] == '.' && !lsa->l_all )
		return 0;

	if( lsa->l_maskrexp ){
		tail = frex_match(lsa->l_maskrexp,file);
		if( tail == NULL || *tail != 0 )
			return 0;
	}

	st_ok = 0;
	path[0] = 0;
	if( dir && dir[0] )
		strcpy(path,dir);
	if( *path )
	if( strtailchr(path) != '/' )
	if( *file != '/' )
		strcat(path,"/");
	strcat(path,file);

	if( lsa->l_stp ){
		st = *lsa->l_stp;
		st_ok = 1;
	}

	op = (char*)oline;
	for( sp = fmt; sc = *sp; sp++ ){
		assertVStr(lsa->l_buf,op+1);
		if( op[0] != 0 )
			op += strlen(op);

		if( sc != '%' || sp[1] == 0 ){
			setVStrPtrInc(op,sc); setVStrEnd(op,0);
			continue;
		}
		sp++;
		width = numscan(&sp);
		sc = *sp;

		if( sc == 'V' ){
			const char *rp;
			if( dir == 0 || dir[0] == 0 ){
				strcpy(op,lsa->l_vbase);
				continue;
			}
			strcpy(op,lsa->l_vbase);
			rp = strrpbrk(file,"/\\");
			if( rp && rp != file && rp[1] )
				rp += 1;
			else	rp = file;
			if( *op && strtailchr(op) != '/' && *rp != '/' )
				strcat(op,"/");
			strcat(op,rp);
			continue;
		}
		if( st_ok == 0 && lsa->l_virtual ){
			if( Vstat(file,AVStr(path),AVStr(name),0,0,&st)==0 ){
				file = name;
				st_ok = 1;
			}else{
				CStr(xpath,1024);
				sprintf(xpath,"%s%s",path,VNODE_EXT);
				if( File_is(xpath) ){
					truncVStr(oline);
					break;
				}
			}
		}
		if( sc == 'N' ){
			strcpy(op,file);
			continue;
		}
		if( sc == 'A' ){
			strcpy(op,path);
			continue;
		}
		if( st_ok == 0 ){
			errno = 0;
			if( lsa->l_reflink || !INHERENT_lstat() )
				rcode = stat(path,&st);
			else	rcode = lstat(path,&st);
			if( rcode != 0 ){
				/* maybe errno=EOVERFLOW */
				fprintf(stderr,"FAILED stat(%s), errno=%d\n",
					path,errno);
				((char*)oline)[0] = 0;
				break;
			}
			st_ok = 1;
		}
		if( sc == 'x' ){
			sc = *++sp;
			switch( sc ){
			    case 0: return 0;
			    case 'P':
				mlstPerm(AVStr(op),&st);
				continue;
			    case 'Y':
				if( S_ISDIR(st.st_mode) ){
					if( streq(file,".") )
						sprintf(op,"cdir");
					else
					if( streq(file,"..") )
						sprintf(op,"pdir");
					else	sprintf(op,"dir");
				}else
				if( S_ISREG(st.st_mode) )
					sprintf(op,"file");
				else	sprintf(op,"unknown");
				continue;
				break;
			    default:
				continue;
			    case 'T':
				tfmt = "%Y%m%d%H%M%S";
				sc = *++sp;
				break;
			    case 'U':
			    {
				struct {
					int u_dev;
					int u_ino;
				} u;
				u.u_dev = st.st_dev;
				u.u_ino = st.st_ino;
				str_to64((char*)&u,sizeof(u),AVStr(buf),sizeof(buf),1);
				Xsscanf(buf,"%[a-zA-Z0-9/+]",AVStr(op));
				continue;
			    }
			    break;
			}
		}

		switch( sc ){
			case 'd': {
				void ftoMD5(FILE *fp,char md5[]);
				FILE *fp;
				CStr(digest,64);
				if( fp = fopen(path,"r") ){
					ftoMD5(fp,(char*)digest);
					fclose(fp);
				}else	strcpy(digest,"-");
				sprintf(op,"%*s",width,digest);
				break;
			}
			case 'm': lsa->l_ikey = st.st_mtime; break;
			case 'a': lsa->l_ikey = st.st_atime; break;
			case 'z': lsa->l_ikey = st.st_size; break;
			case 'I': sprintf(op,"%*d",width,ll2i(st.st_ino)); break;
			case 'T': setVStrPtrInc(op,Ftypec(st)); setVStrEnd(op,0); break;
			case 'M': strcpy(op,Fmodes(st)); break;
			case 'L': sprintf(op,"%*d",width,ll2i(Fnlink(st))); break;
			case 'O': sprintf(op,"%*s",width,Fowner(st,AVStr(buf))); break;
			case 'G': sprintf(op,"%*s",width,Fgroup(st,AVStr(buf))); break;
			/*
			case 'S': sprintf(op,"%*d",width,Fbytes(st)); break;
			*/
			/*
			case 'S': sprintf(op,"%*u",width,Fbytes(st)); break;
			*/
			case 'S': sprintf(op,"%*lld",width,Fbytes(st)); break;
			case 'K': sprintf(op,"%*d",width,Fkbytes(st)); break;
			/*
			case 'D': sprintf(op,"%*s",width,rsctime(st.st_mtime,AVStr(buf))); break;
			case 'U': sprintf(op,"%*s",width,rsctime(st.st_atime,AVStr(buf))); break;
			*/
			case 'C': printTime(op,width,tfmt,st.st_ctime); break;
			case 'D': printTime(op,width,tfmt,st.st_mtime); break;
			case 'U': printTime(op,width,tfmt,st.st_atime); break;
		}
	}
	return 0;
}