|
楼主 |
发表于 2003-4-2 14:29:45
|
显示全部楼层
前面dir.c漏了一个}本来有的,paste时漏了,不好意思
/***************************************************************************
dir_demo1.c - description
-------------------
begin : Wed Apr 2 2003
copyright : (C) 2003 by root
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <sys/stat.h>
#include <unistd.h>
#include <ftw.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
int main()
{
DIR* apa;
struct dirent* rpr;
struct stat* sa;
struct tm* ppa;
char*cp;
char* ppb;
char* cp1="<DIR>";
char* cp2=" ";
int d_s1,d_s2,f_s1,f_s2;//d_s1 is how many directories; d_s2 is how many bytes in
directory; f_s1 is how many files;f_s2 is how may bytes in file
char* gcd;
d_s1=0;
d_s2=0;
f_s1=0;
f_s2=0;
cp=(char *)malloc(sizeof(char)*100);
//ppb=(char *)malloc(sizeof(char)*100);
gcd=get_current_dir_name();
//printf("%s",ccdir)
if((apa=opendir(gcd))==NULL)
{
printf("can't print directory name\n");
}
while((rpr=readdir(apa))!=NULL)
{
stat(rpr->d_name,sa);
if((sa->st_mode)==16877)
{
//cp="<DIR>";
strcpy(cp,cp1);
d_s1+=1;
d_s2+=sa->st_size;
}
else
{
//cp=" ";
strcpy(cp,cp2);
f_s1+=1;
f_s2+=sa->st_size;
}
ppa=localtime(&(sa->st_mtime));
//ppb=ctime(&(sa->st_mtime));
printf("%d-%d-%d
%d:%d",(1900+ppa->tm_year),(1+ppa->tm_mon),ppa->tm_mday,ppa->tm_hour,ppa->tm_min);
printf("%d %s %s\n",sa->st_size,cp,rpr->d_name);
}
printf("Total Directories:%d direcories Total bytes:%d bytes\n",d_s1,d_s2);
printf("Total Files:%d files Total bytes:%d bytes\n",f_s1,f_s2);
closedir(apa);
free(cp);
//free(ppa);
//free(ppb);
return 0;
} |
|