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 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205
|
/*
filename: WCD.C
WCD - Chdir for Dos and Unix.
See file wcd.txt
Author: Erwin Waterlander
Address :
Neercanne 1
5655 AC Eindhoven
The Netherlands
E-mail : waterlan@xs4all.nl
or : erwin.waterlander@philips.com
WWW : http://www.xs4all.nl/~waterlan/
======================================================================
= Copyright =
======================================================================
Copyright (C) 1997-2003 Erwin Waterlander
This program 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.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
=======================================================================
TAB = 3 spaces
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <memory.h>
#include <dirent.h>
#include <dir.h>
#include <errno.h>
#include <unistd.h>
#include "match.h"
#include "std_macro.h"
#include "structures.h"
#include "nameset.h"
#include "WcdStack.h"
#include "dirnode.h"
#include "Text.h"
#include "wcd.h"
#include "stack.h"
#include "Error.h"
#include "display.h"
#include "wfixpath.h"
#include "dosdir.h"
#include "graphics.h"
/* Global variables */
const char *default_mask = ALL_FILES_MASK;
void rmDriveLetter(char path[], int *use_HOME)
{
char *ptr;
/* remove drive letter */
if (*use_HOME == 0 )
if ( (ptr=strstr(path,"/")) != NULL)
strcpy(path,ptr);
}
void rmDirFromList(char *string, nameset n)
{
char dir[DD_MAXDIR];
char subdir[DD_MAXDIR];
int i;
strcpy(dir,string);
wcd_fixpath(dir,sizeof(dir));
strcpy(subdir,dir);
strcat(subdir,"/*");
i = 0;
while (i < n->size )
{
if( dd_match(n->array[i],dir,1) || dd_match(n->array[i],subdir,1))
removeElementAtNamesetArray(i,n,true);
else
i++;
}
}
void writeList(char * filename, nameset n)
{
int i;
FILE *outfile;
if ( (outfile = fopen(filename,"w")) == NULL)
{
fprintf(stderr,"Wcd: error opening tree-file %s for write.\n", filename);
return;
}
else
{
for(i=0;(i<n->size);i++)
{
fprintf(outfile,"%s\n",n->array[i]);
}
fclose(outfile);
}
}
/*************************************************************
*
* void quoteString(char *string)
*
* -----
* quote all metacharacters (; & $ ( ) | < > space), quotes ("),
* apostrophes ('), grave accents (`), wildcard characters
* (* ? [ ]), and backslashes (\) with a backslash.
*
* Quoting every character with a backslash seems to be
* the most portable solution. Using apostrophes ('...') to quote the whole
* string makes it impossible to quote an apostrophe in the path. Using quotes
* ("...") does not work well in csh, because you can't escape a $ character
* inside quotes to stop variable substitution, and you can't escape a grave
* accent.
*
* Functions is_slash() and is_term() in file wfixpath.c have also
* been modified so that a backslash is not replaced by slash anymore
* on UNIX systems.
*
* Erwin Waterlander Oct 17 2001
*************************************************************/
#ifdef BASH
void quoteString(char *string)
{
int i,j,k,kmax;
char help1_str[DD_MAXDIR];
j = strlen(string);
k = 0;
kmax = DD_MAXDIR -3;
for (i=0; (i < j)&&(k < kmax) ; i++)
{
if ( (string[i] == '"') ||
(string[i] == '$') ||
(string[i] == '&') ||
(string[i] == '\'') ||
(string[i] == '(') ||
(string[i] == ')') ||
(string[i] == '*') ||
(string[i] == ';') ||
(string[i] == '<') ||
(string[i] == '>') ||
(string[i] == '?') ||
(string[i] == '[') ||
(string[i] == '\\') ||
(string[i] == ']') ||
(string[i] == ' ') ||
(string[i] == '`') ||
(string[i] == '|')
)
{
help1_str[k] = '\\';
k++;
}
help1_str[k] = string[i];
k++ ;
}
help1_str[k] = '\0' ;
strcpy(string,help1_str);
}
#endif
/*************************************************************
*
* changeDisk
*
* Returns:
* On succes : the new drive number >=0, or the current
* disk number >=0 if there was no drive to go to.
* Fail : -1
*
*************************************************************/
int changeDisk(char *path, int *changed, char *newdrive, int *use_HOME)
{
int i = -1, destDisk;
char *ptr;
char drive[DD_MAXDRIVE];
i = getdisk(); /* current disk */
if (strlen(path)>1)
{
strncpy(drive,path,2);
drive[DD_MAXDRIVE-1] = '\0';
if (dd_match(drive,"[a-z]:",1))
{
destDisk = islower(*drive) ? *drive-'a' : *drive-'A';
if (destDisk >= 0)
{
setdisk(destDisk);
i = getdisk(); /* new disk */
}
if ((i==destDisk) && (i>=0)) /* succes ? */
{
*changed = 1 ;
strcpy(newdrive,drive) ;
if((use_HOME == NULL)||(*use_HOME == 0))
{
ptr = path + 2;
if (strcmp(ptr,"") == 0)
{
strcpy(path,"/");
}
else
{
strcpy(path,ptr);
}
}
}
else
i = -1;
}
}
return(i);
}
/*****************************************************************/
void getCurPath(char *buffer, int size, int *use_HOME)
{
int len;
getcwd(buffer, size);
if(buffer != NULL)
{
len = strlen(buffer);
if (len==0)
buffer[len] = '\0';
wcd_fixpath(buffer,size);
rmDriveLetter(buffer,use_HOME);
}
}
/*****************************************************************
* add current path to file.
*
*****************************************************************/
void addCurPathToFile(char *filename, int *use_HOME, int parents)
{
char tmp[DD_MAXDIR]; /* tmp string buffer */
FILE *outfile;
getCurPath(tmp,DD_MAXDIR,use_HOME);
if(tmp != NULL)
{
/* open the treedata file */
if ((outfile = fopen(filename,"a")) == NULL)
{
fprintf(stderr,"Wcd: error opening file %s for write.\n", filename);
}
else
{
fprintf(outfile,"%s\n",tmp);
printf("Wcd: %s added to file %s\n",tmp,filename);
if (parents)
{
char *ptr ;
while ( (ptr = strrchr(tmp,DIR_SEPARATOR)) != NULL)
{
*ptr = '\0' ;
/* keep one last separator in the path */
if (strrchr(tmp,DIR_SEPARATOR) != NULL)
{
fprintf(outfile,"%s\n",tmp);
printf("Wcd: %s added to file %s\n",tmp,filename);
}
}
}
fclose(outfile);
}
}
}
/****************************************************************/
typedef struct TDirTag {
char* dirname;
struct TDirTag *next;
} TDirEntry;
typedef struct {
TDirEntry *head, *tail;
} TDirList;
/* Function: SpecialDir
*
* Purpose: Test for special directories
*
* Returns: 1 if path = "." or ".."
* 0 otherwise.
*/
int SpecialDir(const char *path)
{
if (*path != '.') return 0;
if (*(++path) == '.') path++;
return (*path=='/' || *path=='\0');
}
/******************************************************************
*
* q_insert - insert directory name to queue
*
******************************************************************/
void q_insert(TDirList *list,const char *s)
{
TDirEntry *ptr;
int len = strlen(s);
if (!len) return;
if ((ptr = (TDirEntry*) malloc(sizeof(TDirEntry))) == NULL )
{
perror("malloc");
return;
}
if ((ptr->dirname = (char*) malloc(len+1)) == NULL )
{
perror("malloc");
free(ptr);
return;
}
strcpy(ptr->dirname, s);
ptr->next = NULL;
if (!list->head) list->head = ptr;
else list->tail->next = ptr;
list->tail = ptr;
}
/*******************************************************************
*
* q_remove - remove directory name from queue
*
*******************************************************************/
int q_remove(TDirList *list,char *s)
{
TDirEntry *ptr = list->head;
if (!ptr) return 0; /* queue empty? */
strcpy(s, ptr->dirname);
list->head = ptr->next;
free(ptr->dirname);
free(ptr);
return 1; /* okay */
}
/********************************************************************
*
* rmTree(dir)
*
* Recursively delete directory: *dir
*
********************************************************************/
void rmTree(char *dir)
{
static struct ffblk fb; /* file block structure */
char tmp[DD_MAXDIR]; /* tmp string */
int rc; /* error code */
TDirList list; /* directory queue */
if (dir)
{
if (chdir(dir)) return; /* Go to the dir, else return */
}
else
return ; /* dir == NULL */
rc = findfirst( default_mask, &fb, FA_DIREC|FA_RDONLY|FA_HIDDEN|FA_SYSTEM|FA_ARCH|FA_LABEL);
list.head = list.tail = 0;
while (rc==0) /* go through all the files in the current dir */
{
if (DD_ISDIREC(fb.ff_attrib))
{
#ifndef VMS
/* Ignore directory entries starting with '.'
* which includes the current and parent directories.
*/
if (!SpecialDir(fb.ff_name))
#endif /* ?!VMS */
q_insert(&list, fb.ff_name); /* add all directories in current dir to list */
}
else
if ( remove(fb.ff_name) != 0) /* not a directory */
fprintf(stderr,"Wcd: Cannot remove file: %s\n", fb.ff_name);
rc = findnext(&fb);
} /* while !rc */
/* recursively parse subdirectories (if any) */
while (q_remove(&list, tmp))
{
rmTree(tmp);
if (rmdir(tmp) != 0)
fprintf(stderr,"Wcd: Cannot remove directory: %s\n", tmp);
}
chdir(DIR_PARENT); /* go to parent directory */
}
/********************************************************************
*
* pathInNameset(char *path, nameset set)
*
* Test if *path is a directory or a subdirectory of one
* of the paths in set.
*
* set is a list of paths.
* wildcards are supported.
*
*
* Returns -1 if directory is not in nameset.
* Returns index number (>=0) of first match found if directory is in nameset.
*
********************************************************************/
int pathInNameset (text path, nameset set)
{
char tmp[DD_MAXDIR+2];
int size, index = 0;
if ((path == NULL)||(set == NULL))
return(-1);
size = getSizeOfNamesetArray(set);
while (index < size)
{
strcpy(tmp,set->array[index]);
strcat(tmp,"/*");
#ifdef MSDOS
if ((dd_match(path,set->array[index],1)) || (dd_match(path, tmp,1)))
#else
if ((dd_match(path,set->array[index],0)) || (dd_match(path, tmp,0)))
#endif
{
return(index);
}
index++;
}
return(-1);
}
/********************************************************************
*
* finddirs(char* dir, int *offset, FILE *outfile, int *use_HOME)
*
********************************************************************/
void finddirs(char* dir, int *offset, FILE *outfile, int *use_HOME, nameset exclude)
{
static struct ffblk fb; /* file block structure */
static char tmp[DD_MAXDIR]; /* tmp string buffer */
int rc; /* error code */
int len ;
TDirList list; /* directory queue */
char *tmp_ptr ;
if (dir)
{
if (chdir(dir)) return; /* ?err */
}
else
return ; /* dir == NULL */
getcwd(tmp, sizeof(tmp));
wcd_fixpath(tmp,sizeof(tmp));
rmDriveLetter(tmp,use_HOME);
if (pathInNameset(tmp,exclude) >= 0)
{
chdir(DIR_PARENT); /* go to parent directory */
return;
}
len = strlen(tmp);
if(*offset < len)
tmp_ptr = tmp + *offset ;
else
tmp_ptr = tmp + len; /* tmp_ptr points to ending '\0' of tmp */
fprintf(outfile,"%s\n", tmp_ptr);
rc = findfirst( default_mask, &fb, FA_DIREC|FA_RDONLY|FA_HIDDEN|FA_SYSTEM|FA_ARCH|FA_LABEL );
list.head = list.tail = 0;
while (rc==0) /* go through all the files in the current dir */
{
if (DD_ISDIREC(fb.ff_attrib))
{
/* Ignore directory entries starting with '.'
* which includes the current and parent directories.
*/
if (!SpecialDir(fb.ff_name))
q_insert(&list, fb.ff_name);
}
rc = findnext(&fb);
} /* while !rc */
/* recursively parse subdirectories (if any) */
while (q_remove(&list, tmp))
finddirs(tmp,offset, outfile, use_HOME, exclude);
if (dir) chdir(DIR_PARENT); /* go to parent directory */
}
/********************************************************************
*
* scanDisk(char *path, char *treefile, int scanreldir, int append, int *use_HOME)
*
* append == 1 : append to treefile
* scanreldir == 1 : write relative paths in rtdata.wcd
*
********************************************************************/
void scanDisk(char *path, char *treefile, int scanreldir, int append, int *use_HOME, nameset exclude)
{
int offset = 0 ; /* offset to remove scanned from path */
char tmp[DD_MAXDIR]; /* tmp string buffer */
char tmp2[DD_MAXDIR]; /* tmp string buffer */
DIR* dirp; /* GJM */
FILE *outfile;
char drive[DD_MAXDRIVE];
int changedrive = 0;
wcd_fixpath(path,DD_MAXDIR);
wcd_fixpath(treefile,DD_MAXDIR);
getcwd(tmp2, sizeof(tmp2)); /* remember current dir */
if((dirp=opendir(path)) != NULL)
{
closedir(dirp); /* GJM */
}
else
{
fprintf(stderr,"Wcd: error: cannot open directory %s\n",path);
return ;
}
printf("Wcd: Please wait. (re)Scanning disk. Building treedata-file from %s\n",path);
#ifdef MSDOS
changeDisk(path,&changedrive,drive,use_HOME);
#endif
if (scanreldir)
{
if ( !chdir(path) )
{
getcwd(tmp, sizeof(tmp)); /* get full path */
#ifdef MSDOS
wcd_fixpath(tmp,sizeof(tmp));
rmDriveLetter(tmp,use_HOME);
#endif
offset = strlen(tmp);
/* do not count ending separator (if present) */
if (offset > 0 && tmp[offset-1]==DIR_SEPARATOR)
offset--;
offset++;
}
chdir(tmp2); /* go back */
}
#ifdef MSDOS
/* open the output file */
if (append)
outfile = fopen(treefile,"a"); /* append to database */
else
outfile = fopen(treefile,"w"); /* create new database */
if (!outfile) /* Try to open in a temp dir */
{
char *ptr;
if ( (ptr = getenv("TEMP")) != NULL )
{
strcpy(treefile,ptr);
strcat(treefile,TREEFILE);
outfile = fopen(treefile,"w");
}
if (!outfile)
{
if ( (ptr = getenv("TMP")) != NULL )
{
strcpy(treefile,ptr);
strcat(treefile,TREEFILE);
outfile = fopen(treefile,"w");
}
if (!outfile)
{
if ( (ptr = getenv("TMPDIR")) != NULL )
{
strcpy(treefile,ptr);
strcat(treefile,TREEFILE);
outfile = fopen(treefile,"w");
}
}
}
}
if (!outfile) /* Did we succeed? */
{
fprintf(stderr,"Wcd: error opening treefile for write.\n");
fprintf(stderr,"Wcd: Set TEMP environment variable if this is a read-only disk.\n");
return ;
}
#else
/* open the output file */
if (append)
outfile = fopen(treefile,"a"); /* append to database */
else
outfile = fopen(treefile,"w"); /* create new database */
if (!outfile)
{
fprintf(stderr,"Wcd: error opening file %s for write.\n", treefile);
return ;
}
#endif
finddirs( path, &offset, outfile, use_HOME, exclude); /* Build treedata-file */
fclose(outfile);
chdir(tmp2); /* go back */
}
/********************************************************************
*
* makeDir(char *path, char *treefile, int *use_HOME)
*
********************************************************************/
void makeDir(char *path, char *treefile, int *use_HOME)
{
char drive[DD_MAXDRIVE];
char tmp2[DD_MAXDIR];
int changedrive = 0;
wcd_fixpath(path,DD_MAXDIR);
/* is there a drive to go to ? */
changeDisk(path,&changedrive,drive,use_HOME);
if (mkdir(path,S_IWUSR)==0)
{
getcwd(tmp2, DD_MAXDIR); /* remember current dir */
if(chdir(path)==0) /* goto dir and add */
addCurPathToFile(treefile,use_HOME,0);
chdir(tmp2) ; /* go back */
}
else
{
fprintf(stderr,"Wcd: Cannot create directory %s\n",path);
}
}
/********************************************************************
*
* deleteDir(char *path, char *treefile, int recursive, int *use_HOME)
*
********************************************************************/
void deleteDir(char *path, char *treefile, int recursive, int *use_HOME)
{
static struct stat buf ;
char tmp2[DD_MAXDIR];
char drive[DD_MAXDRIVE];
int changedrive = 0;
wcd_fixpath(path,DD_MAXDIR);
/* is there a drive to go to ? */
changeDisk(path,&changedrive,drive,use_HOME);
stat(path, &buf) ;
if (S_ISDIR(buf.st_mode)) /* is it a dir */
{
getcwd(tmp2, DD_MAXDIR); /* remember current path */
if(chdir(path)==0)
{
getcwd(path, DD_MAXDIR); /* path to remove */
#ifdef MSDOS
wcd_fixpath(path,DD_MAXDIR);
rmDriveLetter(path,use_HOME);
#endif
chdir(tmp2);
}
if(recursive)
{
char c ;
c = 'x' ;
while ( (c != 'y') && (c != 'Y') && (c != 'n') && (c != 'N'))
{
printf("Wcd: Recursively remove %s Are you sure? y/n :",path);
/* Note that getchar reads from stdin and
is line buffered; this means it will
not return until you press ENTER. */
c = getchar(); /* get first char */
if (c != '\n')
while ((getchar()) != '\n') ; /* skip the rest */
}
if ( (c == 'y') || (c == 'Y') )
{
chdir(tmp2); /* go back */
rmTree(path); /* delete the stuff */
chdir(tmp2); /* go back */
if (rmdir(path) != 0)
fprintf(stderr,"Wcd: Cannot remove directory: %s\n", path);
cleanTreeFile(treefile,path);
} /* ( (c != 'y') || (c != 'Y') ) */
}
else
if (rmdir(path)==0)
{
printf("Wcd: Removed directory: %s\n",path);
cleanTreeFile(treefile,path);
}
else
fprintf(stderr,"Wcd: Cannot remove directory: %s\n",path);
}
else
fprintf(stderr,"Wcd: %s is not a directory.\n",path);
}
/********************************************************************
*
* Read a line from a file.
* Returns: length of the string , not including the ter-
* minating `\0' character.
********************************************************************/
int getline(char s[], int lim, FILE* infile)
{
int c, i ;
for (i=0; i<lim-1 && ((c=getc(infile)) != '\n') && (!feof(infile)) ; ++i)
{
s[i] = c ;
if (c == '\r') i--;
}
s[i] = '\0' ;
if (i == lim-1)
fprintf(stderr,"Wcd: error: line too long ( > %d). Fix: increase DD_MAXDIR.\n",(lim-1));
return i ;
}
/********************************************************************
*
* Read treefile in a structure.
*
********************************************************************/
void read_treefile(char* filename, nameset bd, int silent)
{
FILE *infile;
char path[DD_MAXDIR];
int len;
/* open treedata-file */
if ((infile = fopen(filename,"r")) != NULL)
{
while (!feof(infile) )
{
/* read a line */
len = getline(path,DD_MAXDIR,infile);
if (len > 0 )
{
wcd_fixpath(path,sizeof(path));
addToNamesetArray(textNew(path),bd);
}
} /* while (!feof(infile) ) */
fclose(infile);
}
}
/********************************************************************
*
* void cleanTreeFile(char *filename, char *dir)
*
* remove path from treefile
*
********************************************************************/
void cleanTreeFile(char *filename, char *dir)
{
nameset dirs;
dirs = namesetNew();
read_treefile(filename,dirs,0);
rmDirFromList(dir,dirs);
writeList(filename, dirs);
freeNameset(dirs, 1);
}
/********************************************************************
*
* check_double_match(char *dir, int perfect)
*
* Returns 0 if directory had no match before.
* Returns 1 if directory is double matched.
*
********************************************************************/
int check_double_match(char *dir, nameset set)
{
int i = 0;
if ((dir == NULL) || (set == NULL))
return(0);
while(i < set->size)
{
#ifdef MSDOS
if( stricmp(set->array[i],dir) == 0) return(1);
#else
if( strcmp(set->array[i],dir) == 0) return(1);
#endif
i++;
}
return(0);
}
/********************************************************************
*
* scanfile(char *org_dir, char *filename, int
* ignore_case, nameset pm, nameset wm, nameset bd)
*
*
********************************************************************/
void scanfile(char *org_dir, char *filename, int ignore_case,
nameset pm, nameset wm, nameset bd, int relative, int quiet, int wildOnly)
{
FILE *infile;
char line[DD_MAXDIR]; /* database path */
char *line_end; /* database directory */
char path_str[DD_MAXDIR]; /* path name to match */
char dirwild_str[DD_MAXDIR]; /* directory name to wild match */
char *dir_str ; /* directory name to perfect match */
char relative_prefix[DD_MAXDIR]; /* relative prefix */
char tmp[DD_MAXDIR];
int wild = 0;
/* open treedata-file */
if ((infile = fopen(filename,"r")) == NULL)
{
fprintf(stderr,"Wcd: error opening treedata-file %s\n",filename);
}
else
{
if( (dir_str = strrchr(org_dir,DIR_SEPARATOR)) != NULL)
dir_str++;
else dir_str = org_dir;
strcpy(dirwild_str,dir_str);
if ((strlen(org_dir)>1) && (dd_match(org_dir,"[a-z]:*",1)))
{
strncpy(path_str,org_dir,2);
path_str[DD_MAXDRIVE-1] = '\0';
line_end = org_dir + DD_MAXDRIVE ;
strcat(path_str,"*");
strcat(path_str,line_end);
}
else
{
strcpy(path_str,"*");
strcat(path_str,org_dir);
}
if (!dd_iswild(dir_str))
{
strcat(dirwild_str,"*");
strcat(path_str,"*");
wild = 0;
}
else
wild = 1;
if (wildOnly)
wild = 1;
if (relative)
{
strcpy(relative_prefix,filename);
if( (line_end = strrchr(relative_prefix,DIR_SEPARATOR)) != NULL)
{
line_end++ ;
*line_end = '\0';
}
}
while (!feof(infile) ) /* parse the file */
{
int len;
/* read a line */
len = getline(line,DD_MAXDIR,infile);
if( (line_end = strrchr(line,DIR_SEPARATOR)) != NULL)
line_end++;
else
line_end = line;
/* test for a perfect match */
if ( (wild == 0) && (dd_match(line_end,dir_str,ignore_case)) &&
(dd_match(line,path_str,ignore_case)) )
{
if (relative)
{
strcpy(tmp,relative_prefix);
strcat(tmp,line);
strcpy(line,tmp);
}
if(!quiet) printf("%s\n",line);
if ((pathInNameset(line,bd) == -1) &&
(check_double_match(line,pm)==0))
{
addToNamesetArray(textNew(line),pm);
}
}
else
{
/* test for a wild match if no perfect match */
if ( (dd_match(line_end,dirwild_str,ignore_case)) &&
(dd_match(line,path_str,ignore_case)) && (pm->size == 0))
{
if (relative)
{
strcpy(tmp,relative_prefix);
strcat(tmp,line);
strcpy(line,tmp);
}
if(!quiet) printf("%s\n",line);
if((pathInNameset(line,bd) == -1) &&
(check_double_match(line,wm)==0))
{
addToNamesetArray(textNew(line),wm);
}
}
}
} /* while (!feof(infile) ) */
fclose(infile);
}
}
/********************************************************************
*
* scanaliasfile(char *org_dir, char *filename,
* nameset *pm, nameset *wm, int quiet, int wildOnly)
*
*
********************************************************************/
void scanaliasfile(char *org_dir, char *filename,
nameset pm, nameset wm, int quiet, int wildOnly)
{
FILE *infile;
char *dir;
char line[DD_MAXDIR];
char alias[256];
dir = org_dir;
/* wcd_fixpath() could have added a '/' before the alias
e.g. wcd d:alias => /alias */
if (*dir == '/')
dir++;
/* open treedata-file */
if ((infile = fopen(filename,"r")) != NULL)
{
while (!feof(infile) )
{
int len;
if(fscanf(infile,"%s",alias)==1)
{
/* skip spaces between alias and path */
while ((line[0]=getc(infile)) == ' '){};
len = getline(line+1,DD_MAXDIR,infile);
len++;
if (len > 0 )
/* Only a perfect match counts, case sensitive */
if ((strcmp(alias,dir)==0) && (check_double_match(line,pm)==0))
{
if(!quiet)
printf("%s\n",line);
if(wildOnly)
addToNamesetArray(textNew(line),wm);
else
addToNamesetArray(textNew(line),pm);
}
}
} /* while (!feof(infile) ) */
fclose(infile);
}
}
/********************************************************************
*
* Get int
*
********************************************************************/
int wcd_get_int()
{
int i;
char string[32];
fgets(string,(int)sizeof(string),stdin);
fflush(stdin); /* flush the input stream in case of bad input */
/* fgets retains the newline character (LF) at the end of string
and appends a null byte to string to mark the end of the
string. */
string[strlen(string)-1] = '\0'; /* remove LF */
i=atoi(string);
return(i);
}
/********************************************************************
*
* exit
*
********************************************************************/
int wcd_exit(nameset pm, nameset wm, nameset ef, nameset bd, nameset nfs, WcdStack ws, nameset excl)
{
/* free datastructures */
freeNameset(pm, 1); /* perfect list */
freeNameset(wm, 1); /* wild list */
freeNameset(ef, 1); /* extra files */
freeNameset(bd, 1); /* banned dirs */
freeNameset(nfs, 1); /* relative files */
freeWcdStack(ws, 1); /* directory stack */
freeNameset(excl, 1); /* excluded paths */
return(0);
}
/********************************************************************
*
* Print help
*
********************************************************************/
void printhelp()
{
#ifdef BASH
printf("wcd %s (%s) - Wherever Change Directory (32 bit DOS BASH)\n",VERSION,VERSION_DATE);
#else
printf("wcd %s (%s) - Wherever Change Directory (32 bit)\n",VERSION,VERSION_DATE);
#endif
printf("Usage: wcd [drive:][dir] [-h] [-q] [-Q] [-b] [-l] [-c] [-e[e]] [-E <path>]\n");
printf(" [-s] [-S <path>] [-a[a]] [-A <path>] [-u <username>] [-f <treefile>]\n");
printf(" [-n <path>] [-i] [-d <drive>] [-[m|M|r|rmtree] <dir>] [-t]\n");
printf(" [-v] [-g[d]] [-N] [-o] [-j] [-G <path>] [-z #] [-[#]] [+[#]] [=] [-w]\n");
printf(" [-x <path>] [-xf <file>] [-k]\n");
printf(" dir (partial) name of directory to change to.\n");
printf(" Wildcards *, ? and [SET] are supported!\n");
printf(" -h show this Help -m Make <dir>, add to treefile\n");
printf(" -q unQuiet operation -M Make <dir>, add to extra treefile\n");
printf(" -Q Quieter operation -r Remove <dir>, (-rmtree recursive)\n");
printf(" -u use User's treefile (+u add) -c direct CD mode\n");
printf(" -f use extra treeFile (+f add) -l aLias current dir\n");
printf(" -n use relative treefile (+n add) -b Ban current path\n");
printf(" -s (re)Scan disk from $HOME -v print Version info\n");
printf(" -S Scan disk from <path> (+S rel) -L print software license\n");
printf(" -a Add current path to treedata -e add current path to Extra treedata\n");
printf(" -A Add tree from <path> -E add tree from <path> to Extra treedata\n");
printf(" - Push dir (# times) -i Ignore case (+i regard case)\n");
printf(" + Pop dir (# times) -d set <Drive> for stack & go files (DOS)\n");
printf(" = Show stack -z set max stack siZe\n");
printf(" -w Wild matching only -N use Numbers\n");
printf(" -o use stdOut -g Graphics -gd dump tree\n");
printf(" -j Just go mode -G set path Go-script -GN No Go-script\n");
printf(" -x eXclude path during disk scan -k Keep paths\n");
printf(" -xf eXclude paths from File\n");
#ifdef BASH
printf("This version is for DJGPP DOS bash or win32 ZSH under Win'95/98!\n");
#endif
}
/********************************************************************
*
* empty wcd.go file
*
********************************************************************/
#ifdef BASH
void empty_wcdgo(char *go_file, int changedrive, char *drive, int use_GoScript)
{
FILE *outfile;
if (use_GoScript == 0)
return;
if ((outfile = fopen(go_file,"w")) == NULL)
{
fprintf(stderr,"Wcd: error opening file %s for write.\n", go_file);
exit(0);
}
if(changedrive == 1)
fprintf(outfile,"cd %s\n",drive);
else
fprintf(outfile,"\n");
fclose(outfile);
}
#endif
void getPreviousDir(char* previousDir, char *filename)
{
FILE *infile;
*previousDir = '\0';
if ((infile = fopen(filename, "r")) == NULL)
fprintf(stderr,"Wcd: error opening file %s.\n", filename);
else
getline(previousDir,DD_MAXDIR,infile); /* store path */
}
/********************************************************************
*
* pickDir()
*
* gets current dirname.
* picks dirname from a nameset. The next one after the current,
* otherwise the first dirname.
*
* Returns 0, if no dir found. Otherwise the indexnumber of the
* list + 1.
*
* ******************************************************************/
int pickDir(nameset list,int *use_HOME)
{
char curDir[DD_MAXDIR];
int i;
if (list == NULL) /* there is no list */
return(0);
sort_list(list);
getCurPath(curDir,DD_MAXDIR,use_HOME);
if (curDir[0] == '\0') /* no dirname found */
return(1); /* return first of list */
if ((i = inNameset(curDir,list)) == -1) /* not in list */
return(1); /* return first of list */
i++; /* next dirname */
if (i >= getSizeOfNamesetArray(list))
i = 0; /* wrap to beginning */
return(i+1);
}
/********************************************************************
*
*
********************************************************************/
#ifdef BASH
void writeGoFile(char *go_file, int *changedrive, char *drive, char *best_match, int use_GoScript )
{
FILE *outfile;
if (use_GoScript == 0)
return;
if ((outfile = fopen(go_file,"w")) == NULL)
{
fprintf(stderr,"Wcd: error opening file %s for write.\n", go_file);
return;
}
if (*changedrive)
fprintf(outfile,"cd %s ; ",drive);
fprintf(outfile,"cd %s\n", best_match);
fclose(outfile);
}
#endif
/********************************************************************
*
* MAIN
*
********************************************************************/
int main(int argc, char **argv)
{
FILE *outfile;
char best_match[DD_MAXDIR];
char quiet = 1;
int i;
int exit_wcd = 0;
int use_default_treedata = 1;
int use_numbers = 0; /* use numbers instead of letters in conio or curses interface */
int use_stdout = 0; /* use stdout interface instead of curses */
int stack_hit = 0, stack_index;
char rootdir[DD_MAXDIR],treefile[DD_MAXDIR],banfile[DD_MAXDIR],aliasfile[DD_MAXDIR];
char stackfile[DD_MAXDIR];
char scandir[DD_MAXDIR];
char extratreefile[DD_MAXDIR];
char dir[DD_MAXDIR];
FILE *infile;
char scan_mk_rm = 0; /* scan disk, or make dir, or remove dir */
char *ptr, *stackptr;
int quieter = 0, cd = 0;
int len;
char tmp[DD_MAXDIR]; /* tmp string buffer */
DIR* dirp; /* GJM */
int stack_is_read=0;
WcdStack DirStack;
nameset extra_files, banned_dirs;
nameset perfect_list, wild_list ; /* match lists */
nameset relative_files;
nameset exclude; /* list of paths to exclude from scanning */
static struct stat buf ;
int ignore_case = 1;
char drive[DD_MAXDRIVE];
int changedrive = 0;
char string[256];
int use_HOME = 0 ;
int wildOnly = 0 ;
int justGo = 0;
int graphics = 0;
int keep_paths =0; /* Keep paths in treedata.wcd when wcd can't change to them */
#ifdef WCD_USECURSES
dirnode rootNode ;
#endif
#ifdef BASH
char go_file[DD_MAXDIR];
int use_GoScript = 1;
#endif
if ((ptr = getenv("WCDHOME")) == NULL)
ptr = getenv("HOME");
if (ptr != NULL)
{
use_HOME = 1 ;
strcpy(rootdir,ptr);
wcd_fixpath(rootdir,sizeof(rootdir));
strcpy(treefile,rootdir);
strcat(treefile,TREEFILE);
strcpy(extratreefile,rootdir);
strcat(extratreefile,EXTRA_TREEFILE);
#ifdef BASH
strcpy(go_file,rootdir);
strcat(go_file,GO_FILE);
#endif
strcpy(banfile,rootdir);
strcat(banfile,BANFILE);
strcpy(aliasfile,rootdir);
strcat(aliasfile,ALIASFILE);
strcpy(stackfile,rootdir);
strcat(stackfile,STACKFILE);
}
else
{
#ifdef BASH
strcpy(go_file,STACK_GO_DRIVE);
strcat(go_file,GO_FILE);
#endif
strcpy(rootdir,ROOTDIR);
strcpy(treefile,TREEFILE);
strcpy(extratreefile,EXTRA_TREEFILE);
strcpy(banfile,BANFILE);
strcpy(aliasfile,ALIASFILE);
strcpy(stackfile,STACK_GO_DRIVE);
strcat(stackfile,STACKFILE);
}
strcpy(scandir,rootdir);
strcpy(dir,"");
perfect_list = namesetNew();
wild_list = namesetNew();
extra_files = namesetNew();
banned_dirs = namesetNew();
relative_files = namesetNew();
exclude = namesetNew();
DirStack = WcdStackNew(WCD_STACK_SIZE);
read_treefile(banfile,banned_dirs,1);
stackptr = NULL;
/* ---------------------- parse the commandline ------------*/
for (i=1; i < argc; i++)
{
if (*argv[i]=='-') /* is it a switch? */
switch (argv[i][1]) {
case '\0':
if (stack_is_read == 0)
{
stack_read(DirStack,stackfile);
stack_is_read = 1;
}
stackptr = stack_push(DirStack,1);
if (stackptr != NULL)
{
stack_hit = 1;
stack_write(DirStack,stackfile);
}
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (stack_is_read == 0)
{
stack_read(DirStack,stackfile);
stack_is_read = 1;
}
ptr = argv[i];
ptr++;
stackptr = stack_push(DirStack,atoi(ptr));
if (stackptr != NULL)
{
stack_hit = 1;
stack_write(DirStack,stackfile);
}
break;
case 's':
scanDisk(rootdir,treefile,0,0,&use_HOME,exclude);
scan_mk_rm = 1;
break ;
case 'S':
case 'A':
case 'E':
case 'm':
case 'M':
case 'r':
scan_mk_rm = 1;
break;
case 'a':
if (argv[i][2] == 'a')
addCurPathToFile(treefile,&use_HOME,1);
else
addCurPathToFile(treefile,&use_HOME,0);
scan_mk_rm = 1;
break;
case 'e':
if (argv[i][2] == 'e')
addCurPathToFile(extratreefile,&use_HOME,1);
else
addCurPathToFile(extratreefile,&use_HOME,0);
scan_mk_rm = 1;
break;
case 'B':
case 'b':
addCurPathToFile(banfile,&use_HOME,0);
scan_mk_rm = 1;
break;
case 'l':
printf("Wcd: Enter alias for current directory: ");
fgets(string,(int)sizeof(string),stdin);
fflush(stdin); /* flush the input stream in case of bad input */
/* fgets retains the newline character (LF) at the end of string
and appends a null byte to string to mark the end of the
string. */
string[strlen(string)-1] = '\0'; /* remove LF */
if(strcmp(string,"")!=0)
{
getcwd(tmp, sizeof(tmp));
if(tmp != NULL)
{
len = strlen(tmp);
if (len==0)
tmp[len] = '\0';
/* open the treedata file */
if ((outfile = fopen(aliasfile,"a")) == NULL)
{
fprintf(stderr,"Wcd: error opening file %s for write.\n", aliasfile);
}
else
{
wcd_fixpath(tmp,sizeof(tmp)) ;
rmDriveLetter(tmp,&use_HOME);
fprintf(outfile,"%s %s\n",string,tmp);
printf("Wcd: %s added to aliasfile %s\n",tmp,aliasfile);
fclose(outfile);
}
}
}
#ifdef BASH /* empty wcd.go file */
empty_wcdgo(go_file,0,drive,use_GoScript);
#endif
return wcd_exit(perfect_list,wild_list,extra_files,banned_dirs,relative_files,DirStack,exclude);
break;
case 'q':
quiet = 0;
break;
case 'Q':
quiet = 1;
quieter = 1;
break;
case 'v':
case 'V':
printf("wcd %s (%s) - Wherever Change Directory\n",VERSION,VERSION_DATE);
printf("Chdir for Dos and Unix.\n\n");
printf("DOS 32 bit version.\n");
#ifdef BASH
printf("This version is for DJGPP DOS bash or win32 ZSH under Win'95/98!\n");
#endif
printf("Interface: ");
#ifdef WCD_USECONIO
printf("conio\n");
#elif WCD_USECURSES
# ifdef NCURSES_VERSION
printf("ncurses version %s\n",NCURSES_VERSION);
# elif __PDCURSES__
printf("PDCurses build %d\n",PDC_BUILD);
# else
printf("curses\n");
# endif
#else
printf("stdout\n");
#endif
printf("See also file wcd.txt\n\n");
printf("Download the latest executables and sources from:\n");
printf("http://www.xs4all.nl/~waterlan/\n");
#ifdef BASH /* empty wcd.go file */
empty_wcdgo(go_file,0,drive,use_GoScript);
#endif
return wcd_exit(perfect_list,wild_list,extra_files,banned_dirs,relative_files,DirStack,exclude);
break;
case 'g':
#ifdef WCD_USECURSES
graphics = 1;
if (argv[i][2] == 'd') /* dump tree to stdout */
graphics = 2;
#else
fprintf(stderr,"Wcd: Graphics mode only supported in curses version of wcd.\n");
#endif
break;
case 'L':
printf("wcd %s (%s) - Wherever Change Directory\n",VERSION,VERSION_DATE);
printf("Chdir for Dos and Unix.\n");
printf("Copyright (C) 1997-2003 Erwin Waterlander\n");
printf("Copyright (C) 1994-2002 Ondrej Popp on C3PO\n");
printf("Copyright (C) 1995-1996 DJ Delorie on _fixpath()\n");
printf("Copyright (C) 1995-1996 Borja Etxebarria & Olivier Sirol on Ninux Czo Directory\n");
printf("Copyright (C) 1994-1996 Jason Mathews on DOSDIR\n");
printf("Copyright (C) 1990-1992 Mark Adler, Richard B. Wales, Jean-loup Gailly,\n");
printf("Kai Uwe Rommel and Igor Mandrichenko on recmatch()\n");
printf("Source code to scan Windows LAN was originally written and placed\n");
printf("in the public domain by Felix Kasza.\n\n");
printf("This program is free software; you can redistribute it and/or\n");
printf("modify it under the terms of the GNU General Public License\n");
printf("as published by the Free Software Foundation; either version 2\n");
printf("of the License, or (at your option) any later version.\n\n");
printf("This program is distributed in the hope that it will be useful,\n");
printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
printf("GNU General Public License for more details.\n\n");
printf("You should have received a copy of the GNU General Public License\n");
printf("along with this program; if not, write to the Free Software\n");
printf("Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n");
#ifdef BASH /* empty wcd.go file */
empty_wcdgo(go_file,0,drive,use_GoScript);
#endif
return wcd_exit(perfect_list,wild_list,extra_files,banned_dirs,relative_files,DirStack,exclude);
break;
case 'c':
case 'C':
cd = 1;
break;
case 'x':
case 'f':
case 'z':
case 'n':
case 'J':
case 'G':
#ifdef BASH
if (argv[i][2] == 'N') /* No Go-script */
use_GoScript = 0;
#endif
break;
case 'N':
use_numbers = 1;
break;
case 'o':
use_stdout = 1;
break;
case 'i':
ignore_case = 1;
break;
case 'k':
keep_paths = 1;
break;
case 'w':
wildOnly = 1;
break;
case 'j':
justGo = 1;
break;
case 'd':
break;
default: /* any switch except the above */
printhelp();
#ifdef BASH /* empty wcd.go file */
empty_wcdgo(go_file,0,drive,use_GoScript);
#endif
return wcd_exit(perfect_list,wild_list,extra_files,banned_dirs,relative_files,DirStack,exclude);
}
else
if (*argv[i]=='+') /* Pop dir */
switch (argv[i][1]) {
case '\0':
if (stack_is_read == 0)
{
stack_read(DirStack,stackfile);
stack_is_read = 1;
}
stackptr = stack_pop(DirStack,1);
if (stackptr != NULL)
{
stack_hit = 1;
stack_write(DirStack,stackfile);
}
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (stack_is_read == 0)
{
stack_read(DirStack,stackfile);
stack_is_read = 1;
}
ptr = argv[i];
ptr++;
stackptr = stack_pop(DirStack,atoi(ptr));
if (stackptr != NULL)
{
stack_hit = 1;
stack_write(DirStack,stackfile);
}
break;
case 'f':
break;
case 'S':
scan_mk_rm = 1;
break;
case 'n':
break;
case 'i':
ignore_case = 0;
break;
default:
break;
}
else
if (*argv[i]=='=') /* Print stack */
{
if (stack_is_read == 0)
{
stack_read(DirStack,stackfile);
stack_is_read = 1;
}
stack_index = stack_print(DirStack,use_numbers,use_stdout);
if (stack_index >= 0)
{
stackptr = DirStack->dir[stack_index] ;
if (stackptr != NULL)
{
stack_hit = 1;
stack_write(DirStack,stackfile);
}
}
else
{
#ifdef BASH /* empty wcd.go file */
empty_wcdgo(go_file,0,drive,use_GoScript);
#endif
return wcd_exit(perfect_list,wild_list,extra_files,banned_dirs,relative_files,DirStack,exclude);
}
}
else /* Not a switch. Must be a dir of filename. */
{
if (strcmp(argv[i-1],"-f") == 0 )
{
use_default_treedata = 0;
addToNamesetArray(textNew(argv[i]),extra_files);
}
else
if (strncmp(argv[i-1],"-x",2) == 0 ) /* exclude paths from scanning */
{
strncpy(tmp,argv[i],sizeof(tmp));
wcd_fixpath(tmp,sizeof(tmp));
if (argv[i-1][2] == 'f')
read_treefile(tmp,exclude,0); /* read exclude paths from file */
else
addToNamesetArray(textNew(tmp),exclude); /* get path from argument */
/* printNameset("exclude ==>",exclude,stderr,true); */
}
else
if (strcmp(argv[i-1],"+f") == 0 )
{
addToNamesetArray(textNew(argv[i]),extra_files);
}
else
if (strcmp(argv[i-1],"-n") == 0 )
{
use_default_treedata = 0;
strncpy(tmp,argv[i],sizeof(tmp));
wcd_fixpath(tmp,sizeof(tmp));
#ifdef MSDOS
changeDisk(tmp,&changedrive,drive,&use_HOME);
#endif
stat(tmp, &buf) ;
if (S_ISDIR(buf.st_mode)) /* is it a dir */
{
strcat(tmp,RELTREEFILE);
wcd_fixpath(tmp,sizeof(tmp));
addToNamesetArray(textNew(tmp),relative_files);
}
else /* it is a file */
{
addToNamesetArray(textNew(tmp),relative_files);
}
}
else
if (strcmp(argv[i-1],"+n") == 0 )
{
strncpy(tmp,argv[i],sizeof(tmp));
wcd_fixpath(tmp,sizeof(tmp));
#ifdef MSDOS
changeDisk(tmp,&changedrive,drive,&use_HOME);
#endif
stat(tmp, &buf) ;
if (S_ISDIR(buf.st_mode)) /* is it a dir */
{
strcat(tmp,RELTREEFILE);
wcd_fixpath(tmp,sizeof(tmp));
addToNamesetArray(textNew(tmp),relative_files);
}
else /* it is a file */
{
addToNamesetArray(textNew(tmp),relative_files);
}
}
else
if (strcmp(argv[i-1],"-d") == 0 )
{
if (stack_is_read == 0)
{
stackfile[0] = argv[i][0];
#ifdef BASH
go_file[0] = argv[i][0];
#endif
}
}
else
if (strcmp(argv[i-1],"-S") == 0 )
{
strncpy(scandir,argv[i],sizeof(scandir));
scanDisk(scandir,treefile,0,0,&use_HOME,exclude);
}
else
if (strcmp(argv[i-1],"+S") == 0 )
{
strncpy(scandir,argv[i],sizeof(scandir));
strncpy(treefile,argv[i],sizeof(treefile) - strlen(RELTREEFILE));
strcat(treefile,RELTREEFILE);
scanDisk(scandir,treefile,1,0,&use_HOME,exclude);
}
else
if (strcmp(argv[i-1],"-z") == 0 )
{
if (stack_is_read == 0)
DirStack->maxsize = atoi(argv[i]);
}
else
if (strcmp(argv[i-1],"-A") == 0 )
{
strncpy(scandir,argv[i],sizeof(scandir));
scanDisk(scandir,treefile,0,1,&use_HOME,exclude);
}
else
if (strcmp(argv[i-1],"-E") == 0 )
{
strncpy(scandir,argv[i],sizeof(scandir));
scanDisk(scandir,extratreefile,0,1,&use_HOME,exclude);
}
else
if (strcmp(argv[i-1],"-m") == 0 )
{
strncpy(tmp,argv[i],sizeof(tmp));
makeDir(tmp,treefile,&use_HOME) ;
}
else
if (strcmp(argv[i-1],"-M") == 0 )
{
strncpy(tmp,argv[i],sizeof(tmp));
makeDir(tmp,treefile,&use_HOME) ;
}
else
if (strcmp(argv[i-1],"-r") == 0 ) /* remove one dir */
{
strncpy(tmp,argv[i],sizeof(tmp));
deleteDir(tmp,treefile,0,&use_HOME) ;
}
else
if (strcmp(argv[i-1],"-rmtree") == 0 ) /* remove dir recursive */
{
strncpy(tmp,argv[i],sizeof(tmp));
deleteDir(tmp,treefile,1,&use_HOME) ;
}
#ifdef BASH
else
if (strcmp(argv[i-1],"-G") == 0)
{
strncpy(go_file,argv[i],sizeof(go_file));
wcd_fixpath(go_file,sizeof(go_file)) ;
strcat(go_file,GO_FILE);
}
#endif
else
{
strncpy(dir,argv[i],sizeof(dir));
#ifdef MSDOS
/* Change backslashes to DIR_SEPARATOR if a backslash was typed
Avoid doing compares on strings
with backslashes. */
wcd_fixpath(dir,sizeof(dir));
#endif
}
}
} /* for */
/*--- end parsing commandline -----------*/
if (stack_is_read == 0)
{
stack_read(DirStack,stackfile);
stack_is_read = 1;
}
if ((strcmp(dir,"") == 0 )&&(graphics==0)) /* no directory given, no graphics, so we go HOME */
addToNamesetArray(textNew(rootdir),perfect_list);
/*--- stack hit ? ------------------------*/
if (stack_hit==1)
{
strcpy(best_match,stackptr);
if ((!quieter)&&(!justGo))
printf("-> %s\n",best_match);
#ifdef MSDOS
/* is there a drive to go to ? */
changeDisk(best_match,&changedrive,drive,&use_HOME);
#endif
#ifdef BASH
quoteString(best_match);
if (justGo)
printf("%s\n",best_match);
writeGoFile(go_file,&changedrive,drive,best_match,use_GoScript);
#else
chdir(best_match); /* change to directory */
#endif
stack_write(DirStack,stackfile);
return wcd_exit(perfect_list,wild_list,extra_files,banned_dirs,relative_files,DirStack,exclude);
}
/*--- end stack hit ? ------------------------*/
/*--- Direct CD mode -------------------------*/
if ((cd==1)&&(strcmp(dir,"")!=0)) /* Try open dir direct. */
{
if((dirp=opendir(dir)) != NULL) /* GJM */
{
closedir(dirp); /* GJM */
strcpy(best_match,dir);
if ((!quieter)&&(!justGo))
printf("-> %s\n",best_match);
#ifdef MSDOS
/* is there a drive to go to ? */
changeDisk(best_match,&changedrive,drive,&use_HOME);
#endif
chdir(dir);
getcwd(tmp, sizeof(tmp));
if(tmp != NULL)
{
len = strlen(tmp);
if (len==0)
tmp[len] = '\0';
wcd_fixpath(tmp,sizeof(tmp));
if ( (ptr=strstr(tmp,"/")) != NULL)
{
strcpy(best_match,tmp);
stack_add(DirStack,best_match);
stack_write(DirStack,stackfile);
}
}
#ifdef BASH
quoteString(best_match);
if (justGo)
printf("%s\n",best_match);
writeGoFile(go_file,&changedrive,drive,best_match,use_GoScript);
#else
chdir(best_match); /* change to directory */
#endif
return wcd_exit(perfect_list,wild_list,extra_files,banned_dirs,relative_files,DirStack,exclude);
}
} /* ? direct cd mode ((cd==1)&&(strcmp(dir,"")!=0)) */
/*--- end Direct CD mode -------------------------*/
/* is there a drive to go to ? */
changeDisk(dir,&changedrive,drive,&use_HOME);
/* does default treedata-file exist? */
if (use_default_treedata)
{
if ((infile = fopen(treefile,"r")) == NULL)
scanDisk(rootdir,treefile,0,0,&use_HOME,exclude);
else fclose(infile);
}
if((strcmp(dir,"")==0) && (scan_mk_rm))
{
/* No directory given. Don't go HOME. Exit and stay in current dir,
because we only wanted to scan the disk or make or remove a directory */
#ifdef BASH /* empty wcd.go file */
empty_wcdgo(go_file,changedrive,drive,use_GoScript);
#endif
return wcd_exit(perfect_list,wild_list,extra_files,banned_dirs,relative_files,DirStack,exclude);
}
/*********** scan treedata files ***************/
if (strcmp(dir,"")!=0) /* Is there a directory to go to? */
{
if (use_default_treedata)
scanfile(dir, treefile,ignore_case,perfect_list,wild_list,banned_dirs,0,quiet,wildOnly); /* scan the treedata file */
if ((outfile = fopen(extratreefile,"r")) != NULL)
{
fclose(outfile);
scanfile(dir, extratreefile,ignore_case,perfect_list,wild_list,banned_dirs,0,quiet,wildOnly); /* scan the extra treedata file */
}
/* search extra files */
for (i=0;i<extra_files->size;i++)
{
scanfile(dir, extra_files->array[i],ignore_case,perfect_list,wild_list,banned_dirs,0,quiet,wildOnly); /* scan the extra treedata file */
}
/* search relative files */
for (i=0;i<relative_files->size;i++)
{
scanfile(dir, relative_files->array[i],ignore_case,perfect_list,wild_list,banned_dirs,1,quiet,wildOnly); /* scan the nfs treedata file */
}
/* search alias file */
scanaliasfile(dir, aliasfile, perfect_list, wild_list,quiet,wildOnly);
}
#ifdef WCD_USECURSES
else
{
/* graphics? */
if (graphics)
{
nameset dirs;
dirs = namesetNew();
rootNode = createRootNode();
if(dirs != NULL)
{
if (use_default_treedata)
read_treefile(treefile,dirs,0);
read_treefile(extratreefile,dirs,1);
for (i=0;i<extra_files->size;i++)
{
read_treefile(extra_files->array[i],dirs,0);
}
for (i=0;i<relative_files->size;i++)
{
read_treefile(relative_files->array[i],dirs,0);
}
buildTreeFromNameset(dirs, rootNode);
setXYTree(rootNode);
}
freeNameset(dirs, 1);
if (graphics == 2)
{
dumpTree(rootNode);
ptr = NULL;
}
else
ptr = selectANode(rootNode,&use_HOME,ignore_case);
if (ptr != NULL)
addToNamesetArray(textNew(ptr),perfect_list);
else
{
#ifdef BASH /* empty wcd.go file */
empty_wcdgo(go_file,changedrive,drive,use_GoScript);
#endif
return wcd_exit(perfect_list,wild_list,extra_files,banned_dirs,relative_files,DirStack,exclude);
}
}
}
#endif
/*********** search is done ***************/
if ((perfect_list->size==0)&&(wild_list->size == 0)&&(cd==0)) /* No match at all & no direct CD mode */
{
if((dirp=opendir(dir)) != NULL) /* GJM */
{
/* typed directory exists */
closedir(dirp); /* GJM */
addToNamesetArray(textNew(dir),perfect_list);
}
}
/*******************************/
if ((perfect_list->size==0)&&(wild_list->size == 0)) /* No match at all */
{
printf("Wcd: No directory found matching %s\nWcd: Perhaps you need to rescan the disk or path is banned.\n",dir);
#ifdef BASH /* empty wcd.go file */
empty_wcdgo(go_file,changedrive,drive,use_GoScript);
#endif
return wcd_exit(perfect_list,wild_list,extra_files,banned_dirs,relative_files,DirStack,exclude);
}
else if (perfect_list->size > 1) /* choose from perfect list */
{
#ifdef WCD_USECURSES
if(graphics)
{
rootNode = createRootNode();
buildTreeFromNameset(perfect_list, rootNode);
setXYTree(rootNode);
ptr = selectANode(rootNode,&use_HOME,ignore_case);
if (ptr != NULL)
strcpy(best_match,ptr);
else
exit_wcd = 1;
}
else
#endif
{
if (justGo)
i = pickDir(perfect_list,&use_HOME);
else
i= display_list(perfect_list,1,use_numbers,use_stdout);
if ( (i>0) && (i<=perfect_list->size))
{
i--;
strcpy(best_match,perfect_list->array[i]);
}
else
exit_wcd = 1;
}
if (exit_wcd)
{
#ifdef BASH /* empty wcd.go file */
empty_wcdgo(go_file,changedrive,drive,use_GoScript);
#endif
return wcd_exit(perfect_list,wild_list,extra_files,banned_dirs,relative_files,DirStack,exclude);
}
}
else if (perfect_list->size==1) /* one perfect match */
{
strcpy(best_match,perfect_list->array[0]);
}
else if ((perfect_list->size==0)&&(wild_list->size > 1)) /* more than one wild match, zero perfect matches */
{ /* choose from wild list */
#ifdef WCD_USECURSES
if(graphics)
{
rootNode = createRootNode();
buildTreeFromNameset(wild_list, rootNode);
setXYTree(rootNode);
ptr = selectANode(rootNode,&use_HOME,ignore_case);
if (ptr != NULL)
strcpy(best_match,ptr);
else
exit_wcd = 1;
}
else
#endif
{
if (justGo)
i = pickDir(wild_list,&use_HOME);
else
i = display_list(wild_list,0,use_numbers,use_stdout);
if ( (i>0) && (i<=wild_list->size))
{
i--;
strcpy(best_match,wild_list->array[i]);
}
else
exit_wcd = 1;
}
if (exit_wcd)
{
#ifdef BASH /* empty wcd.go file */
empty_wcdgo(go_file,changedrive,drive,use_GoScript);
#endif
return wcd_exit(perfect_list,wild_list,extra_files,banned_dirs,relative_files,DirStack,exclude);
}
}
else /* (perfect_list->size==0) && (wild_list->size==1) */ /* one wild match, zero perfect matches */
{
strcpy(best_match,wild_list->array[0]);
}
/*******************************/
/* yes, a match (best_match) */
changeDisk(best_match,&changedrive,drive,&use_HOME);
if ((!quieter)&&(!justGo))
printf("-> %s\n",best_match);
if ( chdir(best_match) == 0)
{
getcwd(tmp, sizeof(tmp));
if(tmp != NULL)
{
len = strlen(tmp);
if (len==0)
tmp[len] = '\0';
wcd_fixpath(tmp,sizeof(tmp));
if ( (ptr=strstr(tmp,"/")) != NULL)
{
strcpy(best_match,tmp);
stack_add(DirStack,best_match);
stack_write(DirStack,stackfile);
}
}
}
else
{
fprintf(stderr,"Wcd: Cannot change to %s\n",best_match);
#ifdef BASH /* empty wcd.go file */
empty_wcdgo(go_file,changedrive,drive,use_GoScript);
#endif
if (keep_paths == 0)
cleanTreeFile(treefile,best_match);
return wcd_exit(perfect_list,wild_list,extra_files,banned_dirs,relative_files,DirStack,exclude);
}
#ifdef BASH
quoteString(best_match);
if (justGo)
printf("%s\n",best_match);
writeGoFile(go_file,&changedrive,drive,best_match,use_GoScript);
#else
chdir(best_match); /* change to directory */
#endif
return wcd_exit(perfect_list,wild_list,extra_files,banned_dirs,relative_files,DirStack,exclude);
}
|