2007-07-20 15:15:46 +04:00
|
|
|
/* ncdu - NCurses Disk Usage
|
|
|
|
|
2009-01-11 12:34:19 +03:00
|
|
|
Copyright (c) 2007-2009 Yoran Heling
|
2007-07-20 15:15:46 +04: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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ncdu.h"
|
2009-04-11 12:48:24 +04:00
|
|
|
#include "exclude.h"
|
2009-04-11 13:37:45 +04:00
|
|
|
#include "util.h"
|
2009-04-11 15:47:55 +04:00
|
|
|
#include "calc.h"
|
2009-04-19 13:35:24 +04:00
|
|
|
#include "delete.h"
|
2009-04-13 19:25:46 +04:00
|
|
|
#include "browser.h"
|
2009-04-11 15:47:55 +04:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
2007-07-20 15:15:46 +04:00
|
|
|
|
2009-04-11 11:58:33 +04:00
|
|
|
int pstate;
|
2007-07-20 15:15:46 +04:00
|
|
|
|
2009-04-18 16:12:30 +04:00
|
|
|
int min_rows = 17,
|
|
|
|
min_cols = 60;
|
2007-08-12 00:01:16 +04:00
|
|
|
|
2009-04-10 20:16:33 +04:00
|
|
|
void screen_draw() {
|
|
|
|
int n = 1;
|
2009-04-11 11:58:33 +04:00
|
|
|
switch(pstate) {
|
2009-04-13 19:25:46 +04:00
|
|
|
case ST_CALC: n = calc_draw(); break;
|
|
|
|
case ST_BROWSE: n = browse_draw(); break;
|
2009-04-19 12:03:42 +04:00
|
|
|
case ST_HELP: n = help_draw(); break;
|
2009-04-19 13:35:24 +04:00
|
|
|
case ST_DEL: n = delete_draw(); break;
|
2009-04-10 20:16:33 +04:00
|
|
|
}
|
|
|
|
if(!n)
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int input_handle(int wait) {
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
nodelay(stdscr, wait);
|
|
|
|
screen_draw();
|
|
|
|
while((ch = getch()) != ERR) {
|
|
|
|
if(ch == KEY_RESIZE) {
|
2009-04-18 16:12:30 +04:00
|
|
|
if(ncresize(min_rows, min_cols))
|
|
|
|
min_rows = min_cols = 0;
|
2009-04-10 20:16:33 +04:00
|
|
|
screen_draw();
|
|
|
|
continue;
|
|
|
|
}
|
2009-04-11 11:58:33 +04:00
|
|
|
switch(pstate) {
|
2009-04-13 19:25:46 +04:00
|
|
|
case ST_CALC: return calc_key(ch);
|
|
|
|
case ST_BROWSE: return browse_key(ch);
|
2009-04-19 12:03:42 +04:00
|
|
|
case ST_HELP: return help_key(ch);
|
2009-04-19 13:35:24 +04:00
|
|
|
case ST_DEL: return delete_key(ch);
|
2009-04-10 20:16:33 +04:00
|
|
|
}
|
2009-04-13 19:25:46 +04:00
|
|
|
screen_draw();
|
2009-04-10 20:16:33 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-12 00:01:16 +04:00
|
|
|
/* parse command line */
|
2009-04-08 21:40:18 +04:00
|
|
|
void argv_parse(int argc, char **argv, char *dir) {
|
2009-04-10 20:16:33 +04:00
|
|
|
int i, j, len;
|
2007-08-12 00:01:16 +04:00
|
|
|
|
|
|
|
/* load defaults */
|
2009-04-08 21:40:18 +04:00
|
|
|
memset(dir, 0, PATH_MAX);
|
|
|
|
getcwd(dir, PATH_MAX);
|
2007-08-12 00:01:16 +04:00
|
|
|
|
|
|
|
/* read from commandline */
|
|
|
|
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 12:48:24 +04:00
|
|
|
exclude_add(argv[++i]);
|
|
|
|
else if(exclude_addfile(argv[++i])) {
|
2007-08-12 00:01:16 +04:00
|
|
|
printf("Can't open %s: %s\n", argv[i], strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* small flags */
|
2009-04-10 20:16:33 +04:00
|
|
|
len = strlen(argv[i]);
|
|
|
|
for(j=1; j<len; j++)
|
2007-08-12 00:01:16 +04:00
|
|
|
switch(argv[i][j]) {
|
2009-04-18 16:04:53 +04:00
|
|
|
case 'x': calc_smfs = 1; break;
|
2009-04-19 13:35:24 +04:00
|
|
|
case 'q': calc_delay = delete_delay = 2000; break;
|
2007-08-12 00:01:16 +04:00
|
|
|
case '?':
|
|
|
|
case 'h':
|
2008-09-10 19:14:12 +04:00
|
|
|
printf("ncdu [-hqvx] [--exclude PATTERN] [-X FILE] directory\n\n");
|
2007-11-24 11:54:22 +03: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-12 00:01:16 +04:00
|
|
|
exit(0);
|
|
|
|
case 'v':
|
|
|
|
printf("ncdu %s\n", PACKAGE_VERSION);
|
|
|
|
exit(0);
|
|
|
|
default:
|
2008-09-10 19:14:12 +04:00
|
|
|
printf("Unknown option: -%c\nSee '%s -h' for more information.\n", argv[i][j], argv[0]);
|
2007-08-12 00:01:16 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} else {
|
2009-04-08 21:40:18 +04:00
|
|
|
strncpy(dir, argv[i], PATH_MAX);
|
|
|
|
if(dir[PATH_MAX - 1] != 0) {
|
2008-10-10 14:56:48 +04:00
|
|
|
printf("Error: path length exceeds PATH_MAX\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2009-04-10 20:16:33 +04:00
|
|
|
dir[PATH_MAX - 1] = 0;
|
2007-08-12 00:01:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-20 15:15:46 +04:00
|
|
|
/* main program */
|
|
|
|
int main(int argc, char **argv) {
|
2009-04-16 20:26:39 +04:00
|
|
|
char dir[PATH_MAX];
|
|
|
|
argv_parse(argc, argv, dir);
|
2009-04-13 19:25:46 +04:00
|
|
|
|
2009-04-16 20:26:39 +04:00
|
|
|
calc_init(dir, NULL);
|
2007-08-12 00:01:16 +04:00
|
|
|
|
2007-07-20 15:15:46 +04:00
|
|
|
initscr();
|
|
|
|
cbreak();
|
|
|
|
noecho();
|
|
|
|
curs_set(0);
|
|
|
|
keypad(stdscr, TRUE);
|
2009-04-18 16:12:30 +04:00
|
|
|
if(ncresize(min_rows, min_cols))
|
|
|
|
min_rows = min_cols = 0;
|
2009-04-08 21:40:18 +04:00
|
|
|
|
2009-04-11 11:58:33 +04:00
|
|
|
while(pstate != ST_QUIT) {
|
|
|
|
if(pstate == ST_CALC)
|
2009-04-10 20:16:33 +04:00
|
|
|
calc_process();
|
2009-04-19 13:35:24 +04:00
|
|
|
if(pstate == ST_DEL)
|
|
|
|
delete_process();
|
2009-04-13 19:25:46 +04:00
|
|
|
else if(input_handle(0))
|
2009-04-19 12:03:42 +04:00
|
|
|
break;
|
2009-04-10 20:16:33 +04:00
|
|
|
}
|
|
|
|
|
2007-07-20 15:15:46 +04:00
|
|
|
erase();
|
|
|
|
refresh();
|
|
|
|
endwin();
|
2009-04-11 12:48:24 +04:00
|
|
|
exclude_clear();
|
2007-07-20 15:15:46 +04:00
|
|
|
|
2009-04-08 21:40:18 +04:00
|
|
|
return 0;
|
2007-07-20 15:15:46 +04:00
|
|
|
}
|
2007-07-24 11:58:22 +04:00
|
|
|
|