Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

du.cpp File Reference

#include <config.h>
#include <inttypes.h>
#include <stdio.h>
#include <getopt.h>
#include <sys/types.h>
#include <assert.h>
#include "system.h"
#include "error.h"
#include "exclude.h"
#include "human.h"
#include "quote.h"
#include "save-cwd.h"
#include "savedir.h"
#include "xstrtol.h"
#include "en_tree.h"

Include dependency graph for du.cpp:

Include dependency graph

Compounds

struct  entry
struct  htab
struct  String
 String class from orignal du. More...


Defines

#define PROGRAM_NAME   "du"
#define AUTHORS   "Torbjorn Granlund, David MacKenzie, Larry McVoy, and Paul Eggert"
#define INITIAL_HASH_MODULE   100
#define INITIAL_ENTRY_TAB_SIZE   70
#define INITIAL_PATH_SIZE   100
#define S_ISLNK(s)   0

Typedefs

typedef String String

Enumerations

enum  { EXCLUDE_OPTION = CHAR_MAX + 1, BLOCK_SIZE_OPTION, MAX_DEPTH_OPTION }

Functions

int stat (const char *, struct stat *buf)
int lstat (const char *, struct stat *buf)
void usage (int status)
int en_tree_init (en_tree *target)
en_tree * en_tree_new ()
en_tree * en_tree_seek_end (en_tree *cur)
int en_tree_add_child (en_tree *target, en_tree *child)
void en_tree_rdestroy (en_tree *target)
int en_tree_remove (en_tree *target)
int en_tree_set (en_tree *target, uintmax_t n_blocks, const char *name)
int print_size_p (uintmax_t size, uintmax_t max_blocks, const char *name)
int en_tree_print (en_tree *target, uintmax_t max_blocks)
int en_tree_swap_contents (en_tree *t1, en_tree *t2)
int en_tree_swap_children (en_tree *t1, en_tree *t2)
int en_tree_insert_after (en_tree *target, en_tree *after)
int en_tree_insert_before (en_tree *target, en_tree *before)
int en_tree_swap (en_tree *t1, en_tree *t2)
int en_tree_sort (en_tree *target)
int en_tree_sort_r (en_tree *target)
int fmain (int argc, char **argv)

Variables

char * program_name

Define Documentation

#define PROGRAM_NAME   "du"
 

#define AUTHORS   "Torbjorn Granlund, David MacKenzie, Larry McVoy, and Paul Eggert"
 

#define INITIAL_HASH_MODULE   100
 

#define INITIAL_ENTRY_TAB_SIZE   70
 

#define INITIAL_PATH_SIZE   100
 

#define S_ISLNK      0
 


Typedef Documentation

typedef struct String String
 


Enumeration Type Documentation

anonymous enum
 

Enumeration values:
EXCLUDE_OPTION 
BLOCK_SIZE_OPTION 
MAX_DEPTH_OPTION 

00181 {
00182   EXCLUDE_OPTION = CHAR_MAX + 1,
00183   BLOCK_SIZE_OPTION,
00184   MAX_DEPTH_OPTION
00185 };


Function Documentation

int stat const char *   ,
struct stat *    buf
 

int lstat const char *   ,
struct stat *    buf
 

void usage int    status
 

00217 {
00218   if (status != 0)
00219     fprintf (stderr, _("Try `%s --help' for more information.\n"),
00220         program_name);
00221   else
00222     {
00223       printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
00224       printf (_("\
00225 Summarize disk usage of each FILE, recursively for directories.\n\
00226 \n\
00227   -a, --all             write counts for all files, not just directories\n\
00228       --block-size=SIZE use SIZE-byte blocks\n\
00229   -b, --bytes           print size in bytes\n\
00230   -c, --total           produce a grand total\n\
00231   -D, --dereference-args  dereference PATHs when symbolic link\n\
00232   -h, --human-readable  print sizes in human readable format (e.g., 1K 234M 2G)\n\
00233   -H, --si              likewise, but use powers of 1000 not 1024\n\
00234   -k, --kilobytes       like --block-size=1024\n\
00235   -l, --count-links     count sizes many times if hard linked\n\
00236   -L, --dereference     dereference all symbolic links\n\
00237   -m, --megabytes       like --block-size=1048576\n\
00238   -S, --separate-dirs   do not include size of subdirectories\n\
00239   -s, --summarize       display only a total for each argument\n\
00240   -x, --one-file-system  skip directories on different filesystems\n\
00241   -X FILE, --exclude-from=FILE  Exclude files that match any pattern in FILE.\n\
00242       --exclude=PAT     Exclude files that match PAT.\n\
00243       --max-depth=N     print the total for a directory (or file, with --all)\n\
00244                           only if it is N or fewer levels below the command\n\
00245                           line argument;  --max-depth=0 is the same as\n\
00246                           --summarize\n\
00247       --help            display this help and exit\n\
00248       --version         output version information and exit\n\
00249 "));
00250       puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
00251     }
00252   exit (status);
00253 }

int en_tree_init en_tree *    target
 

00318 {
00319   if (target==NULL) return -1;
00320   target->size=0;
00321   target->next=NULL;
00322   target->prev=NULL;
00323   target->parent=NULL;
00324   target->child=NULL;
00325   target->name=NULL;
00326   return 0;
00327 }

en_tree* en_tree_new  
 

00332 {
00333   en_tree *new_tree;
00334   new_tree=(en_tree *) xmalloc(sizeof(en_tree));
00335   en_tree_init(new_tree);
00336   return new_tree;
00337 }

en_tree* en_tree_seek_end en_tree *    cur
 

00342 {
00343 en_tree *current;
00344   for (current=cur; current->next!=NULL; current=current->next);
00345   return current;
00346 }

int en_tree_add_child en_tree *    target,
en_tree *    child
 

00350 { 
00351   if (target==NULL) return -1;
00352   if (target->child==NULL) target->child=child;
00353   else
00354   {
00355     en_tree *end;
00356     end=en_tree_seek_end(target->child);
00357     end->next=child;
00358     child->prev=end;
00359   }
00360   child->parent=target;
00361   return 0;
00362 }

void en_tree_rdestroy en_tree *    target
 

00367 {
00368   en_tree *cur;
00369   for (cur=target->child; cur!=NULL; cur=cur->next)
00370   {
00371     en_tree_rdestroy(cur);
00372   }
00373   free(target->name);
00374   free(target);
00375 }

int en_tree_remove en_tree *    target
 

00380 {
00381   if (target==NULL) return -1;
00382 
00383 // remove reference in the previous node
00384   if (target->prev!=NULL) target->prev->next=target->next;
00385 
00386 // remove reference in the next node
00387   if (target->next!=NULL) target->next->prev=target->prev;
00388 
00389 // remove reference in the parent node
00390   if (target->parent!=NULL && target->parent->child==target) 
00391     target->parent->child=target->next;
00392 }

int en_tree_set en_tree *    target,
uintmax_t    n_blocks,
const char *    name
 

00397 {
00398   target->size=n_blocks;
00399   if (target->name!=NULL) free (target->name);
00400   if (name==NULL) target->name=NULL;
00401   else target->name=strdup(name);
00402 }

int print_size_p uintmax_t    size,
uintmax_t    max_blocks,
const char *    name
 

00424 {
00425   if (opt_percent_mode>0)
00426   {
00427     printf("%i%%\t", 100*size/max_blocks);
00428     printf("%s\n", name);
00429   }
00430   else print_size(size, name);
00431 }

int en_tree_print en_tree *    target,
uintmax_t    max_blocks
 

00436 {
00437   uintmax_t childtotal=(opt_percent_mode==2)?target->size:max_blocks;
00438   
00439   if (opt_root_first)
00440     print_size_p(target->size, max_blocks, target->name);
00441   
00442   if (!opt_siblings_first)
00443     if (target->child!=NULL) en_tree_print(en_tree_seek_end(target->child), childtotal);
00444   if (target->prev!=NULL) en_tree_print(target->prev, max_blocks);
00445   if (opt_siblings_first)
00446     if (target->child!=NULL) en_tree_print(en_tree_seek_end(target->child), childtotal);
00447   
00448   if (!opt_root_first)
00449     print_size_p(target->size, max_blocks, target->name);
00450 }

int en_tree_swap_contents en_tree *    t1,
en_tree *    t2
 

00455 {
00456   uintmax_t size;
00457   char *name;
00458 
00459   size=t1->size;
00460   t1->size=t2->size;
00461   t2->size=size;
00462 
00463   name=t1->name;
00464   t1->name=t2->name;
00465   t2->name=name;
00466 }

int en_tree_swap_children en_tree *    t1,
en_tree *    t2
 

00471 {
00472 en_tree *child=t1->child;
00473   t1->child=t2->child;
00474   t2->child=child;
00475   for (child=t1->child; child!=NULL; child=child->next)
00476   {
00477     child->parent=t1;
00478   }
00479   for (child=t2->child; child!=NULL; child=child->next)
00480   {
00481     child->parent=t2;
00482   }
00483 }

int en_tree_insert_after en_tree *    target,
en_tree *    after
 

00486 {
00487   after->prev=target;
00488   after->next=target->next;
00489   after->parent=target->parent;
00490   target->next=after;
00491   if (after->next!=NULL) after->next->prev=after;
00492 }

int en_tree_insert_before en_tree *    target,
en_tree *    before
 

00495 {
00496   before->next=target;
00497   before->prev=target->prev;
00498   before->parent=target->parent;
00499   target->prev=before;
00500   if (before->prev!=NULL) 
00501     before->prev->next=before;
00502 
00503   if (before->parent!=NULL && before->parent->child==target) 
00504     before->parent->child=before;
00505 }

int en_tree_swap en_tree *    t1,
en_tree *    t2
 

00508 {
00509   en_tree *first, *second, *prev, *next;
00510   if (t1->next==t2) 
00511   {
00512     first=t2;
00513     second=t1;
00514   }
00515   else
00516   {
00517     first=t1;
00518     second=t2;
00519   }
00520   prev=second->prev;
00521   next=first->next;
00522   en_tree_remove(first);
00523   en_tree_insert_after(prev, first);
00524   en_tree_remove(second);
00525   en_tree_insert_before(next, second);
00526 }

int en_tree_sort en_tree *    target
 

00531 {
00532   en_tree *cur=target;
00533   if (target==NULL) return -1;
00534   while (target->next!=NULL)
00535   {
00536     if (target->next->size<target->size) 
00537     {
00538       en_tree_swap_contents (target, target->next);
00539       en_tree_swap_children (target, target->next);
00540       //en_tree_swap(target, target->next); 
00541       if (target->prev!=NULL) target=target->prev;
00542     }
00543     else target=target->next;
00544   }
00545 }

int en_tree_sort_r en_tree *    target
 

00550 {
00551   en_tree *cur;
00552   en_tree_sort(target);
00553   for (cur=target; cur!=NULL; cur=cur->next)
00554   {
00555     en_tree_sort(cur->child);
00556   }
00557 }

int fmain int    argc,
char **    argv
 

00927 {
00928   int c;
00929   char *cwd_only[2];
00930   int max_depth_specified = 0;
00931 
00932   /* If nonzero, display only a total for each argument. */
00933   int opt_summarize_only = 0;
00934 
00935   cwd_only[0] = ".";
00936   cwd_only[1] = NULL;
00937 
00938   program_name = argv[0];
00939   setlocale (LC_ALL, "");
00940   bindtextdomain (PACKAGE, LOCALEDIR);
00941   textdomain (PACKAGE);
00942 
00943   atexit (close_stdout);
00944 
00945   exclude = new_exclude ();
00946   xstat = lstat;
00947 
00948   human_block_size (getenv ("DU_BLOCK_SIZE"), 0, &output_block_size);
00949 
00950   while ((c = getopt_long (argc, argv, "abcfhgHklmpPrsxDLSX:", long_options, NULL))
00951     != -1)
00952     {
00953       long int tmp_long;
00954       switch (c)
00955    {
00956    case 0:        /* Long option. */
00957      break;
00958 
00959    case 'a':
00960      opt_all = 1;
00961      break;
00962 
00963    case 'b':
00964      output_block_size = 1;
00965      break;
00966 
00967    case 'c':
00968      print_totals = 1;
00969      break;
00970 
00971    case 'f':
00972      opt_siblings_first = 1;
00973      break;
00974 
00975    case 'g':
00976      output_block_size = 1024 * 1024 * 1024;
00977      break;
00978 
00979    case 'h':
00980      output_block_size = -1024;
00981      break;
00982 
00983    case 'H':
00984      output_block_size = -1000;
00985      break;
00986 
00987    case 'k':
00988      output_block_size = 1024;
00989      break;
00990 
00991    case MAX_DEPTH_OPTION:     /* --max-depth=N */
00992      if (xstrtol (optarg, NULL, 0, &tmp_long, NULL) != LONGINT_OK
00993          || tmp_long < 0 || tmp_long > INT_MAX)
00994        error (1, 0, _("invalid maximum depth %s"), quote (optarg));
00995 
00996      max_depth_specified = 1;
00997      max_depth = (int) tmp_long;
00998      break;
00999 
01000    case 'm':
01001      output_block_size = 1024 * 1024;
01002      break;
01003 
01004    case 'l':
01005      opt_count_all = 1;
01006      break;
01007 
01008    case 'p':
01009      opt_percent_mode = 1;
01010      break;
01011 
01012    case 'P':
01013      opt_percent_mode = 2;
01014      break;
01015 
01016    case 'r':
01017      opt_root_first = 1;
01018      break;
01019 
01020    case 's':
01021      opt_summarize_only = 1;
01022      break;
01023 
01024    case 'x':
01025      opt_one_file_system = 1;
01026      break;
01027 
01028    case 'D':
01029      opt_dereference_arguments = 1;
01030      break;
01031 
01032    case 'L':
01033      xstat = stat;
01034      break;
01035 
01036    case 'S':
01037      opt_separate_dirs = 1;
01038      break;
01039 
01040    case 'X':
01041      if (add_exclude_file (add_exclude, exclude, optarg, '\n') != 0)
01042        error (1, errno, "%s", quote (optarg));
01043      break;
01044 
01045    case EXCLUDE_OPTION:
01046      add_exclude (exclude, optarg);
01047      break;
01048 
01049    case BLOCK_SIZE_OPTION:
01050      human_block_size (optarg, 1, &output_block_size);
01051      break;
01052 
01053    case_GETOPT_HELP_CHAR;
01054 
01055    case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
01056 
01057    default:
01058      usage (1);
01059    }
01060     }
01061 
01062   if (opt_all && opt_summarize_only)
01063     {
01064       error (0, 0, _("cannot both summarize and show all entries"));
01065       usage (1);
01066     }
01067 
01068   if (opt_summarize_only && max_depth_specified && max_depth == 0)
01069     {
01070       error (0, 0,
01071         _("warning: summarizing is the same as using --max-depth=0"));
01072     }
01073 
01074   if (opt_summarize_only && max_depth_specified && max_depth != 0)
01075     {
01076       error (0, 0,
01077         _("warning: summarizing conflicts with --max-depth=%d"),
01078           max_depth);
01079       usage (1);
01080     }
01081 
01082   if (opt_summarize_only)
01083     max_depth = 0;
01084 
01085   /* Initialize the hash structure for inode numbers.  */
01086   hash_init (INITIAL_HASH_MODULE, INITIAL_ENTRY_TAB_SIZE);
01087 
01088   str_init (&path, INITIAL_PATH_SIZE);
01089 
01090   du_files (optind == argc ? cwd_only : argv + optind);
01091 
01092   exit (exit_status);
01093 }


Variable Documentation

char* program_name
 


Generated on Mon Apr 7 19:42:14 2003 for DuTree by doxygen1.2.18