cex.c (42186B)
1 #include <sys/stat.h> 2 #include <sys/param.h> 3 #include <ctype.h> 4 #include <unistd.h> 5 #include <stdlib.h> 6 #include <libgen.h> 7 #include <string.h> 8 #include <time.h> 9 #include <langinfo.h> 10 #include <pwd.h> 11 #include <grp.h> 12 #include <dirent.h> 13 #include <errno.h> 14 #include <curses.h> 15 16 #include "config.h" 17 18 #if GIT_INFO 19 #include <git2.h> 20 #endif 21 22 #define ctrl(x) ((x) & 0x1f) 23 #define arrlen(arr) (sizeof(arr)/sizeof(0[arr])) 24 25 typedef enum {OTHER, LEFT, RIGHT, UP, DOWN} Direction; 26 typedef enum {CURRENT, PARENT, CHILD} WinType; 27 typedef enum {COPY, REMOVE, MOVE} SelAction; 28 29 typedef struct _win_file { 30 char d_name[256]; 31 unsigned char d_type; 32 bool selected; 33 bool link_valid; 34 } WinFile; 35 36 typedef struct _dir_win { 37 WinType wintype; 38 WINDOW *window; 39 WinFile *winfiles; 40 char path[PATH_MAX]; 41 char *message; 42 int maxx; 43 int maxy; 44 int startpr; 45 int highlight; 46 int filecount; 47 bool holdupdate; 48 bool usehighlight; 49 } DirWin; 50 51 static void init_dirwins(void); 52 static void init_screen(void); 53 static void resize(void); 54 static void start(void); 55 static void wpath(const char *filename); 56 static void reset_flags(void); 57 static void set_startpr(DirWin *dirwin); 58 static void set_win_files(DirWin *dirwin); 59 static void set_win_message(DirWin *dirwin, char *message); 60 static void print_win(DirWin *dirwin); 61 static void update_child_win(void); 62 static void print_top_title(void); 63 static void print_bot_title(void); 64 static void print_content(void); 65 static void print_error(size_t size, const char *fmt, ...); 66 static void show_file_mime(void); 67 static void select_file(const char *path); 68 static void select_files(void); 69 static void exe_selection(SelAction action, const char *askn); 70 static void clear_selected(void); 71 static void clear_search(void); 72 static void combo_key(int keypress); 73 static void combo_go(int keypress); 74 static void combo_inf(int keypress); 75 static void combo_open(int keypress); 76 static void combo_make(int keypress); 77 static void change_dir(const char *chdname, Direction direction, bool abspath); 78 static void change_parent_dir(Direction direction); 79 static void open_child(bool exec); 80 static void open_nohup_xdg(void); 81 static void search(void); 82 static void move_top(DirWin *dirwin); 83 static void move_bot(DirWin *dirwin); 84 static void move_page_up(DirWin *dirwin); 85 static void move_page_down(DirWin *dirwin); 86 static void move_up(DirWin *dirwin); 87 static void move_down(DirWin *dirwin); 88 static void first_search(void); 89 static void next_search(void); 90 static void prev_search(void); 91 static void free_dirwin(DirWin *dirwin); 92 static void run_command(size_t size, const char *fmt, ...); 93 static void run_sudo_command(size_t size, const char *fmt, ...); 94 static void clean(void); 95 static void fatal(const char *fmt, ...); 96 static bool is_dir(DirWin *dirwin, size_t index); 97 static bool is_selected(DirWin *dirwin, size_t count); 98 static bool remove_selected(const char *sel); 99 static bool prompt_confirm(size_t size, const char *fmt, ...); 100 static int compare_file(const void *a, const void *b); 101 static int get_mime(char *buf, size_t bufsize, const char *path); 102 static int get_mime_default(char *buf, size_t bufsize, const char *mime); 103 static int read_command(char *buf, size_t bufsize, const char *cmd); 104 static int make_file(void); 105 static int make_dir(void); 106 static int make_mod(void); 107 static int make_chown(void); 108 static int sudo_chown(char *owner, char *group, long namelen); 109 static void make_access(void); 110 static void make_open_default(void); 111 static void make_mime_default(const char *mime, const char *app); 112 static int rm_file(const char *fname); 113 static int rename_file(const char *fname); 114 static char *truncate_buf_prefix(char *buf, size_t bufsize, size_t max_len); 115 static char *human_readable_bytes(char *buf, intmax_t bytes); 116 static char *prompt_answer(char *buf, size_t size, const char *question); 117 static char *substr(const char *haystack, const char *needle, bool casesens); 118 static char *get_file_info(char *buf, size_t bufsize, const char *filepath); 119 static char *get_fullpath(char *buf, DirWin *dirwin, size_t index); 120 static char *get_dirname(char *buf, const char *path); 121 static char *replace_home(char *buf, const char *path); 122 static mode_t get_st_mode(DirWin *dirwin, size_t index); 123 124 static DirWin curwin, parwin, childwin; 125 static char *userhome, *username, *editor, **selected, searchq[SEARCHLEN+1]; 126 static int maxy, maxx, start_sel = -1; 127 static size_t selc, searchc; 128 static bool print_bot, hide = DEFAULT_HIDE, searchcs = DEFAULT_SEARCH_CASE_SENS; 129 130 #if GIT_INFO 131 static void init_status_options(git_status_options *out_opts); 132 static int is_dirty(git_repository *repo, const git_status_options *opts); 133 static char *retrieve_git_branch(char *buf, size_t bufsize, const char *repo_path); 134 135 static char gitbranch[128]; 136 #endif 137 138 int 139 main(int argc, char **argv) 140 { 141 struct passwd userinf; 142 const char *filename = NULL; 143 int opt; 144 bool writepath = FALSE; 145 146 while ((opt = getopt(argc, argv, "f:")) != -1) { 147 switch (opt) { 148 case 'f': 149 if (strlen(optarg) >= PATH_MAX) 150 fatal("Output path is too long.\n"); 151 filename = optarg; 152 writepath = TRUE; 153 break; 154 default: 155 fatal("Unknown option: %c\n", optopt); 156 break; 157 } 158 } 159 160 #if GIT_INFO 161 git_libgit2_init(); 162 #endif 163 164 editor = getenv("EDITOR"); 165 166 userinf = *getpwuid(getuid()); 167 username = strdup(userinf.pw_name); 168 userhome = strdup(userinf.pw_dir); 169 170 init_dirwins(); 171 init_screen(); 172 173 start(); 174 175 if (writepath) 176 wpath(filename); 177 178 clean(); 179 } 180 181 void 182 init_dirwins(void) 183 { 184 curwin.wintype = CURRENT; 185 parwin.wintype = PARENT; 186 childwin.wintype = CHILD; 187 188 reset_flags(); 189 190 childwin.highlight = 0; 191 192 if (getcwd(curwin.path, PATH_MAX) == NULL) 193 fatal("getcwd() error"); 194 195 get_dirname(parwin.path, curwin.path); 196 197 set_win_files(&curwin); 198 set_win_files(&parwin); 199 200 if (curwin.winfiles != NULL && curwin.winfiles[0].d_type == DT_DIR) { 201 strcpy(childwin.path, curwin.path); 202 update_child_win(); 203 } 204 } 205 206 void 207 init_screen(void) 208 { 209 initscr(); 210 noecho(); 211 start_color(); 212 use_default_colors(); 213 keypad(stdscr, TRUE); 214 curs_set(0); 215 216 clear(); 217 cbreak(); 218 219 init_pair(COLOR_BLACK, COLOR_BLACK, -1); 220 init_pair(COLOR_RED, COLOR_RED, -1); 221 init_pair(COLOR_GREEN,COLOR_GREEN, -1); 222 init_pair(COLOR_YELLOW, COLOR_YELLOW, -1); 223 init_pair(COLOR_BLUE, COLOR_BLUE, -1); 224 init_pair(COLOR_MAGENTA, COLOR_MAGENTA, -1); 225 init_pair(COLOR_CYAN, COLOR_CYAN, -1); 226 init_pair(COLOR_WHITE, COLOR_WHITE, -1); 227 228 curwin.highlight = 0; 229 curwin.startpr = 0; 230 231 parwin.highlight = 0; 232 parwin.startpr = 0; 233 234 childwin.highlight = 0; 235 childwin.startpr = 0; 236 237 resize(); 238 239 #if GIT_INFO 240 retrieve_git_branch(gitbranch, sizeof(gitbranch), curwin.path); 241 #endif 242 print_top_title(); 243 print_bot_title(); 244 245 print_win(&parwin); 246 print_win(&curwin); 247 print_win(&childwin); 248 } 249 250 void 251 resize(void) 252 { 253 int startx; 254 255 getmaxyx(stdscr, maxy, maxx); 256 257 if (parwin.window) delwin(parwin.window); 258 parwin.maxy = maxy-BORDER_SPACE_SIZE; 259 parwin.maxx = maxx/4-BORDER_SPACE_SIZE > 0 ? maxx/4-BORDER_SPACE_SIZE : 1; 260 startx = maxx > BORDER_SPACE_SIZE ? BORDER_SPACE_SIZE : 0; 261 parwin.window = newwin(parwin.maxy, parwin.maxx, BORDER_SPACE_SIZE, startx); 262 263 if (curwin.window) delwin(curwin.window); 264 curwin.maxy = maxy-BORDER_SPACE_SIZE; 265 curwin.maxx = maxx/4-BORDER_SPACE_SIZE > 0 ? maxx/4-BORDER_SPACE_SIZE : 1; 266 startx = maxx > curwin.maxx+BORDER_SPACE_SIZE*2 ? curwin.maxx+BORDER_SPACE_SIZE*2 : 0; 267 curwin.window = newwin(curwin.maxy, curwin.maxx, BORDER_SPACE_SIZE, startx); 268 269 if (childwin.window) delwin(childwin.window); 270 childwin.maxy = maxy-BORDER_SPACE_SIZE; 271 childwin.maxx = maxx/2-BORDER_SPACE_SIZE > 0 ? maxx/2-BORDER_SPACE_SIZE : 1; 272 startx = maxx > childwin.maxx+BORDER_SPACE_SIZE*2 ? childwin.maxx+BORDER_SPACE_SIZE*2 : 0; 273 childwin.window = newwin(childwin.maxy, childwin.maxx, BORDER_SPACE_SIZE, startx); 274 275 erase(); 276 refresh(); 277 278 update_child_win(); 279 } 280 281 void 282 start(void) 283 { 284 int c, sel_anchor; 285 char chdname[PATH_MAX]; 286 287 while ((c = getch()) != KEY_QUIT) { 288 move(1, 1); 289 clrtoeol(); 290 reset_flags(); 291 switch (c) { 292 case KEY_UP: 293 case KEY_VUP: 294 move_up(&curwin); 295 update_child_win(); 296 break; 297 case KEY_DOWN: 298 case KEY_VDOWN: 299 move_down(&curwin); 300 update_child_win(); 301 break; 302 case KEY_VPUP: 303 if (is_dir(&parwin, MAX(parwin.highlight-1, 0))) 304 change_parent_dir(UP); 305 break; 306 case KEY_VPDOWN: 307 if (is_dir(&parwin, MIN(parwin.highlight+1, parwin.filecount-1))) 308 change_parent_dir(DOWN); 309 break; 310 case KEY_PPAGE: 311 case ctrl('u'): 312 move_page_up(&curwin); 313 update_child_win(); 314 break; 315 case KEY_NPAGE: 316 case ctrl('d'): 317 move_page_down(&curwin); 318 update_child_win(); 319 break; 320 case KEY_BOT: 321 move_bot(&curwin); 322 update_child_win(); 323 break; 324 case 10: 325 case KEY_VRIGHT: 326 case KEY_VRIGHT_ABS: 327 if (is_dir(&curwin, curwin.highlight)) 328 change_dir(get_fullpath(chdname, &curwin, curwin.highlight), 329 RIGHT, c == KEY_VRIGHT_ABS); 330 else if (S_ISREG(get_st_mode(&curwin, curwin.highlight))) 331 open_child(FALSE); 332 break; 333 case KEY_LEFT: 334 case KEY_VLEFT: 335 change_dir(get_dirname(chdname, curwin.path), LEFT, FALSE); 336 break; 337 case KEY_SEARCH: 338 search(); 339 update_child_win(); 340 break; 341 case KEY_SEARCH_CASE: 342 searchcs = searchcs ? FALSE : TRUE; 343 set_win_files(&curwin); 344 set_win_files(&parwin); 345 update_child_win(); 346 break; 347 case KEY_NEXT_SEARCH: 348 next_search(); 349 update_child_win(); 350 break; 351 case KEY_PREV_SEARCH: 352 prev_search(); 353 update_child_win(); 354 break; 355 case KEY_SEL_FILE: 356 select_file(get_fullpath(chdname, &curwin, curwin.highlight)); 357 set_win_files(&curwin); 358 update_child_win(); 359 break; 360 case KEY_SEL_FILES: 361 select_files(); 362 sel_anchor = start_sel; 363 set_win_files(&curwin); 364 start_sel = sel_anchor; 365 update_child_win(); 366 break; 367 case KEY_CLEAR_SEL: 368 clear_selected(); 369 set_win_files(&curwin); 370 set_win_files(&parwin); 371 update_child_win(); 372 break; 373 case KEY_CLEAR_SEARCH: 374 clear_search(); 375 set_win_files(&curwin); 376 break; 377 case KEY_CP_SEL: 378 exe_selection(COPY, NULL); 379 break; 380 case KEY_RM_SEL: 381 exe_selection(REMOVE, "Remove"); 382 break; 383 case KEY_MV_SEL: 384 exe_selection(MOVE, "Move"); 385 break; 386 case KEY_RM_FILE: 387 if (curwin.filecount == 0) 388 break; 389 rm_file(curwin.winfiles[curwin.highlight].d_name); 390 set_win_files(&curwin); 391 update_child_win(); 392 break; 393 case KEY_RENAME_FILE: 394 if (curwin.filecount == 0) 395 break; 396 rename_file(curwin.winfiles[curwin.highlight].d_name); 397 set_win_files(&curwin); 398 update_child_win(); 399 break; 400 case KEY_HIDE: 401 hide = hide ? FALSE : TRUE; 402 curwin.highlight = 0; 403 curwin.startpr = 0; 404 set_win_files(&curwin); 405 set_win_files(&parwin); 406 update_child_win(); 407 break; 408 case KEY_COMBO_GO: 409 case KEY_COMBO_INF: 410 case KEY_COMBO_OPEN: 411 case KEY_COMBO_MAKE: 412 combo_key(c); 413 break; 414 case KEY_RESIZE: 415 resize(); 416 break; 417 default: 418 break; 419 } 420 421 print_top_title(); 422 if (print_bot) 423 print_bot_title(); 424 print_win(&parwin); 425 print_win(&curwin); 426 print_win(&childwin); 427 } 428 } 429 430 void 431 wpath(const char *filename) 432 { 433 FILE *fptr; 434 435 if ((fptr = fopen(filename, "w")) == NULL) 436 fatal("Error opening file \"%s\"\n", filename); 437 fprintf(fptr, "%s\n", curwin.path); 438 fclose(fptr); 439 } 440 441 void 442 reset_flags(void) 443 { 444 parwin.usehighlight = FALSE; 445 curwin.usehighlight = TRUE; 446 childwin.usehighlight = TRUE; 447 448 parwin.holdupdate = FALSE; 449 curwin.holdupdate = FALSE; 450 childwin.holdupdate = FALSE; 451 452 print_bot = TRUE; 453 } 454 455 void 456 set_startpr(DirWin *dirwin) 457 { 458 if (dirwin->winfiles == NULL) { 459 dirwin->startpr = 0; 460 return; 461 } 462 463 dirwin->startpr = MAX(dirwin->highlight - dirwin->maxy/2, 0); 464 } 465 466 void 467 set_win_files(DirWin *dirwin) 468 { 469 struct dirent *ent; 470 size_t count = 0, cap = 16; 471 char linkpath[PATH_MAX]; 472 DIR *dir; 473 474 if (dirwin->wintype == CURRENT) 475 start_sel = -1; 476 477 free_dirwin(dirwin); 478 479 if (dirwin->wintype == PARENT && strcmp(curwin.path, "/") == 0) 480 return; 481 482 if ((dir = opendir(dirwin->path)) == NULL) { 483 switch (errno) { 484 case EACCES: 485 set_win_message(dirwin, "No permission."); 486 return; 487 default: 488 fatal("Could not open directory: %s", dirwin->path); 489 } 490 } 491 492 if ((dirwin->winfiles = (WinFile*) malloc(cap * sizeof(WinFile))) == NULL) 493 fatal("Fatal: failed to malloc.\n"); 494 495 while ((ent = readdir(dir)) != NULL) { 496 if (strcmp(ent->d_name, ".") == 0 497 || strcmp(ent->d_name, "..") == 0 498 || (hide && ent->d_name[0] == '.')) 499 continue; 500 if (strnlen(ent->d_name, sizeof(dirwin->winfiles[0].d_name)) 501 == sizeof(dirwin->winfiles[0].d_name)) 502 continue; 503 if (count == cap) { 504 cap *= 2; 505 if ((dirwin->winfiles = (WinFile*) realloc(dirwin->winfiles, cap * sizeof(WinFile))) == NULL) 506 fatal("Fatal: failed to realloc.\n"); 507 } 508 strcpy(dirwin->winfiles[count].d_name, ent->d_name); 509 dirwin->winfiles[count].d_type = ent->d_type; 510 dirwin->filecount = (int) count + 1; 511 dirwin->winfiles[count].selected = is_selected(dirwin, count); 512 if (ent->d_type == DT_LNK) { 513 get_fullpath(linkpath, dirwin, count); 514 dirwin->winfiles[count].link_valid = (linkpath[0] != '\0' 515 && access(linkpath, F_OK) == 0); 516 } else 517 dirwin->winfiles[count].link_valid = TRUE; 518 count++; 519 } 520 closedir(dir); 521 522 dirwin->filecount = count; 523 524 if (count == 0) 525 set_win_message(dirwin, "empty"); 526 else 527 qsort(dirwin->winfiles, count, sizeof(WinFile), compare_file); 528 } 529 530 bool 531 is_selected(DirWin *dirwin, size_t index) 532 { 533 char buf[PATH_MAX]; 534 535 if (selc == 0 || selected == NULL) 536 return FALSE; 537 538 get_fullpath(buf, dirwin, index); 539 540 for (size_t i = 0; i < selc; i++) { 541 if (strcmp(buf, selected[i]) == 0) 542 return TRUE; 543 } 544 545 return FALSE; 546 } 547 548 bool 549 remove_selected(const char *sel) 550 { 551 for (size_t i = 0; i < selc; i++) { 552 if (strcmp(selected[i], sel) == 0) { 553 free(selected[i]); 554 selected[i] = selected[selc-1]; 555 if (--(selc) == 0) { 556 free(selected); 557 selected = NULL; 558 } 559 return TRUE; 560 } 561 } 562 563 return FALSE; 564 } 565 566 void 567 set_win_message(DirWin *dirwin, char *message) 568 { 569 free_dirwin(dirwin); 570 dirwin->message = message; 571 } 572 573 void 574 print_win(DirWin *dirwin) 575 { 576 const char *curbase; 577 578 if (dirwin->holdupdate) 579 return; 580 581 size_t len, width = dirwin->maxx > 1 ? (size_t) dirwin->maxx-1 : 0; 582 char *subs, name[sizeof(dirwin->winfiles[0].d_name)+1]; 583 int sindex, y = 0, x = 1; 584 585 werase(dirwin->window); 586 587 set_startpr(dirwin); 588 589 if (dirwin->message != NULL) { 590 if (dirwin->wintype == CURRENT) { 591 wattron(dirwin->window, A_REVERSE); 592 mvwaddstr(dirwin->window, y, x, dirwin->message); 593 wattroff(dirwin->window, A_REVERSE); 594 } else { 595 wattron(dirwin->window, COLOR_PAIR(CHILDWIN_MESSAGE_COLOR)); 596 mvwaddstr(dirwin->window, y, x, dirwin->message); 597 wattroff(dirwin->window, COLOR_PAIR(CHILDWIN_MESSAGE_COLOR)); 598 } 599 } else if (dirwin->winfiles != NULL) { 600 curbase = strrchr(curwin.path, '/'); 601 if (curbase != NULL && curbase != curwin.path) 602 curbase++; 603 else if (curbase == NULL) 604 curbase = curwin.path; 605 while (!dirwin->usehighlight) { 606 for (int i = 0; i < dirwin->filecount; ++i) { 607 if (strcmp(curbase, dirwin->winfiles[i].d_name) == 0) { 608 dirwin->usehighlight = TRUE; 609 dirwin->highlight = i; 610 set_startpr(dirwin); 611 break; 612 } 613 } 614 break; 615 } 616 for (int i = dirwin->startpr; i < dirwin->filecount; ++i) { 617 mvwaddch(dirwin->window, y, x-1, ' '); 618 if (dirwin->winfiles[i].selected) { 619 wattron(dirwin->window, COLOR_PAIR(MARK_SELECTED_COLOR)); 620 mvwaddch(dirwin->window, y, x-1, '|'); 621 wattroff(dirwin->window, COLOR_PAIR(MARK_SELECTED_COLOR)); 622 } 623 len = MIN(strnlen(dirwin->winfiles[i].d_name, 624 sizeof(dirwin->winfiles[i].d_name)), width); 625 memcpy(name, dirwin->winfiles[i].d_name, len); 626 name[len] = '\0'; 627 if (dirwin->usehighlight && dirwin->highlight == i) { 628 wattron(dirwin->window, A_REVERSE); 629 mvwaddstr(dirwin->window, y, x, name); 630 wattroff(dirwin->window, A_REVERSE); 631 } else if (dirwin->winfiles[i].d_type == DT_DIR) { 632 wattron(dirwin->window, COLOR_PAIR(DIR_COLOR)); 633 wattron(dirwin->window, A_BOLD); 634 mvwaddstr(dirwin->window, y, x, name); 635 wattroff(dirwin->window, COLOR_PAIR(DIR_COLOR)); 636 wattroff(dirwin->window, A_BOLD); 637 } else if (dirwin->winfiles[i].d_type == DT_LNK) { 638 if (dirwin->winfiles[i].link_valid) { 639 wattron(dirwin->window, COLOR_PAIR(LN_COLOR)); 640 mvwaddstr(dirwin->window, y, x, name); 641 wattroff(dirwin->window, COLOR_PAIR(LN_COLOR)); 642 } else { 643 wattron(dirwin->window, COLOR_PAIR(INVALID_LN_COLOR)); 644 mvwaddstr(dirwin->window, y, x, name); 645 wattroff(dirwin->window, COLOR_PAIR(INVALID_LN_COLOR)); 646 } 647 } else 648 mvwaddstr(dirwin->window, y, x, name); 649 if (dirwin->wintype == CURRENT && searchc > 0 650 && ((subs = substr(name, searchq, searchcs)) != NULL)) { 651 sindex = (int) (subs - name); 652 wattron(dirwin->window, COLOR_PAIR(SEARCH_MATCH_COLOR)); 653 if (dirwin->usehighlight && dirwin->highlight == i) 654 wattron(dirwin->window, A_REVERSE); 655 mvwaddnstr(dirwin->window, y, x+sindex, subs, (int) strlen(searchq)); 656 wattroff(dirwin->window, COLOR_PAIR(SEARCH_MATCH_COLOR)); 657 if (dirwin->usehighlight && dirwin->highlight == i) 658 wattroff(dirwin->window, A_REVERSE); 659 } 660 y++; 661 } 662 } 663 wrefresh(dirwin->window); 664 } 665 666 void 667 update_child_win(void) 668 { 669 if (curwin.filecount <= 0) { 670 free_dirwin(&childwin); 671 return; 672 } 673 674 get_fullpath(childwin.path, &curwin, curwin.highlight); 675 676 switch (curwin.winfiles[curwin.highlight].d_type) { 677 case DT_LNK: 678 if (access(childwin.path, F_OK) != 0) { 679 set_win_message(&childwin, "Link no longer exists."); 680 break; 681 } 682 if (is_dir(&curwin, curwin.highlight)) 683 goto dir; 684 else if (S_ISREG(get_st_mode(&curwin, curwin.highlight))) 685 goto reg; 686 else 687 goto def; 688 break; 689 case DT_DIR: 690 dir: 691 set_win_files(&childwin); 692 break; 693 case DT_REG: 694 reg: 695 print_content(); 696 break; 697 default: 698 def: 699 werase(childwin.window); 700 free_dirwin(&childwin); 701 set_win_message(&childwin, "Not a regular file or directory."); 702 wrefresh(childwin.window); 703 break; 704 } 705 } 706 707 void 708 print_top_title(void) 709 { 710 char path[PATH_MAX]; 711 712 move(0, 0); 713 clrtoeol(); 714 715 if (curwin.filecount == 0) 716 snprintf(path, sizeof(path), "%s%s", curwin.path, 717 strcmp(curwin.path, "/") == 0 ? "" : "/"); 718 else 719 get_fullpath(path, &curwin, curwin.highlight); 720 attron(COLOR_PAIR(TOP_TITLE_COLOR)); 721 #if GIT_INFO 722 truncate_buf_prefix(path, sizeof(path), maxx - strlen(username) - 2 - strlen(gitbranch)); 723 printw("%s:%s", username, path); 724 attron(COLOR_PAIR(GIT_BRANCH_COLOR)); 725 printw("%s", gitbranch); 726 attroff(COLOR_PAIR(GIT_BRANCH_COLOR)); 727 #else 728 truncate_buf_prefix(path, sizeof(path), maxx - strlen(username) - 2); 729 printw("%s:%s", username, path); 730 #endif 731 attroff(COLOR_PAIR(TOP_TITLE_COLOR)); 732 } 733 734 void 735 print_bot_title(void) 736 { 737 char pathbuf[PATH_MAX], fileinf[maxx + 1]; 738 739 if (curwin.filecount == 0) 740 fileinf[0] = '\0'; 741 else 742 get_file_info(fileinf, sizeof(fileinf), 743 get_fullpath(pathbuf, &curwin, curwin.highlight)); 744 745 move(maxy-1, 0); 746 clrtoeol(); 747 748 attron(COLOR_PAIR(BOT_TITLE_COUNT_COLOR)); 749 printw("(%d/%d) ", curwin.filecount == 0 ? 0 : curwin.highlight+1, curwin.filecount); 750 attroff(COLOR_PAIR(BOT_TITLE_COUNT_COLOR)); 751 752 attron(COLOR_PAIR(BOT_TITLE_INFO_COLOR)); 753 addstr(fileinf); 754 attroff(COLOR_PAIR(BOT_TITLE_INFO_COLOR)); 755 } 756 757 char * 758 get_file_info(char *buf, size_t bufsize, const char *filepath) 759 { 760 struct stat statbuf; 761 struct passwd *usr; 762 struct tm *tm; 763 struct group *grp; 764 char mods[11], datestr[256], modfill = '-'; 765 char hrbytes[200]; 766 767 if ((stat(filepath, &statbuf)) != 0) { 768 switch (errno) { 769 case EACCES: 770 snprintf(buf, bufsize, "%s", "No permission."); 771 break; 772 default: 773 snprintf(buf, bufsize, "%s", "Retrieving filestats failed."); 774 break; 775 } 776 return buf; 777 } 778 779 if ((usr = getpwuid(statbuf.st_uid)) == NULL) { 780 snprintf(buf, bufsize, "%s", "Retrieving username failed."); 781 return buf; 782 } 783 if ((grp = getgrgid(statbuf.st_gid)) == NULL) { 784 snprintf(buf, bufsize, "%s", "Retrieving groupname failed."); 785 return buf; 786 } 787 788 mods[0] = S_ISDIR(statbuf.st_mode) ? 'd' : modfill; 789 mods[1] = statbuf.st_mode & S_IRUSR ? 'r' : modfill; 790 mods[2] = statbuf.st_mode & S_IWUSR ? 'w' : modfill; 791 mods[3] = statbuf.st_mode & S_IXUSR ? 'x' : modfill; 792 mods[4] = statbuf.st_mode & S_IRGRP ? 'r' : modfill; 793 mods[5] = statbuf.st_mode & S_IWGRP ? 'w' : modfill; 794 mods[6] = statbuf.st_mode & S_IXGRP ? 'x' : modfill; 795 mods[7] = statbuf.st_mode & S_IROTH ? 'r' : modfill; 796 mods[8] = statbuf.st_mode & S_IWOTH ? 'w' : modfill; 797 mods[9] = statbuf.st_mode & S_IXOTH ? 'x' : modfill; 798 mods[10] = '\0'; 799 800 tm = localtime(&statbuf.st_mtime); 801 strftime(datestr, sizeof(datestr), nl_langinfo(D_T_FMT), tm); 802 803 snprintf(buf, bufsize, "%10.10s %s %s %s %s", 804 mods, 805 usr->pw_name, 806 grp->gr_name, 807 human_readable_bytes(hrbytes, (intmax_t) statbuf.st_size), 808 datestr); 809 810 return buf; 811 } 812 813 void 814 print_content(void) 815 { 816 FILE *fp = NULL; 817 size_t size = childwin.maxx; 818 char str[size+1]; 819 int y = 0, x = 0; 820 821 werase(childwin.window); 822 free_dirwin(&childwin); 823 824 if ((fp = fopen(childwin.path, "r")) == NULL) { 825 switch (errno) { 826 case EACCES: 827 set_win_message(&childwin, "No permission."); 828 return; 829 default: 830 fatal("Error opening file \"%s\"\n", childwin.path); 831 } 832 } 833 834 childwin.holdupdate = TRUE; 835 836 while (fgets(str, size, fp) != NULL && y <= childwin.maxy) { 837 mvwaddstr(childwin.window, y, x, str); 838 y++; 839 } 840 841 fclose(fp); 842 wrefresh(childwin.window); 843 } 844 845 void 846 print_error(size_t size, const char *fmt, ...) 847 { 848 va_list ap; 849 char msg[size]; 850 851 va_start(ap, fmt); 852 vsnprintf(msg, size, fmt, ap); 853 va_end(ap); 854 855 move(maxy-1, 0); 856 clrtoeol(); 857 addstr(msg); 858 print_bot = FALSE; 859 } 860 861 void 862 show_file_mime(void) 863 { 864 int appsize = 256; 865 char mimebuf[MIME_MAX], appbuf[appsize]; 866 867 if (curwin.filecount == 0) 868 return; 869 870 if (get_mime(mimebuf, MIME_MAX, curwin.winfiles[curwin.highlight].d_name) != 0) 871 strcpy(mimebuf, "Can't retrieve mime."); 872 873 if (get_mime_default(appbuf, appsize, mimebuf) != 0) 874 strcpy(appbuf, "Can't retrieve default for mime."); 875 876 move(1, 1); 877 clrtoeol(); 878 printw("%s: %s", mimebuf, appbuf); 879 } 880 881 void 882 combo_key(int keypress) 883 { 884 int c; 885 886 move(maxy-1, maxx-1); 887 clrtoeol(); 888 addch(keypress); 889 890 halfdelay(10); 891 892 while ((c = getch()) != ERR) { 893 switch (keypress) { 894 case KEY_COMBO_GO: 895 combo_go(c); 896 break; 897 case KEY_COMBO_INF: 898 combo_inf(c); 899 break; 900 case KEY_COMBO_OPEN: 901 combo_open(c); 902 break; 903 case KEY_COMBO_MAKE: 904 combo_make(c); 905 break; 906 default: 907 break; 908 } 909 break; 910 } 911 912 cbreak(); 913 move(maxy-1, maxx-1); 914 clrtoeol(); 915 } 916 917 void 918 combo_go(int key) 919 { 920 char pathbuf[PATH_MAX]; 921 922 switch (key) { 923 case KEY_COMBO_GO_TOP: 924 move_top(&curwin); 925 update_child_win(); 926 break; 927 case KEY_COMBO_GO_HOME: 928 change_dir(userhome, OTHER, FALSE); 929 break; 930 case KEY_COMBO_GO_ACCESS: 931 change_dir(replace_home(pathbuf, LN_ACCESS_DIR), OTHER, FALSE); 932 break; 933 default: 934 break; 935 } 936 } 937 938 void 939 combo_inf(int key) 940 { 941 switch (key) { 942 case KEY_COMBO_INF_OPEN: 943 show_file_mime(); 944 break; 945 default: 946 break; 947 } 948 } 949 950 void 951 combo_open(int key) 952 { 953 char chdname[PATH_MAX]; 954 955 switch (key) { 956 case KEY_COMBO_OPEN_NOHUP_XDG: 957 if (is_dir(&curwin, curwin.highlight)) 958 change_dir(get_fullpath(chdname, &curwin, curwin.highlight), RIGHT, FALSE); 959 else if (S_ISREG(get_st_mode(&curwin, curwin.highlight))) 960 open_nohup_xdg(); 961 break; 962 case KEY_COMBO_OPEN_EXEC: 963 if (is_dir(&curwin, curwin.highlight)) 964 change_dir(get_fullpath(chdname, &curwin, curwin.highlight), RIGHT, FALSE); 965 else if (S_ISREG(get_st_mode(&curwin, curwin.highlight))) 966 open_child(TRUE); 967 break; 968 default: 969 break; 970 } 971 } 972 973 void 974 combo_make(int key) 975 { 976 switch (key) { 977 case KEY_COMBO_MAKE_FILE: 978 make_file(); 979 set_win_files(&curwin); 980 update_child_win(); 981 break; 982 case KEY_COMBO_MAKE_DIR: 983 make_dir(); 984 set_win_files(&curwin); 985 update_child_win(); 986 break; 987 case KEY_COMBO_MAKE_MOD: 988 make_mod(); 989 break; 990 case KEY_COMBO_MAKE_CHOWN: 991 make_chown(); 992 break; 993 case KEY_COMBO_MAKE_ACCESS: 994 make_access(); 995 break; 996 case KEY_COMBO_MAKE_OPEN_DEFAULT: 997 make_open_default(); 998 break; 999 default: 1000 break; 1001 } 1002 } 1003 1004 void 1005 change_dir(const char *chdname, Direction direction, bool abspath) 1006 { 1007 if (chdir(chdname) != 0) 1008 return; 1009 1010 if (!abspath) 1011 strcpy(curwin.path, chdname); 1012 else if (getcwd(curwin.path, PATH_MAX) == NULL) 1013 fatal("getcwd() error"); 1014 1015 switch (direction) { 1016 case RIGHT: 1017 curwin.highlight = 0; 1018 break; 1019 case LEFT: 1020 curwin.highlight = parwin.highlight; 1021 break; 1022 default: 1023 curwin.highlight = 0; 1024 break; 1025 1026 } 1027 set_win_files(&curwin); 1028 1029 get_dirname(parwin.path, curwin.path); 1030 set_win_files(&parwin); 1031 1032 update_child_win(); 1033 #if GIT_INFO 1034 retrieve_git_branch(gitbranch, sizeof(gitbranch), curwin.path); 1035 #endif 1036 } 1037 1038 void 1039 change_parent_dir(Direction direction) 1040 { 1041 char pp[PATH_MAX]; 1042 1043 switch (direction) { 1044 case UP: 1045 move_up(&parwin); 1046 break; 1047 case DOWN: 1048 move_down(&parwin); 1049 break; 1050 default: 1051 return; 1052 } 1053 1054 parwin.usehighlight = TRUE; 1055 curwin.highlight = 0; 1056 change_dir(get_fullpath(pp, &parwin, parwin.highlight), direction, FALSE); 1057 } 1058 1059 void 1060 open_child(bool exec) 1061 { 1062 sigset_t set; 1063 char mime[MIME_MAX], mimedefault[MIME_APP_MAX], pathbuf[PATH_MAX]; 1064 bool istext; 1065 1066 if (curwin.message != NULL) 1067 return; 1068 1069 if (!exec) { 1070 if (get_mime(mime, MIME_MAX, curwin.winfiles[curwin.highlight].d_name) != 0) { 1071 move(1, 1); 1072 clrtoeol(); 1073 printw("Can\'t open %s", curwin.winfiles[curwin.highlight].d_name); 1074 return; 1075 } 1076 1077 istext = strncmp(mime, "text/", 5) == 0; 1078 1079 if (!istext && get_mime_default(mimedefault, MIME_APP_MAX, mime) != 0) { 1080 move(1, 1); 1081 clrtoeol(); 1082 printw("Can\'t open for mime %s", mime); 1083 return; 1084 } 1085 } 1086 1087 endwin(); 1088 1089 if (sigemptyset (&set) == -1) 1090 fatal("Sigemptyset failed."); 1091 if (sigaddset(&set, SIGWINCH) == -1) 1092 fatal("Sigaddset failed."); 1093 if (sigprocmask(SIG_BLOCK, &set, NULL) != 0) 1094 fatal("Blocking sigprocmask failed."); 1095 1096 if (exec) 1097 run_command(PATH_MAX + 4, "\"./%s\"", curwin.winfiles[curwin.highlight].d_name); 1098 else 1099 run_command(PATH_MAX + 12, "%s \"%s\"", istext ? editor : "xdg-open", 1100 get_fullpath(pathbuf, &curwin, curwin.highlight)); 1101 1102 if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) 1103 fatal("Unblocking sigprocmask failed."); 1104 1105 erase(); 1106 noecho(); 1107 cbreak(); 1108 1109 refresh(); 1110 wrefresh(parwin.window); 1111 wrefresh(curwin.window); 1112 wrefresh(childwin.window); 1113 update_child_win(); 1114 } 1115 1116 void 1117 open_nohup_xdg(void) 1118 { 1119 char *cmdfmt = "nohup xdg-open \"%s\" > /dev/null 2>&1 &"; 1120 size_t size = PATH_MAX + strlen(cmdfmt); 1121 1122 run_command(size, cmdfmt, curwin.winfiles[curwin.highlight].d_name); 1123 } 1124 1125 void 1126 search(void) 1127 { 1128 int c, y, x; 1129 1130 clear_search(); 1131 1132 move(maxy-1, 0); 1133 clrtoeol(); 1134 1135 attron(COLOR_PAIR(PROMPT_COLOR)); 1136 addstr("Search name? "); 1137 attroff(COLOR_PAIR(PROMPT_COLOR)); 1138 1139 echo(); 1140 curs_set(1); 1141 1142 getyx(stdscr, y, x); 1143 1144 while ((c = mvwgetch(stdscr, y, x)) != 10) { 1145 switch (c) { 1146 case KEY_BACKSPACE: 1147 if (searchc > 0) { 1148 searchq[--(searchc)] = '\0'; 1149 move(y, --x); 1150 clrtoeol(); 1151 } 1152 break; 1153 default: 1154 if (searchc < SEARCHLEN) { 1155 searchq[(searchc)++] = c; 1156 searchq[searchc] = '\0'; 1157 move(y, ++x); 1158 } 1159 break; 1160 } 1161 first_search(); 1162 print_win(&curwin); 1163 } 1164 noecho(); 1165 curs_set(0); 1166 } 1167 1168 void 1169 move_top(DirWin *dirwin) 1170 { 1171 dirwin->highlight = 0; 1172 } 1173 1174 void 1175 move_bot(DirWin *dirwin) 1176 { 1177 dirwin->highlight = MAX(dirwin->filecount-1, 0); 1178 } 1179 1180 void 1181 move_page_up(DirWin *dirwin) 1182 { 1183 dirwin->highlight = MAX(dirwin->highlight - dirwin->maxy/2, 0); 1184 } 1185 1186 void 1187 move_page_down(DirWin *dirwin) 1188 { 1189 dirwin->highlight = MIN(dirwin->highlight + dirwin->maxy/2, 1190 MAX(dirwin->filecount-1, 0)); 1191 } 1192 1193 void 1194 move_up(DirWin *dirwin) 1195 { 1196 if (dirwin->highlight > 0) 1197 dirwin->highlight--; 1198 } 1199 1200 void 1201 move_down(DirWin *dirwin) 1202 { 1203 if (dirwin->highlight < dirwin->filecount-1) 1204 dirwin->highlight++; 1205 } 1206 1207 void 1208 first_search(void) 1209 { 1210 if (searchc == 0 || curwin.filecount == 0) 1211 return; 1212 1213 if (substr(curwin.winfiles[curwin.highlight].d_name, searchq, searchcs) != NULL) 1214 return; 1215 1216 for (int i = 0; i < MAX(curwin.filecount-1, 0); i++) { 1217 if (substr(curwin.winfiles[i].d_name, searchq, searchcs) != NULL) { 1218 curwin.highlight = i; 1219 return; 1220 } 1221 } 1222 } 1223 1224 void 1225 next_search(void) 1226 { 1227 if (searchc == 0) 1228 return; 1229 1230 for (int i = curwin.highlight; i < MAX(curwin.filecount-1, 0); i++) { 1231 if (substr(curwin.winfiles[i+1].d_name, searchq, searchcs) != NULL) { 1232 curwin.highlight = i+1; 1233 return; 1234 } 1235 } 1236 } 1237 1238 void 1239 prev_search(void) 1240 { 1241 if (searchc == 0) 1242 return; 1243 1244 for (int i = curwin.highlight; i > 0; i--) { 1245 if (substr(curwin.winfiles[i-1].d_name, searchq, searchcs) != NULL) { 1246 curwin.highlight = i-1; 1247 return; 1248 } 1249 } 1250 } 1251 1252 void 1253 free_dirwin(DirWin *dirwin) 1254 { 1255 if (dirwin->winfiles != NULL) 1256 free(dirwin->winfiles); 1257 dirwin->filecount = 0; 1258 dirwin->winfiles = NULL; 1259 dirwin->message = NULL; 1260 } 1261 1262 void 1263 clean(void) 1264 { 1265 wclear(curwin.window); 1266 wclear(parwin.window); 1267 wclear(childwin.window); 1268 1269 free(username); 1270 free(userhome); 1271 1272 clear(); 1273 1274 delwin(parwin.window); 1275 delwin(curwin.window); 1276 delwin(childwin.window); 1277 delwin(stdscr); 1278 1279 free_dirwin(&curwin); 1280 free_dirwin(&parwin); 1281 free_dirwin(&childwin); 1282 1283 clear_selected(); 1284 1285 endwin(); 1286 1287 #if GIT_INFO 1288 git_libgit2_shutdown(); 1289 #endif 1290 } 1291 1292 void 1293 fatal(const char *fmt, ...) 1294 { 1295 va_list ap; 1296 1297 va_start(ap, fmt); 1298 vfprintf(stderr, fmt, ap); 1299 va_end(ap); 1300 1301 clean(); 1302 1303 exit(EXIT_FAILURE); 1304 } 1305 1306 bool 1307 is_dir(DirWin *dirwin, size_t index) 1308 { 1309 if (dirwin->filecount <= 0) 1310 return FALSE; 1311 1312 if (dirwin->winfiles[index].d_type == DT_DIR) 1313 return TRUE; 1314 1315 return S_ISDIR(get_st_mode(dirwin, index)); 1316 } 1317 1318 mode_t 1319 get_st_mode(DirWin *dirwin, size_t index) 1320 { 1321 struct stat statbuf; 1322 char linkpath[PATH_MAX], abspath[PATH_MAX]; 1323 char *resolved; 1324 1325 get_fullpath(linkpath, dirwin, index); 1326 resolved = realpath(linkpath, abspath); 1327 if (resolved != NULL && lstat(resolved, &statbuf) >= 0) 1328 return statbuf.st_mode; 1329 1330 return -1; 1331 } 1332 1333 char * 1334 truncate_buf_prefix(char *buf, size_t bufsize, size_t max_len) 1335 { 1336 const char ellipsis[] = ".."; 1337 const size_t ell_len = sizeof(ellipsis) - 1; 1338 size_t keep, src_index, cur_len = 0; 1339 1340 if (!buf || bufsize == 0 || max_len < ell_len + 2) { 1341 if (bufsize > 0) buf[bufsize - 1] = '\0'; 1342 return buf; 1343 } 1344 1345 cur_len = strlen(buf); 1346 if (cur_len <= max_len - 1) 1347 return buf; 1348 1349 keep = max_len - 1 - ell_len; 1350 1351 if (keep == 0) { 1352 if (max_len >= ell_len + 1) { 1353 memcpy(buf, ellipsis, ell_len); 1354 buf[ell_len] = '\0'; 1355 } else { 1356 buf[0] = '.'; 1357 buf[1] = '\0'; 1358 } 1359 return buf; 1360 } 1361 1362 src_index = cur_len - keep; 1363 memmove(buf + ell_len, buf + src_index, keep + 1); 1364 memcpy(buf, ellipsis, ell_len); 1365 1366 if (ell_len + keep >= max_len) 1367 buf[max_len - 1] = '\0'; 1368 1369 return buf; 1370 } 1371 1372 char * 1373 human_readable_bytes(char *buf, intmax_t bytes) 1374 { 1375 double dblBytes = bytes; 1376 int power = SIZE_DECIMAL_FORMAT ? 1000 : 1024; 1377 char *suffix[] = {"B", "KB", "MB", "GB", "TB"}; 1378 size_t length = arrlen(suffix); 1379 size_t i = 0; 1380 1381 if (bytes > power) 1382 for (i = 0; (bytes / power) > 0 && i < length-1; i++, bytes /= power) 1383 dblBytes = bytes / (double) power; 1384 1385 sprintf(buf, "%.02lf%s", dblBytes, suffix[i]); 1386 1387 return buf; 1388 } 1389 1390 char * 1391 prompt_answer(char *buf, size_t size, const char *question) 1392 { 1393 move(maxy-1, 0); 1394 clrtoeol(); 1395 1396 attron(COLOR_PAIR(PROMPT_COLOR)); 1397 addstr(question); 1398 attroff(COLOR_PAIR(PROMPT_COLOR)); 1399 1400 if (size == 0) 1401 return buf; 1402 1403 echo(); 1404 curs_set(1); 1405 getnstr(buf, (int) size - 1); 1406 noecho(); 1407 curs_set(0); 1408 1409 return buf; 1410 } 1411 1412 bool 1413 prompt_confirm(size_t size, const char *fmt, ...) 1414 { 1415 char response[2], question[size]; 1416 va_list ap; 1417 1418 va_start(ap, fmt); 1419 vsnprintf(question, size, fmt, ap); 1420 va_end(ap); 1421 1422 prompt_answer(response, sizeof(response), question); 1423 1424 return strcasecmp(response, "y") == 0; 1425 } 1426 1427 int 1428 compare_file(const void *a, const void *b) 1429 { 1430 int typecompare; 1431 const WinFile *ma = a; 1432 const WinFile *mb = b; 1433 1434 if ((typecompare = ma->d_type - mb->d_type) == 0) 1435 return strcmp(ma->d_name, mb->d_name); 1436 1437 return typecompare; 1438 } 1439 1440 int 1441 get_mime(char *buf, size_t bufsize, const char *path) 1442 { 1443 char *cmdfmt = "xdg-mime query filetype \"%s\""; 1444 size_t size = PATH_MAX + strlen(cmdfmt); 1445 char cmd[size]; 1446 1447 snprintf(cmd, size, cmdfmt, path); 1448 1449 return read_command(buf, bufsize, cmd); 1450 } 1451 1452 int 1453 get_mime_default(char *buf, size_t bufsize, const char *mime) 1454 { 1455 char *cmdfmt = "xdg-mime query default \"%s\""; 1456 size_t cmdsize = MIME_MAX + strlen(cmdfmt); 1457 char cmd[cmdsize]; 1458 1459 snprintf(cmd, cmdsize, cmdfmt, mime); 1460 1461 return read_command(buf, bufsize, cmd); 1462 } 1463 1464 int 1465 read_command(char *buf, size_t bufsize, const char *cmd) 1466 { 1467 FILE *file; 1468 1469 if ((file = popen(cmd, "r")) == NULL) { 1470 buf = NULL; 1471 return 1; 1472 } 1473 1474 if (fgets(buf, bufsize, file) == NULL) { 1475 buf = NULL; 1476 pclose(file); 1477 return 2; 1478 } 1479 1480 buf[strcspn(buf, "\n")] = '\0'; 1481 1482 pclose(file); 1483 1484 return 0; 1485 } 1486 1487 void 1488 run_command(size_t size, const char *fmt, ...) 1489 { 1490 va_list ap; 1491 char cmd[size]; 1492 1493 va_start(ap, fmt); 1494 vsnprintf(cmd, size, fmt, ap); 1495 va_end(ap); 1496 1497 system(cmd); 1498 } 1499 1500 void 1501 run_sudo_command(size_t size, const char *fmt, ...) 1502 { 1503 va_list ap; 1504 char cmd[size], *msg; 1505 sigset_t set; 1506 1507 if (!USE_SUDO_COMMANDS) { 1508 msg = "Requires sudo command but USE_SUDO_COMMANDS is disabled."; 1509 print_error(strlen(msg), msg); 1510 return; 1511 } 1512 1513 endwin(); 1514 1515 if (sigemptyset (&set) == -1) 1516 fatal("Sigemptyset failed."); 1517 if (sigaddset(&set, SIGWINCH) == -1) 1518 fatal("Sigaddset failed."); 1519 if (sigprocmask(SIG_BLOCK, &set, NULL) != 0) 1520 fatal("Blocking sigprocmask failed."); 1521 1522 va_start(ap, fmt); 1523 vsnprintf(cmd, size, fmt, ap); 1524 va_end(ap); 1525 1526 system(cmd); 1527 1528 if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0) 1529 fatal("Unblocking sigprocmask failed."); 1530 1531 erase(); 1532 refresh(); 1533 wrefresh(parwin.window); 1534 wrefresh(curwin.window); 1535 wrefresh(childwin.window); 1536 } 1537 1538 int 1539 make_dir(void) 1540 { 1541 char name[PATH_MAX]; 1542 1543 prompt_answer(name, PATH_MAX, "Name of directory? "); 1544 1545 return mkdir(name, 0755); 1546 } 1547 1548 int 1549 make_file(void) 1550 { 1551 FILE *fptr; 1552 char name[PATH_MAX]; 1553 1554 prompt_answer(name, PATH_MAX, "Name of file? "); 1555 1556 if (strlen(name) > 0) { 1557 if ((fptr = fopen(name, "w")) == NULL) 1558 fatal("Error opening file \"%s\"\n", name); 1559 fclose(fptr); 1560 } 1561 1562 return 0; 1563 } 1564 1565 int 1566 rm_file(const char *fname) 1567 { 1568 char *msg, *rmdirfmt = "rm -rf \"%s\"", *pfmt = "Remove %s? (y/N) "; 1569 int res = 0; 1570 size_t cmdsize = PATH_MAX + strlen(rmdirfmt), psize; 1571 1572 if (fname == NULL) 1573 return 0; 1574 1575 psize = strlen(pfmt) + strlen(fname); 1576 1577 if (prompt_confirm(psize, pfmt, fname)) { 1578 if (is_dir(&curwin, curwin.highlight)) 1579 run_command(cmdsize, rmdirfmt, fname); 1580 else 1581 res = remove(fname); 1582 curwin.highlight = curwin.highlight > 0 ? curwin.highlight -1 : 0; 1583 } 1584 1585 if (res != 0) { 1586 switch (errno) { 1587 case EACCES: 1588 msg = "No permission."; 1589 break; 1590 case EEXIST: 1591 case ENOTEMPTY: 1592 msg = "Directory is not empty."; 1593 break; 1594 default: 1595 msg = "Could not remove file."; 1596 break; 1597 } 1598 print_error(strlen(msg), msg); 1599 } 1600 1601 return res; 1602 } 1603 1604 int 1605 rename_file(const char *fname) 1606 { 1607 char name[PATH_MAX]; 1608 1609 if (fname == NULL) 1610 return 0; 1611 1612 prompt_answer(name, PATH_MAX, "New name? "); 1613 1614 if (strlen(name) > 0) 1615 return rename(fname, name); 1616 1617 return 0; 1618 } 1619 1620 int 1621 make_mod(void) 1622 { 1623 char name[4]; 1624 bool isvalid; 1625 1626 if (curwin.filecount == 0) 1627 return 0; 1628 1629 prompt_answer(name, sizeof(name), "File mods (numeric)? "); 1630 1631 if (strlen(name) == 3) { 1632 isvalid = TRUE; 1633 for (int i = 0; i < 3; i++) { 1634 if (name[i] < '0' || name[i] > '7') 1635 isvalid = FALSE; 1636 } 1637 if (isvalid) 1638 chmod(curwin.winfiles[curwin.highlight].d_name, strtol(name, NULL, 8)); 1639 else 1640 return -1; 1641 } 1642 1643 return 0; 1644 } 1645 1646 int 1647 make_chown(void) 1648 { 1649 struct passwd *usr; 1650 struct group *grp; 1651 long maxlogin = sysconf(_SC_LOGIN_NAME_MAX); 1652 size_t len = maxlogin > 0 ? (size_t) maxlogin + 1 : 256; 1653 char owner[len], group[len], *err; 1654 1655 if (curwin.filecount == 0) 1656 return 0; 1657 1658 prompt_answer(owner, len, "Owner name? "); 1659 1660 if ((usr = getpwnam(owner)) == NULL) { 1661 err = "No results found for owner %s."; 1662 print_error(strlen(err) + len, err, owner); 1663 return -1; 1664 } 1665 1666 prompt_answer(group, len, "Group name? "); 1667 1668 if ((grp = getgrnam(group)) == NULL) { 1669 err = "No results found for group %s."; 1670 print_error(strlen(err) + len, err, group); 1671 return -1; 1672 } 1673 1674 if ((chown(curwin.winfiles[curwin.highlight].d_name, usr->pw_uid, grp->gr_gid)) != 0) { 1675 switch (errno) { 1676 case EACCES: 1677 case EPERM: 1678 sudo_chown(owner, group, (long) len); 1679 break; 1680 default: 1681 err = "Error during chown: %d."; 1682 print_error(strlen(err), err, errno); 1683 return errno; 1684 } 1685 } 1686 1687 return 0; 1688 } 1689 1690 int 1691 sudo_chown(char *owner, char *group, long namelen) 1692 { 1693 size_t cmdsize; 1694 char pathbuf[PATH_MAX], *cmdfmt = "sudo chown %s:%s %s"; 1695 1696 cmdsize = namelen*2 + PATH_MAX + strlen(cmdfmt); 1697 run_sudo_command(cmdsize, cmdfmt, owner, group, get_fullpath(pathbuf, &curwin, curwin.highlight)); 1698 1699 return 0; 1700 } 1701 1702 void 1703 make_access(void) 1704 { 1705 char pathbuf[PATH_MAX], accessdir[PATH_MAX]; 1706 char *cmdfmt = "ln -s \"%s\" \"%s/\"", *pfmt = "Make access \"%s\"? (y/N) "; 1707 size_t cmdsize = PATH_MAX*2 + strlen(cmdfmt), psize = strlen(pfmt) + PATH_MAX; 1708 1709 if (curwin.filecount == 0) 1710 return; 1711 1712 if (!prompt_confirm(psize, pfmt, curwin.winfiles[curwin.highlight].d_name)) 1713 return; 1714 1715 if (mkdir(replace_home(accessdir, LN_ACCESS_DIR), 0755) != 0 && errno != EEXIST) { 1716 print_error(256, "Could not create access directory: %s", strerror(errno)); 1717 return; 1718 } 1719 1720 run_command(cmdsize, cmdfmt, get_fullpath(pathbuf, &curwin, curwin.highlight), LN_ACCESS_DIR); 1721 } 1722 1723 void 1724 make_open_default(void) 1725 { 1726 int appsize = 256, qsize = MIME_MAX+25; 1727 char mime[MIME_MAX], question[qsize], app[appsize]; 1728 1729 if (curwin.filecount == 0) 1730 return; 1731 1732 if (get_mime(mime, MIME_MAX, curwin.winfiles[curwin.highlight].d_name) != 0) 1733 strcpy(mime, "Can't retrieve mime."); 1734 1735 snprintf(question, qsize, "Application for mime %s?", mime); 1736 1737 prompt_answer(app, appsize, question); 1738 1739 if (strlen(app) > 0) 1740 make_mime_default(mime, app); 1741 } 1742 1743 void 1744 make_mime_default(const char *mime, const char *app) 1745 { 1746 char *cmdfmt = "xdg-mime default \"%s\" \"%s\""; 1747 size_t cmdsize = MIME_MAX + 256 + strlen(cmdfmt); 1748 1749 run_command(cmdsize, cmdfmt, app, mime); 1750 } 1751 1752 void 1753 exe_selection(SelAction action, const char *askn) 1754 { 1755 char *pfmt = "%s selection (%d files) ? (y/N) "; 1756 size_t cmdsize = PATH_MAX*2 + 20; 1757 1758 if (selected == NULL || selc == 0) 1759 return; 1760 1761 if (askn != NULL && !prompt_confirm(strlen(pfmt) + strlen(askn) + 5, pfmt, askn, selc)) 1762 return; 1763 1764 switch (action) { 1765 case COPY: 1766 for (size_t i = 0; i <selc; i++) 1767 run_command(cmdsize, "cp -rf \"%s\" \"%s\"", selected[i], curwin.path); 1768 break; 1769 case REMOVE: 1770 for (size_t i = 0; i <selc; i++) 1771 run_command(cmdsize, "rm -rf \"%s\"", selected[i]); 1772 break; 1773 case MOVE: 1774 for (size_t i = 0; i <selc; i++) 1775 run_command(cmdsize, "mv \"%s\" \"%s\"", selected[i], curwin.path); 1776 break; 1777 default: 1778 break; 1779 } 1780 1781 clear_selected(); 1782 curwin.highlight = 0; 1783 set_win_files(&curwin); 1784 set_win_files(&parwin); 1785 update_child_win(); 1786 } 1787 1788 void 1789 select_file(const char *path) 1790 { 1791 if (path[0] == '\0') 1792 return; 1793 1794 if (remove_selected(path)) 1795 return; 1796 1797 if ((selected = (char**) realloc(selected, (selc+1) * sizeof(char *))) == NULL) 1798 fatal("Fatal: failed to realloc.\n"); 1799 1800 selected[selc] = strdup(path); 1801 (selc)++; 1802 } 1803 1804 void 1805 select_files(void) 1806 { 1807 char pathbuf[PATH_MAX]; 1808 1809 if (curwin.winfiles == NULL || curwin.filecount == 0){ 1810 start_sel = -1; 1811 return; 1812 } 1813 1814 if (start_sel == -1) { 1815 start_sel = curwin.highlight; 1816 return; 1817 } 1818 1819 for (int i = MIN(start_sel, curwin.highlight); i <= MAX(start_sel, curwin.highlight); i++) 1820 select_file(get_fullpath(pathbuf, &curwin, i)); 1821 1822 start_sel = -1; 1823 } 1824 1825 void 1826 clear_selected(void) 1827 { 1828 if (selected != NULL) 1829 for (size_t i = 0; i < selc; i++) 1830 free(selected[i]); 1831 1832 free(selected); 1833 selected = NULL; 1834 selc = 0; 1835 start_sel = -1; 1836 } 1837 1838 void 1839 clear_search(void) 1840 { 1841 searchq[0] = '\0'; 1842 searchc = 0; 1843 } 1844 1845 char * 1846 substr(const char *haystack, const char *needle, bool casesens) 1847 { 1848 return casesens ? strstr(haystack, needle) : strcasestr(haystack, needle); 1849 } 1850 1851 char * 1852 get_fullpath(char *buf, DirWin *dirwin, size_t index) 1853 { 1854 if (dirwin->filecount == 0 || dirwin->winfiles == NULL) { 1855 buf[0] = '\0'; 1856 return buf; 1857 } 1858 1859 index = MIN(index, dirwin->filecount-1); 1860 index = MAX(index, 0); 1861 1862 if (strcmp(dirwin->path, "/") == 0) 1863 snprintf(buf, PATH_MAX, "/%s", dirwin->winfiles[index].d_name); 1864 else if (snprintf(buf, PATH_MAX, "%s/%s", dirwin->path, 1865 dirwin->winfiles[index].d_name) >= PATH_MAX) { 1866 buf[0] = '\0'; 1867 } 1868 1869 return buf; 1870 } 1871 1872 char * 1873 replace_home(char *buf, const char *path) 1874 { 1875 const char *envv = "$HOME"; 1876 int len; 1877 1878 if (strncmp(path, envv, strlen(envv)) == 0) 1879 len = snprintf(buf, PATH_MAX, "%s%s", userhome, 1880 path + strlen(envv)); 1881 else 1882 len = snprintf(buf, PATH_MAX, "%s", path); 1883 1884 if (len < 0 || len >= PATH_MAX) 1885 buf[0] = '\0'; 1886 1887 return buf; 1888 } 1889 1890 char * 1891 get_dirname(char *buf, const char *path) 1892 { 1893 char *parent = dirname(strcpy(buf, path)); 1894 1895 if (parent != buf) 1896 memmove(buf, parent, strlen(parent) + 1); 1897 1898 return buf; 1899 } 1900 1901 #if GIT_INFO 1902 void 1903 init_status_options(git_status_options *out_opts) 1904 { 1905 memset(out_opts, 0, sizeof(*out_opts)); 1906 1907 out_opts->version = GIT_STATUS_OPTIONS_VERSION; 1908 out_opts->show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR; 1909 out_opts->flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED | GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS; 1910 } 1911 1912 int 1913 is_dirty(git_repository *repo, const git_status_options *opts) 1914 { 1915 size_t change_count; 1916 int dirty = 0; 1917 1918 git_status_list *list = NULL; 1919 if (git_status_list_new(&list, repo, opts) < 0) 1920 return -1; 1921 1922 change_count = git_status_list_entrycount(list); 1923 1924 for (size_t i = 0; i < change_count; ++i) { 1925 const git_status_entry *entry = git_status_byindex(list, i); 1926 if (entry->status != GIT_STATUS_CURRENT) { 1927 dirty = 1; 1928 break; 1929 } 1930 } 1931 1932 git_status_list_free(list); 1933 1934 return dirty; 1935 } 1936 1937 char * 1938 retrieve_git_branch(char *buf, size_t bufsize, const char *repo_path) 1939 { 1940 git_repository *repo = NULL; 1941 git_reference *head_ref = NULL; 1942 const char *branch_name = NULL; 1943 git_status_options opts; 1944 1945 if (!buf || bufsize == 0) 1946 return NULL; 1947 buf[0] = '\0'; 1948 1949 if (repo_path == NULL) 1950 return NULL; 1951 1952 if (git_repository_open_ext(&repo, repo_path, 0, NULL) == 0) 1953 if (git_repository_head(&head_ref, repo) == 0) 1954 if (git_reference_is_branch(head_ref)) 1955 branch_name = git_reference_shorthand(head_ref); 1956 1957 if (branch_name != NULL) { 1958 init_status_options(&opts); 1959 snprintf(buf, bufsize, " (%s%s)", branch_name, is_dirty(repo, &opts) ? "*" : ""); 1960 } 1961 1962 git_reference_free(head_ref); 1963 git_repository_free(repo); 1964 1965 return buf; 1966 } 1967 #endif