Code:
#include <stdio.h>
#include <string.h>
#include <iostream.h>
typedef struct info* infoptr;
struct info{
char permission[11];
char digit[5];
char owner[20];
char group[20];
long int size;
char mtime[15];
char fname[20];
infoptr next;
};
void main(){
FILE* fp;
infoptr head, cur;
int i, dcount=0, fcount=0;
long int fsize=0, dsize=0, size=0;
system( "ls -l>info.txt" );
fp = fopen("info.txt","r");
if( fp==NULL )
head = NULL;
else{
infoptr temp_info1, temp_info2, cur;
temp_info1 = new info;
head = temp_info1;
fscanf( fp, "%s", temp_info1->permission );
fscanf( fp, "%s", temp_info1->digit );
fscanf( fp, "%s", temp_info1->owner );
fscanf( fp, "%s", temp_info1->group );
fscanf( fp, "%ld", &temp_info1->size );
fscanf( fp, "%s", temp_info1->mtime );
fscanf( fp, "%s", temp_info1->fname );
temp_info1->next = NULL;
while( !feof(fp) ){
temp_info2 = new info;
temp_info1->next = temp_info2;
temp_info1=temp_info2;
fscanf( fp, "%s", temp_info1->permission );
fscanf( fp, "%s", temp_info1->digit );
fscanf( fp, "%s", temp_info1->owner );
fscanf( fp, "%s", temp_info1->group );
fscanf( fp, "%ld", &temp_info1->size );
fscanf( fp, "%s", temp_info1->mtime );
fscanf( fp, "%s", temp_info1->fname );
}
temp_info1->next = NULL;
} // end if
printf( "List of my files:\n" );
for( cur=head; cur!=NULL; cur=cur->next ){
if( strchr(cur->fname, '.')!=NULL ){ //???
printf( "\t%s\n", cur->fname );
fcount++;
fsize+= cur->size;}
}
printf( "\nTotal files: %d\n", fcount );
printf( "Total files size: %ld bytes\n", fsize );
printf( "\nList of my Directories:\n" );
for( cur=head; cur!=NULL; cur=cur->next ){
if( strchr(cur->fname, '.')==NULL ){ //???
printf( "\t%s\n", cur->fname );
dcount++;
dsize+= cur->size;}
}
printf( "\nTotal directories: %d\n", dcount );
printf( "Total directories size: %ld bytes\n", dsize );
size = fsize+dsize;
printf( "Total size of all files & directories: %ld bytes\n", size );
}//end main