cant see solution, should be easy array problem.
gives an output to like this:
Code:
[23:56 root lab3]# ./uppg4 5 1
totBytes: 0: -0.008235
totBytes: 1: -0.008235
totBytes: 2: -0.008235
totBytes: 3: -0.008235
totBytes: 4: -0.008235
totBytes: 5: -0.008235
bin
totBytes: 0: -0.000000
totBytes: 1: -0.000000
totBytes: 2: -0.000000
totBytes: 3: -0.000000
totBytes: 4: -0.000000
totBytes: 5: -0.000000
dd
totBytes: 0: -0.000000
totBytes: 1: -0.000000
totBytes: 2: -0.000000
totBytes: 3: -0.000000
totBytes: 4: -0.000000
totBytes: 5: -0.000000
cp
totBytes: 0: -0.000000
totBytes: 1: -0.000000
totBytes: 2: -0.000000
totBytes: 3: -0.000000
totBytes: 4: -0.000000
totBytes: 5: -0.000000
df
totBytes: 0: -0.000000
totBytes: 1: -0.000000
totBytes: 2: -0.000000
totBytes: 3: -0.000000
totBytes: 4: -0.000000
totBytes: 5: -0.000000
Total files processed = 5
Total bytes of all files processed = 177916 bytes
Total Waste Data Waste Inode Waste Block size
0.003549 0.990006 0.003549 512
0.003519 0.981616 0.003519 1024
0.003460 0.965256 0.003460 2048
0.003460 0.965256 0.003460 4096
0.003385 0.944272 0.003385 8192
0.002782 0.775652 0.002782 16384
As seen totalBytes is showing negative numbers first 5 loops, then only - zero.. This is totally wrong. Should start from 0 and add (number of blocks) * blocksize to itself at each loop.. Why do esnt it do that????
Code:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
static unsigned int inode_counter = 0;
static int max_counter; // max antar filer
static int foo;
/* den totala storleken som filerna tar upp för olika blockstorleker*/
static int totalBytes[6]={0};
static int total_bytes = 0;
static unsigned int totalSize = 0; // summan av filerna storlek
static unsigned int totalhole = 0; // totala antal filer med hål
int inode_list[1024]; // inoder för monteringspunkter
void print_result()
{
int i;
printf("\nTotal files processed = %d\n", inode_counter);
printf("Total bytes of all files processed = %d bytes\n", total_bytes);
printf("\nTotal Waste\tData Waste\tInode Waste\tBlock size\n");
for (i = 0; i < 6; i++) {
printf("%f\t%f\t%f\t%d\n",
((float)(totalSize/totalBytes[i])) + ((float)(inode_counter*128)/(totalBytes[i]+inode_counter*128)), //Total Waste
((float)totalSize/(totalBytes[i])), // Data Waste
((float)(inode_counter*128)/(totalBytes[i]+inode_counter*128)), // Inode Waste
512<<i); // Blocks
}
exit(EXIT_SUCCESS);
}
/* är det en monterings inode? */
int okey_inode(unsigned int inode)
{
int i;
for (i = 0; i < sizeof(inode_list)/sizeof(int); i++) {
if (inode_list[i] == inode)
return 0;
}
return 1;
}
/* är det en fil med hål */
int is_hole(struct stat *buf)
{
if (buf->st_blocks*512 < buf->st_size)
totalhole++;
return 1;
return 0;
}
void print_dir(const char *dir)
{
DIR *dp;
struct dirent *dr;
struct stat buf;
int i;
if(!(dp = opendir(dir))) {
fprintf(stderr, "Cant open dir: %s\n", dir);
return;
}
chdir(dir);
while ((dr = readdir(dp)) != NULL) {
if (inode_counter == max_counter)
print_result();
lstat(dr->d_name, &buf);
if (!strcmp(dr->d_name, "."))
continue;
else if(!strcmp(dr->d_name, ".."))
continue;
else if(!okey_inode(buf.st_ino))
continue;
inode_counter++;
is_hole(&buf);
totalSize += buf.st_size;
for (i = 0; i < 6; i++) {
int nblocks = buf.st_size/(512<<i);
if ((buf.st_size % (512<<i)) != 0)
nblocks++;
totalBytes[i] += nblocks*(514<<i);
printf("totBytes: %d: %f\n", i, totalBytes[i]);
}
total_bytes += buf.st_size;
if (!(inode_counter % foo))
printf("%s\n", dr->d_name);
if (S_ISLNK(buf.st_mode))
continue;
if (S_ISDIR(buf.st_mode))
print_dir(dr->d_name);
}
chdir("..");
closedir(dp);
}
int
main(int argc, char **argv)
{
char buffer[4096];
char tmp[4096];
struct stat buf_stat;
FILE *fp;
int i = 0;
if (argc < 3) {
fprintf(stderr, "%s antalfiler antalutskrifter\n", argv[0]);
exit(EXIT_SUCCESS);
}
if (!(fp = fopen("/etc/mtab", "r"))) {
fprintf(stderr, "Cant find mtab\n");
exit(EXIT_FAILURE);
}
memset(inode_list, 0, sizeof(inode_list));
max_counter = atoi(argv[1]);
foo = atoi(argv[2]);
while (fgets(buffer, sizeof(buffer), fp)) {
sscanf(buffer,"%*s %s",tmp);
//printf("%s\n", tmp);
if (lstat(tmp, &buf_stat) == -1) {
perror("lstat");
exit(EXIT_FAILURE);
}
inode_list[i++] = buf_stat.st_ino;
}
print_dir("/");
print_result();
}