2007-07-20 03:15:46 -08:00
|
|
|
/* ncdu - NCurses Disk Usage
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2007 Yoran Heling
|
|
|
|
|
|
|
|
|
|
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 "config.h"
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
#include <errno.h>
|
2007-07-21 05:55:51 -08:00
|
|
|
#include <fnmatch.h>
|
2007-07-20 03:15:46 -08:00
|
|
|
|
2007-07-21 05:55:51 -08:00
|
|
|
#include <ncurses.h>
|
2007-07-20 03:15:46 -08:00
|
|
|
#include <form.h>
|
|
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
2007-07-23 23:58:22 -08:00
|
|
|
#include <sys/time.h>
|
2007-07-20 03:15:46 -08:00
|
|
|
#include <dirent.h>
|
|
|
|
|
|
2007-07-20 03:29:50 -08:00
|
|
|
/* PATH_MAX 260 on Cygwin is too small for /proc/registry */
|
|
|
|
|
#ifdef __CYGWIN__
|
|
|
|
|
# if PATH_MAX < 1024
|
|
|
|
|
# undef PATH_MAX
|
|
|
|
|
# define PATH_MAX 1024
|
|
|
|
|
# endif
|
|
|
|
|
#endif
|
|
|
|
|
|
2007-07-20 03:15:46 -08:00
|
|
|
/* get PATH_MAX */
|
|
|
|
|
#ifndef PATH_MAX
|
|
|
|
|
# ifdef _POSIX_PATH_MAX
|
|
|
|
|
# define PATH_MAX _POSIX_PATH_MAX
|
|
|
|
|
# else
|
|
|
|
|
# define PATH_MAX 4096
|
|
|
|
|
# endif
|
|
|
|
|
#endif
|
|
|
|
|
/* and LINK_MAX */
|
|
|
|
|
#ifndef LINK_MAX
|
|
|
|
|
# ifdef _POSIX_LINK_MAX
|
|
|
|
|
# define LINK_MAX _POSIX_LINK_MAX
|
|
|
|
|
# else
|
|
|
|
|
# define LINK_MAX 32
|
|
|
|
|
# endif
|
|
|
|
|
#endif
|
|
|
|
|
/* check for S_ISLNK */
|
|
|
|
|
#ifndef S_ISLNK
|
|
|
|
|
# ifndef S_IFLNK
|
|
|
|
|
# define S_IFLNK 0120000
|
|
|
|
|
# endif
|
|
|
|
|
# define S_ISLNK(x) (x & S_IFLNK)
|
|
|
|
|
#endif
|
|
|
|
|
|
2007-08-11 12:01:16 -08:00
|
|
|
/* There are many ways to test the endianness on a system, however, I couldn't
|
|
|
|
|
* find a universal way to do this, so I used this small hack that should work
|
|
|
|
|
* on any system. */
|
|
|
|
|
static unsigned int endian_test = 1;
|
|
|
|
|
#define IS_BIG_ENDIAN (!(*(char *) &endian_test))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-07-20 03:15:46 -08:00
|
|
|
/*
|
|
|
|
|
* G L O B A L F L A G S
|
|
|
|
|
*/
|
|
|
|
|
/* File Flags (struct dir -> flags) */
|
2007-07-29 06:06:05 -08:00
|
|
|
#define FF_DIR 0x01
|
|
|
|
|
#define FF_FILE 0x02
|
|
|
|
|
#define FF_ERR 0x04 /* error while reading this item */
|
|
|
|
|
#define FF_OTHFS 0x08 /* excluded because it was an other filesystem */
|
|
|
|
|
#define FF_EXL 0x10 /* excluded using exlude patterns */
|
|
|
|
|
#define FF_SERR 0x20 /* error in subdirectory */
|
|
|
|
|
#define FF_BSEL 0x40 /* selected */
|
2007-08-12 01:52:24 -08:00
|
|
|
#define FF_PAR 0x80 /* reference to parent directory (hack - only used in browser.c) */
|
2007-07-20 03:15:46 -08:00
|
|
|
|
|
|
|
|
/* Settings Flags (int sflags) */
|
2007-08-01 07:01:42 -08:00
|
|
|
#define SF_SMFS 0x01 /* same filesystem */
|
|
|
|
|
#define SF_SI 0x02 /* use powers of 1000 instead of 1024 */
|
|
|
|
|
#define SF_IGNS 0x04 /* ignore too small terminal sizes */
|
|
|
|
|
#define SF_NOCFM 0x08 /* don't confirm file deletion */
|
|
|
|
|
#define SF_IGNE 0x10 /* ignore errors when deleting */
|
2007-07-20 03:15:46 -08:00
|
|
|
|
|
|
|
|
/* Browse Flags (int bflags) */
|
2007-08-01 07:01:42 -08:00
|
|
|
#define BF_NAME 0x01
|
|
|
|
|
#define BF_SIZE 0x02
|
|
|
|
|
#define BF_NDIRF 0x04 /* Normally, dirs before files, setting this disables it */
|
|
|
|
|
#define BF_DESC 0x08
|
|
|
|
|
#define BF_HIDE 0x10 /* don't show hidden files... */
|
|
|
|
|
#define BF_SORT 0x20 /* no need to resort, list is already in correct order */
|
|
|
|
|
#define BF_AS 0x40 /* show apparent sizes instead of disk usage */
|
2007-07-20 03:15:46 -08:00
|
|
|
|
2007-08-11 12:01:16 -08:00
|
|
|
/* Export Flags */
|
|
|
|
|
#define EF_DIR 0x01
|
|
|
|
|
#define EF_FILE 0x02
|
|
|
|
|
#define EF_ERR 0x04
|
|
|
|
|
#define EF_OTHFS 0x08
|
|
|
|
|
#define EF_EXL 0x10
|
|
|
|
|
|
|
|
|
|
|
2007-07-20 03:15:46 -08:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* S T R U C T U R E S
|
|
|
|
|
*/
|
|
|
|
|
struct dir {
|
2007-07-26 08:37:33 -08:00
|
|
|
struct dir *parent, *next, *sub;
|
2007-08-11 12:01:16 -08:00
|
|
|
unsigned char *name;
|
2007-08-01 05:17:26 -08:00
|
|
|
off_t size, asize;
|
2007-08-11 12:01:16 -08:00
|
|
|
unsigned long items;
|
2007-08-01 05:17:26 -08:00
|
|
|
unsigned char flags;
|
|
|
|
|
};
|
2007-07-20 03:15:46 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* G L O B A L V A R I A B L E S
|
|
|
|
|
*
|
|
|
|
|
* (all defined in main.c)
|
|
|
|
|
*/
|
|
|
|
|
/* main directory data */
|
2007-07-25 10:38:49 -08:00
|
|
|
extern struct dir *dat;
|
2007-07-20 03:15:46 -08:00
|
|
|
/* updated when window is resized */
|
|
|
|
|
extern int winrows, wincols;
|
|
|
|
|
/* global settings */
|
2007-08-11 12:01:16 -08:00
|
|
|
extern char sdir[PATH_MAX], *s_export;
|
2007-07-20 03:15:46 -08:00
|
|
|
extern int sflags, bflags, sdelay, bgraph;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* G L O B A L F U N C T I O N S
|
|
|
|
|
*/
|
|
|
|
|
/* util.c */
|
|
|
|
|
extern char *cropdir(const char *, int);
|
|
|
|
|
extern char *cropsize(const off_t);
|
2007-08-01 07:01:42 -08:00
|
|
|
extern char *fullsize(const off_t);
|
2007-07-20 03:15:46 -08:00
|
|
|
extern void ncresize(void);
|
|
|
|
|
extern struct dir * freedir(struct dir *);
|
|
|
|
|
extern char *getpath(struct dir *, char *);
|
|
|
|
|
/* settings.c */
|
|
|
|
|
extern int settingsWin(void);
|
|
|
|
|
/* calc.c */
|
2007-07-25 10:38:49 -08:00
|
|
|
extern struct dir *showCalc(char *);
|
2007-07-20 03:15:46 -08:00
|
|
|
/* browser.c */
|
2007-07-23 23:58:22 -08:00
|
|
|
extern void drawBrowser(int);
|
2007-07-20 03:15:46 -08:00
|
|
|
extern void showBrowser(void);
|
2007-07-23 23:58:22 -08:00
|
|
|
/* help.c */
|
|
|
|
|
extern void showHelp(void);
|
|
|
|
|
/* delete.c */
|
|
|
|
|
extern struct dir *showDelete(struct dir *);
|
2007-07-21 05:55:51 -08:00
|
|
|
/* exclude.c */
|
|
|
|
|
extern void addExclude(char *);
|
|
|
|
|
extern int addExcludeFile(char *);
|
|
|
|
|
extern int matchExclude(char *);
|
2007-08-11 12:01:16 -08:00
|
|
|
/* export.c */
|
|
|
|
|
extern void exportFile(char *, struct dir *);
|
|
|
|
|
extern struct dir *importFile(char *);
|