Thread: cant see solution, should be easy array problem.

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    3

    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();
    }

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    Ok.. I should have used &#37;d instead of %f when printing it out..

    What can I do to prevent overflow for totalBytes[i] ?

    Code:
    totBytes: 0: 1484891136
    totBytes: 1: 1504659456
    totBytes: 2: 1549002752
    totBytes: 3: 1647611904
    totBytes: 4: 1870077952
    totBytes: 5: -1944338432
    Last edited by jinn; 10-30-2007 at 05:24 PM.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Declare the vars in question as unsigned and then print with the proper format specifier for an unsigned int. Otherwise, use a larger variable (64-bit perhaps?).

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    well Ill be damned.. I tried unsigned before but forgot to print with unsigned format.. Thanks alot.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Turning up the warning level of your compiler might help in finding simple mistakes like that one in the future.

    Congrats on getting it working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  2. Problem Putting INTs Into a CHAR Array
    By cram in forum C++ Programming
    Replies: 13
    Last Post: 10-13-2004, 07:53 AM
  3. Problem with assigning value to array elements
    By sagitt13 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 11:26 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM