Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

system.c 30.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/******************************************************************************
SYSTEM.C     Miscellaneous system functions.

    Copyright 1993 - 2000 Paul J. Sidorsky

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as
    published by the Free Software Foundation.

    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
Deucе's avatar
Deucе committed
17
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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

This module contains miscellaneous technical functions and low-level file I/O
functions.
******************************************************************************/

#include "top.h"

/* calc_days_ago() - Calculate number of days between a date and today.
   Parameters:  lastdate - Pointer to date to be tested.
   Returns:  Number of days since the given date.
   Notes:  Normally used in the logon sequence to tell the user how long
           since their last call.
*/
XINT calc_days_ago(XDATE *lastdate)
{
struct date thisdate; /* Today's date. */
long d1 = 0, d2 = 0; /* Days in first date, days in today's date. */
long daysago = 0; /* Return value. */
XINT d; /* Counter. */

getdate(&thisdate);

/* This function converts each date into the number of days elapsed since
   what is technically 01/01/0001.  (In other words, 01/01/0001 is the
   base date and gets a value of 0).  This provides a numeric basis by which
   to compare the two dates. */

/* This function was written when I was new to C and was unfamiliar with
   UNIX-style time (the time_t type).  Using time_t would no doubt be
   much easier. */

/* Convert each year into days. */
for (d = 1; d < lastdate->da_year; d++)
	{
	d1 += 365;
    /* Adjust for leap year. */
	if (d % 4 == 0)
		{
		d1++;
		}
    }
/* Convert each month into days. */
for (d = 1; d < lastdate->da_mon; d++)
	{
    switch(d)
    	{
        case 1 : d1 += 31; break;
        case 2 :
			d1 += 28;
            /* Adjust for current leap year. */
            if (lastdate->da_year % 4 == 0)
            	{
                d1++;
                }
			break;
        case 3 : d1 += 31; break;
        case 4 : d1 += 30; break;
        case 5 : d1 += 31; break;
        case 6 : d1 += 30; break;
        case 7 : d1 += 31; break;
        case 8 : d1 += 31; break;
        case 9 : d1 += 30; break;
        case 10: d1 += 31; break;
        case 11: d1 += 30; break;
		}
    }
/* Count each day for the current month. */
for (d = 1; d < lastdate->da_day; d++)
	{
    d1++;
    }

/* The same counting procedure above is repeated for today's date. */
for (d = 1; d < thisdate.da_year; d++)
	{
	d2 += 365;
	if (d % 4 == 0)
		{
		d2++;
		}
    }
for (d = 1; d < thisdate.da_mon; d++)
	{
    switch(d)
    	{
        case 1 : d2 += 31; break;
        case 2 :
			d2 += 28;
            if (thisdate.da_year % 4 == 0)
            	{
                d2++;
                }
			break;
        case 3 : d2 += 31; break;
        case 4 : d2 += 30; break;
        case 5 : d2 += 31; break;
        case 6 : d2 += 30; break;
        case 7 : d2 += 31; break;
        case 8 : d2 += 31; break;
        case 9 : d2 += 30; break;
        case 10: d2 += 31; break;
        case 11: d2 += 30; break;
		}
    }
for (d = 1; d < thisdate.da_day; d++)
	{
    d2++;
    }

/* The difference is now a simple subtraction operation. */
daysago = d2 - d1;

return daysago;
}

/* This is the old name processor that was used up to and including TOP
   v2.00g1.  The new one (used in v2.00 and up) appears later.  This old
   processor also used an external scoring function called find_max_score()
   which follows the main processor function below. */
/*XINT find_node_from_name(char *newstring, char *handle, char *oldstring)
{
XINT x, y;
XINT *count = NULL;
XINT cc;
XINT maxs, themax;
char fntt[256]; // Dynam with MAXSTRLEN!

count = calloc(MAXNODES, 2);
if (!count)
    {
    dispatch_message(MSG_GENERIC, top_output(OUT_STRINGNF,
                                             getlang("OutOfMemory")),
                     od_control.od_node, 0, 0);
    od_log_write(top_output(OUT_STRING, getlang("LogOutOfMemory")));
    return -1;
    }
memset(count, 0, MAXNODES * 2);

check_nodes_used(TRUE);

for (x = 0, cc = 1; x < strlen(oldstring) && x < 30 && cc > 0; x++)
	{
	for (y = 0, cc = 0; y < MAXNODES; y++)
    	{
        filter_string(fntt, handles[y].string);
        if (toupper(fntt[x]) == toupper(oldstring[x]) && count[y] == x
			&& activenodes[y])
        	{
            count[y]++;
            cc++;
            }
        }
	}

maxs = find_max_score(count, &themax);

dofree(count);

if (maxs >= 0 && (forgetstatus[maxs] & FGT_HASFORGOTME))
	{
    lastsendtonode = maxs;
    return -3;
    }
if (maxs >= 0)
    {
    if (activenodes[maxs] == 2)
        {
        lastsendtonode = maxs;
        return -4;
        }
    }

if (maxs == -1)
	{
	fixname(handle, oldstring);
    newstring[0] = '\0';
    return -1;
    }

if (maxs == -2)
	{
	fixname(handle, oldstring);
    newstring[0] = '\0';
    return -2;
    }

strncpy(handle, oldstring, themax);
if (themax >= strlen(oldstring))
    {
    newstring[0] = '\0';
    }
else
    {
    strcpy(newstring, &oldstring[themax]);
    }

return maxs;
}

XINT find_max_score(XINT *check, XINT *thescore)
{
XINT dd;
XINT maxmax = 0, maxmaxset, highnode;
char fmtt[256]; // Dynam with MAXSTRLEN!

for (dd = 0, maxmaxset = 0, highnode = -1; dd < MAXNODES; dd++)
	{
    filter_string(fmtt, handles[dd].string);
    if (check[dd] > 0 && check[dd] == strlen(fmtt))
    	{
        memcpy(thescore, &check[dd], 2);
        return dd;
        }

	if (check[dd] == maxmax && maxmaxset)
    	{
        return -2;
        }

    if (check[dd] > maxmax)
    	{
        maxmaxset = 1;
        maxmax = check[dd];
        highnode = dd;
        }
    }

if (maxmax == 0)
	{
    return -1;
    }

memcpy(thescore, &maxmax, 2);

return highnode;
}*/
/* End of old name processor. */

/* show_time() - Displays the current time and time remaining.
   Parameters:  None.
   Returns:  Nothing.
*/
void show_time(void)
{
struct date dat; /* Current date. */
struct time tim; /* Current time. */
clock_t tmpc, mins; /* Time in TOP in ticks, time in TOP in minutes. */

/* In local and LAN modes, adjust the time limit in case it was deducted
   before it was "frozen".  It looks a little better if it is consistent. */
if (localmode || lanmode)
	{
    od_control.user_deducted_time = 0;
    od_control.user_timelimit = 1440;
    }

/* Initialize variables. */
getdate(&dat); gettime(&tim);
tmpc = myclock();
mins = (tmpc / CLK_TCK) / 60L;

/* top_output() needs all numeric values to be strings. */
itoa(dat.da_mon, outnum[0], 10);
itoa(dat.da_day, outnum[1], 10);
itoa(dat.da_year, outnum[2], 10);
itoa(tim.ti_hour, outnum[3], 10);
itoa(tim.ti_min, outnum[4], 10);
itoa(tim.ti_sec, outnum[5], 10);
/* Show the current time and date. */
top_output(OUT_SCREEN, getlang("DateTime"), outnum[0], outnum[1], outnum[2],
           outnum[3], outnum[4], outnum[5]);
/* Show how long the user has been in TOP. */
itoa(mins, outnum[0], 10);
itoa(od_control.user_timelimit, outnum[1], 10);
top_output(OUT_SCREEN, getlang("TimeIn"), outnum[0], outnum[1]);
if (cfg.usecredits > 0)
    {
    /* Show number of credits left if the sysop is using RA credits. */
    ltoa(od_control.user_credit, outnum[0], 10);
    top_output(OUT_SCREEN, getlang("CreditDisplay"), outnum[0]);
    }

return;
}

/* quit_top() - Exits TOP normally, displaying a farewell to the user.
   Parameters:  None.
   Returns:  Nothing.
*/
void quit_top(void)
    {
    XINT qtd, qpnum; /* Unused poker variables. */
//|    poker_game_typ qgame;

/* Unused poker code that folds all hands this node is playing in. */
/*//|    for (qtd = 0; qtd < cfg.pokermaxgames; qtd++)
        {
        if (pokeringame[qtd])
            {
            poker_lockgame(qtd);
            poker_loadgame(qtd, &qgame);
            if (poker_checkturn(POKSTG_ANYTURN, od_control.od_node, &qgame))
                {
                poker_foldplayer(od_control.od_node, qtd, &qgame);
                }

            qpnum = poker_getplayernum(POKPL_WAITING, od_control.od_node,
                                       &qgame);
            qgame.wanttoplay[qpnum] = -1;
            if (--qgame.wtpcount < 1)
                {
                qgame.inuse = 0;
                }
            poker_savegame(qtd, &qgame);
            poker_unlockgame(qtd);
            }
        }*///|

    /* Show closing credits. */
    quit_top_screen();

    /* Adjust RA credit value if needed. */
    fixcredits();

    /* Exit normally. */
    od_log_write(top_output(OUT_STRING, getlang("LogEnded"), ver));
    od_exit(0, FALSE);

    }

/* before_exit() - Cleanup function before TOP is exited.
   Parameters:  None.
   Returns:  Nothing.
   Notes:  Never called directly.  Called by the door kit during an
           od_exit() function call.
*/
void _USERENTRY before_exit(void)
{

/* Show any final messages before we logout. */
process_messages(TRUE);

/* Remove the node from TOP. */
if (isregistered)
	{
    unregister_node(1);
    dispatch_message(MSG_TOSSOUT, "\0", -1, 0, 0);
    }

/* De-init. */
closefiles();
memfree();

return;
}

/* errorabort() - Aborts TOP with an error code.
   Parameters:  code - Code to abort with (ERR_ constant).
                info - Error information, usually a file name.
   Returns:  Nothing.
   Notes:  This function should be called whenever a critical TOP file
           (such as USERS.TOP or NODEIDX.TCH) cannot be accessed.
*/
void errorabort(XINT code, char *info)
{
char logent[256]; /* Log entry buffer. */
XINT d; /* Not used. */

strcpy(outbuf, top_output(OUT_STRINGNF, getlang("FileErrorPrefix")));

/* Select proper message to show based on the error code. */
switch(code)
	{
    case ERR_CANTOPEN:
        strcat(outbuf, top_output(OUT_STRINGNF, getlang("CantOpen"),
               info));
        break;
    case ERR_CANTREAD:
        strcat(outbuf, top_output(OUT_STRINGNF, getlang("CantRead"),
               info));
        break;
    case ERR_CANTWRITE:
        strcat(outbuf, top_output(OUT_STRINGNF, getlang("CantWrite"),
               info));
        break;
    case ERR_CANTACCESS:
        strcat(outbuf, top_output(OUT_STRINGNF, getlang("CantAccess"),
               info));
        break;
    case ERR_CANTCLOSE:
        strcat(outbuf, top_output(OUT_STRINGNF, getlang("CantClose"),
               info));
        break;
	}

/* Log the error code. */
strcpy(logent, getlang("LogAlert"));
strcat(logent, &outbuf[4]);
filter_string(logent, logent);
od_log_write(logent);

/* Inform the user we have to exit. */
strcat(outbuf, getlang("FileErrorMsgPrefix"));
strcat(outbuf, getlang("InformSysop"));
strcat(outbuf, getlang("FileErrorMsgSuffix"));
top_output(OUT_SCREEN, outbuf);

od_exit(220 + code, FALSE);
}

/* closefile() - Closes a file.
   Parameters:  thefil - Pointer to file stream to close.
   Returns:  Error condition from fclose().
*/
XINT closefile(FILE *thefil)
{

/* This function is currently just a wrapper for fclose().  It is provided
   in case some common pre-close procedure needs to be performed. */

return fclose(thefil);
}

/* rec_locking() - Performs record-locking operations.
   Parameters:  op - Operation to perform (REC_ constant).
                hand - File handle to perform locking on.
                ofs - Starting position of data to lock.
                len - Length of data to lock.
   Returns:  TRUE if the operation failed, FALSE on success.
   Notes:  This function is called for _ALL_ record locking done inside
           TOP.  It provides try-counting and prevents an infinite loop
           from occuring.  It also notifies the user of a problem.  It is
           currently impossible for this function to return TRUE as it
           will exit TOP if a problem occurs.
*/
XINT rec_locking(char op, XINT hand, long ofs, long len)
{
/* Attempt counter, return code, flag if attempt limit exceeded,
   keypress holder. */
XINT d, failed, past = 0, key;
clock_t cl; /* Current time (used for interval determination). */

/* Loop until the operation succeeds, counting each attempt. */
for (d = 0, failed = 1; failed; d++)
	{
    /* If an attempt has just failed, wait the configured interval
       before trying again. */
    if (failed == -1)
    	{
        for(cl = myclock();
            ((float) myclock() - (float) cl) / (float) CLK_TCK <
            (float) RETRYINTERVAL;);
        }
    /* Perform the record locking. */
    switch(op)
    	{
        case REC_LOCK: failed = lock(hand, ofs, len); break;
        case REC_UNLOCK: failed = unlock(hand, ofs, len); break;
        }
    /* Once the configured number of tries has elapsed, inform the
       user of a problem. */
    if (d == MAXRETRIES)
	    {
        top_output(OUT_SCREEN, getlang("AccessProblem"));
        top_output(OUT_SCREEN, getlang("AccessPrompt"));
        past = 1;
    	}
    /* If we are passed the try limit, the user can abort back to the
       BBS. */
    if (past)
    	{
        /* Check for a key without waiting. */
        key = od_get_key(FALSE);
        /* ESC aborts to the BBS. */
        if (key == 27)
        	{
            top_output(OUT_SCREEN, getlang("AccessShutDown"));
            od_exit(250, FALSE);
            }
        }
    }

return failed;
}

/* sh_open() - Open a file handle in shared mode.
   Parameters:  nam - File name to open.
                acc - Access flags.
                sh - Sharing flags.
                mod - Mode flags.
   Returns:  File handle, or -1 on failure.
   Notes:  This function is used for _ALL_ non-streamed file opening
           in TOP.  It contains a loop to retry the configured number
           of times.
*/
XINT sh_open(char *nam, XINT acc, XINT sh, XINT mod)
{
XINT d, rethand = -1; /* Try counter, file handle. */

/* Loop until success or until the configured number of tries passes. */
for (d = 0; d < MAXOPENRETRIES && rethand == -1; d++);
    {
	rethand = sopen(nam, acc, sh, mod);
    }

return rethand;
}

/* openfile() - Opens a file stream.
   Parameters:  name - File name to open.
                mode - Mode string.
                errabort - Flag if TOP should abort on error.
   Returns:  Pointer to a file stream for the new file, or NULL on failure.
   Notes:  This is an older function from an early version of TOP that used
           a streamed communication system.  It is still used for some BBS
           interface files.  It contains code to retry and to abort on
           failure.
*/
FILE XFAR *openfile(char *name, char *mode, char errabort)
{
FILE XFAR *tmpfil = NULL; /* File handle holder. */
XINT cl; /* Retry counter. */
unsigned XINT sharebits; /* Sharing mode bits. */

strlwr(mode);
/* TOP used to need a different share mode based on how the file was
   opened. */
/*if (strstr(mode, "r") && !strstr(mode, "+"))
	{
    sharebits = SH_DENYWR;
    }
else
	{*/
	sharebits = SH_DENYRW;
/*    }*/

tmpfil = NULL;
/* Loop until success or until the maximum number of tries elapses. */
for (cl = 0; cl < MAXRETRIES && !tmpfil; cl++)
    {
    tmpfil = _fsopen(name, mode, sharebits);
    }

if (!tmpfil && errabort && errno == EACCES)
	{
    /* Abort on error, if requested. */
    top_output(OUT_SCREEN, getlang("SharingVioError"), name);
    unregister_node(0);
    od_log_write(top_output(OUT_STRING, getlang("LogSharingVioError")));
    od_log_write(top_output(OUT_STRING, getlang("LogSharingVioFile"), name));
    od_exit(251, FALSE);
    }

return tmpfil;
}

/* dofree() - Safely frees previously allocated memory.
   Parameters:  pfree() - Pointer to memory to free.
   Returns:  Nothing.
   Notes:  Used to free _ALL_ memory in TOP safely.  TOP does not always
           use all of its memory buffers so it is possible some will be
           NULL when they are freed.  Borland C does not check for this
           and it can cause problems if it occurs.  This function checks
           for NULL before freeing. */
void dofree(void XFAR *pfree)
{

if (pfree != NULL)
    {
    /* Only free non-NULL buffers. */
    free(pfree);
    pfree = NULL;
    }

return;
}

/* closefiles() - Closes TOP files before exiting.
   Parameters:  None.
   Returns:  Nothing.
*/
void closefiles(void)
{

/* Only major TOP files are closed because Borland C will close all files
   upon exit anyhow.  If the compiler is changed then all files will have
   to be closed here. */

close(userfil); close(nidxfil); close(nidx2fil); close(msginfil);
close(msgoutfil); close(useronfil); close(ipcinfil); close(ipcoutfil);
close(midxinfil); close(midxoutfil); close(chgfil); close(chidxfil);
close(bioidxfil); close(biorespfil);

return;
}

/* memfree() - Frees TOP memory before exiting.
   Parameters:  None.
   Returns:  Nothing.
*/
void memfree(void)
{
XINT mfd; /* Counter. */

/* Only major TOP memory is freed because Borland C will free all memory
   upon exit anyhow.  If the compiler is changed then all memory will need
   to be freed here. */

dofree(word_str); dofree(word_pos); dofree(word_len);
dofree(handles); dofree(activenodes);
dofree(outbuf); dofree(forgetstatus);
dofree(busynodes); dofree(wordret); dofree(node); dofree(outputstr);
dofree(nodecfg); dofree(lastcmd);
dofree(chan); dofree(bioques);

/* Free language items. */
for (mfd = 0; mfd < numlang; mfd++)
   	{
    dofree(langptrs[mfd]);
    }
dofree(langptrs);

return;
}

/* moreprompt() - Pauses between screens, prompting the user to continue.
   Parameters:  None.
   Returns:  Negative number if the user aborted, positive number if the
             user requested nonstop mode, or 0 for normal continuation.
*/
char moreprompt(void)
    {
    char nsmp = 0; /* Return code. */
    XINT key; /* Keypress holder. */

    /* All more prompts default to Yes. */

    top_output(OUT_SCREEN, getlang("MorePrompt"));
    key = od_get_answer(top_output(OUT_STRING, getlang("MoreKeys")));
    top_output(OUT_SCREEN, getlang("MorePromptSuffix"));

    /* The keys are held in the MoreKeys language item. */

    /* Abort (no more). */
    if (key == getlangchar("MoreKeys", 1))
        {
        nsmp = -1;
        top_output(OUT_SCREEN, getlang("MoreAbortSuffix"));
        }
    /* Nonstop mode. */
    if (key == getlangchar("MoreKeys", 2))
        {
        nsmp = 1;
        }

    return nsmp;
    }

/* get_extension() - Determines file extension based on terminal type.
   Parameters:  ename - Base name of file to test.
                ebuf - Buffer to hold extension.
   Returns:  Nothing.
   Notes:  This function gets an extension for an external display file,
           based on the user's terminal type.  Either ".asc", ".ans",
           ".avt", or ".rip" is returned.  This function is used with
           pictorial actions only, which can't be displayed by the door
           kit.  All other external files are shown through the doorkit,
           which determines the type of extension itself.  See TOP.DOC's
           section on External Files for information.  This function needs
           a valid base file name in the ename parameter to properly test
           for nonexistent files.
*/
void get_extension(char *ename, char *ebuf)
    {
    char ework[256]; /* Working file name buffer. */

    /* Test for RIP. */
    strcpy(ework, ename);
    if (od_control.user_rip)
        {
        strcat(ework, ".rip");
        if (!access(ework, 0))
            {
            strcpy(ebuf, ".rip");
            return;
            }
        }

    /* Test for AVATAR. */
    strcpy(ework, ename);
    if (od_control.user_avatar)
        {
        strcat(ework, ".avt");
        if (!access(ework, 0))
            {
            strcpy(ebuf, ".avt");
            return;
            }
        }

    /* Test for ANSI. */
    strcpy(ework, ename);
    if (od_control.user_ansi)
        {
        strcat(ework, ".ans");
        if (!access(ework, 0))
            {
            strcpy(ebuf, ".ans");
            return;
            }
        }

    /* Default to ASCII. */
    strcpy(ebuf, ".asc");

    }

/* myclock() - Midnight clock wrap compensator for DOS.
   Parameters:  None.
   Returns:  Current clock tick value from the clock() function.
   Notes:  This function is a replacement for the clock() function.  It
           corrects a bug which wraps the number of elapsed clock ticks
           since program start if the program runs through midnight.  It
           is not defined for TOP for OS/2 as this bug does not occur
           under OS/2.  (TOP/2 can still operate properly if this
           function is defined for it, but it would add overhead.)  It is
           not clear whether the bug is in DOS or in the Borland runtime
           library.
*/
#ifndef __OS2__
clock_t myclock(void)
    {
    clock_t retclock; /* Clock tick value to return. */

    /* Get the current clock tick value. */
    retclock = clock();

    /* What happens is that after midnight, a day's worth of clock ticks
       (1 573 040) is somehow subtracted from the clock tick value.  These
       just have to be added back to restore the proper clock value. */
    if (retclock < 0)
        {
        retclock += 1573040L;
        }

    return retclock;
    }
#endif

/* fixcredits() - Adjusts number of RA credits upon exit.
   Parameters:  None.
   Returns:  Nothing.
   Notes:  In some cases, RA seems to do its own credit subtracting upon
           return from a door, so the original number of credits needs to be
           restored before exiting or the credits would be subtracted twice.
           This is done if the value of cfg.usecredits is positive.  If it is
           negative, TOP will subtract the credits itself (i.e. the value
           will not be restored).
*/
void fixcredits(void)
    {

    if (cfg.usecredits > 0)
        {
        od_control.user_credit = orignumcreds;
        }

    }

/* find_node_from_name() - Determines a node number based on a name.
   Parameters:  newstring - Buffer to hold the remainder of the string.
                handle - Buffer holding the full handle of the user.
                oldstring - String to test.
   Returns:  A node number (>= 0) if a match was found, or a negative
             number on error:  -1 if no match could be made, -2 if the
             handle is not specific enough, -3 if a match was made but the
             user was forgotten by the user who's name matches, or -4 if
             a match was made but the user who matches is not on our
             current channel.  (-3 and -4 are "recoverable" conditions.)
             See notes.
   Notes:  This function is the TOP "name processor", which attempts to find
           a user currently in TOP based on a full or partial name input
           (passed to it in oldstring).  If at least a partially matching
           name is found, on return handle will contain the text in
           oldstring that matched, and newstring will contain everything
           after the part that was used to make the match (including any
           leading spaces).  If no match can be found, newstring will be
           blank and handle will contain the part of the string that failed
           to match.  If -3 or -4 is returned (for reasons described in the
           return code information above), handle and newstring will contain
           the proper values, and the global variable lastsendtonode will
           contain the node that matched. This allows the caller to either
           report the condition to the user, or ignore the condition if it
           is not relevant.  For example, BAN and INVITE commands must span
           channels so it does not matter if a -4 is returned.  This
           function works by comparing "scores" for each node.  The scoring
           process is described inside the function body.
*/
XINT find_node_from_name(char *newstring, char *handle, char *oldstring)
    {
    /* Node counter, length of current handle, length of oldstring. */
    XINT x, lhan, lold;
    XINT cursco = 0, maxs = 0; /* Current score, best score to date. */
    XINT highnode = -1; /* Current node that has the high score. */
    char tied = 0; /* Flag if there is a tie score. */

    /* All matching is case-insensitive! */

    lold = strlen(oldstring);

    check_nodes_used(TRUE);

    /* Test exact matches for each node. */
    for (x = 0; x < MAXNODES; x++)
        {
        lhan = strlen(handles[x].string);
        /* There can only be an exact match if oldstring is at least as
           long as the entire handle. */
        if (lhan >= lold)
            {
            /* An exact match means the start oldstring contains all of
               the characters of the handle being tested.  It may contain
               more characters after (for text or other purposes). */
            if (activenodes[x] &&
                !strnicmp(oldstring, handles[x].string, lhan))
                {
                /* The longest exact match is saved. */
                if (lhan > maxs)
                    {
                    maxs = lhan;
                    highnode = x;
                    }
                }
            }
        }

    /* Only check for a partial match if there wasn't an exact match above.
       This is done because if for example one user was named "Joe" and
       another was named "Joey", it would be impossible to speak to "Joe"
       because the score for both names would tie and return a -2
       (not-specific enough).  This is one of the shortcomings of the
       old name processor that appears earlier in this module. */
    if (maxs == 0)
        {
        /* Test each node. */
        for (x = 0; x < MAXNODES; x++)
            {
            cursco = 0;
            lhan = strlen(handles[x].string);

            /* The "score" for a node is simply how many consecutive
               characters match the start of oldstring.  For example, if
               oldstring contained "al Hey there!" and there was a user
               inside TOP named "Allan", the "score" for that node
               would be 2. */
            while (toupper(handles[x].string[cursco]) ==
                   toupper(oldstring[cursco]) &&
                   cursco < lhan && cursco < lold)
                {
                cursco++;
                }
            /* Check for a tie. */
            if (maxs > 0 && cursco == maxs)
                {
                tied = 1;
                }
            if (cursco > maxs)
                {
                /* This is now the new "high score". */
                tied = 0;
                maxs = cursco;
                highnode = x;
                }
            }

        /* To guard against inadvertant matching, there must be a
           whitespace character after the matched part of the name, unless
           there is a whitespace character at the end of the match part
           (which usually means the user specified one full word of a
           multi-word name).  For example, if the string contained "Jose"
           and there was only a user named "Joe" that matched, the match
           would be negated so messages for "Jose" would not accidentally
           go to "Joe".  This most commonly occurs when there are two or
           more similarly named users inside TOP and one of them leaves.
           The old name processor, included earlier in this module, does
           not fix this problem and is prone to such unwanted matches. */
        if (maxs > 0)
            {
            if (!iswhite(oldstring[maxs - 1]) && !iswhite(oldstring[maxs]))
                {
                maxs = 0;
                tied = 0;
                }
            }

        /* Tie situation. */
        if (tied)
            {
            memset(handle, 0, 31);
            strncpy(handle, oldstring, 30);
            fixname(handle, handle);
            newstring[0] = '\0';
            return -2;
            }

        /* No match was found. */
        if (maxs == 0)
            {
            memset(handle, 0, 31);
            strncpy(handle, oldstring, 30);
            fixname(handle, handle);
            newstring[0] = '\0';
            return -1;
            }
        }

    /* As mentioned in the notes for this function, the cases below are
       "recoverable" errors. */

    /* A match was found but the user who matches has forgotten this
       user. */
    if (forgetstatus[highnode] & FGT_HASFORGOTME)
        {
        lastsendtonode = highnode;
        return -3;
        }

    /* A match was found but the user who matches is not in our current
       channel. */
    if (activenodes[highnode] == 2)
        {
        lastsendtonode = highnode;
        return -4;
        }

    /* handle only contains the text used to make the match.  It is up to
       the calling function to get the entire handle from the master handle
       index (using the returned node number from this function) if it is
       needed for display purposes. */
    strncpy(handle, oldstring, maxs);

    if (maxs >= strlen(oldstring))
        {
        /* If oldstring only contained a name, newstring will contain
           nothing. */
        newstring[0] = '\0';
        }
    else
        {
        /* Compensate if the user specified a full word of a multi-word
           handle.  TOP considers the space as part of the matched handle
           and, without compensation, would not include it in newstring.
           However, many routines expect the space at the front so the
           compensation puts it back there. */
        if (iswhite(handle[maxs - 1]))
            {
            maxs--;
            }
        /* Newstring contains everything after the text used for the
           match. */
        strcpy(newstring, &oldstring[maxs]);
        }

    return highnode;
    }

/* sh_unlink() - Share-aware file deletion function.
   Parameters:  filname - Name of file to delete.
   Returns:  FALSE on success, TRUE on error.
   Notes:  This function is needed because on some systems, if a program
           tries to delete a file while it is in use, a message will pop
           up on the system which will require a human response before
           the program can continue.  This is obviously undesirable in
           an automatic BBS that may never even have a human on the
           premises where the system is located.
*/
XINT sh_unlink(char *filnam)
    {

    /* Don't delete if we don't have full access to the file. */
    if (access(filnam, 6))
        {
        return -1;
        }

    return (unlink(filnam));
    }