du.c revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1989-2012 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* Glenn Fowler
* AT&T Research
*
* du -- report number of blocks used by . | file ...
*/
static const char usage[] =
"[-?\n@(#)$Id: du (AT&T Research) 2012-01-26 $\n]"
"[+NAME?du - summarize disk usage]"
"[+DESCRIPTION?\bdu\b reports the number of blocks contained in all files"
" and recursively all directories named by the \apath\a arguments."
" The current directory is used if no \apath\a is given. Usage for"
" all files and directories is counted, even when the listing is"
" omitted. Directories and files are only counted once, even if"
" they appear more than once as a hard link, a target of a"
" symbolic link, or an operand.]"
"[+?The default block size is 512. The block count includes only the actual"
" data blocks used by each file and directory, and may not include"
" other filesystem data required to represent the file. Blocks are"
" counted only for the first link to a file; subsequent links are"
" ignored. Partial blocks are rounded up for each file.]"
"[+?If more than one of \b-b\b, \b-h\b, \b-k\b, \b-K,\b or \b-m\b are"
" specified only the rightmost takes affect.]"
"[a:all?List usage for each file. If neither \b--all\b nor \b--summary\b"
" is specified then only usage for the \apath\a arguments and"
" directories is listed.]"
"[b:blocksize?Set the block size to \asize\a. If omitted, \asize\a defaults"
" to 512.]#?[size:=512]"
"[f:silent?Do not report file and directory access errors.]"
"[h:binary-scale|human-readable?Scale disk usage to powers of 1024.]"
"[K:decimal-scale?Scale disk usage to powers of 1000.]"
"[k:kilobytes?List usage in units of 1024 bytes.]"
"[m:megabytes?List usage in units of 1024K bytes.]"
"[s:summary|summarize?Only display the total for each \apath\a argument.]"
"[t|c:total?Display a grand total for all files and directories.]"
"[v|r:verbose?Report all file and directory access errors. This is the"
" default.]"
"[x|X|l:xdev|local|mount|one-file-system?Do not descend into directories in"
" different filesystems than their parents.]"
"[L:logical|follow?Follow symbolic links. The default is \b--physical\b.]"
"[H:metaphysical?Follow command argument symbolic links, otherwise don't"
" follow. The default is \b--physical\b.]"
"[P:physical?Don't follow symbolic links. The default is \b--physical\b.]"
"\n"
"\n[ path ... ]\n"
"\n"
"[+SEE ALSO?\bfind\b(1), \bls\b(1), \btw\b(1)]"
;
#include <ast.h>
#include <ls.h>
#include <cdt.h>
#include <fts.h>
#include <error.h>
#define BLOCKS(n) (Count_t)((blocksize==LS_BLOCKSIZE)?(n):(((n)*LS_BLOCKSIZE+blocksize-1)/blocksize))
typedef struct Fileid_s /* unique file id */
{
} Fileid_t;
typedef struct Hit_s /* file already seen */
{
} Hit_t;
static void
{
static int warned;
{
}
else if (!warned)
{
warned = 1;
}
}
int
{
char* s;
char* d;
Count_t n;
Count_t b;
int dirs;
int flags;
int list;
int logical;
int multiple;
int all = 0;
int silent = 0;
int summary = 0;
int scale = 0;
int total = 0;
unsigned long blocksize = 0;
blocksize = 0;
for (;;)
{
{
case 'a':
all = 1;
continue;
case 'b':
continue;
case 'f':
silent = 1;
continue;
case 'h':
scale = 1024;
blocksize = 0;
continue;
case 'K':
scale = 1000;
blocksize = 0;
continue;
case 'k':
blocksize = 1024;
continue;
case 'm':
continue;
case 's':
summary = 1;
continue;
case 't':
total = 1;
continue;
case 'x':
continue;
case 'v':
silent = 0;
continue;
case 'H':
continue;
case 'L':
continue;
case 'P':
flags |= FTS_PHYSICAL;
continue;
case '?':
break;
case ':':
break;
}
break;
}
if (error_info.errors)
if (blocksize)
scale = 0;
else
flags |= FTS_PHYSICAL;
{
{
{
{
*s = 0;
}
else
d = "..";
{
continue;
}
{
continue;
}
if (s)
*s = '/';
}
{
continue;
}
}
n = 0;
{
case FTS_NS:
if (!silent)
continue;
case FTS_D:
if (dirs)
continue;
case FTS_DC:
if (!silent)
continue;
case FTS_DNR:
if (!silent)
break;
case FTS_DNX:
if (!silent)
break;
case FTS_DP:
if (ent->fts_pointer)
{
}
break;
case FTS_SL:
if (logical)
{
continue;
}
/*FALLTHROUGH*/
default:
if (!all)
list = 0;
break;
}
count += b;
n += b;
{
if (scale)
else
}
}
if (total)
{
if (scale)
else
}
return error_info.errors != 0;
}