/* ncdu - NCurses Disk Usage Copyright (c) 2007-2020 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 "util.h" #include #include #include #include #include #ifdef HAVE_LOCALE_H #include #endif int uic_theme; int winrows, wincols; int subwinr, subwinc; int si; static char thou_sep; char *cropstr(const char *from, int s) { static char dat[4096]; int i, j, o = strlen(from); if(o < s) { strcpy(dat, from); return dat; } j=s/2-3; for(i=0; i 0); tmp[i] = '\0'; /* reverse and add thousand separators */ j = 0; while(i--) { dat[j++] = tmp[i]; if(i != 0 && i%3 == 0) dat[j++] = thou_sep; } dat[j] = '\0'; return dat; } char *fmtmode(unsigned short mode) { static char buf[11]; unsigned short ft = mode & S_IFMT; buf[0] = ft == S_IFDIR ? 'd' : ft == S_IFREG ? '-' : ft == S_IFLNK ? 'l' : ft == S_IFIFO ? 'p' : ft == S_IFSOCK ? 's' : ft == S_IFCHR ? 'c' : ft == S_IFBLK ? 'b' : '?'; buf[1] = mode & 0400 ? 'r' : '-'; buf[2] = mode & 0200 ? 'w' : '-'; buf[3] = mode & 0100 ? 'x' : '-'; buf[4] = mode & 0040 ? 'r' : '-'; buf[5] = mode & 0020 ? 'w' : '-'; buf[6] = mode & 0010 ? 'x' : '-'; buf[7] = mode & 0004 ? 'r' : '-'; buf[8] = mode & 0002 ? 'w' : '-'; buf[9] = mode & 0001 ? 'x' : '-'; buf[10] = 0; return buf; } void read_locale() { thou_sep = '.'; #ifdef HAVE_LOCALE_H setlocale(LC_ALL, ""); char *locale_thou_sep = localeconv()->thousands_sep; if(locale_thou_sep && 1 == strlen(locale_thou_sep)) thou_sep = locale_thou_sep[0]; #endif } int ncresize(int minrows, int mincols) { int ch; getmaxyx(stdscr, winrows, wincols); while((minrows && winrows < minrows) || (mincols && wincols < mincols)) { erase(); mvaddstr(0, 0, "Warning: terminal too small,"); mvaddstr(1, 1, "please either resize your terminal,"); mvaddstr(2, 1, "press i to ignore, or press q to quit."); refresh(); nodelay(stdscr, 0); ch = getch(); getmaxyx(stdscr, winrows, wincols); if(ch == 'q') { erase(); refresh(); endwin(); exit(0); } if(ch == 'i') return 1; } erase(); return 0; } void nccreate(int height, int width, const char *title) { int i; uic_set(UIC_DEFAULT); subwinr = winrows/2-height/2; subwinc = wincols/2-width/2; /* clear window */ for(i=0; iflags & FF_HLNKC)) return; /* remove size from parents. * This works the same as with adding: only the parents in which THIS is the * only occurrence of the hard link will be modified, if the same file still * exists within the parent it shouldn't get removed from the count. * XXX: Same note as for dir_mem.c / hlink_check(): * this is probably not the most efficient algorithm */ for(i=1,par=d->parent; i&∥ par=par->parent) { if(d->hlnk) for(t=d->hlnk; i&&t!=d; t=t->hlnk) for(pt=t->parent; i&&pt; pt=pt->parent) if(pt==par) i=0; if(i) { par->size = adds64(par->size, -d->size); par->asize = adds64(par->size, -d->asize); } } /* remove from hlnk */ if(d->hlnk) { for(t=d->hlnk; t->hlnk!=d; t=t->hlnk) ; t->hlnk = d->hlnk; } } static void freedir_rec(struct dir *dr) { struct dir *tmp, *tmp2; tmp2 = dr; while((tmp = tmp2) != NULL) { freedir_hlnk(tmp); /* remove item */ if(tmp->sub) freedir_rec(tmp->sub); tmp2 = tmp->next; free(tmp); } } void freedir(struct dir *dr) { if(!dr) return; /* free dr->sub recursively */ if(dr->sub) freedir_rec(dr->sub); /* update references */ if(dr->parent && dr->parent->sub == dr) dr->parent->sub = dr->next; if(dr->prev) dr->prev->next = dr->next; if(dr->next) dr->next->prev = dr->prev; freedir_hlnk(dr); /* update sizes of parent directories if this isn't a hard link. * If this is a hard link, freedir_hlnk() would have done so already * * mtime is 0 here because recalculating the maximum at every parent * dir is expensive, but might be good feature to add later if desired */ addparentstats(dr->parent, dr->flags & FF_HLNKC ? 0 : -dr->size, dr->flags & FF_HLNKC ? 0 : -dr->asize, 0, -(dr->items+1)); free(dr); } const char *getpath(struct dir *cur) { static char *dat; static int datl = 0; struct dir *d, **list; int c, i; if(!cur->name[0]) return "/"; c = i = 1; for(d=cur; d!=NULL; d=d->parent) { i += strlen(d->name)+1; c++; } if(datl == 0) { datl = i; dat = xmalloc(i); } else if(datl < i) { datl = i; dat = xrealloc(dat, i); } list = xmalloc(c*sizeof(struct dir *)); c = 0; for(d=cur; d!=NULL; d=d->parent) list[c++] = d; dat[0] = '\0'; while(c--) { if(list[c]->parent) strcat(dat, "/"); strcat(dat, list[c]->name); } free(list); return dat; } struct dir *getroot(struct dir *d) { while(d && d->parent) d = d->parent; return d; } void addparentstats(struct dir *d, int64_t size, int64_t asize, uint64_t mtime, int items) { struct dir_ext *e; while(d) { d->size = adds64(d->size, size); d->asize = adds64(d->asize, asize); d->items += items; if (d->flags & FF_EXT) { e = dir_ext_ptr(d); e->mtime = (e->mtime > mtime) ? e->mtime : mtime; } d = d->parent; } } /* Apparently we can just resume drawing after endwin() and ncurses will pick * up where it left. Probably not very portable... */ #define oom_msg "\nOut of memory, press enter to try again or Ctrl-C to give up.\n" #define wrap_oom(f) \ void *ptr;\ char buf[128];\ while((ptr = f) == NULL) {\ close_nc();\ write(2, oom_msg, sizeof(oom_msg));\ read(0, buf, sizeof(buf));\ }\ return ptr; void *xmalloc(size_t size) { wrap_oom(malloc(size)) } void *xcalloc(size_t n, size_t size) { wrap_oom(calloc(n, size)) } void *xrealloc(void *mem, size_t size) { wrap_oom(realloc(mem, size)) }