2007-07-20 03:15:46 -08:00
|
|
|
/* ncdu - NCurses Disk Usage
|
|
|
|
|
|
2010-02-27 05:20:57 -09:00
|
|
|
Copyright (c) 2007-2010 Yoran Heling
|
2007-07-20 03:15:46 -08:00
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
|
a copy of this software and associated documentation files (the
|
|
|
|
|
"Software"), to deal in the Software without restriction, including
|
|
|
|
|
without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
|
permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
|
the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included
|
|
|
|
|
in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
|
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
|
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
|
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
|
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
2009-04-26 01:08:40 -08:00
|
|
|
#include "global.h"
|
2009-04-11 03:47:55 -08:00
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
|
|
#include <unistd.h>
|
2009-04-25 23:49:51 -08:00
|
|
|
#include <sys/time.h>
|
2009-10-18 02:05:36 -08:00
|
|
|
#include <locale.h>
|
2007-07-20 03:15:46 -08:00
|
|
|
|
2009-04-10 23:58:33 -08:00
|
|
|
int pstate;
|
2007-07-20 03:15:46 -08:00
|
|
|
|
2009-04-18 04:12:30 -08:00
|
|
|
int min_rows = 17,
|
|
|
|
|
min_cols = 60;
|
2009-04-25 23:49:51 -08:00
|
|
|
long update_delay = 100,
|
|
|
|
|
lastupdate = 999;
|
|
|
|
|
|
2007-08-11 12:01:16 -08:00
|
|
|
|
2009-04-10 08:16:33 -08:00
|
|
|
void screen_draw() {
|
2009-04-10 23:58:33 -08:00
|
|
|
switch(pstate) {
|
2009-04-25 23:49:51 -08:00
|
|
|
case ST_CALC: calc_draw(); break;
|
|
|
|
|
case ST_BROWSE: browse_draw(); break;
|
|
|
|
|
case ST_HELP: help_draw(); break;
|
|
|
|
|
case ST_DEL: delete_draw(); break;
|
2009-04-10 08:16:33 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-04-25 23:49:51 -08:00
|
|
|
/* wait:
|
|
|
|
|
* -1: non-blocking, always draw screen
|
|
|
|
|
* 0: blocking wait for input and always draw screen
|
|
|
|
|
* 1: non-blocking, draw screen only if a configured delay has passed or after keypress
|
|
|
|
|
*/
|
2009-04-10 08:16:33 -08:00
|
|
|
int input_handle(int wait) {
|
|
|
|
|
int ch;
|
2009-04-25 23:49:51 -08:00
|
|
|
struct timeval tv;
|
2009-04-10 08:16:33 -08:00
|
|
|
|
2009-04-25 23:49:51 -08:00
|
|
|
nodelay(stdscr, wait?1:0);
|
|
|
|
|
if(wait != 1)
|
|
|
|
|
screen_draw();
|
|
|
|
|
else {
|
|
|
|
|
gettimeofday(&tv, (void *)NULL);
|
|
|
|
|
tv.tv_usec = (1000*(tv.tv_sec % 1000) + (tv.tv_usec / 1000)) / update_delay;
|
|
|
|
|
if(lastupdate != tv.tv_usec) {
|
|
|
|
|
screen_draw();
|
|
|
|
|
lastupdate = tv.tv_usec;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-04-10 08:16:33 -08:00
|
|
|
while((ch = getch()) != ERR) {
|
|
|
|
|
if(ch == KEY_RESIZE) {
|
2009-04-18 04:12:30 -08:00
|
|
|
if(ncresize(min_rows, min_cols))
|
|
|
|
|
min_rows = min_cols = 0;
|
2009-04-10 08:16:33 -08:00
|
|
|
screen_draw();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2009-04-10 23:58:33 -08:00
|
|
|
switch(pstate) {
|
2009-04-13 07:25:46 -08:00
|
|
|
case ST_CALC: return calc_key(ch);
|
|
|
|
|
case ST_BROWSE: return browse_key(ch);
|
2009-04-19 00:03:42 -08:00
|
|
|
case ST_HELP: return help_key(ch);
|
2009-04-19 01:35:24 -08:00
|
|
|
case ST_DEL: return delete_key(ch);
|
2009-04-10 08:16:33 -08:00
|
|
|
}
|
2009-04-13 07:25:46 -08:00
|
|
|
screen_draw();
|
2009-04-10 08:16:33 -08:00
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-08-11 12:01:16 -08:00
|
|
|
/* parse command line */
|
2009-04-23 12:10:48 -08:00
|
|
|
char *argv_parse(int argc, char **argv) {
|
2009-04-10 08:16:33 -08:00
|
|
|
int i, j, len;
|
2009-04-23 12:10:48 -08:00
|
|
|
char *dir = NULL;
|
2007-08-11 12:01:16 -08:00
|
|
|
|
2009-04-23 12:10:48 -08:00
|
|
|
/* read from commandline */
|
2007-08-11 12:01:16 -08:00
|
|
|
for(i=1; i<argc; i++) {
|
|
|
|
|
if(argv[i][0] == '-') {
|
|
|
|
|
/* flags requiring arguments */
|
|
|
|
|
if(argv[i][1] == 'X' || !strcmp(argv[i], "--exclude-from") || !strcmp(argv[i], "--exclude")
|
|
|
|
|
|| argv[i][1] == 'e' || argv[i][1] == 'l') {
|
|
|
|
|
if(i+1 >= argc) {
|
|
|
|
|
printf("Option %s requires an argument\n", argv[i]);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
else if(strcmp(argv[i], "--exclude") == 0)
|
2009-04-11 00:48:24 -08:00
|
|
|
exclude_add(argv[++i]);
|
|
|
|
|
else if(exclude_addfile(argv[++i])) {
|
2007-08-11 12:01:16 -08:00
|
|
|
printf("Can't open %s: %s\n", argv[i], strerror(errno));
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
/* small flags */
|
2009-04-10 08:16:33 -08:00
|
|
|
len = strlen(argv[i]);
|
|
|
|
|
for(j=1; j<len; j++)
|
2007-08-11 12:01:16 -08:00
|
|
|
switch(argv[i][j]) {
|
2009-04-18 04:04:53 -08:00
|
|
|
case 'x': calc_smfs = 1; break;
|
2009-04-25 23:49:51 -08:00
|
|
|
case 'q': update_delay = 2000; break;
|
2007-08-11 12:01:16 -08:00
|
|
|
case '?':
|
|
|
|
|
case 'h':
|
2008-09-10 07:14:12 -08:00
|
|
|
printf("ncdu [-hqvx] [--exclude PATTERN] [-X FILE] directory\n\n");
|
2007-11-23 23:54:22 -09:00
|
|
|
printf(" -h This help message\n");
|
|
|
|
|
printf(" -q Quiet mode, refresh interval 2 seconds\n");
|
|
|
|
|
printf(" -v Print version\n");
|
|
|
|
|
printf(" -x Same filesystem\n");
|
|
|
|
|
printf(" --exclude PATTERN Exclude files that match PATTERN\n");
|
|
|
|
|
printf(" -X, --exclude-from FILE Exclude files that match any pattern in FILE\n");
|
2007-08-11 12:01:16 -08:00
|
|
|
exit(0);
|
|
|
|
|
case 'v':
|
|
|
|
|
printf("ncdu %s\n", PACKAGE_VERSION);
|
|
|
|
|
exit(0);
|
|
|
|
|
default:
|
2008-09-10 07:14:12 -08:00
|
|
|
printf("Unknown option: -%c\nSee '%s -h' for more information.\n", argv[i][j], argv[0]);
|
2007-08-11 12:01:16 -08:00
|
|
|
exit(1);
|
|
|
|
|
}
|
2009-04-23 12:10:48 -08:00
|
|
|
} else
|
|
|
|
|
dir = argv[i];
|
2007-08-11 12:01:16 -08:00
|
|
|
}
|
2009-04-23 12:10:48 -08:00
|
|
|
return dir;
|
2007-08-11 12:01:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-07-20 03:15:46 -08:00
|
|
|
/* main program */
|
|
|
|
|
int main(int argc, char **argv) {
|
2009-04-23 12:10:48 -08:00
|
|
|
char *dir;
|
|
|
|
|
|
2009-10-18 02:05:36 -08:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
|
2009-04-23 12:10:48 -08:00
|
|
|
if((dir = argv_parse(argc, argv)) == NULL)
|
2009-04-26 02:45:42 -08:00
|
|
|
dir = ".";
|
2009-04-13 07:25:46 -08:00
|
|
|
|
2009-04-16 08:26:39 -08:00
|
|
|
calc_init(dir, NULL);
|
2007-08-11 12:01:16 -08:00
|
|
|
|
2007-07-20 03:15:46 -08:00
|
|
|
initscr();
|
|
|
|
|
cbreak();
|
|
|
|
|
noecho();
|
|
|
|
|
curs_set(0);
|
|
|
|
|
keypad(stdscr, TRUE);
|
2009-04-18 04:12:30 -08:00
|
|
|
if(ncresize(min_rows, min_cols))
|
|
|
|
|
min_rows = min_cols = 0;
|
2009-04-08 09:40:18 -08:00
|
|
|
|
2009-05-12 08:48:30 -08:00
|
|
|
while(1) {
|
|
|
|
|
if(pstate == ST_CALC && calc_process())
|
|
|
|
|
break;
|
2009-04-24 08:17:34 -08:00
|
|
|
else if(pstate == ST_DEL)
|
2009-04-19 01:35:24 -08:00
|
|
|
delete_process();
|
2009-04-13 07:25:46 -08:00
|
|
|
else if(input_handle(0))
|
2009-04-19 00:03:42 -08:00
|
|
|
break;
|
2009-04-10 08:16:33 -08:00
|
|
|
}
|
|
|
|
|
|
2007-07-20 03:15:46 -08:00
|
|
|
erase();
|
|
|
|
|
refresh();
|
|
|
|
|
endwin();
|
2009-04-11 00:48:24 -08:00
|
|
|
exclude_clear();
|
2007-07-20 03:15:46 -08:00
|
|
|
|
2009-04-08 09:40:18 -08:00
|
|
|
return 0;
|
2007-07-20 03:15:46 -08:00
|
|
|
}
|
2007-07-23 23:58:22 -08:00
|
|
|
|