home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 September / PCO_0998.ISO / filesbbs / dos / sbbs_src.exe / SBBS / AUTONODE / AUTONODE.C
Encoding:
C/C++ Source or Header  |  1997-04-13  |  6.4 KB  |  194 lines

  1. /* AUTONODE.C */
  2.  
  3. /* Developed 1990-1997 by Rob Swindell; PO Box 501, Yorba Linda, CA 92885 */
  4.  
  5. #include <io.h>
  6. #include <dir.h>
  7. #include <fcntl.h>
  8. #include <errno.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <process.h>
  13. #include <sys\stat.h>
  14.  
  15. #define uchar unsigned char
  16. #define uint unsigned int
  17. #define ulong unsigned long
  18. #define LOOP_NODEDAB 100    /* Retries on NODE.DAB locking/unlocking    */
  19.  
  20. typedef struct {                        /* Node information kept in NODE.DAB */
  21.     uchar    status,                        /* Current Status of Node */
  22.             errors,                        /* Number of Critical Errors */
  23.             action;                        /* Action User is doing on Node */
  24.     uint    useron,                        /* User on Node */
  25.             connection,                    /* Connection rate of Node */
  26.             misc,                        /* Miscellaneous bits for node */
  27.             aux;                        /* Auxillary word for node */
  28.     ulong    extaux;                        /* Extended aux dword for node */
  29.             } node_t;
  30.  
  31. enum {                                /* Node Status */
  32.      NODE_WFC                        /* Waiting for Call */
  33.     ,NODE_LOGON                      /* at logon prompt */
  34.     ,NODE_NEWUSER                     /* New user applying */
  35.     ,NODE_INUSE                         /* In Use */
  36.     ,NODE_QUIET                         /* In Use - quiet mode */
  37.     ,NODE_OFFLINE                     /* Offline */
  38.     ,NODE_NETTING                     /* Networking */
  39.     ,NODE_EVENT_WAITING                /* Waiting for all nodes to be inactive */
  40.     ,NODE_EVENT_RUNNING                /* Running an external event */
  41.     ,NODE_EVENT_LIMBO                /* Allowing another node to run an event */
  42.     };
  43.  
  44. int nodefile;
  45.  
  46. void main();
  47. void getnodedat(uchar number, node_t *node, char lockit);
  48. void putnodedat(uchar number, node_t node);
  49. void truncsp(char *str);
  50.  
  51. void main(int argc,char *argv[])
  52. {
  53.     FILE *fp;
  54.     char str[256],nodepath[256],*p
  55.         ,sbbs_ctrl[256],sbbs_node[256],path[MAXPATH]
  56.         ,*arg[10]={NULL};
  57.     int file,num_nodes,autonode,disk,x,y;
  58.     node_t node;
  59.  
  60. printf("\nSynchronet AUTONODE v2.00\n");
  61.  
  62.     if(!strcmp(argv[1],"/?")) {
  63.         printf("\nUsage: AUTONODE [file] [args,...]");
  64.         printf("\n\nWhere [file]     is the name of the file to run and");
  65.         printf("\n      [args,...] are the command line arguments to use");
  66.         printf("\n\nNOTE: The default command line is 'SBBS l q'\n");
  67.         return; }
  68.     p=getenv("SBBSCTRL");
  69.     if(!p) {
  70.         printf("\n\7You must set the SBBSCTRL environment variable first.");
  71.         printf("\n\nExample: SET SBBSCTRL=C:\\SBBS\\CTRL\n");
  72.         return; }
  73.     sprintf(sbbs_ctrl,"%.40s",p);
  74.     strupr(sbbs_ctrl);
  75.     if(sbbs_ctrl[strlen(sbbs_ctrl)-1]!='\\')
  76.         strcat(sbbs_ctrl,"\\");
  77.     p=getenv("SBBSNODE");
  78.     if(!p) {
  79.         printf("\n\7You must set the SBBSNODE environment variable first.");
  80.         printf("\n\nExample: SET SBBSNODE=C:\\SBBS\\NODE1\n");
  81.         return; }
  82.     sprintf(sbbs_node,"%.40s",p);
  83.     strupr(sbbs_node);
  84.     if(sbbs_node[strlen(sbbs_node)-1]!='\\')
  85.         strcat(sbbs_node,"\\");
  86.  
  87.     sprintf(str,"%sNODE.DAB",sbbs_ctrl);
  88.     if((nodefile=open(str,O_RDWR|O_BINARY|O_DENYNONE))==-1) {
  89.         printf("Error opening %s",str); exit(1); }
  90.  
  91.     sprintf(str,"%sMAIN.CNF",sbbs_ctrl);
  92.     if((file=open(str,O_RDONLY|O_DENYNONE|O_BINARY))==-1) {
  93.         printf("Error opening %s",str);
  94.         exit(1); }
  95.     lseek(file,227L,SEEK_SET);
  96.     read(file,&num_nodes,2);
  97.     printf("\nNumber of Available Nodes = %d",num_nodes);
  98.     lseek(file,64L*(long)num_nodes,SEEK_CUR);
  99.     lseek(file,328L,SEEK_CUR);
  100.     read(file,&autonode,2);
  101.     printf("\nNumber of First Autonode  = %d",autonode);
  102.     for(x=autonode;x<=num_nodes;x++) {
  103.         printf("\nChecking Node #%d",x);
  104.         getnodedat(x,&node,1);
  105.         if(node.status==NODE_OFFLINE) {
  106.             printf("\nFOUND! Node #%d is OFFLINE\n",x);
  107.             node.status=NODE_WFC;
  108.             putnodedat(x,node);
  109.             lseek(file,(229L+((long)(x-1)*64L)),SEEK_SET);
  110.             read(file,nodepath,128);
  111.             truncsp(nodepath);
  112.             if(nodepath[strlen(nodepath)-1]=='\\')
  113.                 nodepath[strlen(nodepath)-1]=0;     /* remove '\' */
  114.             if(nodepath[0]=='.')
  115.                 sprintf(path,"%s%s",sbbs_node,nodepath);
  116.             else strcpy(path,nodepath);
  117.             if(path[1]==':')
  118.                 setdisk(path[0]-'A');
  119.             if(chdir(path)) {
  120.                 printf("\nError changing into '%s'",path);
  121.                 getnodedat(x,&node,1);
  122.                 node.status=NODE_OFFLINE;
  123.                 putnodedat(x,node);
  124.                 exit(1); }
  125.             if(argc==1) {
  126.                 execl(getenv("COMSPEC"),getenv("COMSPEC"),"/c","SBBS","l","q",
  127.                     NULL); }
  128.             else {
  129.                 arg[0]=argv[0];
  130.                 strcpy(str,"/c"); arg[1]=str;
  131.                 for(x=1;x<argc;x++) arg[1+x]=argv[x];
  132.                 execv(getenv("COMSPEC"),arg); }
  133.             getnodedat(x,&node,1);
  134.             node.status=NODE_OFFLINE;
  135.             putnodedat(x,node);
  136.             return; } }
  137.     printf("\n\n\7All local nodes are in use!\n");
  138. }
  139.  
  140. /****************************************************************************/
  141. /* Reads the data for node number 'number' into the structure 'node'        */
  142. /* from NODE.DAB                                                            */
  143. /* if lockit is non-zero, locks this node's record. putnodedat() unlocks it */
  144. /****************************************************************************/
  145. void getnodedat(uchar number, node_t *node, char lockit)
  146. {
  147.     char str[256];
  148.     int count=0;
  149.  
  150. number--;    /* make zero based */
  151. while(count<LOOP_NODEDAB) {
  152.     lseek(nodefile,(long)number*sizeof(node_t),SEEK_SET);
  153.     if(lockit
  154.         && lock(nodefile,(long)number*sizeof(node_t),sizeof(node_t))==-1) {
  155.         count++;
  156.         continue; }
  157.     if(read(nodefile,node,sizeof(node_t))==sizeof(node_t))
  158.         break;
  159.     count++; }
  160. if(count==LOOP_NODEDAB)
  161.     printf("\7Error unlocking and reading NODE.DAB\n");
  162. }
  163.  
  164. /****************************************************************************/
  165. /* Write the data from the structure 'node' into NODE.DAB                      */
  166. /* getnodedat(num,&node,1); must have been called before calling this func  */
  167. /*          NOTE: ------^   the indicates the node record has been locked   */
  168. /****************************************************************************/
  169. void putnodedat(uchar number, node_t node)
  170. {
  171.     char str[256];
  172.     int count;
  173.  
  174. number--;    /* make zero based */
  175. lseek(nodefile,(long)number*sizeof(node_t),SEEK_SET);
  176. if(write(nodefile,&node,sizeof(node_t))!=sizeof(node_t)) {
  177.     unlock(nodefile,(long)number*sizeof(node_t),sizeof(node_t));
  178.     printf("\7Error writing NODE.DAB for node %u\n",number+1);
  179.     return; }
  180. unlock(nodefile,(long)number*sizeof(node_t),sizeof(node_t));
  181. }
  182. /****************************************************************************/
  183. /* Truncates white-space chars off end of 'str' and terminates at first tab */
  184. /****************************************************************************/
  185. void truncsp(char *str)
  186. {
  187.     char c;
  188.  
  189. str[strcspn(str,"\t")]=0;
  190. c=strlen(str);
  191. while(c && str[c-1]<=32) c--;
  192. str[c]=0;
  193. }
  194.