home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 September / PCO_0998.ISO / filesbbs / dos / sbbs_src.exe / SBBS / UTI / UTI.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-13  |  7.8 KB  |  299 lines

  1. #line 1 "UTI.C"
  2.  
  3. /* Developed 1990-1997 by Rob Swindell; PO Box 501, Yorba Linda, CA 92885 */
  4.  
  5. /* Shared routines for most of the UTI driver programs */
  6.  
  7. #include "sbbs.h"
  8. #include "uti.h"
  9.  
  10. #define bputs    lputs
  11. #define bprintf lprintf
  12.  
  13. int logfile;
  14. char scrnbuf[4000],tmp[256];
  15. struct text_info txtinfo;
  16.  
  17. /****************************************************************************/
  18. /* Performs printf() through local assembly routines                        */
  19. /* Called from everywhere                                                   */
  20. /****************************************************************************/
  21. int lprintf(char *fmat, ...) {
  22.     char sbuf[256];
  23.     int chcount;
  24.  
  25. chcount=vsprintf(sbuf,fmat,_va_ptr);
  26. lputs(sbuf);
  27. return(chcount);
  28. }
  29.  
  30. long lputs(char far *str)
  31. {
  32.     char tmp[256];
  33.     int i,j,k;
  34.  
  35. j=strlen(str);
  36. for(i=k=0;i<j;i++)        /* remove CRs */
  37.     if(str[i]==CR && str[i+1]==LF)
  38.         continue;
  39.     else
  40.         tmp[k++]=str[i];
  41. tmp[k]=0;
  42. return(fputs(tmp,stdout));
  43. }
  44.  
  45. /****************************************************************************/
  46. /* Truncates white-space chars off end of 'str' and terminates at first tab */
  47. /****************************************************************************/
  48. void truncsp(char *str)
  49. {
  50.     char c;
  51.  
  52. str[strcspn(str,"\t")]=0;
  53. c=strlen(str);
  54. while(c && str[c-1]<=SP) c--;
  55. str[c]=0;
  56. }
  57.  
  58. /****************************************************************************/
  59. /* Puts a backslash on path strings                                         */
  60. /****************************************************************************/
  61. void backslash(char *str)
  62. {
  63.     int i;
  64.  
  65. i=strlen(str);
  66. if(i && str[i-1]!='\\') {
  67.     str[i]='\\'; str[i+1]=0; }
  68. }
  69.  
  70.  
  71. /****************************************************************************/
  72. /* Converts an ASCII Hex string into an ulong                       */
  73. /****************************************************************************/
  74. ulong ahtoul(char *str)
  75. {
  76.     ulong l,val=0;
  77.  
  78. while((l=(*str++)|0x20)!=0x20)
  79.     val=(l&0xf)+(l>>6&1)*9+val*16;
  80. return(val);
  81. }
  82.  
  83. /****************************************************************************/
  84. /* Network open function. Opens all files DENYALL and retries LOOP_NOPEN    */
  85. /* number of times if the attempted file is already open or denying access  */
  86. /* for some other reason.    All files are opened in BINARY mode.            */
  87. /****************************************************************************/
  88. int nopen(char *str, int access)
  89. {
  90.     char logstr[256];
  91.     int file,share,count=0;
  92.  
  93. if(access==O_RDONLY) share=O_DENYWRITE;
  94.     else share=O_DENYALL;
  95. while(((file=open(str,O_BINARY|share|access,S_IWRITE))==-1)
  96.     && errno==EACCES && count++<LOOP_NOPEN);
  97. if(count>(LOOP_NOPEN/2))
  98.     lprintf("NOPEN COLLISION - File: %s Count: %d"
  99.         ,str,count);
  100. if(file==-1 && errno==EACCES)
  101.     lputs("\7\r\nNOPEN: ACCESS DENIED\r\n\7");
  102. return(file);
  103. }
  104.  
  105. /****************************************************************************/
  106. /* This function performs an nopen, but returns a file stream with a buffer */
  107. /* allocated.                                                                */
  108. /****************************************************************************/
  109. FILE *fnopen(int *file, char *str, int access)
  110. {
  111.     char mode[128];
  112.     FILE *stream;
  113.  
  114. if(((*file)=nopen(str,access))==-1)
  115.     return(NULL);
  116.  
  117. if(access&O_APPEND) {
  118.     if(access&O_RDONLY)
  119.         strcpy(mode,"a+");
  120.     else
  121.         strcpy(mode,"a"); }
  122. else {
  123.     if(access&O_WRONLY)
  124.         strcpy(mode,"r+");
  125.     else
  126.         strcpy(mode,"r"); }
  127. stream=fdopen((*file),mode);
  128. if(stream==NULL) {
  129.     close(*file);
  130.     return(NULL); }
  131. setvbuf(stream,NULL,_IOFBF,16*1024);
  132. return(stream);
  133. }
  134.  
  135.  
  136. /****************************************************************************/
  137. /* Returns the length of the file in 'filespec'                             */
  138. /****************************************************************************/
  139. long flength(char *filespec)
  140. {
  141.     struct ffblk f;
  142.  
  143. if(findfirst(filespec,&f,0)==NULL)
  144.     return(f.ff_fsize);
  145. return(-1L);
  146. }
  147.  
  148.  
  149. /****************************************************************************/
  150. /* Error handling routine. Prints to local and remote screens the error     */
  151. /* information, function, action, object and access and then attempts to    */
  152. /* write the error information into the file ERROR.LOG and NODE.LOG         */
  153. /****************************************************************************/
  154. void errormsg(int line, char *source, char action, char *object, ulong access)
  155. {
  156.     char str[512];
  157.     char actstr[256];
  158.  
  159. switch(action) {
  160.     case ERR_OPEN:
  161.         strcpy(actstr,"opening");
  162.         break;
  163.     case ERR_CLOSE:
  164.         strcpy(actstr,"closeing");
  165.         break;
  166.     case ERR_FDOPEN:
  167.         strcpy(actstr,"fdopen");
  168.         break;
  169.     case ERR_READ:
  170.         strcpy(actstr,"reading");
  171.         break;
  172.     case ERR_WRITE:
  173.         strcpy(actstr,"writing");
  174.         break;
  175.     case ERR_REMOVE:
  176.         strcpy(actstr,"removing");
  177.         break;
  178.     case ERR_ALLOC:
  179.         strcpy(actstr,"allocating memory");
  180.         break;
  181.     case ERR_CHK:
  182.         strcpy(actstr,"checking");
  183.         break;
  184.     case ERR_LEN:
  185.         strcpy(actstr,"checking length");
  186.         break;
  187.     case ERR_EXEC:
  188.         strcpy(actstr,"executing");
  189.         break;
  190.     default:
  191.         strcpy(actstr,"UNKNOWN"); }
  192. lprintf("\7\r\nERROR -     file: %s",source);
  193. lprintf("\7\r\n            line: %u",line);
  194. lprintf("\7\r\n          action: %s",actstr);   /* tell user about error */
  195. lprintf("\7\r\n          object: %s",object);
  196. lprintf("\7\r\n          access: %lu (%lxh)",access,access);
  197. lputs("\r\n\r\n<Hit any key>");
  198. getch();
  199. lputs("\r\n");
  200. }
  201.  
  202.  
  203. void allocfail(uint size)
  204. {
  205. lprintf("\7Error allocating %u bytes of memory.\r\n",size);
  206. exit(1);
  207. }
  208.  
  209. void bail(int code)
  210. {
  211.     char str[256];
  212.     time_t t;
  213.     struct time curtime;
  214.     struct date date;
  215.  
  216. if(!code) {
  217.     puttext(1,1,80,25,scrnbuf); /* restore screen if no error */
  218.     textattr(txtinfo.attribute);
  219.     gotoxy(txtinfo.curx,txtinfo.cury); }
  220. t=time(NULL);
  221. unixtodos(t,&date,&curtime);
  222. sprintf(str,"%02u/%02u/%02u %02u:%02u:%02u    Exiting (%d)\r\n\r\n"
  223.     ,date.da_mon,date.da_day,date.da_year-1900
  224.     ,curtime.ti_hour,curtime.ti_min,curtime.ti_sec
  225.     ,code);
  226. write(logfile,str,strlen(str));
  227. exit(code);
  228. }
  229.  
  230. int getsubnum(char *code)
  231. {
  232.     int i;
  233.  
  234. for(i=0;i<total_subs;i++) {
  235. //      printf("%s vs %s\n",code,sub[i]->code);
  236.     if(!stricmp(code,sub[i]->code))
  237.         return(i); }
  238. return(-1);
  239. }
  240.  
  241. void uti_init(char *name, int argc, char **argv)
  242. {
  243.     char str[256],*p;
  244.     int i;
  245.     read_cfg_text_t txt;
  246.     time_t t;
  247.     struct tm *tm;
  248.  
  249. setvbuf(stdout,NULL,_IONBF,0);
  250. putenv("TZ=UTC0");
  251.  
  252. txt.openerr="\7\r\nError opening %s for read.\r\n";
  253. txt.reading="\r\nReading %s...";
  254. txt.readit="\rRead %s       ";
  255. txt.allocerr="\7\r\nError allocating %u bytes of memory\r\n";
  256. txt.error="\7\r\nERROR: Offset %lu in %s\r\n\r\n";
  257.  
  258. p=getenv("SBBSNODE");
  259. if(p==NULL) {
  260.     printf("\7\nSBBSNODE environment variable not set.\n");
  261.     exit(1); }
  262. strcpy(node_dir,p);
  263.  
  264. strupr(node_dir);
  265.  
  266. if(node_dir[strlen(node_dir)-1]!='\\')
  267.     strcat(node_dir,"\\");
  268.  
  269. read_node_cfg(txt);
  270. if(ctrl_dir[0]=='.') {   /* Relative path */
  271.     strcpy(str,ctrl_dir);
  272.     sprintf(ctrl_dir,"%s%s",node_dir,str); }
  273. read_main_cfg(txt);
  274. if(data_dir[0]=='.') {   /* Relative path */
  275.     strcpy(str,data_dir);
  276.     sprintf(data_dir,"%s%s",node_dir,str); }
  277. read_msgs_cfg(txt);
  278. sprintf(str,"%sUTI.LOG",data_dir);
  279. if((logfile=nopen(str,O_WRONLY|O_CREAT|O_APPEND))==-1) {
  280.     printf("\7\nCan't open %s\n",str);
  281.     exit(-1); }
  282. t=time(NULL);
  283. tm=gmtime(&t);
  284. sprintf(str,"%02u/%02u/%02u %02u:%02u:%02u    %-8s %s \""
  285.     ,tm->tm_mon+1,tm->tm_mday,tm->tm_year
  286.     ,tm->tm_hour,tm->tm_min,tm->tm_sec
  287.     ,name,VER);
  288. printf("\n\n");
  289. for(i=1;i<argc;i++) {
  290.     if(i>1)
  291.         strcat(str," ");
  292.     strcat(str,argv[i]);
  293.     printf("%s ",argv[i]); }
  294. strcat(str,"\"");
  295. write(logfile,str,strlen(str));
  296. write(logfile,"\r\n",2);
  297. printf("\n\nWorking...");
  298. }
  299.