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