home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / top2src.zip / SYSTEM.C < prev    next >
C/C++ Source or Header  |  2000-07-13  |  32KB  |  1,006 lines

  1. /******************************************************************************
  2. SYSTEM.C     Miscellaneous system functions.
  3.  
  4.     Copyright 1993 - 2000 Paul J. Sidorsky
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License, version 2, as
  8.     published by the Free Software Foundation.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19. This module contains miscellaneous technical functions and low-level file I/O
  20. functions.
  21. ******************************************************************************/
  22.  
  23. #include "top.h"
  24.  
  25. /* calc_days_ago() - Calculate number of days between a date and today.
  26.    Parameters:  lastdate - Pointer to date to be tested.
  27.    Returns:  Number of days since the given date.
  28.    Notes:  Normally used in the logon sequence to tell the user how long
  29.            since their last call.
  30. */
  31. XINT calc_days_ago(XDATE *lastdate)
  32. {
  33. struct date thisdate; /* Today's date. */
  34. long d1 = 0, d2 = 0; /* Days in first date, days in today's date. */
  35. long daysago = 0; /* Return value. */
  36. XINT d; /* Counter. */
  37.  
  38. getdate(&thisdate);
  39.  
  40. /* This function converts each date into the number of days elapsed since
  41.    what is technically 01/01/0001.  (In other words, 01/01/0001 is the
  42.    base date and gets a value of 0).  This provides a numeric basis by which
  43.    to compare the two dates. */
  44.  
  45. /* This function was written when I was new to C and was unfamiliar with
  46.    UNIX-style time (the time_t type).  Using time_t would no doubt be
  47.    much easier. */
  48.  
  49. /* Convert each year into days. */
  50. for (d = 1; d < lastdate->da_year; d++)
  51.     {
  52.     d1 += 365;
  53.     /* Adjust for leap year. */
  54.     if (d % 4 == 0)
  55.         {
  56.         d1++;
  57.         }
  58.     }
  59. /* Convert each month into days. */
  60. for (d = 1; d < lastdate->da_mon; d++)
  61.     {
  62.     switch(d)
  63.         {
  64.         case 1 : d1 += 31; break;
  65.         case 2 :
  66.             d1 += 28;
  67.             /* Adjust for current leap year. */
  68.             if (lastdate->da_year % 4 == 0)
  69.                 {
  70.                 d1++;
  71.                 }
  72.             break;
  73.         case 3 : d1 += 31; break;
  74.         case 4 : d1 += 30; break;
  75.         case 5 : d1 += 31; break;
  76.         case 6 : d1 += 30; break;
  77.         case 7 : d1 += 31; break;
  78.         case 8 : d1 += 31; break;
  79.         case 9 : d1 += 30; break;
  80.         case 10: d1 += 31; break;
  81.         case 11: d1 += 30; break;
  82.         }
  83.     }
  84. /* Count each day for the current month. */
  85. for (d = 1; d < lastdate->da_day; d++)
  86.     {
  87.     d1++;
  88.     }
  89.  
  90. /* The same counting procedure above is repeated for today's date. */
  91. for (d = 1; d < thisdate.da_year; d++)
  92.     {
  93.     d2 += 365;
  94.     if (d % 4 == 0)
  95.         {
  96.         d2++;
  97.         }
  98.     }
  99. for (d = 1; d < thisdate.da_mon; d++)
  100.     {
  101.     switch(d)
  102.         {
  103.         case 1 : d2 += 31; break;
  104.         case 2 :
  105.             d2 += 28;
  106.             if (thisdate.da_year % 4 == 0)
  107.                 {
  108.                 d2++;
  109.                 }
  110.             break;
  111.         case 3 : d2 += 31; break;
  112.         case 4 : d2 += 30; break;
  113.         case 5 : d2 += 31; break;
  114.         case 6 : d2 += 30; break;
  115.         case 7 : d2 += 31; break;
  116.         case 8 : d2 += 31; break;
  117.         case 9 : d2 += 30; break;
  118.         case 10: d2 += 31; break;
  119.         case 11: d2 += 30; break;
  120.         }
  121.     }
  122. for (d = 1; d < thisdate.da_day; d++)
  123.     {
  124.     d2++;
  125.     }
  126.  
  127. /* The difference is now a simple subtraction operation. */
  128. daysago = d2 - d1;
  129.  
  130. return daysago;
  131. }
  132.  
  133. /* This is the old name processor that was used up to and including TOP
  134.    v2.00g1.  The new one (used in v2.00 and up) appears later.  This old
  135.    processor also used an external scoring function called find_max_score()
  136.    which follows the main processor function below. */
  137. /*XINT find_node_from_name(char *newstring, char *handle, char *oldstring)
  138. {
  139. XINT x, y;
  140. XINT *count = NULL;
  141. XINT cc;
  142. XINT maxs, themax;
  143. char fntt[256]; // Dynam with MAXSTRLEN!
  144.  
  145. count = calloc(MAXNODES, 2);
  146. if (!count)
  147.     {
  148.     dispatch_message(MSG_GENERIC, top_output(OUT_STRINGNF,
  149.                                              getlang("OutOfMemory")),
  150.                      od_control.od_node, 0, 0);
  151.     od_log_write(top_output(OUT_STRING, getlang("LogOutOfMemory")));
  152.     return -1;
  153.     }
  154. memset(count, 0, MAXNODES * 2);
  155.  
  156. check_nodes_used(TRUE);
  157.  
  158. for (x = 0, cc = 1; x < strlen(oldstring) && x < 30 && cc > 0; x++)
  159.     {
  160.     for (y = 0, cc = 0; y < MAXNODES; y++)
  161.         {
  162.         filter_string(fntt, handles[y].string);
  163.         if (toupper(fntt[x]) == toupper(oldstring[x]) && count[y] == x
  164.             && activenodes[y])
  165.             {
  166.             count[y]++;
  167.             cc++;
  168.             }
  169.         }
  170.     }
  171.  
  172. maxs = find_max_score(count, &themax);
  173.  
  174. dofree(count);
  175.  
  176. if (maxs >= 0 && (forgetstatus[maxs] & FGT_HASFORGOTME))
  177.     {
  178.     lastsendtonode = maxs;
  179.     return -3;
  180.     }
  181. if (maxs >= 0)
  182.     {
  183.     if (activenodes[maxs] == 2)
  184.         {
  185.         lastsendtonode = maxs;
  186.         return -4;
  187.         }
  188.     }
  189.  
  190. if (maxs == -1)
  191.     {
  192.     fixname(handle, oldstring);
  193.     newstring[0] = '\0';
  194.     return -1;
  195.     }
  196.  
  197. if (maxs == -2)
  198.     {
  199.     fixname(handle, oldstring);
  200.     newstring[0] = '\0';
  201.     return -2;
  202.     }
  203.  
  204. strncpy(handle, oldstring, themax);
  205. if (themax >= strlen(oldstring))
  206.     {
  207.     newstring[0] = '\0';
  208.     }
  209. else
  210.     {
  211.     strcpy(newstring, &oldstring[themax]);
  212.     }
  213.  
  214. return maxs;
  215. }
  216.  
  217. XINT find_max_score(XINT *check, XINT *thescore)
  218. {
  219. XINT dd;
  220. XINT maxmax = 0, maxmaxset, highnode;
  221. char fmtt[256]; // Dynam with MAXSTRLEN!
  222.  
  223. for (dd = 0, maxmaxset = 0, highnode = -1; dd < MAXNODES; dd++)
  224.     {
  225.     filter_string(fmtt, handles[dd].string);
  226.     if (check[dd] > 0 && check[dd] == strlen(fmtt))
  227.         {
  228.         memcpy(thescore, &check[dd], 2);
  229.         return dd;
  230.         }
  231.  
  232.     if (check[dd] == maxmax && maxmaxset)
  233.         {
  234.         return -2;
  235.         }
  236.  
  237.     if (check[dd] > maxmax)
  238.         {
  239.         maxmaxset = 1;
  240.         maxmax = check[dd];
  241.         highnode = dd;
  242.         }
  243.     }
  244.  
  245. if (maxmax == 0)
  246.     {
  247.     return -1;
  248.     }
  249.  
  250. memcpy(thescore, &maxmax, 2);
  251.  
  252. return highnode;
  253. }*/
  254. /* End of old name processor. */
  255.  
  256. /* show_time() - Displays the current time and time remaining.
  257.    Parameters:  None.
  258.    Returns:  Nothing.
  259. */
  260. void show_time(void)
  261. {
  262. struct date dat; /* Current date. */
  263. struct time tim; /* Current time. */
  264. clock_t tmpc, mins; /* Time in TOP in ticks, time in TOP in minutes. */
  265.  
  266. /* In local and LAN modes, adjust the time limit in case it was deducted
  267.    before it was "frozen".  It looks a little better if it is consistent. */
  268. if (localmode || lanmode)
  269.     {
  270.     od_control.user_deducted_time = 0;
  271.     od_control.user_timelimit = 1440;
  272.     }
  273.  
  274. /* Initialize variables. */
  275. getdate(&dat); gettime(&tim);
  276. tmpc = myclock();
  277. mins = (tmpc / CLK_TCK) / 60L;
  278.  
  279. /* top_output() needs all numeric values to be strings. */
  280. itoa(dat.da_mon, outnum[0], 10);
  281. itoa(dat.da_day, outnum[1], 10);
  282. itoa(dat.da_year, outnum[2], 10);
  283. itoa(tim.ti_hour, outnum[3], 10);
  284. itoa(tim.ti_min, outnum[4], 10);
  285. itoa(tim.ti_sec, outnum[5], 10);
  286. /* Show the current time and date. */
  287. top_output(OUT_SCREEN, getlang("DateTime"), outnum[0], outnum[1], outnum[2],
  288.            outnum[3], outnum[4], outnum[5]);
  289. /* Show how long the user has been in TOP. */
  290. itoa(mins, outnum[0], 10);
  291. itoa(od_control.user_timelimit, outnum[1], 10);
  292. top_output(OUT_SCREEN, getlang("TimeIn"), outnum[0], outnum[1]);
  293. if (cfg.usecredits > 0)
  294.     {
  295.     /* Show number of credits left if the sysop is using RA credits. */
  296.     ltoa(od_control.user_credit, outnum[0], 10);
  297.     top_output(OUT_SCREEN, getlang("CreditDisplay"), outnum[0]);
  298.     }
  299.  
  300. return;
  301. }
  302.  
  303. /* quit_top() - Exits TOP normally, displaying a farewell to the user.
  304.    Parameters:  None.
  305.    Returns:  Nothing.
  306. */
  307. void quit_top(void)
  308.     {
  309.     XINT qtd, qpnum; /* Unused poker variables. */
  310. //|    poker_game_typ qgame;
  311.  
  312. /* Unused poker code that folds all hands this node is playing in. */
  313. /*//|    for (qtd = 0; qtd < cfg.pokermaxgames; qtd++)
  314.         {
  315.         if (pokeringame[qtd])
  316.             {
  317.             poker_lockgame(qtd);
  318.             poker_loadgame(qtd, &qgame);
  319.             if (poker_checkturn(POKSTG_ANYTURN, od_control.od_node, &qgame))
  320.                 {
  321.                 poker_foldplayer(od_control.od_node, qtd, &qgame);
  322.                 }
  323.  
  324.             qpnum = poker_getplayernum(POKPL_WAITING, od_control.od_node,
  325.                                        &qgame);
  326.             qgame.wanttoplay[qpnum] = -1;
  327.             if (--qgame.wtpcount < 1)
  328.                 {
  329.                 qgame.inuse = 0;
  330.                 }
  331.             poker_savegame(qtd, &qgame);
  332.             poker_unlockgame(qtd);
  333.             }
  334.         }*///|
  335.  
  336.     /* Show closing credits. */
  337.     quit_top_screen();
  338.  
  339.     /* Adjust RA credit value if needed. */
  340.     fixcredits();
  341.  
  342.     /* Exit normally. */
  343.     od_log_write(top_output(OUT_STRING, getlang("LogEnded"), ver));
  344.     od_exit(0, FALSE);
  345.  
  346.     }
  347.  
  348. /* before_exit() - Cleanup function before TOP is exited.
  349.    Parameters:  None.
  350.    Returns:  Nothing.
  351.    Notes:  Never called directly.  Called by the door kit during an
  352.            od_exit() function call.
  353. */
  354. void _USERENTRY before_exit(void)
  355. {
  356.  
  357. /* Show any final messages before we logout. */
  358. process_messages(TRUE);
  359.  
  360. /* Remove the node from TOP. */
  361. if (isregistered)
  362.     {
  363.     unregister_node(1);
  364.     dispatch_message(MSG_TOSSOUT, "\0", -1, 0, 0);
  365.     }
  366.  
  367. /* De-init. */
  368. closefiles();
  369. memfree();
  370.  
  371. return;
  372. }
  373.  
  374. /* errorabort() - Aborts TOP with an error code.
  375.    Parameters:  code - Code to abort with (ERR_ constant).
  376.                 info - Error information, usually a file name.
  377.    Returns:  Nothing.
  378.    Notes:  This function should be called whenever a critical TOP file
  379.            (such as USERS.TOP or NODEIDX.TCH) cannot be accessed.
  380. */
  381. void errorabort(XINT code, char *info)
  382. {
  383. char logent[256]; /* Log entry buffer. */
  384. XINT d; /* Not used. */
  385.  
  386. strcpy(outbuf, top_output(OUT_STRINGNF, getlang("FileErrorPrefix")));
  387.  
  388. /* Select proper message to show based on the error code. */
  389. switch(code)
  390.     {
  391.     case ERR_CANTOPEN:
  392.         strcat(outbuf, top_output(OUT_STRINGNF, getlang("CantOpen"),
  393.                info));
  394.         break;
  395.     case ERR_CANTREAD:
  396.         strcat(outbuf, top_output(OUT_STRINGNF, getlang("CantRead"),
  397.                info));
  398.         break;
  399.     case ERR_CANTWRITE:
  400.         strcat(outbuf, top_output(OUT_STRINGNF, getlang("CantWrite"),
  401.                info));
  402.         break;
  403.     case ERR_CANTACCESS:
  404.         strcat(outbuf, top_output(OUT_STRINGNF, getlang("CantAccess"),
  405.                info));
  406.         break;
  407.     case ERR_CANTCLOSE:
  408.         strcat(outbuf, top_output(OUT_STRINGNF, getlang("CantClose"),
  409.                info));
  410.         break;
  411.     }
  412.  
  413. /* Log the error code. */
  414. strcpy(logent, getlang("LogAlert"));
  415. strcat(logent, &outbuf[4]);
  416. filter_string(logent, logent);
  417. od_log_write(logent);
  418.  
  419. /* Inform the user we have to exit. */
  420. strcat(outbuf, getlang("FileErrorMsgPrefix"));
  421. strcat(outbuf, getlang("InformSysop"));
  422. strcat(outbuf, getlang("FileErrorMsgSuffix"));
  423. top_output(OUT_SCREEN, outbuf);
  424.  
  425. od_exit(220 + code, FALSE);
  426. }
  427.  
  428. /* closefile() - Closes a file.
  429.    Parameters:  thefil - Pointer to file stream to close.
  430.    Returns:  Error condition from fclose().
  431. */
  432. XINT closefile(FILE *thefil)
  433. {
  434.  
  435. /* This function is currently just a wrapper for fclose().  It is provided
  436.    in case some common pre-close procedure needs to be performed. */
  437.  
  438. return fclose(thefil);
  439. }
  440.  
  441. /* rec_locking() - Performs record-locking operations.
  442.    Parameters:  op - Operation to perform (REC_ constant).
  443.                 hand - File handle to perform locking on.
  444.                 ofs - Starting position of data to lock.
  445.                 len - Length of data to lock.
  446.    Returns:  TRUE if the operation failed, FALSE on success.
  447.    Notes:  This function is called for _ALL_ record locking done inside
  448.            TOP.  It provides try-counting and prevents an infinite loop
  449.            from occuring.  It also notifies the user of a problem.  It is
  450.            currently impossible for this function to return TRUE as it
  451.            will exit TOP if a problem occurs.
  452. */
  453. XINT rec_locking(char op, XINT hand, long ofs, long len)
  454. {
  455. /* Attempt counter, return code, flag if attempt limit exceeded,
  456.    keypress holder. */
  457. XINT d, failed, past = 0, key;
  458. clock_t cl; /* Current time (used for interval determination). */
  459.  
  460. /* Loop until the operation succeeds, counting each attempt. */
  461. for (d = 0, failed = 1; failed; d++)
  462.     {
  463.     /* If an attempt has just failed, wait the configured interval
  464.        before trying again. */
  465.     if (failed == -1)
  466.         {
  467.         for(cl = myclock();
  468.             ((float) myclock() - (float) cl) / (float) CLK_TCK <
  469.             (float) RETRYINTERVAL;);
  470.         }
  471.     /* Perform the record locking. */
  472.     switch(op)
  473.         {
  474.         case REC_LOCK: failed = lock(hand, ofs, len); break;
  475.         case REC_UNLOCK: failed = unlock(hand, ofs, len); break;
  476.         }
  477.     /* Once the configured number of tries has elapsed, inform the
  478.        user of a problem. */
  479.     if (d == MAXRETRIES)
  480.         {
  481.         top_output(OUT_SCREEN, getlang("AccessProblem"));
  482.         top_output(OUT_SCREEN, getlang("AccessPrompt"));
  483.         past = 1;
  484.         }
  485.     /* If we are passed the try limit, the user can abort back to the
  486.        BBS. */
  487.     if (past)
  488.         {
  489.         /* Check for a key without waiting. */
  490.         key = od_get_key(FALSE);
  491.         /* ESC aborts to the BBS. */
  492.         if (key == 27)
  493.             {
  494.             top_output(OUT_SCREEN, getlang("AccessShutDown"));
  495.             od_exit(250, FALSE);
  496.             }
  497.         }
  498.     }
  499.  
  500. return failed;
  501. }
  502.  
  503. /* sh_open() - Open a file handle in shared mode.
  504.    Parameters:  nam - File name to open.
  505.                 acc - Access flags.
  506.                 sh - Sharing flags.
  507.                 mod - Mode flags.
  508.    Returns:  File handle, or -1 on failure.
  509.    Notes:  This function is used for _ALL_ non-streamed file opening
  510.            in TOP.  It contains a loop to retry the configured number
  511.            of times.
  512. */
  513. XINT sh_open(char *nam, XINT acc, XINT sh, XINT mod)
  514. {
  515. XINT d, rethand = -1; /* Try counter, file handle. */
  516.  
  517. /* Loop until success or until the configured number of tries passes. */
  518. for (d = 0; d < MAXOPENRETRIES && rethand == -1; d++);
  519.     {
  520.     rethand = sopen(nam, acc, sh, mod);
  521.     }
  522.  
  523. return rethand;
  524. }
  525.  
  526. /* openfile() - Opens a file stream.
  527.    Parameters:  name - File name to open.
  528.                 mode - Mode string.
  529.                 errabort - Flag if TOP should abort on error.
  530.    Returns:  Pointer to a file stream for the new file, or NULL on failure.
  531.    Notes:  This is an older function from an early version of TOP that used
  532.            a streamed communication system.  It is still used for some BBS
  533.            interface files.  It contains code to retry and to abort on
  534.            failure.
  535. */
  536. FILE XFAR *openfile(char *name, char *mode, char errabort)
  537. {
  538. FILE XFAR *tmpfil = NULL; /* File handle holder. */
  539. XINT cl; /* Retry counter. */
  540. unsigned XINT sharebits; /* Sharing mode bits. */
  541.  
  542. strlwr(mode);
  543. /* TOP used to need a different share mode based on how the file was
  544.    opened. */
  545. /*if (strstr(mode, "r") && !strstr(mode, "+"))
  546.     {
  547.     sharebits = SH_DENYWR;
  548.     }
  549. else
  550.     {*/
  551.     sharebits = SH_DENYRW;
  552. /*    }*/
  553.  
  554. tmpfil = NULL;
  555. /* Loop until success or until the maximum number of tries elapses. */
  556. for (cl = 0; cl < MAXRETRIES && !tmpfil; cl++)
  557.     {
  558.     tmpfil = _fsopen(name, mode, sharebits);
  559.     }
  560.  
  561. if (!tmpfil && errabort && errno == EACCES)
  562.     {
  563.     /* Abort on error, if requested. */
  564.     top_output(OUT_SCREEN, getlang("SharingVioError"), name);
  565.     unregister_node(0);
  566.     od_log_write(top_output(OUT_STRING, getlang("LogSharingVioError")));
  567.     od_log_write(top_output(OUT_STRING, getlang("LogSharingVioFile"), name));
  568.     od_exit(251, FALSE);
  569.     }
  570.  
  571. return tmpfil;
  572. }
  573.  
  574. /* dofree() - Safely frees previously allocated memory.
  575.    Parameters:  pfree() - Pointer to memory to free.
  576.    Returns:  Nothing.
  577.    Notes:  Used to free _ALL_ memory in TOP safely.  TOP does not always
  578.            use all of its memory buffers so it is possible some will be
  579.            NULL when they are freed.  Borland C does not check for this
  580.            and it can cause problems if it occurs.  This function checks
  581.            for NULL before freeing. */
  582. void dofree(void XFAR *pfree)
  583. {
  584.  
  585. if (pfree != NULL)
  586.     {
  587.     /* Only free non-NULL buffers. */
  588.     free(pfree);
  589.     pfree = NULL;
  590.     }
  591.  
  592. return;
  593. }
  594.  
  595. /* closefiles() - Closes TOP files before exiting.
  596.    Parameters:  None.
  597.    Returns:  Nothing.
  598. */
  599. void closefiles(void)
  600. {
  601.  
  602. /* Only major TOP files are closed because Borland C will close all files
  603.    upon exit anyhow.  If the compiler is changed then all files will have
  604.    to be closed here. */
  605.  
  606. close(userfil); close(nidxfil); close(nidx2fil); close(msginfil);
  607. close(msgoutfil); close(useronfil); close(ipcinfil); close(ipcoutfil);
  608. close(midxinfil); close(midxoutfil); close(chgfil); close(chidxfil);
  609. close(bioidxfil); close(biorespfil);
  610.  
  611. return;
  612. }
  613.  
  614. /* memfree() - Frees TOP memory before exiting.
  615.    Parameters:  None.
  616.    Returns:  Nothing.
  617. */
  618. void memfree(void)
  619. {
  620. XINT mfd; /* Counter. */
  621.  
  622. /* Only major TOP memory is freed because Borland C will free all memory
  623.    upon exit anyhow.  If the compiler is changed then all memory will need
  624.    to be freed here. */
  625.  
  626. dofree(word_str); dofree(word_pos); dofree(word_len);
  627. dofree(handles); dofree(activenodes);
  628. dofree(outbuf); dofree(regname); dofree(regsystem); dofree(forgetstatus);
  629. dofree(busynodes); dofree(wordret); dofree(node); dofree(outputstr);
  630. dofree(nodecfg); dofree(lastcmd);
  631. dofree(chan); dofree(bioques);
  632.  
  633. /* Free language items. */
  634. for (mfd = 0; mfd < numlang; mfd++)
  635.        {
  636.     dofree(langptrs[mfd]);
  637.     }
  638. dofree(langptrs);
  639.  
  640. return;
  641. }
  642.  
  643. /* moreprompt() - Pauses between screens, prompting the user to continue.
  644.    Parameters:  None.
  645.    Returns:  Negative number if the user aborted, positive number if the
  646.              user requested nonstop mode, or 0 for normal continuation.
  647. */
  648. char moreprompt(void)
  649.     {
  650.     char nsmp = 0; /* Return code. */
  651.     XINT key; /* Keypress holder. */
  652.  
  653.     /* All more prompts default to Yes. */
  654.  
  655.     top_output(OUT_SCREEN, getlang("MorePrompt"));
  656.     key = od_get_answer(top_output(OUT_STRING, getlang("MoreKeys")));
  657.     top_output(OUT_SCREEN, getlang("MorePromptSuffix"));
  658.  
  659.     /* The keys are held in the MoreKeys language item. */
  660.  
  661.     /* Abort (no more). */
  662.     if (key == getlangchar("MoreKeys", 1))
  663.         {
  664.         nsmp = -1;
  665.         top_output(OUT_SCREEN, getlang("MoreAbortSuffix"));
  666.         }
  667.     /* Nonstop mode. */
  668.     if (key == getlangchar("MoreKeys", 2))
  669.         {
  670.         nsmp = 1;
  671.         }
  672.  
  673.     return nsmp;
  674.     }
  675.  
  676. /* get_extension() - Determines file extension based on terminal type.
  677.    Parameters:  ename - Base name of file to test.
  678.                 ebuf - Buffer to hold extension.
  679.    Returns:  Nothing.
  680.    Notes:  This function gets an extension for an external display file,
  681.            based on the user's terminal type.  Either ".ASC", ".ANS",
  682.            ".AVT", or ".RIP" is returned.  This function is used with
  683.            pictorial actions only, which can't be displayed by the door
  684.            kit.  All other external files are shown through the doorkit,
  685.            which determines the type of extension itself.  See TOP.DOC's
  686.            section on External Files for information.  This function needs
  687.            a valid base file name in the ename parameter to properly test
  688.            for nonexistent files.
  689. */
  690. void get_extension(char *ename, char *ebuf)
  691.     {
  692.     char ework[256]; /* Working file name buffer. */
  693.  
  694.     /* Test for RIP. */
  695.     strcpy(ework, ename);
  696.     if (od_control.user_rip)
  697.         {
  698.         strcat(ework, ".RIP");
  699.         if (!access(ework, 0))
  700.             {
  701.             strcpy(ebuf, ".RIP");
  702.             return;
  703.             }
  704.         }
  705.  
  706.     /* Test for AVATAR. */
  707.     strcpy(ework, ename);
  708.     if (od_control.user_avatar)
  709.         {
  710.         strcat(ework, ".AVT");
  711.         if (!access(ework, 0))
  712.             {
  713.             strcpy(ebuf, ".AVT");
  714.             return;
  715.             }
  716.         }
  717.  
  718.     /* Test for ANSI. */
  719.     strcpy(ework, ename);
  720.     if (od_control.user_ansi)
  721.         {
  722.         strcat(ework, ".ANS");
  723.         if (!access(ework, 0))
  724.             {
  725.             strcpy(ebuf, ".ANS");
  726.             return;
  727.             }
  728.         }
  729.  
  730.     /* Default to ASCII. */
  731.     strcpy(ebuf, ".ASC");
  732.  
  733.     }
  734.  
  735. /* myclock() - Midnight clock wrap compensator for DOS.
  736.    Parameters:  None.
  737.    Returns:  Current clock tick value from the clock() function.
  738.    Notes:  This function is a replacement for the clock() function.  It
  739.            corrects a bug which wraps the number of elapsed clock ticks
  740.            since program start if the program runs through midnight.  It
  741.            is not defined for TOP for OS/2 as this bug does not occur
  742.            under OS/2.  (TOP/2 can still operate properly if this
  743.            function is defined for it, but it would add overhead.)  It is
  744.            not clear whether the bug is in DOS or in the Borland runtime
  745.            library.
  746. */
  747. #ifndef __OS2__
  748. clock_t myclock(void)
  749.     {
  750.     clock_t retclock; /* Clock tick value to return. */
  751.  
  752.     /* Get the current clock tick value. */
  753.     retclock = clock();
  754.  
  755.     /* What happens is that after midnight, a day's worth of clock ticks
  756.        (1 573 040) is somehow subtracted from the clock tick value.  These
  757.        just have to be added back to restore the proper clock value. */
  758.     if (retclock < 0)
  759.         {
  760.         retclock += 1573040L;
  761.         }
  762.  
  763.     return retclock;
  764.     }
  765. #endif
  766.  
  767. /* fixcredits() - Adjusts number of RA credits upon exit.
  768.    Parameters:  None.
  769.    Returns:  Nothing.
  770.    Notes:  In some cases, RA seems to do its own credit subtracting upon
  771.            return from a door, so the original number of credits needs to be
  772.            restored before exiting or the credits would be subtracted twice.
  773.            This is done if the value of cfg.usecredits is positive.  If it is
  774.            negative, TOP will subtract the credits itself (i.e. the value
  775.            will not be restored).
  776. */
  777. void fixcredits(void)
  778.     {
  779.  
  780.     if (cfg.usecredits > 0)
  781.         {
  782.         od_control.user_credit = orignumcreds;
  783.         }
  784.  
  785.     }
  786.  
  787. /* find_node_from_name() - Determines a node number based on a name.
  788.    Parameters:  newstring - Buffer to hold the remainder of the string.
  789.                 handle - Buffer holding the full handle of the user.
  790.                 oldstring - String to test.
  791.    Returns:  A node number (>= 0) if a match was found, or a negative
  792.              number on error:  -1 if no match could be made, -2 if the
  793.              handle is not specific enough, -3 if a match was made but the
  794.              user was forgotten by the user who's name matches, or -4 if
  795.              a match was made but the user who matches is not on our
  796.              current channel.  (-3 and -4 are "recoverable" conditions.)
  797.              See notes.
  798.    Notes:  This function is the TOP "name processor", which attempts to find
  799.            a user currently in TOP based on a full or partial name input
  800.            (passed to it in oldstring).  If at least a partially matching
  801.            name is found, on return handle will contain the text in
  802.            oldstring that matched, and newstring will contain everything
  803.            after the part that was used to make the match (including any
  804.            leading spaces).  If no match can be found, newstring will be
  805.            blank and handle will contain the part of the string that failed
  806.            to match.  If -3 or -4 is returned (for reasons described in the
  807.            return code information above), handle and newstring will contain
  808.            the proper values, and the global variable lastsendtonode will
  809.            contain the node that matched. This allows the caller to either
  810.            report the condition to the user, or ignore the condition if it
  811.            is not relevant.  For example, BAN and INVITE commands must span
  812.            channels so it does not matter if a -4 is returned.  This
  813.            function works by comparing "scores" for each node.  The scoring
  814.            process is described inside the function body.
  815. */
  816. XINT find_node_from_name(char *newstring, char *handle, char *oldstring)
  817.     {
  818.     /* Node counter, length of current handle, length of oldstring. */
  819.     XINT x, lhan, lold;
  820.     XINT cursco = 0, maxs = 0; /* Current score, best score to date. */
  821.     XINT highnode = -1; /* Current node that has the high score. */
  822.     char tied = 0; /* Flag if there is a tie score. */
  823.  
  824.     /* All matching is case-insensitive! */
  825.  
  826.     lold = strlen(oldstring);
  827.  
  828.     check_nodes_used(TRUE);
  829.  
  830.     /* Test exact matches for each node. */
  831.     for (x = 0; x < MAXNODES; x++)
  832.         {
  833.         lhan = strlen(handles[x].string);
  834.         /* There can only be an exact match if oldstring is at least as
  835.            long as the entire handle. */
  836.         if (lhan >= lold)
  837.             {
  838.             /* An exact match means the start oldstring contains all of
  839.                the characters of the handle being tested.  It may contain
  840.                more characters after (for text or other purposes). */
  841.             if (activenodes[x] &&
  842.                 !strnicmp(oldstring, handles[x].string, lhan))
  843.                 {
  844.                 /* The longest exact match is saved. */
  845.                 if (lhan > maxs)
  846.                     {
  847.                     maxs = lhan;
  848.                     highnode = x;
  849.                     }
  850.                 }
  851.             }
  852.         }
  853.  
  854.     /* Only check for a partial match if there wasn't an exact match above.
  855.        This is done because if for example one user was named "Joe" and
  856.        another was named "Joey", it would be impossible to speak to "Joe"
  857.        because the score for both names would tie and return a -2
  858.        (not-specific enough).  This is one of the shortcomings of the
  859.        old name processor that appears earlier in this module. */
  860.     if (maxs == 0)
  861.         {
  862.         /* Test each node. */
  863.         for (x = 0; x < MAXNODES; x++)
  864.             {
  865.             cursco = 0;
  866.             lhan = strlen(handles[x].string);
  867.  
  868.             /* The "score" for a node is simply how many consecutive
  869.                characters match the start of oldstring.  For example, if
  870.                oldstring contained "al Hey there!" and there was a user
  871.                inside TOP named "Allan", the "score" for that node
  872.                would be 2. */
  873.             while (toupper(handles[x].string[cursco]) ==
  874.                    toupper(oldstring[cursco]) &&
  875.                    cursco < lhan && cursco < lold)
  876.                 {
  877.                 cursco++;
  878.                 }
  879.             /* Check for a tie. */
  880.             if (maxs > 0 && cursco == maxs)
  881.                 {
  882.                 tied = 1;
  883.                 }
  884.             if (cursco > maxs)
  885.                 {
  886.                 /* This is now the new "high score". */
  887.                 tied = 0;
  888.                 maxs = cursco;
  889.                 highnode = x;
  890.                 }
  891.             }
  892.  
  893.         /* To guard against inadvertant matching, there must be a
  894.            whitespace character after the matched part of the name, unless
  895.            there is a whitespace character at the end of the match part
  896.            (which usually means the user specified one full word of a
  897.            multi-word name).  For example, if the string contained "Jose"
  898.            and there was only a user named "Joe" that matched, the match
  899.            would be negated so messages for "Jose" would not accidentally
  900.            go to "Joe".  This most commonly occurs when there are two or
  901.            more similarly named users inside TOP and one of them leaves.
  902.            The old name processor, included earlier in this module, does
  903.            not fix this problem and is prone to such unwanted matches. */
  904.         if (maxs > 0)
  905.             {
  906.             if (!iswhite(oldstring[maxs - 1]) && !iswhite(oldstring[maxs]))
  907.                 {
  908.                 maxs = 0;
  909.                 tied = 0;
  910.                 }
  911.             }
  912.  
  913.         /* Tie situation. */
  914.         if (tied)
  915.             {
  916.             memset(handle, 0, 31);
  917.             strncpy(handle, oldstring, 30);
  918.             fixname(handle, handle);
  919.             newstring[0] = '\0';
  920.             return -2;
  921.             }
  922.  
  923.         /* No match was found. */
  924.         if (maxs == 0)
  925.             {
  926.             memset(handle, 0, 31);
  927.             strncpy(handle, oldstring, 30);
  928.             fixname(handle, handle);
  929.             newstring[0] = '\0';
  930.             return -1;
  931.             }
  932.         }
  933.  
  934.     /* As mentioned in the notes for this function, the cases below are
  935.        "recoverable" errors. */
  936.  
  937.     /* A match was found but the user who matches has forgotten this
  938.        user. */
  939.     if (forgetstatus[highnode] & FGT_HASFORGOTME)
  940.         {
  941.         lastsendtonode = highnode;
  942.         return -3;
  943.         }
  944.  
  945.     /* A match was found but the user who matches is not in our current
  946.        channel. */
  947.     if (activenodes[highnode] == 2)
  948.         {
  949.         lastsendtonode = highnode;
  950.         return -4;
  951.         }
  952.  
  953.     /* handle only contains the text used to make the match.  It is up to
  954.        the calling function to get the entire handle from the master handle
  955.        index (using the returned node number from this function) if it is
  956.        needed for display purposes. */
  957.     strncpy(handle, oldstring, maxs);
  958.  
  959.     if (maxs >= strlen(oldstring))
  960.         {
  961.         /* If oldstring only contained a name, newstring will contain
  962.            nothing. */
  963.         newstring[0] = '\0';
  964.         }
  965.     else
  966.         {
  967.         /* Compensate if the user specified a full word of a multi-word
  968.            handle.  TOP considers the space as part of the matched handle
  969.            and, without compensation, would not include it in newstring.
  970.            However, many routines expect the space at the front so the
  971.            compensation puts it back there. */
  972.         if (iswhite(handle[maxs - 1]))
  973.             {
  974.             maxs--;
  975.             }
  976.         /* Newstring contains everything after the text used for the
  977.            match. */
  978.         strcpy(newstring, &oldstring[maxs]);
  979.         }
  980.  
  981.     return highnode;
  982.     }
  983.  
  984. /* sh_unlink() - Share-aware file deletion function.
  985.    Parameters:  filname - Name of file to delete.
  986.    Returns:  FALSE on success, TRUE on error.
  987.    Notes:  This function is needed because on some systems, if a program
  988.            tries to delete a file while it is in use, a message will pop
  989.            up on the system which will require a human response before
  990.            the program can continue.  This is obviously undesirable in
  991.            an automatic BBS that may never even have a human on the
  992.            premises where the system is located.
  993. */
  994. XINT sh_unlink(char *filnam)
  995.     {
  996.  
  997.     /* Don't delete if we don't have full access to the file. */
  998.     if (access(filnam, 6))
  999.         {
  1000.         return -1;
  1001.         }
  1002.  
  1003.     return (unlink(filnam));
  1004.     }
  1005.  
  1006.