1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122
|
/*******************************************************************************
* *
* file.c -- Nirvana Editor file i/o *
* *
* Copyright (C) 1999 Mark Edel *
* *
* This is free software; you can redistribute it and/or modify it under the *
* terms of the GNU General Public License as published by the Free Software *
* Foundation; either version 2 of the License, or (at your option) any later *
* version. In addition, you may distribute versions of this program linked to *
* Motif or Open Motif. See README for details. *
* *
* This software is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
* for more details. *
* *
* You should have received a copy of the GNU General Public License along with *
* software; if not, write to the Free Software Foundation, Inc., 59 Temple *
* Place, Suite 330, Boston, MA 02111-1307 USA *
* *
* Nirvana Text Editor *
* May 10, 1991 *
* *
* Written by Mark Edel *
* *
*******************************************************************************/
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#include "file.h"
#include "textBuf.h"
#include "text.h"
#include "window.h"
#include "preferences.h"
#include "undo.h"
#include "menu.h"
#include "tags.h"
#include "server.h"
#include "interpret.h"
#include "../util/misc.h"
#include "../util/DialogF.h"
#include "../util/fileUtils.h"
#include "../util/getfiles.h"
#include "../util/printUtils.h"
#include "../util/utils.h"
#include "../util/nedit_malloc.h"
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef VMS
#include "../util/VMSparam.h"
#include <types.h>
#include <stat.h>
#include <unixio.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#ifndef __MVS__
#include <sys/param.h>
#endif
#include <fcntl.h>
#endif /*VMS*/
#include <Xm/Xm.h>
#include <Xm/ToggleB.h>
#include <Xm/FileSB.h>
#include <Xm/RowColumn.h>
#include <Xm/Form.h>
#include <Xm/Label.h>
#ifdef HAVE_DEBUG_H
#include "../debug.h"
#endif
#include <inttypes.h>
/* Maximum frequency in miliseconds of checking for external modifications.
The periodic check is only performed on buffer modification, and the check
interval is only to prevent checking on every keystroke in case of a file
system which is slow to process stat requests (which I'm not sure exists) */
#define MOD_CHECK_INTERVAL 3000
static int doSave(WindowInfo *window);
static void safeClose(WindowInfo *window);
static int doOpen(WindowInfo *window, const char *name, const char *path,
int flags);
static void backupFileName(WindowInfo *window, char *name, size_t len);
static int writeBckVersion(WindowInfo *window);
static int bckError(WindowInfo *window, const char *errString, const char *file);
static int fileWasModifiedExternally(WindowInfo *window);
static const char *errorString(void);
static void addWrapNewlines(WindowInfo *window);
static void setFormatCB(Widget w, XtPointer clientData, XtPointer callData);
static void addWrapCB(Widget w, XtPointer clientData, XtPointer callData);
static int cmpWinAgainstFile(WindowInfo *window, const char *fileName);
static int min(int i1, int i2);
static void modifiedWindowDestroyedCB(Widget w, XtPointer clientData,
XtPointer callData);
static void forceShowLineNumbers(WindowInfo *window);
#ifdef VMS
void removeVersionNumber(char *fileName);
#endif /*VMS*/
WindowInfo *EditNewFile(WindowInfo *inWindow, char *geometry, int iconic,
const char *languageMode, const char *defaultPath)
{
char name[MAXPATHLEN];
WindowInfo *window;
size_t pathlen;
char *path;
/*... test for creatability? */
/* Find a (relatively) unique name for the new file */
UniqueUntitledName(name);
/* create new window/document */
if (inWindow)
window = CreateDocument(inWindow, name);
else
window = CreateWindow(name, geometry, iconic);
path = window->path;
strcpy(window->filename, name);
strcpy(path, (defaultPath && *defaultPath) ? defaultPath : GetCurrentDir());
pathlen = strlen(window->path);
#ifndef VMS
/* do we have a "/" at the end? if not, add one */
if (0 < pathlen && path[pathlen - 1] != '/' && pathlen < MAXPATHLEN - 1) {
strcpy(&path[pathlen], "/");
}
#else /* VMS */
/* A logical name should be followed by a colon so that the filename can
be added to it to make a full file specification; otherwise a directory
path had the form
device_or_logicalname:[dir.dir.dir]
this requires no separator before the file name.
*/
if (0 < pathlen && strchr(":]", path[pathlen - 1]) == NULL) {
strcpy(&path[pathlen], ":"); /* could not find a separator at end */
}
/* TODO: is this enough for VMS? what of posix emulation? */
/* TODO: what about other platforms? */
#endif /* VMS */
SetWindowModified(window, FALSE);
CLEAR_ALL_LOCKS(window->lockReasons);
UpdateWindowReadOnly(window);
UpdateStatsLine(window);
UpdateWindowTitle(window);
RefreshTabState(window);
if (languageMode == NULL)
DetermineLanguageMode(window, True);
else
SetLanguageMode(window, FindLanguageMode(languageMode), True);
ShowTabBar(window, GetShowTabBar(window));
if (iconic && IsIconic(window))
RaiseDocument(window);
else
RaiseDocumentWindow(window);
SortTabBar(window);
return window;
}
/*
** Open an existing file specified by name and path. Use the window inWindow
** unless inWindow is NULL or points to a window which is already in use
** (displays a file other than Untitled, or is Untitled but modified). Flags
** can be any of:
**
** CREATE: If file is not found, (optionally) prompt the
** user whether to create
** SUPPRESS_CREATE_WARN When creating a file, don't ask the user
** PREF_READ_ONLY Make the file read-only regardless
**
** If languageMode is passed as NULL, it will be determined automatically
** from the file extension or file contents.
**
** If bgOpen is True, then the file will be open in background. This
** works in association with the SetLanguageMode() function that has
** the syntax highlighting deferred, in order to speed up the file-
** opening operation when multiple files are being opened in succession.
*/
WindowInfo *EditExistingFile(WindowInfo *inWindow, const char *name,
const char *path, int flags, char *geometry, int iconic,
const char *languageMode, int tabbed, int bgOpen)
{
WindowInfo *window;
char fullname[MAXPATHLEN];
/* first look to see if file is already displayed in a window */
window = FindWindowWithFile(name, path);
if (window != NULL) {
if (!bgOpen) {
if (iconic)
RaiseDocument(window);
else
RaiseDocumentWindow(window);
}
return window;
}
/* If an existing window isn't specified; or the window is already
in use (not Untitled or Untitled and modified), or is currently
busy running a macro; create the window */
if (inWindow == NULL) {
window = CreateWindow(name, geometry, iconic);
}
else if (inWindow->filenameSet || inWindow->fileChanged ||
inWindow->macroCmdData != NULL) {
if (tabbed) {
window = CreateDocument(inWindow, name);
}
else {
window = CreateWindow(name, geometry, iconic);
}
}
else {
/* open file in untitled document */
window = inWindow;
strcpy(window->path, path);
strcpy(window->filename, name);
if (!iconic && !bgOpen) {
RaiseDocumentWindow(window);
}
}
/* Open the file */
if (!doOpen(window, name, path, flags)) {
/* The user may have destroyed the window instead of closing the
warning dialog; don't close it twice */
safeClose(window);
return NULL;
}
forceShowLineNumbers(window);
/* Decide what language mode to use, trigger language specific actions */
if (languageMode == NULL)
DetermineLanguageMode(window, True);
else
SetLanguageMode(window, FindLanguageMode(languageMode), True);
/* update tab label and tooltip */
RefreshTabState(window);
SortTabBar(window);
ShowTabBar(window, GetShowTabBar(window));
if (!bgOpen)
RaiseDocument(window);
/* Bring the title bar and statistics line up to date, doOpen does
not necessarily set the window title or read-only status */
UpdateWindowTitle(window);
UpdateWindowReadOnly(window);
UpdateStatsLine(window);
/* Add the name to the convenience menu of previously opened files */
strcpy(fullname, path);
strcat(fullname, name);
if(GetPrefAlwaysCheckRelTagsSpecs())
AddRelTagsFile(GetPrefTagFile(), path, TAG);
AddToPrevOpenMenu(fullname);
return window;
}
void RevertToSaved(WindowInfo *window)
{
char name[MAXPATHLEN], path[MAXPATHLEN];
int i;
int insertPositions[MAX_PANES], topLines[MAX_PANES];
int horizOffsets[MAX_PANES];
int openFlags = 0;
Widget text;
/* Can't revert untitled windows */
if (!window->filenameSet)
{
DialogF(DF_WARN, window->shell, 1, "Error",
"Window '%s' was never saved, can't re-read", "OK",
window->filename);
return;
}
/* save insert & scroll positions of all of the panes to restore later */
for (i=0; i<=window->nPanes; i++) {
text = i==0 ? window->textArea : window->textPanes[i-1];
insertPositions[i] = TextGetCursorPos(text);
TextGetScroll(text, &topLines[i], &horizOffsets[i]);
}
/* re-read the file, update the window title if new file is different */
strcpy(name, window->filename);
strcpy(path, window->path);
RemoveBackupFile(window);
ClearUndoList(window);
openFlags |= IS_USER_LOCKED(window->lockReasons) ? PREF_READ_ONLY : 0;
if (!doOpen(window, name, path, openFlags)) {
/* This is a bit sketchy. The only error in doOpen that irreperably
damages the window is "too much binary data". It should be
pretty rare to be reverting something that was fine only to find
that now it has too much binary data. */
if (!window->fileMissing)
safeClose(window);
else {
/* Treat it like an externally modified file */
window->lastModTime=0;
window->fileMissing=FALSE;
}
return;
}
forceShowLineNumbers(window);
UpdateWindowTitle(window);
UpdateWindowReadOnly(window);
/* restore the insert and scroll positions of each pane */
for (i=0; i<=window->nPanes; i++) {
text = i==0 ? window->textArea : window->textPanes[i-1];
TextSetCursorPos(text, insertPositions[i]);
TextSetScroll(text, topLines[i], horizOffsets[i]);
}
}
/*
** Checks whether a window is still alive, and closes it only if so.
** Intended to be used when the file could not be opened for some reason.
** Normally the window is still alive, but the user may have closed the
** window instead of the error dialog. In that case, we shouldn't close the
** window a second time.
*/
static void safeClose(WindowInfo *window)
{
WindowInfo* p = WindowList;
while(p) {
if (p == window) {
CloseWindow(window);
return;
}
p = p->next;
}
}
static int doOpen(WindowInfo *window, const char *name, const char *path,
int flags)
{
char fullname[MAXPATHLEN];
struct stat statbuf;
int fileLen, readLen;
char *fileString, *c;
FILE *fp = NULL;
int fd;
int resp;
/* initialize lock reasons */
CLEAR_ALL_LOCKS(window->lockReasons);
/* Update the window data structure */
strcpy(window->filename, name);
strcpy(window->path, path);
window->filenameSet = TRUE;
window->fileMissing = TRUE;
/* Get the full name of the file */
strcpy(fullname, path);
strcat(fullname, name);
/* Open the file */
#ifndef DONT_USE_ACCESS
/* The only advantage of this is if you use clearcase,
which messes up the mtime of files opened with r+,
even if they're never actually written.
To avoid requiring special builds for clearcase users,
this is now the default. */
{
if ((fp = fopen(fullname, "r")) != NULL) {
if(access(fullname, W_OK) != 0)
SET_PERM_LOCKED(window->lockReasons, TRUE);
#else
fp = fopen(fullname, "rb+");
if (fp == NULL) {
/* Error opening file or file is not writeable */
fp = fopen(fullname, "rb");
if (fp != NULL) {
/* File is read only */
SET_PERM_LOCKED(window->lockReasons, TRUE);
#endif
} else if (flags & CREATE && errno == ENOENT) {
/* Give option to create (or to exit if this is the only window) */
if (!(flags & SUPPRESS_CREATE_WARN)) {
/* on Solaris 2.6, and possibly other OSes, dialog won't
show if parent window is iconized. */
RaiseShellWindow(window->shell, False);
/* ask user for next action if file not found */
if (WindowList == window && window->next == NULL) {
resp = DialogF(DF_WARN, window->shell, 3, "New File",
"Can't open %s:\n%s", "New File", "Cancel",
"Exit NEdit", fullname, errorString());
}
else {
resp = DialogF(DF_WARN, window->shell, 2, "New File",
"Can't open %s:\n%s", "New File", "Cancel", fullname,
errorString());
}
if (resp == 2) {
return FALSE;
}
else if (resp == 3) {
exit(EXIT_SUCCESS);
}
}
/* Test if new file can be created */
if ((fd = creat(fullname, 0666)) == -1) {
DialogF(DF_ERR, window->shell, 1, "Error creating File",
"Can't create %s:\n%s", "OK", fullname, errorString());
return FALSE;
}
else {
#ifdef VMS
/* get correct version number and close before removing */
getname(fd, fullname);
#endif
close(fd);
remove(fullname);
}
SetWindowModified(window, FALSE);
if ((flags & PREF_READ_ONLY) != 0) {
SET_USER_LOCKED(window->lockReasons, TRUE);
}
UpdateWindowReadOnly(window);
return TRUE;
}
else {
/* A true error */
DialogF(DF_ERR, window->shell, 1, "Error opening File",
"Could not open %s%s:\n%s", "OK", path, name,
errorString());
return FALSE;
}
}
/* Get the length of the file, the protection mode, and the time of the
last modification to the file */
if (fstat(fileno(fp), &statbuf) != 0) {
fclose(fp);
window->filenameSet = FALSE; /* Temp. prevent check for changes. */
DialogF(DF_ERR, window->shell, 1, "Error opening File",
"Error opening %s", "OK", name);
window->filenameSet = TRUE;
return FALSE;
}
if (S_ISDIR(statbuf.st_mode)) {
fclose(fp);
window->filenameSet = FALSE; /* Temp. prevent check for changes. */
DialogF(DF_ERR, window->shell, 1, "Error opening File",
"Can't open directory %s", "OK", name);
window->filenameSet = TRUE;
return FALSE;
}
#ifdef S_ISBLK
if (S_ISBLK(statbuf.st_mode)) {
fclose(fp);
window->filenameSet = FALSE; /* Temp. prevent check for changes. */
DialogF(DF_ERR, window->shell, 1, "Error opening File",
"Can't open block device %s", "OK", name);
window->filenameSet = TRUE;
return FALSE;
}
#endif
fileLen = statbuf.st_size;
/* Allocate space for the whole contents of the file (unfortunately) */
fileString = (char *)NEditMalloc(fileLen+1); /* +1 = space for null */
if (fileString == NULL) {
fclose(fp);
window->filenameSet = FALSE; /* Temp. prevent check for changes. */
DialogF(DF_ERR, window->shell, 1, "Error while opening File",
"File is too large to edit", "OK");
window->filenameSet = TRUE;
return FALSE;
}
/* Read the file into fileString and terminate with a null */
readLen = fread(fileString, sizeof(char), fileLen, fp);
if (ferror(fp)) {
fclose(fp);
window->filenameSet = FALSE; /* Temp. prevent check for changes. */
DialogF(DF_ERR, window->shell, 1, "Error while opening File",
"Error reading %s:\n%s", "OK", name, errorString());
window->filenameSet = TRUE;
NEditFree(fileString);
return FALSE;
}
fileString[readLen] = 0;
/* Close the file */
if (fclose(fp) != 0) {
/* unlikely error */
DialogF(DF_WARN, window->shell, 1, "Error while opening File",
"Unable to close file", "OK");
/* we read it successfully, so continue */
}
/* Any errors that happen after this point leave the window in a
"broken" state, and thus RevertToSaved will abandon the window if
window->fileMissing is FALSE and doOpen fails. */
window->fileMode = statbuf.st_mode;
window->fileUid = statbuf.st_uid;
window->fileGid = statbuf.st_gid;
window->lastModTime = statbuf.st_mtime;
window->device = statbuf.st_dev;
window->inode = statbuf.st_ino;
window->fileMissing = FALSE;
/* Detect and convert DOS and Macintosh format files */
if (GetPrefForceOSConversion()) {
window->fileFormat = FormatOfFile(fileString);
if (window->fileFormat == DOS_FILE_FORMAT) {
ConvertFromDosFileString(fileString, &readLen, NULL);
} else if (window->fileFormat == MAC_FILE_FORMAT) {
ConvertFromMacFileString(fileString, readLen);
}
}
/* Display the file contents in the text widget */
window->ignoreModify = True;
BufSetAll(window->buffer, fileString);
window->ignoreModify = False;
/* Check that the length that the buffer thinks it has is the same
as what we gave it. If not, there were probably nuls in the file.
Substitute them with another character. If that is impossible, warn
the user, make the file read-only, and force a substitution */
if (window->buffer->length != readLen) {
if (!BufSubstituteNullChars(fileString, readLen, window->buffer)) {
resp = DialogF(DF_ERR, window->shell, 2, "Error while opening File",
"Too much binary data in file. You may view\n"
"it, but not modify or re-save its contents.", "View",
"Cancel");
if (resp == 2) {
return FALSE;
}
SET_TMBD_LOCKED(window->lockReasons, TRUE);
for (c = fileString; c < &fileString[readLen]; c++) {
if (*c == '\0') {
*c = (char) 0xfe;
}
}
window->buffer->nullSubsChar = (char) 0xfe;
}
window->ignoreModify = True;
BufSetAll(window->buffer, fileString);
window->ignoreModify = False;
}
/* Release the memory that holds fileString */
NEditFree(fileString);
/* Set window title and file changed flag */
if ((flags & PREF_READ_ONLY) != 0) {
SET_USER_LOCKED(window->lockReasons, TRUE);
}
if (IS_PERM_LOCKED(window->lockReasons)) {
window->fileChanged = FALSE;
UpdateWindowTitle(window);
} else {
SetWindowModified(window, FALSE);
if (IS_ANY_LOCKED(window->lockReasons)) {
UpdateWindowTitle(window);
}
}
UpdateWindowReadOnly(window);
return TRUE;
}
int IncludeFile(WindowInfo *window, const char *name)
{
struct stat statbuf;
int fileLen, readLen;
char *fileString;
FILE *fp = NULL;
/* Open the file */
fp = fopen(name, "rb");
if (fp == NULL)
{
DialogF(DF_ERR, window->shell, 1, "Error opening File",
"Could not open %s:\n%s", "OK", name, errorString());
return FALSE;
}
/* Get the length of the file */
if (fstat(fileno(fp), &statbuf) != 0)
{
DialogF(DF_ERR, window->shell, 1, "Error opening File",
"Error opening %s", "OK", name);
fclose(fp);
return FALSE;
}
if (S_ISDIR(statbuf.st_mode))
{
DialogF(DF_ERR, window->shell, 1, "Error opening File",
"Can't open directory %s", "OK", name);
fclose(fp);
return FALSE;
}
fileLen = statbuf.st_size;
/* allocate space for the whole contents of the file */
fileString = (char *)NEditMalloc(fileLen+1); /* +1 = space for null */
if (fileString == NULL)
{
DialogF(DF_ERR, window->shell, 1, "Error opening File",
"File is too large to include", "OK");
fclose(fp);
return FALSE;
}
/* read the file into fileString and terminate with a null */
readLen = fread(fileString, sizeof(char), fileLen, fp);
if (ferror(fp))
{
DialogF(DF_ERR, window->shell, 1, "Error opening File",
"Error reading %s:\n%s", "OK", name, errorString());
fclose(fp);
NEditFree(fileString);
return FALSE;
}
fileString[readLen] = 0;
/* Detect and convert DOS and Macintosh format files */
switch (FormatOfFile(fileString)) {
case DOS_FILE_FORMAT:
ConvertFromDosFileString(fileString, &readLen, NULL);
break;
case MAC_FILE_FORMAT:
ConvertFromMacFileString(fileString, readLen);
break;
default:
/* Default is Unix, no conversion necessary. */
break;
}
/* If the file contained ascii nulls, re-map them */
if (!BufSubstituteNullChars(fileString, readLen, window->buffer))
{
DialogF(DF_ERR, window->shell, 1, "Error opening File",
"Too much binary data in file", "OK");
}
/* close the file */
if (fclose(fp) != 0)
{
/* unlikely error */
DialogF(DF_WARN, window->shell, 1, "Error opening File",
"Unable to close file", "OK");
/* we read it successfully, so continue */
}
/* insert the contents of the file in the selection or at the insert
position in the window if no selection exists */
if (window->buffer->primary.selected)
BufReplaceSelected(window->buffer, fileString);
else
BufInsert(window->buffer, TextGetCursorPos(window->lastFocus),
fileString);
/* release the memory that holds fileString */
NEditFree(fileString);
return TRUE;
}
/*
** Close all files and windows, leaving one untitled window
*/
int CloseAllFilesAndWindows(void)
{
while (WindowList->next != NULL ||
WindowList->filenameSet || WindowList->fileChanged) {
/*
* When we're exiting through a macro, the document running the
* macro does not disappear from the list, so we could get stuck
* in an endless loop if we try to close it. Therefore, we close
* other documents first. (Note that the document running the macro
* may get closed because it is in the same window as another
* document that gets closed, but it won't disappear; it becomes
* Untitled.)
*/
if (WindowList == MacroRunWindow() && WindowList->next != NULL) {
if (!CloseAllDocumentInWindow(WindowList->next)) {
return False;
}
}
else {
if (!CloseAllDocumentInWindow(WindowList)) {
return False;
}
}
}
return TRUE;
}
int CloseFileAndWindow(WindowInfo *window, int preResponse)
{
int response, stat;
/* Make sure that the window is not in iconified state */
if (window->fileChanged)
RaiseDocumentWindow(window);
/* If the window is a normal & unmodified file or an empty new file,
or if the user wants to ignore external modifications then
just close it. Otherwise ask for confirmation first. */
if (!window->fileChanged &&
/* Normal File */
((!window->fileMissing && window->lastModTime > 0) ||
/* New File*/
(window->fileMissing && window->lastModTime == 0) ||
/* File deleted/modified externally, ignored by user. */
!GetPrefWarnFileMods()))
{
CloseWindow(window);
/* up-to-date windows don't have outstanding backup files to close */
} else
{
if (preResponse == PROMPT_SBC_DIALOG_RESPONSE)
{
response = DialogF(DF_WARN, window->shell, 3, "Save File",
"Save %s before closing?", "Yes", "No", "Cancel", window->filename);
} else
{
response = preResponse;
}
if (response == YES_SBC_DIALOG_RESPONSE)
{
/* Save */
stat = SaveWindow(window);
if (stat)
{
CloseWindow(window);
} else
{
return FALSE;
}
} else if (response == NO_SBC_DIALOG_RESPONSE)
{
/* Don't Save */
RemoveBackupFile(window);
CloseWindow(window);
} else /* 3 == Cancel */
{
return FALSE;
}
}
return TRUE;
}
int SaveWindow(WindowInfo *window)
{
int stat;
/* Try to ensure our information is up-to-date */
CheckForChangesToFile(window);
/* Return success if the file is normal & unchanged or is a
read-only file. */
if ( (!window->fileChanged && !window->fileMissing &&
window->lastModTime > 0) ||
IS_ANY_LOCKED_IGNORING_PERM(window->lockReasons))
return TRUE;
/* Prompt for a filename if this is an Untitled window */
if (!window->filenameSet)
return SaveWindowAs(window, NULL, False);
/* Check for external modifications and warn the user */
if (GetPrefWarnFileMods() && fileWasModifiedExternally(window))
{
stat = DialogF(DF_WARN, window->shell, 2, "Save File",
"%s has been modified by another program.\n\n"
"Continuing this operation will overwrite any external\n"
"modifications to the file since it was opened in NEdit,\n"
"and your work or someone else's may potentially be lost.\n\n"
"To preserve the modified file, cancel this operation and\n"
"use Save As... to save this file under a different name,\n"
"or Revert to Saved to revert to the modified version.",
"Continue", "Cancel", window->filename);
if (stat == 2)
{
/* Cancel and mark file as externally modified */
window->lastModTime = 0;
window->fileMissing = FALSE;
return FALSE;
}
}
#ifdef VMS
RemoveBackupFile(window);
stat = doSave(window);
#else
if (writeBckVersion(window))
return FALSE;
stat = doSave(window);
if (stat)
RemoveBackupFile(window);
#endif /*VMS*/
return stat;
}
int SaveWindowAs(WindowInfo *window, const char *newName, int addWrap)
{
int response, retVal, fileFormat;
char fullname[MAXPATHLEN], filename[MAXPATHLEN], pathname[MAXPATHLEN];
WindowInfo *otherWindow;
/* Get the new name for the file */
if (newName == NULL) {
response = PromptForNewFile(window, "Save File As", fullname,
&fileFormat, &addWrap);
if (response != GFN_OK)
return FALSE;
window->fileFormat = fileFormat;
} else
{
strcpy(fullname, newName);
}
if (1 == NormalizePathname(fullname))
{
return False;
}
/* Add newlines if requested */
if (addWrap)
addWrapNewlines(window);
if (ParseFilename(fullname, filename, pathname) != 0) {
return FALSE;
}
/* If the requested file is this file, just save it and return */
if (!strcmp(window->filename, filename) &&
!strcmp(window->path, pathname)) {
if (writeBckVersion(window))
return FALSE;
return doSave(window);
}
/* If the file is open in another window, make user close it. Note that
it is possible for user to close the window by hand while the dialog
is still up, because the dialog is not application modal, so after
doing the dialog, check again whether the window still exists. */
otherWindow = FindWindowWithFile(filename, pathname);
if (otherWindow != NULL)
{
response = DialogF(DF_WARN, window->shell, 2, "File open",
"%s is open in another NEdit window", "Cancel",
"Close Other Window", filename);
if (response == 1)
{
return FALSE;
}
if (otherWindow == FindWindowWithFile(filename, pathname))
{
if (!CloseFileAndWindow(otherWindow, PROMPT_SBC_DIALOG_RESPONSE))
{
return FALSE;
}
}
}
/* Destroy the file closed property for the original file */
DeleteFileClosedProperty(window);
/* Change the name of the file and save it under the new name */
RemoveBackupFile(window);
strcpy(window->filename, filename);
strcpy(window->path, pathname);
window->fileMode = 0;
window->fileUid = 0;
window->fileGid = 0;
CLEAR_ALL_LOCKS(window->lockReasons);
retVal = doSave(window);
UpdateWindowReadOnly(window);
RefreshTabState(window);
/* Add the name to the convenience menu of previously opened files */
AddToPrevOpenMenu(fullname);
/* If name has changed, language mode may have changed as well, unless
it's an Untitled window for which the user already set a language
mode; it's probably the right one. */
if (PLAIN_LANGUAGE_MODE == window->languageMode || window->filenameSet) {
DetermineLanguageMode(window, False);
}
window->filenameSet = True;
/* Update the stats line and window title with the new filename */
UpdateWindowTitle(window);
UpdateStatsLine(window);
SortTabBar(window);
return retVal;
}
static int doSave(WindowInfo *window)
{
char *fileString = NULL;
char fullname[MAXPATHLEN];
struct stat statbuf;
FILE *fp;
int fileLen, result;
/* Get the full name of the file */
strcpy(fullname, window->path);
strcat(fullname, window->filename);
/* Check for root and warn him if he wants to write to a file with
none of the write bits set. */
if ((0 == getuid())
&& (0 == stat(fullname, &statbuf))
&& !(statbuf.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
{
result = DialogF(DF_WARN, window->shell, 2, "Writing Read-only File",
"File '%s' is marked as read-only.\n"
"Do you want to save anyway?",
"Save", "Cancel", window->filename);
if (1 != result)
{
return True;
}
}
#ifdef VMS
/* strip the version number from the file so VMS will begin a new one */
removeVersionNumber(fullname);
#endif
/* add a terminating newline if the file doesn't already have one for
Unix utilities which get confused otherwise
NOTE: this must be done _before_ we create/open the file, because the
(potential) buffer modification can trigger a check for file
changes. If the file is created for the first time, it has
zero size on disk, and the check would falsely conclude that the
file has changed on disk, and would pop up a warning dialog */
if (BufGetCharacter(window->buffer, window->buffer->length - 1) != '\n'
&& window->buffer->length != 0
&& GetPrefAppendLF())
{
BufInsert(window->buffer, window->buffer->length, "\n");
}
/* open the file */
#ifdef VMS
fp = fopen(fullname, "w", "rfm = stmlf");
#else
fp = fopen(fullname, "wb");
#endif /* VMS */
if (fp == NULL)
{
result = DialogF(DF_WARN, window->shell, 2, "Error saving File",
"Unable to save %s:\n%s\n\nSave as a new file?",
"Save As...", "Cancel",
window->filename, errorString());
if (result == 1)
{
return SaveWindowAs(window, NULL, 0);
}
return FALSE;
}
#ifdef VMS
/* get the complete name of the file including the new version number */
fgetname(fp, fullname);
#endif
/* get the text buffer contents and its length */
fileString = BufGetAll(window->buffer);
fileLen = window->buffer->length;
/* If null characters are substituted for, put them back */
BufUnsubstituteNullChars(fileString, window->buffer);
/* If the file is to be saved in DOS or Macintosh format, reconvert */
if (window->fileFormat == DOS_FILE_FORMAT)
{
if (!ConvertToDosFileString(&fileString, &fileLen))
{
DialogF(DF_ERR, window->shell, 1, "Out of Memory",
"Out of memory! Try\nsaving in Unix format", "OK");
return FALSE;
}
} else if (window->fileFormat == MAC_FILE_FORMAT)
{
ConvertToMacFileString(fileString, fileLen);
}
/* write to the file */
#ifdef IBM_FWRITE_BUG
write(fileno(fp), fileString, fileLen);
#else
fwrite(fileString, sizeof(char), fileLen, fp);
#endif
if (ferror(fp))
{
DialogF(DF_ERR, window->shell, 1, "Error saving File",
"%s not saved:\n%s", "OK", window->filename, errorString());
fclose(fp);
remove(fullname);
NEditFree(fileString);
return FALSE;
}
/* close the file */
if (fclose(fp) != 0)
{
DialogF(DF_ERR, window->shell, 1, "Error closing File",
"Error closing file:\n%s", "OK", errorString());
NEditFree(fileString);
return FALSE;
}
/* free the text buffer copy returned from XmTextGetString */
NEditFree(fileString);
#ifdef VMS
/* reflect the fact that NEdit is now editing a new version of the file */
ParseFilename(fullname, window->filename, window->path);
#endif /*VMS*/
/* success, file was written */
SetWindowModified(window, FALSE);
/* update the modification time */
if (stat(fullname, &statbuf) == 0) {
window->lastModTime = statbuf.st_mtime;
window->fileMissing = FALSE;
window->device = statbuf.st_dev;
window->inode = statbuf.st_ino;
} else {
/* This needs to produce an error message -- the file can't be
accessed! */
window->lastModTime = 0;
window->fileMissing = TRUE;
window->device = 0;
window->inode = 0;
}
return TRUE;
}
/*
** Create a backup file for the current window. The name for the backup file
** is generated using the name and path stored in the window and adding a
** tilde (~) on UNIX and underscore (_) on VMS to the beginning of the name.
*/
int WriteBackupFile(WindowInfo *window)
{
char *fileString = NULL;
char name[MAXPATHLEN];
FILE *fp;
int fd, fileLen;
/* Generate a name for the autoSave file */
backupFileName(window, name, sizeof(name));
/* remove the old backup file.
Well, this might fail - we'll notice later however. */
remove(name);
/* open the file, set more restrictive permissions (using default
permissions was somewhat of a security hole, because permissions were
independent of those of the original file being edited */
#ifdef VMS
if ((fp = fopen(name, "w", "rfm = stmlf")) == NULL)
#else
if ((fd = open(name, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR | S_IWUSR)) < 0
|| (fp = fdopen(fd, "w")) == NULL)
#endif /* VMS */
{
DialogF(DF_WARN, window->shell, 1, "Error writing Backup",
"Unable to save backup for %s:\n%s\n"
"Automatic backup is now off", "OK", window->filename,
errorString());
window->autoSave = FALSE;
SetToggleButtonState(window, window->autoSaveItem, FALSE, FALSE);
return FALSE;
}
/* Set VMS permissions */
#ifdef VMS
chmod(name, S_IRUSR | S_IWUSR);
#endif
/* get the text buffer contents and its length */
fileString = BufGetAll(window->buffer);
fileLen = window->buffer->length;
/* If null characters are substituted for, put them back */
BufUnsubstituteNullChars(fileString, window->buffer);
/* add a terminating newline if the file doesn't already have one */
if (fileLen != 0 && fileString[fileLen-1] != '\n')
fileString[fileLen++] = '\n'; /* null terminator no longer needed */
/* write out the file */
#ifdef IBM_FWRITE_BUG
write(fileno(fp), fileString, fileLen);
#else
fwrite(fileString, sizeof(char), fileLen, fp);
#endif
if (ferror(fp))
{
DialogF(DF_ERR, window->shell, 1, "Error saving Backup",
"Error while saving backup for %s:\n%s\n"
"Automatic backup is now off", "OK", window->filename,
errorString());
fclose(fp);
remove(name);
NEditFree(fileString);
window->autoSave = FALSE;
return FALSE;
}
/* close the backup file */
if (fclose(fp) != 0) {
NEditFree(fileString);
return FALSE;
}
/* Free the text buffer copy returned from XmTextGetString */
NEditFree(fileString);
return TRUE;
}
/*
** Remove the backup file associated with this window
*/
void RemoveBackupFile(WindowInfo *window)
{
char name[MAXPATHLEN];
/* Don't delete backup files when backups aren't activated. */
if (window->autoSave == FALSE)
return;
backupFileName(window, name, sizeof(name));
remove(name);
}
/*
** Generate the name of the backup file for this window from the filename
** and path in the window data structure & write into name
*/
static void backupFileName(WindowInfo *window, char *name, size_t len)
{
char bckname[MAXPATHLEN];
#ifdef VMS
if (window->filenameSet)
sprintf(name, "%s_%s", window->path, window->filename);
else
sprintf(name, "%s_%s", "SYS$LOGIN:", window->filename);
#else
if (window->filenameSet)
{
sprintf(name, "%s~%s", window->path, window->filename);
} else
{
strcpy(bckname, "~");
strncat(bckname, window->filename, MAXPATHLEN - 1);
PrependHome(bckname, name, len);
}
#endif /*VMS*/
}
/*
** If saveOldVersion is on, copies the existing version of the file to
** <filename>.bck in anticipation of a new version being saved. Returns
** True if backup fails and user requests that the new file not be written.
*/
static int writeBckVersion(WindowInfo *window)
{
#ifndef VMS
char fullname[MAXPATHLEN], bckname[MAXPATHLEN];
struct stat statbuf;
int in_fd, out_fd;
char *io_buffer;
#define IO_BUFFER_SIZE ((size_t)(1024*1024))
/* Do only if version backups are turned on */
if (!window->saveOldVersion) {
return False;
}
/* Get the full name of the file */
strcpy(fullname, window->path);
strcat(fullname, window->filename);
/* Generate name for old version */
if ((strlen(fullname) + 5) > (size_t) MAXPATHLEN) {
return bckError(window, "file name too long", window->filename);
}
sprintf(bckname, "%s.bck", fullname);
/* Delete the old backup file */
/* Errors are ignored; we'll notice them later. */
remove(bckname);
/* open the file being edited. If there are problems with the
old file, don't bother the user, just skip the backup */
in_fd = open(fullname, O_RDONLY);
if (in_fd<0) {
return FALSE;
}
/* Get permissions of the file.
We preserve the normal permissions but not ownership, extended
attributes, et cetera. */
if (fstat(in_fd, &statbuf) != 0) {
close(in_fd);
return FALSE;
}
/* open the destination file exclusive and with restrictive permissions. */
out_fd = open(bckname, O_CREAT|O_EXCL|O_TRUNC|O_WRONLY, S_IRUSR | S_IWUSR);
if (out_fd < 0) {
close(in_fd);
return bckError(window, "Error open backup file", bckname);
}
/* Set permissions on new file */
if (fchmod(out_fd, statbuf.st_mode) != 0) {
close(in_fd);
close(out_fd);
remove(bckname);
return bckError(window, "fchmod() failed", bckname);
}
/* Allocate I/O buffer */
io_buffer = (char*) NEditMalloc(IO_BUFFER_SIZE);
if (NULL == io_buffer) {
close(in_fd);
close(out_fd);
remove(bckname);
return bckError(window, "out of memory", bckname);
}
/* copy loop */
for(;;) {
ssize_t bytes_read;
ssize_t bytes_written;
bytes_read = read(in_fd, io_buffer, IO_BUFFER_SIZE);
if (bytes_read < 0) {
close(in_fd);
close(out_fd);
remove(bckname);
free(io_buffer);
return bckError(window, "read() error", window->filename);
}
if (0 == bytes_read) {
break; /* EOF */
}
/* write to the file */
bytes_written = write(out_fd, io_buffer, (size_t) bytes_read);
if (bytes_written != bytes_read) {
close(in_fd);
close(out_fd);
remove(bckname);
free(io_buffer);
return bckError(window, errorString(), bckname);
}
}
/* close the input and output files */
close(in_fd);
close(out_fd);
free(io_buffer);
#endif /* VMS */
return FALSE;
}
/*
** Error processing for writeBckVersion, gives the user option to cancel
** the subsequent save, or continue and optionally turn off versioning
*/
static int bckError(WindowInfo *window, const char *errString, const char *file)
{
int resp;
resp = DialogF(DF_ERR, window->shell, 3, "Error writing Backup",
"Couldn't write .bck (last version) file.\n%s: %s", "Cancel Save",
"Turn off Backups", "Continue", file, errString);
if (resp == 1)
return TRUE;
if (resp == 2) {
window->saveOldVersion = FALSE;
#ifndef VMS
SetToggleButtonState(window, window->saveLastItem, FALSE, FALSE);
#endif
}
return FALSE;
}
void PrintWindow(WindowInfo *window, int selectedOnly)
{
textBuffer *buf = window->buffer;
selection *sel = &buf->primary;
char *fileString = NULL;
int fileLen;
/* get the contents of the text buffer from the text area widget. Add
wrapping newlines if necessary to make it match the displayed text */
if (selectedOnly) {
if (!sel->selected) {
XBell(TheDisplay, 0);
return;
}
if (sel->rectangular) {
fileString = BufGetSelectionText(buf);
fileLen = strlen(fileString);
} else
fileString = TextGetWrapped(window->textArea, sel->start, sel->end,
&fileLen);
} else
fileString = TextGetWrapped(window->textArea, 0, buf->length, &fileLen);
/* If null characters are substituted for, put them back */
BufUnsubstituteNullChars(fileString, buf);
/* add a terminating newline if the file doesn't already have one */
if (fileLen != 0 && fileString[fileLen-1] != '\n')
fileString[fileLen++] = '\n'; /* null terminator no longer needed */
/* Print the string */
PrintString(fileString, fileLen, window->shell, window->filename);
/* Free the text buffer copy returned from XmTextGetString */
NEditFree(fileString);
}
/*
** Print a string (length is required). parent is the dialog parent, for
** error dialogs, and jobName is the print title.
*/
void PrintString(const char *string, int length, Widget parent, const char *jobName)
{
char tmpFileName[L_tmpnam]; /* L_tmpnam defined in stdio.h */
FILE *fp;
int fd;
/* Generate a temporary file name */
/* If the glibc is used, the linker issues a warning at this point. This is
very thoughtful of him, but does not apply to NEdit. The recommended
replacement mkstemp(3) uses the same algorithm as NEdit, namely
1. Create a filename
2. Open the file with the O_CREAT|O_EXCL flags
So all an attacker can do is a DoS on the print function. */
tmpnam(tmpFileName);
/* open the temporary file */
#ifdef VMS
if ((fp = fopen(tmpFileName, "w", "rfm = stmlf")) == NULL)
#else
if ((fd = open(tmpFileName, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR | S_IWUSR)) < 0 || (fp = fdopen(fd, "w")) == NULL)
#endif /* VMS */
{
DialogF(DF_WARN, parent, 1, "Error while Printing",
"Unable to write file for printing:\n%s", "OK",
errorString());
return;
}
#ifdef VMS
chmod(tmpFileName, S_IRUSR | S_IWUSR);
#endif
/* write to the file */
#ifdef IBM_FWRITE_BUG
write(fileno(fp), string, length);
#else
fwrite(string, sizeof(char), length, fp);
#endif
if (ferror(fp))
{
DialogF(DF_ERR, parent, 1, "Error while Printing",
"%s not printed:\n%s", "OK", jobName, errorString());
fclose(fp); /* should call close(fd) in turn! */
remove(tmpFileName);
return;
}
/* close the temporary file */
if (fclose(fp) != 0)
{
DialogF(DF_ERR, parent, 1, "Error while Printing",
"Error closing temp. print file:\n%s", "OK",
errorString());
remove(tmpFileName);
return;
}
/* Print the temporary file, then delete it and return success */
#ifdef VMS
strcat(tmpFileName, ".");
PrintFile(parent, tmpFileName, jobName, True);
#else
PrintFile(parent, tmpFileName, jobName);
remove(tmpFileName);
#endif /*VMS*/
return;
}
/*
** Wrapper for GetExistingFilename which uses the current window's path
** (if set) as the default directory.
*/
int PromptForExistingFile(WindowInfo *window, char *prompt, char *fullname)
{
char *savedDefaultDir;
int retVal;
/* Temporarily set default directory to window->path, prompt for file,
then, if the call was unsuccessful, restore the original default
directory */
savedDefaultDir = GetFileDialogDefaultDirectory();
if (*window->path != '\0')
SetFileDialogDefaultDirectory(window->path);
retVal = GetExistingFilename(window->shell, prompt, fullname);
if (retVal != GFN_OK)
SetFileDialogDefaultDirectory(savedDefaultDir);
NEditFree(savedDefaultDir);
return retVal;
}
/*
** Wrapper for HandleCustomNewFileSB which uses the current window's path
** (if set) as the default directory, and asks about embedding newlines
** to make wrapping permanent.
*/
int PromptForNewFile(WindowInfo *window, char *prompt, char *fullname,
int *fileFormat, int *addWrap)
{
int n, retVal;
Arg args[20];
XmString s1, s2;
Widget fileSB, wrapToggle;
Widget formatForm, formatBtns, unixFormat, dosFormat, macFormat;
char *savedDefaultDir;
*fileFormat = window->fileFormat;
/* Temporarily set default directory to window->path, prompt for file,
then, if the call was unsuccessful, restore the original default
directory */
savedDefaultDir = GetFileDialogDefaultDirectory();
if (*window->path != '\0')
SetFileDialogDefaultDirectory(window->path);
/* Present a file selection dialog with an added field for requesting
long line wrapping to become permanent via inserted newlines */
n = 0;
XtSetArg(args[n],
XmNselectionLabelString,
s1 = XmStringCreateLocalized("New File Name:")); n++;
XtSetArg(args[n], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL); n++;
XtSetArg(args[n],
XmNdialogTitle,
s2 = XmStringCreateSimple(prompt)); n++;
fileSB = CreateFileSelectionDialog(window->shell,"FileSelect",args,n);
XmStringFree(s1);
XmStringFree(s2);
formatForm = XtVaCreateManagedWidget("formatForm", xmFormWidgetClass,
fileSB, NULL);
formatBtns = XtVaCreateManagedWidget("formatBtns",
xmRowColumnWidgetClass, formatForm,
XmNradioBehavior, XmONE_OF_MANY,
XmNorientation, XmHORIZONTAL,
XmNpacking, XmPACK_TIGHT,
XmNtopAttachment, XmATTACH_FORM,
XmNleftAttachment, XmATTACH_FORM,
NULL);
XtVaCreateManagedWidget("formatBtns", xmLabelWidgetClass, formatBtns,
XmNlabelString, s1=XmStringCreateSimple("Format:"), NULL);
XmStringFree(s1);
unixFormat = XtVaCreateManagedWidget("unixFormat",
xmToggleButtonWidgetClass, formatBtns,
XmNlabelString, s1 = XmStringCreateSimple("Unix"),
XmNset, *fileFormat == UNIX_FILE_FORMAT,
XmNuserData, (XtPointer)UNIX_FILE_FORMAT,
XmNmarginHeight, 0,
XmNalignment, XmALIGNMENT_BEGINNING,
XmNmnemonic, 'U',
NULL);
XmStringFree(s1);
XtAddCallback(unixFormat, XmNvalueChangedCallback, setFormatCB,
fileFormat);
dosFormat = XtVaCreateManagedWidget("dosFormat",
xmToggleButtonWidgetClass, formatBtns,
XmNlabelString, s1 = XmStringCreateSimple("DOS"),
XmNset, *fileFormat == DOS_FILE_FORMAT,
XmNuserData, (XtPointer)DOS_FILE_FORMAT,
XmNmarginHeight, 0,
XmNalignment, XmALIGNMENT_BEGINNING,
XmNmnemonic, 'O',
NULL);
XmStringFree(s1);
XtAddCallback(dosFormat, XmNvalueChangedCallback, setFormatCB,
fileFormat);
macFormat = XtVaCreateManagedWidget("macFormat",
xmToggleButtonWidgetClass, formatBtns,
XmNlabelString, s1 = XmStringCreateSimple("Macintosh"),
XmNset, *fileFormat == MAC_FILE_FORMAT,
XmNuserData, (XtPointer)MAC_FILE_FORMAT,
XmNmarginHeight, 0,
XmNalignment, XmALIGNMENT_BEGINNING,
XmNmnemonic, 'M',
NULL);
XmStringFree(s1);
XtAddCallback(macFormat, XmNvalueChangedCallback, setFormatCB,
fileFormat);
if (window->wrapMode == CONTINUOUS_WRAP) {
wrapToggle = XtVaCreateManagedWidget("addWrap",
xmToggleButtonWidgetClass, formatForm,
XmNlabelString, s1 = XmStringCreateSimple("Add line breaks where wrapped"),
XmNalignment, XmALIGNMENT_BEGINNING,
XmNmnemonic, 'A',
XmNtopAttachment, XmATTACH_WIDGET,
XmNtopWidget, formatBtns,
XmNleftAttachment, XmATTACH_FORM,
NULL);
XtAddCallback(wrapToggle, XmNvalueChangedCallback, addWrapCB,
addWrap);
XmStringFree(s1);
}
*addWrap = False;
XtVaSetValues(XmFileSelectionBoxGetChild(fileSB, XmDIALOG_FILTER_LABEL),
XmNmnemonic, 'l',
XmNuserData, XmFileSelectionBoxGetChild(fileSB, XmDIALOG_FILTER_TEXT),
NULL);
XtVaSetValues(XmFileSelectionBoxGetChild(fileSB, XmDIALOG_DIR_LIST_LABEL),
XmNmnemonic, 'D',
XmNuserData, XmFileSelectionBoxGetChild(fileSB, XmDIALOG_DIR_LIST),
NULL);
XtVaSetValues(XmFileSelectionBoxGetChild(fileSB, XmDIALOG_LIST_LABEL),
XmNmnemonic, 'F',
XmNuserData, XmFileSelectionBoxGetChild(fileSB, XmDIALOG_LIST),
NULL);
XtVaSetValues(XmFileSelectionBoxGetChild(fileSB, XmDIALOG_SELECTION_LABEL),
XmNmnemonic, prompt[strspn(prompt, "lFD")],
XmNuserData, XmFileSelectionBoxGetChild(fileSB, XmDIALOG_TEXT),
NULL);
AddDialogMnemonicHandler(fileSB, FALSE);
RemapDeleteKey(XmFileSelectionBoxGetChild(fileSB, XmDIALOG_FILTER_TEXT));
RemapDeleteKey(XmFileSelectionBoxGetChild(fileSB, XmDIALOG_TEXT));
retVal = HandleCustomNewFileSB(fileSB, fullname,
window->filenameSet ? window->filename : NULL);
if (retVal != GFN_OK)
SetFileDialogDefaultDirectory(savedDefaultDir);
NEditFree(savedDefaultDir);
return retVal;
}
/*
** Find a name for an untitled file, unique in the name space of in the opened
** files in this session, i.e. Untitled or Untitled_nn, and write it into
** the string "name".
*/
void UniqueUntitledName(char *name)
{
WindowInfo *w;
int i;
for (i=0; i<INT_MAX; i++) {
if (i == 0)
sprintf(name, "Untitled");
else
sprintf(name, "Untitled_%d", i);
for (w=WindowList; w!=NULL; w=w->next)
if (!strcmp(w->filename, name))
break;
if (w == NULL)
break;
}
}
/*
** Callback that guards us from trying to access a window after it has
** been destroyed while a modal dialog is up.
*/
static void modifiedWindowDestroyedCB(Widget w, XtPointer clientData,
XtPointer callData)
{
*(Bool*)clientData = TRUE;
}
/*
** Check if the file in the window was changed by an external source.
** and put up a warning dialog if it has.
*/
void CheckForChangesToFile(WindowInfo *window)
{
static WindowInfo* lastCheckWindow = NULL;
static Time lastCheckTime = 0;
char fullname[MAXPATHLEN];
struct stat statbuf;
Time timestamp;
FILE *fp;
int resp, silent = 0;
XWindowAttributes winAttr;
Boolean windowIsDestroyed = False;
if(!window->filenameSet)
return;
/* If last check was very recent, don't impact performance */
timestamp = XtLastTimestampProcessed(XtDisplay(window->shell));
if (window == lastCheckWindow &&
timestamp - lastCheckTime < MOD_CHECK_INTERVAL)
return;
lastCheckWindow = window;
lastCheckTime = timestamp;
/* Update the status, but don't pop up a dialog if we're called
from a place where the window might be iconic (e.g., from the
replace dialog) or on another desktop.
This works, but I bet it costs a round-trip to the server.
Might be better to capture MapNotify/Unmap events instead.
For tabs that are not on top, we don't want the dialog either,
and we don't even need to contact the server to find out. By
performing this check first, we avoid a server round-trip for
most files in practice. */
if (!IsTopDocument(window))
silent = 1;
else {
XGetWindowAttributes(XtDisplay(window->shell),
XtWindow(window->shell),
&winAttr);
if (winAttr.map_state != IsViewable)
silent = 1;
}
/* Get the file mode and modification time */
strcpy(fullname, window->path);
strcat(fullname, window->filename);
if (stat(fullname, &statbuf) != 0) {
/* Return if we've already warned the user or we can't warn him now */
if (window->fileMissing || silent) {
return;
}
/* Can't stat the file -- maybe it's been deleted.
The filename is now invalid */
window->fileMissing = TRUE;
window->lastModTime = 1;
window->device = 0;
window->inode = 0;
/* Warn the user, if they like to be warned (Maybe this should be its
own preference setting: GetPrefWarnFileDeleted()) */
if (GetPrefWarnFileMods()) {
char* title;
char* body;
/* See note below about pop-up timing and XUngrabPointer */
XUngrabPointer(XtDisplay(window->shell), timestamp);
/* If the window (and the dialog) are destroyed while the dialog
is up (typically closed via the window manager), we should
avoid accessing the window afterwards. */
XtAddCallback(window->shell, XmNdestroyCallback,
modifiedWindowDestroyedCB, &windowIsDestroyed);
/* Set title, message body and button to match stat()'s error. */
switch (errno) {
case ENOENT:
/* A component of the path file_name does not exist. */
title = "File not Found";
body = "File '%s' (or directory in its path)\n"
"no longer exists.\n"
"Another program may have deleted or moved it.";
resp = DialogF(DF_ERR, window->shell, 2, title, body,
"Save", "Cancel", window->filename);
break;
case EACCES:
/* Search permission denied for a path component. We add
one to the response because Re-Save wouldn't really
make sense here. */
title = "Permission Denied";
body = "You no longer have access to file '%s'.\n"
"Another program may have changed the permissions of\n"
"one of its parent directories.";
resp = 1 + DialogF(DF_ERR, window->shell, 1, title, body,
"Cancel", window->filename);
break;
default:
/* Everything else. This hints at an internal error (eg.
ENOTDIR) or at some bad state at the host. */
title = "File not Accessible";
body = "Error while checking the status of file '%s':\n"
" '%s'\n"
"Please make sure that no data is lost before closing\n"
"this window.";
resp = DialogF(DF_ERR, window->shell, 2, title, body,
"Save", "Cancel", window->filename,
errorString());
break;
}
if (!windowIsDestroyed) {
XtRemoveCallback(window->shell, XmNdestroyCallback,
modifiedWindowDestroyedCB, &windowIsDestroyed);
}
switch (resp) {
case 1:
SaveWindow(window);
break;
/* Good idea, but this leads to frequent crashes, see
SF#1578869. Reinsert this if circumstances change by
uncommenting this part and inserting a "Close" button
before each Cancel button above.
case 2:
CloseWindow(window);
return;
*/
}
}
/* A missing or (re-)saved file can't be read-only. */
/* TODO: A document without a file can be locked though. */
/* Make sure that the window was not destroyed behind our back! */
if (!windowIsDestroyed) {
SET_PERM_LOCKED(window->lockReasons, False);
UpdateWindowTitle(window);
UpdateWindowReadOnly(window);
}
return;
}
/* Check that the file's read-only status is still correct (but
only if the file can still be opened successfully in read mode) */
if (window->fileMode != statbuf.st_mode ||
window->fileUid != statbuf.st_uid ||
window->fileGid != statbuf.st_gid) {
window->fileMode = statbuf.st_mode;
window->fileUid = statbuf.st_uid;
window->fileGid = statbuf.st_gid;
if ((fp = fopen(fullname, "r")) != NULL) {
int readOnly;
fclose(fp);
#ifndef DONT_USE_ACCESS
readOnly = access(fullname, W_OK) != 0;
#else
if (((fp = fopen(fullname, "r+")) != NULL)) {
readOnly = FALSE;
fclose(fp);
} else
readOnly = TRUE;
#endif
if (IS_PERM_LOCKED(window->lockReasons) != readOnly) {
SET_PERM_LOCKED(window->lockReasons, readOnly);
UpdateWindowTitle(window);
UpdateWindowReadOnly(window);
}
}
}
/* Warn the user if the file has been modified, unless checking is
turned off or the user has already been warned. Popping up a dialog
from a focus callback (which is how this routine is usually called)
seems to catch Motif off guard, and if the timing is just right, the
dialog can be left with a still active pointer grab from a Motif menu
which is still in the process of popping down. The workaround, below,
of calling XUngrabPointer is inelegant but seems to fix the problem. */
if (!silent &&
((window->lastModTime != 0 &&
window->lastModTime != statbuf.st_mtime) ||
window->fileMissing) ){
window->lastModTime = 0; /* Inhibit further warnings */
window->fileMissing = FALSE;
if (!GetPrefWarnFileMods())
return;
if (GetPrefWarnRealFileMods() &&
!cmpWinAgainstFile(window, fullname)) {
/* Contents hasn't changed. Update the modification time. */
window->lastModTime = statbuf.st_mtime;
return;
}
XUngrabPointer(XtDisplay(window->shell), timestamp);
if (window->fileChanged)
resp = DialogF(DF_WARN, window->shell, 2,
"File modified externally",
"%s has been modified by another program. Reload?\n\n"
"WARNING: Reloading will discard changes made in this\n"
"editing session!", "Reload", "Cancel", window->filename);
else
resp = DialogF(DF_WARN, window->shell, 2,
"File modified externally",
"%s has been modified by another\nprogram. Reload?",
"Reload", "Cancel", window->filename);
if (resp == 1)
RevertToSaved(window);
}
}
/*
** Return true if the file displayed in window has been modified externally
** to nedit. This should return FALSE if the file has been deleted or is
** unavailable.
*/
static int fileWasModifiedExternally(WindowInfo *window)
{
char fullname[MAXPATHLEN];
struct stat statbuf;
if(!window->filenameSet)
return FALSE;
/* if (window->lastModTime == 0)
return FALSE; */
strcpy(fullname, window->path);
strcat(fullname, window->filename);
if (stat(fullname, &statbuf) != 0)
return FALSE;
if (window->lastModTime == statbuf.st_mtime)
return FALSE;
if (GetPrefWarnRealFileMods() &&
!cmpWinAgainstFile(window, fullname)) {
return FALSE;
}
return TRUE;
}
/*
** Check the read-only or locked status of the window and beep and return
** false if the window should not be written in.
*/
int CheckReadOnly(WindowInfo *window)
{
if (IS_ANY_LOCKED(window->lockReasons)) {
XBell(TheDisplay, 0);
return True;
}
return False;
}
/*
** Wrapper for strerror so all the calls don't have to be ifdef'd for VMS.
*/
static const char *errorString(void)
{
#ifdef VMS
return strerror(errno, vaxc$errno);
#else
return strerror(errno);
#endif
}
#ifdef VMS
/*
** Removing the VMS version number from a file name (if has one).
*/
void removeVersionNumber(char *fileName)
{
char *versionStart;
versionStart = strrchr(fileName, ';');
if (versionStart != NULL)
*versionStart = '\0';
}
#endif /*VMS*/
/*
** Callback procedure for File Format toggle buttons. Format is stored
** in userData field of widget button
*/
static void setFormatCB(Widget w, XtPointer clientData, XtPointer callData)
{
if (XmToggleButtonGetState(w)) {
XtPointer userData;
XtVaGetValues(w, XmNuserData, &userData, NULL);
*(int*) clientData = (int) (intptr_t) userData;
}
}
/*
** Callback procedure for toggle button requesting newlines to be inserted
** to emulate continuous wrapping.
*/
static void addWrapCB(Widget w, XtPointer clientData, XtPointer callData)
{
int resp;
int *addWrap = (int *)clientData;
if (XmToggleButtonGetState(w))
{
resp = DialogF(DF_WARN, w, 2, "Add Wrap",
"This operation adds permanent line breaks to\n"
"match the automatic wrapping done by the\n"
"Continuous Wrap mode Preferences Option.\n\n"
"*** This Option is Irreversable ***\n\n"
"Once newlines are inserted, continuous wrapping\n"
"will no longer work automatically on these lines", "OK",
"Cancel");
if (resp == 2)
{
XmToggleButtonSetState(w, False, False);
*addWrap = False;
} else
{
*addWrap = True;
}
} else
{
*addWrap = False;
}
}
/*
** Change a window created in NEdit's continuous wrap mode to the more
** conventional Unix format of embedded newlines. Indicate to the user
** by turning off Continuous Wrap mode.
*/
static void addWrapNewlines(WindowInfo *window)
{
int fileLen, i, insertPositions[MAX_PANES], topLines[MAX_PANES];
int horizOffset;
Widget text;
char *fileString;
/* save the insert and scroll positions of each pane */
for (i=0; i<=window->nPanes; i++) {
text = i==0 ? window->textArea : window->textPanes[i-1];
insertPositions[i] = TextGetCursorPos(text);
TextGetScroll(text, &topLines[i], &horizOffset);
}
/* Modify the buffer to add wrapping */
fileString = TextGetWrapped(window->textArea, 0,
window->buffer->length, &fileLen);
BufSetAll(window->buffer, fileString);
NEditFree(fileString);
/* restore the insert and scroll positions of each pane */
for (i=0; i<=window->nPanes; i++) {
text = i==0 ? window->textArea : window->textPanes[i-1];
TextSetCursorPos(text, insertPositions[i]);
TextSetScroll(text, topLines[i], 0);
}
/* Show the user that something has happened by turning off
Continuous Wrap mode */
SetToggleButtonState(window, window->continuousWrapItem, False, True);
}
/*
* Number of bytes read at once by cmpWinAgainstFile
*/
#define PREFERRED_CMPBUF_LEN 32768
/*
* Check if the contens of the textBuffer *buf is equal
* the contens of the file named fileName. The format of
* the file (UNIX/DOS/MAC) is handled properly.
*
* Return values
* 0: no difference found
* !0: difference found or could not compare contents.
*/
static int cmpWinAgainstFile(WindowInfo *window, const char *fileName)
{
char fileString[PREFERRED_CMPBUF_LEN + 2];
struct stat statbuf;
int fileLen, restLen, nRead, bufPos, rv, offset, filePos;
char pendingCR = 0;
int fileFormat = window->fileFormat;
char message[MAXPATHLEN+50];
textBuffer *buf = window->buffer;
FILE *fp;
fp = fopen(fileName, "r");
if (!fp)
return (1);
if (fstat(fileno(fp), &statbuf) != 0) {
fclose(fp);
return (1);
}
fileLen = statbuf.st_size;
/* For DOS files, we can't simply check the length */
if (fileFormat != DOS_FILE_FORMAT) {
if (fileLen != buf->length) {
fclose(fp);
return (1);
}
} else {
/* If a DOS file is smaller on disk, it's certainly different */
if (fileLen < buf->length) {
fclose(fp);
return (1);
}
}
/* For large files, the comparison can take a while. If it takes too long,
the user should be given a clue about what is happening. */
sprintf(message, "Comparing externally modified %s ...", window->filename);
restLen = min(PREFERRED_CMPBUF_LEN, fileLen);
bufPos = 0;
filePos = 0;
while (restLen > 0) {
AllWindowsBusy(message);
if (pendingCR) {
fileString[0] = pendingCR;
offset = 1;
} else {
offset = 0;
}
nRead = fread(fileString+offset, sizeof(char), restLen, fp);
if (nRead != restLen) {
fclose(fp);
AllWindowsUnbusy();
return (1);
}
filePos += nRead;
nRead += offset;
/* check for on-disk file format changes, but only for the first hunk */
if (bufPos == 0 && fileFormat != FormatOfFile(fileString)) {
fclose(fp);
AllWindowsUnbusy();
return (1);
}
if (fileFormat == MAC_FILE_FORMAT)
ConvertFromMacFileString(fileString, nRead);
else if (fileFormat == DOS_FILE_FORMAT)
ConvertFromDosFileString(fileString, &nRead, &pendingCR);
/* Beware of 0 chars ! */
BufSubstituteNullChars(fileString, nRead, buf);
rv = BufCmp(buf, bufPos, nRead, fileString);
if (rv) {
fclose(fp);
AllWindowsUnbusy();
return (rv);
}
bufPos += nRead;
restLen = min(fileLen - filePos, PREFERRED_CMPBUF_LEN);
}
AllWindowsUnbusy();
fclose(fp);
if (pendingCR) {
rv = BufCmp(buf, bufPos, 1, &pendingCR);
if (rv) {
return (rv);
}
bufPos += 1;
}
if (bufPos != buf->length) {
return (1);
}
return (0);
}
/*
** Force ShowLineNumbers() to re-evaluate line counts for the window if line
** counts are required.
*/
static void forceShowLineNumbers(WindowInfo *window)
{
Boolean showLineNum = window->showLineNumbers;
if (showLineNum) {
window->showLineNumbers = False;
ShowLineNumbers(window, showLineNum);
}
}
static int min(int i1, int i2)
{
return i1 <= i2 ? i1 : i2;
}
|