1 /* 2 * Copyright (C) 1984-2015 Mark Nudelman 3 * 4 * You may distribute under the terms of either the GNU General Public 5 * License or the Less License, as specified in the README file. 6 * 7 * For more information, see the README file. 8 */ 9 10 11 /* 12 * Routines to mess around with filenames (and files). 13 * Much of this is very OS dependent. 14 */ 15 16 #include "less.h" 17 #include "lglob.h" 18 #if MSDOS_COMPILER 19 #include <dos.h> 20 #if MSDOS_COMPILER==WIN32C && !defined(_MSC_VER) 21 #include <dir.h> 22 #endif 23 #if MSDOS_COMPILER==DJGPPC 24 #include <glob.h> 25 #include <dir.h> 26 #define _MAX_PATH PATH_MAX 27 #endif 28 #endif 29 #ifdef _OSK 30 #include <rbf.h> 31 #ifndef _OSK_MWC32 32 #include <modes.h> 33 #endif 34 #endif 35 #if OS2 36 #include <signal.h> 37 #endif 38 39 #if HAVE_STAT 40 #include <sys/stat.h> 41 #ifndef S_ISDIR 42 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) 43 #endif 44 #ifndef S_ISREG 45 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) 46 #endif 47 #endif 48 49 50 extern int force_open; 51 extern int secure; 52 extern int use_lessopen; 53 extern int ctldisp; 54 extern int utf_mode; 55 extern IFILE curr_ifile; 56 extern IFILE old_ifile; 57 #if SPACES_IN_FILENAMES 58 extern char openquote; 59 extern char closequote; 60 #endif 61 62 /* 63 * Remove quotes around a filename. 64 */ 65 public char * 66 shell_unquote(char *str) 67 { 68 char *name; 69 char *p; 70 71 name = p = (char *) ecalloc(strlen(str)+1, sizeof(char)); 72 if (*str == openquote) 73 { 74 str++; 75 while (*str != '\0') 76 { 77 if (*str == closequote) 78 { 79 if (str[1] != closequote) 80 break; 81 str++; 82 } 83 *p++ = *str++; 84 } 85 } else 86 { 87 char *esc = get_meta_escape(); 88 int esclen = (int) strlen(esc); 89 while (*str != '\0') 90 { 91 if (esclen > 0 && strncmp(str, esc, esclen) == 0) 92 str += esclen; 93 *p++ = *str++; 94 } 95 } 96 *p = '\0'; 97 return (name); 98 } 99 100 /* 101 * Get the shell's escape character. 102 */ 103 public char * 104 get_meta_escape(void) 105 { 106 char *s; 107 108 s = lgetenv("LESSMETAESCAPE"); 109 if (s == NULL) 110 s = DEF_METAESCAPE; 111 return (s); 112 } 113 114 /* 115 * Get the characters which the shell considers to be "metacharacters". 116 */ 117 static char * 118 metachars(void) 119 { 120 static char *mchars = NULL; 121 122 if (mchars == NULL) 123 { 124 mchars = lgetenv("LESSMETACHARS"); 125 if (mchars == NULL) 126 mchars = DEF_METACHARS; 127 } 128 return (mchars); 129 } 130 131 /* 132 * Is this a shell metacharacter? 133 */ 134 static int 135 metachar(char c) 136 { 137 return (strchr(metachars(), c) != NULL); 138 } 139 140 /* 141 * Insert a backslash before each metacharacter in a string. 142 */ 143 public char * 144 shell_quote(char *s) 145 { 146 char *p; 147 char *newstr; 148 int len; 149 char *esc = get_meta_escape(); 150 int esclen = (int) strlen(esc); 151 int use_quotes = 0; 152 int have_quotes = 0; 153 154 /* 155 * Determine how big a string we need to allocate. 156 */ 157 len = 1; /* Trailing null byte */ 158 for (p = s; *p != '\0'; p++) 159 { 160 len++; 161 if (*p == openquote || *p == closequote) 162 have_quotes = 1; 163 if (metachar(*p)) 164 { 165 if (esclen == 0) 166 { 167 /* 168 * We've got a metachar, but this shell 169 * doesn't support escape chars. Use quotes. 170 */ 171 use_quotes = 1; 172 } else 173 { 174 /* 175 * Allow space for the escape char. 176 */ 177 len += esclen; 178 } 179 } 180 } 181 if (use_quotes) 182 { 183 if (have_quotes) 184 /* 185 * We can't quote a string that contains quotes. 186 */ 187 return (NULL); 188 len = (int) strlen(s) + 3; 189 } 190 /* 191 * Allocate and construct the new string. 192 */ 193 newstr = p = (char *) ecalloc(len, sizeof(char)); 194 if (use_quotes) 195 { 196 SNPRINTF3(newstr, len, "%c%s%c", openquote, s, closequote); 197 } else 198 { 199 while (*s != '\0') 200 { 201 if (metachar(*s)) 202 { 203 /* 204 * Add the escape char. 205 */ 206 strcpy(p, esc); 207 p += esclen; 208 } 209 *p++ = *s++; 210 } 211 *p = '\0'; 212 } 213 return (newstr); 214 } 215 216 /* 217 * Return a pathname that points to a specified file in a specified directory. 218 * Return NULL if the file does not exist in the directory. 219 */ 220 static char * 221 dirfile(char *dirname, char *filename) 222 { 223 char *pathname; 224 char *qpathname; 225 int len; 226 int f; 227 228 if (dirname == NULL || *dirname == '\0') 229 return (NULL); 230 /* 231 * Construct the full pathname. 232 */ 233 len = (int) (strlen(dirname) + strlen(filename) + 2); 234 pathname = (char *) calloc(len, sizeof(char)); 235 if (pathname == NULL) 236 return (NULL); 237 SNPRINTF3(pathname, len, "%s%s%s", dirname, PATHNAME_SEP, filename); 238 /* 239 * Make sure the file exists. 240 */ 241 qpathname = shell_unquote(pathname); 242 f = open(qpathname, OPEN_READ); 243 if (f < 0) 244 { 245 free(pathname); 246 pathname = NULL; 247 } else 248 { 249 close(f); 250 } 251 free(qpathname); 252 return (pathname); 253 } 254 255 /* 256 * Return the full pathname of the given file in the "home directory". 257 */ 258 public char * 259 homefile(char *filename) 260 { 261 char *pathname; 262 263 /* 264 * Try $HOME/filename. 265 */ 266 pathname = dirfile(lgetenv("HOME"), filename); 267 if (pathname != NULL) 268 return (pathname); 269 #if OS2 270 /* 271 * Try $INIT/filename. 272 */ 273 pathname = dirfile(lgetenv("INIT"), filename); 274 if (pathname != NULL) 275 return (pathname); 276 #endif 277 #if MSDOS_COMPILER || OS2 278 /* 279 * Look for the file anywhere on search path. 280 */ 281 pathname = (char *) calloc(_MAX_PATH, sizeof(char)); 282 #if MSDOS_COMPILER==DJGPPC 283 { 284 char *res = searchpath(filename); 285 if (res == 0) 286 *pathname = '\0'; 287 else 288 strcpy(pathname, res); 289 } 290 #else 291 _searchenv(filename, "PATH", pathname); 292 #endif 293 if (*pathname != '\0') 294 return (pathname); 295 free(pathname); 296 #endif 297 return (NULL); 298 } 299 300 /* 301 * Expand a string, substituting any "%" with the current filename, 302 * and any "#" with the previous filename. 303 * But a string of N "%"s is just replaced with N-1 "%"s. 304 * Likewise for a string of N "#"s. 305 * {{ This is a lot of work just to support % and #. }} 306 */ 307 public char * 308 fexpand(char *s) 309 { 310 char *fr, *to; 311 int n; 312 char *e; 313 IFILE ifile; 314 315 #define fchar_ifile(c) \ 316 ((c) == '%' ? curr_ifile : \ 317 (c) == '#' ? old_ifile : NULL_IFILE) 318 319 /* 320 * Make one pass to see how big a buffer we 321 * need to allocate for the expanded string. 322 */ 323 n = 0; 324 for (fr = s; *fr != '\0'; fr++) 325 { 326 switch (*fr) 327 { 328 case '%': 329 case '#': 330 if (fr > s && fr[-1] == *fr) 331 { 332 /* 333 * Second (or later) char in a string 334 * of identical chars. Treat as normal. 335 */ 336 n++; 337 } else if (fr[1] != *fr) 338 { 339 /* 340 * Single char (not repeated). Treat specially. 341 */ 342 ifile = fchar_ifile(*fr); 343 if (ifile == NULL_IFILE) 344 n++; 345 else 346 n += (int) strlen(get_filename(ifile)); 347 } 348 /* 349 * Else it is the first char in a string of 350 * identical chars. Just discard it. 351 */ 352 break; 353 default: 354 n++; 355 break; 356 } 357 } 358 359 e = (char *) ecalloc(n+1, sizeof(char)); 360 361 /* 362 * Now copy the string, expanding any "%" or "#". 363 */ 364 to = e; 365 for (fr = s; *fr != '\0'; fr++) 366 { 367 switch (*fr) 368 { 369 case '%': 370 case '#': 371 if (fr > s && fr[-1] == *fr) 372 { 373 *to++ = *fr; 374 } else if (fr[1] != *fr) 375 { 376 ifile = fchar_ifile(*fr); 377 if (ifile == NULL_IFILE) 378 *to++ = *fr; 379 else 380 { 381 strcpy(to, get_filename(ifile)); 382 to += strlen(to); 383 } 384 } 385 break; 386 default: 387 *to++ = *fr; 388 break; 389 } 390 } 391 *to = '\0'; 392 return (e); 393 } 394 395 396 #if TAB_COMPLETE_FILENAME 397 398 /* 399 * Return a blank-separated list of filenames which "complete" 400 * the given string. 401 */ 402 public char * 403 fcomplete(char *s) 404 { 405 char *fpat; 406 char *qs; 407 408 if (secure) 409 return (NULL); 410 /* 411 * Complete the filename "s" by globbing "s*". 412 */ 413 #if MSDOS_COMPILER && (MSDOS_COMPILER == MSOFTC || MSDOS_COMPILER == BORLANDC) 414 /* 415 * But in DOS, we have to glob "s*.*". 416 * But if the final component of the filename already has 417 * a dot in it, just do "s*". 418 * (Thus, "FILE" is globbed as "FILE*.*", 419 * but "FILE.A" is globbed as "FILE.A*"). 420 */ 421 { 422 char *slash; 423 int len; 424 for (slash = s+strlen(s)-1; slash > s; slash--) 425 if (*slash == *PATHNAME_SEP || *slash == '/') 426 break; 427 len = (int) strlen(s) + 4; 428 fpat = (char *) ecalloc(len, sizeof(char)); 429 if (strchr(slash, '.') == NULL) 430 SNPRINTF1(fpat, len, "%s*.*", s); 431 else 432 SNPRINTF1(fpat, len, "%s*", s); 433 } 434 #else 435 { 436 int len = (int) strlen(s) + 2; 437 fpat = (char *) ecalloc(len, sizeof(char)); 438 SNPRINTF1(fpat, len, "%s*", s); 439 } 440 #endif 441 qs = lglob(fpat); 442 s = shell_unquote(qs); 443 if (strcmp(s,fpat) == 0) 444 { 445 /* 446 * The filename didn't expand. 447 */ 448 free(qs); 449 qs = NULL; 450 } 451 free(s); 452 free(fpat); 453 return (qs); 454 } 455 #endif 456 457 /* 458 * Try to determine if a file is "binary". 459 * This is just a guess, and we need not try too hard to make it accurate. 460 */ 461 public int 462 bin_file(int f) 463 { 464 int n; 465 int bin_count = 0; 466 char data[256]; 467 constant char* p; 468 char* pend; 469 470 if (!seekable(f)) 471 return (0); 472 if (lseek(f, (off_t)0, SEEK_SET) == BAD_LSEEK) 473 return (0); 474 n = read(f, data, sizeof(data)); 475 if (n <= 0) 476 return (0); 477 if (utf_mode) 478 { 479 bin_count = utf_bin_count(data, n); 480 } else 481 { 482 pend = &data[n]; 483 for (p = data; p < pend; ) 484 { 485 LWCHAR c = step_char(&p, +1, pend); 486 if (ctldisp == OPT_ONPLUS && IS_CSI_START(c)) 487 { 488 do { 489 c = step_char(&p, +1, pend); 490 } while (p < pend && is_ansi_middle(c)); 491 } else if (binary_char(c)) 492 bin_count++; 493 } 494 } 495 /* 496 * Call it a binary file if there are more than 5 binary characters 497 * in the first 256 bytes of the file. 498 */ 499 return (bin_count > 5); 500 } 501 502 /* 503 * Try to determine the size of a file by seeking to the end. 504 */ 505 static POSITION 506 seek_filesize(int f) 507 { 508 off_t spos; 509 510 spos = lseek(f, (off_t)0, SEEK_END); 511 if (spos == BAD_LSEEK) 512 return (NULL_POSITION); 513 return ((POSITION) spos); 514 } 515 516 /* 517 * Read a string from a file. 518 * Return a pointer to the string in memory. 519 */ 520 static char * 521 readfd(FILE *fd) 522 { 523 int len; 524 int ch; 525 char *buf; 526 char *p; 527 528 /* 529 * Make a guess about how many chars in the string 530 * and allocate a buffer to hold it. 531 */ 532 len = 100; 533 buf = (char *) ecalloc(len, sizeof(char)); 534 for (p = buf; ; p++) 535 { 536 if ((ch = getc(fd)) == '\n' || ch == EOF) 537 break; 538 if (p - buf >= len-1) 539 { 540 /* 541 * The string is too big to fit in the buffer we have. 542 * Allocate a new buffer, twice as big. 543 */ 544 len *= 2; 545 *p = '\0'; 546 p = (char *) ecalloc(len, sizeof(char)); 547 strcpy(p, buf); 548 free(buf); 549 buf = p; 550 p = buf + strlen(buf); 551 } 552 *p = ch; 553 } 554 *p = '\0'; 555 return (buf); 556 } 557 558 559 560 #if HAVE_POPEN 561 562 FILE *popen(); 563 564 /* 565 * Execute a shell command. 566 * Return a pointer to a pipe connected to the shell command's standard output. 567 */ 568 static FILE * 569 shellcmd(char *cmd) 570 { 571 FILE *fd; 572 573 #if HAVE_SHELL 574 char *shell; 575 576 shell = lgetenv("SHELL"); 577 if (shell != NULL && *shell != '\0') 578 { 579 char *scmd; 580 char *esccmd; 581 582 /* 583 * Read the output of <$SHELL -c cmd>. 584 * Escape any metacharacters in the command. 585 */ 586 esccmd = shell_quote(cmd); 587 if (esccmd == NULL) 588 { 589 fd = popen(cmd, "r"); 590 } else 591 { 592 int len = (int) (strlen(shell) + strlen(esccmd) + 5); 593 scmd = (char *) ecalloc(len, sizeof(char)); 594 SNPRINTF3(scmd, len, "%s %s %s", shell, shell_coption(), esccmd); 595 free(esccmd); 596 fd = popen(scmd, "r"); 597 free(scmd); 598 } 599 } else 600 #endif 601 { 602 fd = popen(cmd, "r"); 603 } 604 /* 605 * Redirection in `popen' might have messed with the 606 * standard devices. Restore binary input mode. 607 */ 608 SET_BINARY(0); 609 return (fd); 610 } 611 612 #endif /* HAVE_POPEN */ 613 614 615 /* 616 * Expand a filename, doing any system-specific metacharacter substitutions. 617 */ 618 public char * 619 lglob(char *filename) 620 { 621 char *gfilename; 622 char *ofilename; 623 624 ofilename = fexpand(filename); 625 if (secure) 626 return (ofilename); 627 filename = shell_unquote(ofilename); 628 629 #ifdef DECL_GLOB_LIST 630 { 631 /* 632 * The globbing function returns a list of names. 633 */ 634 int length; 635 char *p; 636 char *qfilename; 637 DECL_GLOB_LIST(list) 638 639 GLOB_LIST(filename, list); 640 if (GLOB_LIST_FAILED(list)) 641 { 642 free(filename); 643 return (ofilename); 644 } 645 length = 1; /* Room for trailing null byte */ 646 for (SCAN_GLOB_LIST(list, p)) 647 { 648 INIT_GLOB_LIST(list, p); 649 qfilename = shell_quote(p); 650 if (qfilename != NULL) 651 { 652 length += strlen(qfilename) + 1; 653 free(qfilename); 654 } 655 } 656 gfilename = (char *) ecalloc(length, sizeof(char)); 657 for (SCAN_GLOB_LIST(list, p)) 658 { 659 INIT_GLOB_LIST(list, p); 660 qfilename = shell_quote(p); 661 if (qfilename != NULL) 662 { 663 sprintf(gfilename + strlen(gfilename), "%s ", qfilename); 664 free(qfilename); 665 } 666 } 667 /* 668 * Overwrite the final trailing space with a null terminator. 669 */ 670 *--p = '\0'; 671 GLOB_LIST_DONE(list); 672 } 673 #else 674 #ifdef DECL_GLOB_NAME 675 { 676 /* 677 * The globbing function returns a single name, and 678 * is called multiple times to walk thru all names. 679 */ 680 char *p; 681 int len; 682 int n; 683 char *pathname; 684 char *qpathname; 685 DECL_GLOB_NAME(fnd,drive,dir,fname,ext,handle) 686 687 GLOB_FIRST_NAME(filename, &fnd, handle); 688 if (GLOB_FIRST_FAILED(handle)) 689 { 690 free(filename); 691 return (ofilename); 692 } 693 694 _splitpath(filename, drive, dir, fname, ext); 695 len = 100; 696 gfilename = (char *) ecalloc(len, sizeof(char)); 697 p = gfilename; 698 do { 699 n = (int) (strlen(drive) + strlen(dir) + strlen(fnd.GLOB_NAME) + 1); 700 pathname = (char *) ecalloc(n, sizeof(char)); 701 SNPRINTF3(pathname, n, "%s%s%s", drive, dir, fnd.GLOB_NAME); 702 qpathname = shell_quote(pathname); 703 free(pathname); 704 if (qpathname != NULL) 705 { 706 n = (int) strlen(qpathname); 707 while (p - gfilename + n + 2 >= len) 708 { 709 /* 710 * No room in current buffer. 711 * Allocate a bigger one. 712 */ 713 len *= 2; 714 *p = '\0'; 715 p = (char *) ecalloc(len, sizeof(char)); 716 strcpy(p, gfilename); 717 free(gfilename); 718 gfilename = p; 719 p = gfilename + strlen(gfilename); 720 } 721 strcpy(p, qpathname); 722 free(qpathname); 723 p += n; 724 *p++ = ' '; 725 } 726 } while (GLOB_NEXT_NAME(handle, &fnd) == 0); 727 728 /* 729 * Overwrite the final trailing space with a null terminator. 730 */ 731 *--p = '\0'; 732 GLOB_NAME_DONE(handle); 733 } 734 #else 735 #if HAVE_POPEN 736 { 737 /* 738 * We get the shell to glob the filename for us by passing 739 * an "echo" command to the shell and reading its output. 740 */ 741 FILE *fd; 742 char *s; 743 char *lessecho; 744 char *cmd; 745 char *esc; 746 int len; 747 748 esc = get_meta_escape(); 749 if (strlen(esc) == 0) 750 esc = "-"; 751 esc = shell_quote(esc); 752 if (esc == NULL) 753 { 754 free(filename); 755 return (ofilename); 756 } 757 lessecho = lgetenv("LESSECHO"); 758 if (lessecho == NULL || *lessecho == '\0') 759 lessecho = "lessecho"; 760 /* 761 * Invoke lessecho, and read its output (a globbed list of filenames). 762 */ 763 len = (int) (strlen(lessecho) + strlen(ofilename) + (7*strlen(metachars())) + 24); 764 cmd = (char *) ecalloc(len, sizeof(char)); 765 SNPRINTF4(cmd, len, "%s -p0x%x -d0x%x -e%s ", lessecho, openquote, closequote, esc); 766 free(esc); 767 for (s = metachars(); *s != '\0'; s++) 768 sprintf(cmd + strlen(cmd), "-n0x%x ", *s); 769 sprintf(cmd + strlen(cmd), "-- %s", ofilename); 770 fd = shellcmd(cmd); 771 free(cmd); 772 if (fd == NULL) 773 { 774 /* 775 * Cannot create the pipe. 776 * Just return the original (fexpanded) filename. 777 */ 778 free(filename); 779 return (ofilename); 780 } 781 gfilename = readfd(fd); 782 pclose(fd); 783 if (*gfilename == '\0') 784 { 785 free(gfilename); 786 free(filename); 787 return (ofilename); 788 } 789 } 790 #else 791 /* 792 * No globbing functions at all. Just use the fexpanded filename. 793 */ 794 gfilename = save(filename); 795 #endif 796 #endif 797 #endif 798 free(filename); 799 free(ofilename); 800 return (gfilename); 801 } 802 803 /* 804 * Return number of %s escapes in a string. 805 * Return a large number if there are any other % escapes besides %s. 806 */ 807 static int 808 num_pct_s(char *lessopen) 809 { 810 int num = 0; 811 812 while (*lessopen != '\0') 813 { 814 if (*lessopen == '%') 815 { 816 if (lessopen[1] == '%') 817 ++lessopen; 818 else if (lessopen[1] == 's') 819 ++num; 820 else 821 return (999); 822 } 823 ++lessopen; 824 } 825 return (num); 826 } 827 828 /* 829 * See if we should open a "replacement file" 830 * instead of the file we're about to open. 831 */ 832 public char * 833 open_altfile(char *filename, int *pf, void **pfd) 834 { 835 #if !HAVE_POPEN 836 return (NULL); 837 #else 838 char *lessopen; 839 char *cmd; 840 int len; 841 FILE *fd; 842 #if HAVE_FILENO 843 int returnfd = 0; 844 #endif 845 846 if (!use_lessopen || secure) 847 return (NULL); 848 ch_ungetchar(-1); 849 if ((lessopen = lgetenv("LESSOPEN")) == NULL) 850 return (NULL); 851 while (*lessopen == '|') 852 { 853 /* 854 * If LESSOPEN starts with a |, it indicates 855 * a "pipe preprocessor". 856 */ 857 #if !HAVE_FILENO 858 error("LESSOPEN pipe is not supported", NULL_PARG); 859 return (NULL); 860 #else 861 lessopen++; 862 returnfd++; 863 #endif 864 } 865 if (*lessopen == '-') { 866 /* 867 * Lessopen preprocessor will accept "-" as a filename. 868 */ 869 lessopen++; 870 } else { 871 if (strcmp(filename, "-") == 0) 872 return (NULL); 873 } 874 if (num_pct_s(lessopen) > 1) 875 { 876 error("Invalid LESSOPEN variable", NULL_PARG); 877 return (NULL); 878 } 879 880 len = (int) (strlen(lessopen) + strlen(filename) + 2); 881 cmd = (char *) ecalloc(len, sizeof(char)); 882 SNPRINTF1(cmd, len, lessopen, filename); 883 fd = shellcmd(cmd); 884 free(cmd); 885 if (fd == NULL) 886 { 887 /* 888 * Cannot create the pipe. 889 */ 890 return (NULL); 891 } 892 #if HAVE_FILENO 893 if (returnfd) 894 { 895 int f; 896 char c; 897 898 /* 899 * Read one char to see if the pipe will produce any data. 900 * If it does, push the char back on the pipe. 901 */ 902 f = fileno(fd); 903 SET_BINARY(f); 904 if (read(f, &c, 1) != 1) 905 { 906 /* 907 * Pipe is empty. 908 * If more than 1 pipe char was specified, 909 * the exit status tells whether the file itself 910 * is empty, or if there is no alt file. 911 * If only one pipe char, just assume no alt file. 912 */ 913 int status = pclose(fd); 914 if (returnfd > 1 && status == 0) { 915 *pfd = NULL; 916 *pf = -1; 917 return (save(FAKE_EMPTYFILE)); 918 } 919 return (NULL); 920 } 921 ch_ungetchar(c); 922 *pfd = (void *) fd; 923 *pf = f; 924 return (save("-")); 925 } 926 #endif 927 cmd = readfd(fd); 928 pclose(fd); 929 if (*cmd == '\0') 930 /* 931 * Pipe is empty. This means there is no alt file. 932 */ 933 return (NULL); 934 return (cmd); 935 #endif /* HAVE_POPEN */ 936 } 937 938 /* 939 * Close a replacement file. 940 */ 941 public void 942 close_altfile(char *altfilename, char *filename, void *pipefd) 943 { 944 #if HAVE_POPEN 945 char *lessclose; 946 FILE *fd; 947 char *cmd; 948 int len; 949 950 if (secure) 951 return; 952 if (pipefd != NULL) 953 { 954 #if OS2 955 /* 956 * The pclose function of OS/2 emx sometimes fails. 957 * Send SIGINT to the piped process before closing it. 958 */ 959 kill(((FILE*)pipefd)->_pid, SIGINT); 960 #endif 961 pclose((FILE*) pipefd); 962 } 963 if ((lessclose = lgetenv("LESSCLOSE")) == NULL) 964 return; 965 if (num_pct_s(lessclose) > 2) 966 { 967 error("Invalid LESSCLOSE variable", NULL_PARG); 968 return; 969 } 970 len = (int) (strlen(lessclose) + strlen(filename) + strlen(altfilename) + 2); 971 cmd = (char *) ecalloc(len, sizeof(char)); 972 SNPRINTF2(cmd, len, lessclose, filename, altfilename); 973 fd = shellcmd(cmd); 974 free(cmd); 975 if (fd != NULL) 976 pclose(fd); 977 #endif 978 } 979 980 /* 981 * Is the specified file a directory? 982 */ 983 public int 984 is_dir(char *filename) 985 { 986 int isdir = 0; 987 988 filename = shell_unquote(filename); 989 #if HAVE_STAT 990 { 991 int r; 992 struct stat statbuf; 993 994 r = stat(filename, &statbuf); 995 isdir = (r >= 0 && S_ISDIR(statbuf.st_mode)); 996 } 997 #else 998 #ifdef _OSK 999 { 1000 int f; 1001 1002 f = open(filename, S_IREAD | S_IFDIR); 1003 if (f >= 0) 1004 close(f); 1005 isdir = (f >= 0); 1006 } 1007 #endif 1008 #endif 1009 free(filename); 1010 return (isdir); 1011 } 1012 1013 /* 1014 * Returns NULL if the file can be opened and 1015 * is an ordinary file, otherwise an error message 1016 * (if it cannot be opened or is a directory, etc.) 1017 */ 1018 public char * 1019 bad_file(char *filename) 1020 { 1021 char *m = NULL; 1022 1023 filename = shell_unquote(filename); 1024 if (!force_open && is_dir(filename)) 1025 { 1026 static char is_a_dir[] = " is a directory"; 1027 1028 m = (char *) ecalloc(strlen(filename) + sizeof(is_a_dir), 1029 sizeof(char)); 1030 strcpy(m, filename); 1031 strcat(m, is_a_dir); 1032 } else 1033 { 1034 #if HAVE_STAT 1035 int r; 1036 struct stat statbuf; 1037 1038 r = stat(filename, &statbuf); 1039 if (r < 0) 1040 { 1041 m = errno_message(filename); 1042 } else if (force_open) 1043 { 1044 m = NULL; 1045 } else if (!S_ISREG(statbuf.st_mode)) 1046 { 1047 static char not_reg[] = " is not a regular file (use -f to see it)"; 1048 m = (char *) ecalloc(strlen(filename) + sizeof(not_reg), 1049 sizeof(char)); 1050 strcpy(m, filename); 1051 strcat(m, not_reg); 1052 } 1053 #endif 1054 } 1055 free(filename); 1056 return (m); 1057 } 1058 1059 /* 1060 * Return the size of a file, as cheaply as possible. 1061 * In Unix, we can stat the file. 1062 */ 1063 public POSITION 1064 filesize(int f) 1065 { 1066 #if HAVE_STAT 1067 struct stat statbuf; 1068 1069 if (fstat(f, &statbuf) >= 0) 1070 return ((POSITION) statbuf.st_size); 1071 #else 1072 #ifdef _OSK 1073 long size; 1074 1075 if ((size = (long) _gs_size(f)) >= 0) 1076 return ((POSITION) size); 1077 #endif 1078 #endif 1079 return (seek_filesize(f)); 1080 } 1081 1082 /* 1083 * 1084 */ 1085 public char * 1086 shell_coption(void) 1087 { 1088 return ("-c"); 1089 } 1090 1091 /* 1092 * Return last component of a pathname. 1093 */ 1094 public char * 1095 last_component(char *name) 1096 { 1097 char *slash; 1098 1099 for (slash = name + strlen(name); slash > name; ) 1100 { 1101 --slash; 1102 if (*slash == *PATHNAME_SEP || *slash == '/') 1103 return (slash + 1); 1104 } 1105 return (name); 1106 } 1107 1108