/* ncdu - NCurses Disk Usage Copyright (c) 2007-2010 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 int winrows, wincols; int subwinr, subwinc; char cropstrdat[4096]; char formatsizedat[9]; /* "xxx.xMiB" */ char fullsizedat[20]; /* max: 999.999.999.999.999 */ char *getpathdat; int getpathdatl = 0; char *cropstr(const char *from, int s) { int i, j, o = strlen(from); if(o < s) { strcpy(cropstrdat, from); return cropstrdat; } j=s/2-3; for(i=0; i 0); tmp[i] = '\0'; /* reverse and add thousand seperators */ j = 0; while(i--) { fullsizedat[j++] = tmp[i]; if(i != 0 && i%3 == 0) fullsizedat[j++] = '.'; } fullsizedat[j] = '\0'; return fullsizedat; } 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, char *title) { int i; 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 occurence 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 calc.c / calc_hlnk_check(): * this is probably not the most efficient algorithm */ for(i=1,par=DR(d)->parent; i&∥ par=DR(par)->parent) { if(DR(d)->hlnk) for(t=DR(d)->hlnk; i&&t!=d; t=DR(t)->hlnk) for(pt=DR(t)->parent; i&&pt; pt=DR(pt)->parent) if(pt==par) i=0; if(i) { DW(par)->size -= DR(d)->size; DW(par)->asize -= DR(d)->asize; } } /* remove from hlnk */ if(DR(d)->hlnk) { for(t=DR(d)->hlnk; DR(t)->hlnk!=d; t=DR(t)->hlnk) ; DW(t)->hlnk = DR(d)->hlnk; } } void freedir_rec(compll_t dr) { compll_t tmp, tmp2; tmp2 = dr; while((tmp = tmp2)) { freedir_hlnk(tmp); /* remove item */ if(DR(tmp)->sub) freedir_rec(DR(tmp)->sub); tmp2 = DR(tmp)->next; compll_free(tmp); } } void freedir(compll_t dr) { compll_t tmp; /* free dr->sub recursively */ if(DR(dr)->sub) freedir_rec(DR(dr)->sub); /* update references */ if(DR(dr)->parent && DR(DR(dr)->parent)->sub == dr) DW(DR(dr)->parent)->sub = DR(dr)->next; if(DR(dr)->prev) DW(DR(dr)->prev)->next = DR(dr)->next; if(DR(dr)->next) DW(DR(dr)->next)->prev = DR(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 */ for(tmp=DR(dr)->parent; tmp; tmp=DR(tmp)->parent) { if(!(DR(dr)->flags & FF_HLNKC)) { DW(tmp)->size -= DR(dr)->size; DW(tmp)->asize -= DR(dr)->asize; } DW(tmp)->items -= DR(dr)->items+1; } compll_free(dr); } char *getpath(compll_t cur) { compll_t d, *list; int c, i; if(!DR(cur)->name[0]) return "/"; c = i = 1; for(d=cur; d; d=DR(d)->parent) { i += strlen(DR(d)->name)+1; c++; } if(getpathdatl == 0) { getpathdatl = i; getpathdat = malloc(i); } else if(getpathdatl < i) { getpathdatl = i; getpathdat = realloc(getpathdat, i); } list = malloc(c*sizeof(compll_t)); c = 0; for(d=cur; d; d=DR(d)->parent) list[c++] = d; getpathdat[0] = '\0'; while(c--) { if(DR(list[c])->parent) strcat(getpathdat, "/"); strcat(getpathdat, DR(list[c])->name); } free(list); return getpathdat; }