home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / c / crcbbs.arc / CRCPC0A.C < prev    next >
Text File  |  1985-10-22  |  13KB  |  513 lines

  1. #include "crcpchdr.h"
  2.  
  3. char *incptr(ptr)   /* Increment pointer only if not pointing at NULL char */
  4. char *ptr;
  5. {
  6. return ((*ptr)?stpblk(++ptr):ptr);
  7. }
  8.  
  9. int notnull(str)
  10. char *str;
  11. {
  12. for (;*str;str++) if (*str>' ') return 1;
  13. return 0;    /* All spaces or control codes in [ASCII] string */
  14. }
  15.  
  16. static char get_cmd(args)
  17. char *args;
  18. {
  19. struct cmd_list *search;
  20. char word[20];
  21.  
  22. stptok(args,word,sizeof(word)," ");
  23. uppercase(word);
  24. for (search=commands;search!=NULL;search=search->next) {
  25.     if (abbrev(search->name,word,search->abrl)) {
  26.         if (search->class<=userclass) return(search->cmd_id);
  27.         }
  28.     }
  29. return('\0');    /* didnt find it */
  30. }
  31.  
  32.  
  33. init_vars()
  34. {
  35. tflag = deftflag;
  36. termtype = deftermtype;
  37. utwidth = defutwidth;
  38. utlength = defutlength;
  39. utcls[0] = '\0';
  40. maxconnect = defmaxconnect;    /* Allow user on at max this many min.s */
  41. nousrto = FALSE;        /* Also initialized by 'read_userfile()'*/
  42. nousrfort = FALSE;        /*  "        "    "    "    */
  43. noforcenow = TRUE;        /* */
  44. beingforced = FALSE;        /* But not doing it yet */
  45. beginner = defbeginner;
  46. termwidth = deftermwidth;
  47. duplex = defduplex;
  48. }
  49.  
  50. char menu(retopts,retquiet)
  51. char **retopts;
  52. int *retquiet;
  53. {
  54. char c,d;
  55. char filename[15];
  56. char destfile[15];
  57. char *type;
  58. char *ftype;
  59. char *delete;
  60. int gnl_flag, quiet;
  61. static char inpbuff[LINESIZE+2];
  62. static char options[20];
  63. char *args, *getenv();
  64.  
  65. if (!beginner) goto skipcrap;
  66. drawit:
  67. cls();
  68. output = TRUE;
  69. printf("\r\n%s %s - User: %s - %s %s\r\n\r\n",SYSNAME,SYSVER,user_id,date(),time());
  70. help(MENUFILE,"t");
  71. cr();
  72.  
  73. skipcrap:
  74. gnl_flag = FALSE;
  75. forever {
  76.     output = TRUE ;
  77.     *retquiet = FALSE;
  78.     *retopts = "";
  79.     if (beingforced) {
  80.         beingforced = FALSE;
  81.         return('Y');
  82.         }
  83.     prompt();
  84.     args = disp_reply("",inpbuff,LINESIZE);
  85.     if (!dsr()) return('Q');
  86.     uppercase(args);        /* UPPERCASE ALL ARGUMENTS */
  87.     c = get_cmd(args);
  88.     args = strip_options(skptok(args),options);
  89.     quiet = find_option(options,'q');
  90.     *retquiet = quiet;        /* Pass back to caller if 'Q' cmmd */
  91.     *retopts = options;        /* " */
  92.     switch(c) { /* SWITCH */
  93.         case '?': goto drawit;
  94.     break;    case '~': dossys(getenv(UTIL_PROG),FALSE);
  95.     break;    case '+': dossys(skptok(inpbuff),TRUE);
  96.     break;    case '=': dossys(skptok(inpbuff),FALSE);
  97.     break;    case '_': runfile(skptok(inpbuff),TRUE);
  98.     break;    case '-': runfile(skptok(inpbuff),FALSE);
  99.     break;    case '/': user_set(c,args,options);
  100.     break;    case '#': mk_dir(c,args,options);
  101.     break;    case '%': rm_dir(c,args,options);
  102.     break;    case '@': xfer_file(c,args,options);
  103.     break;    case '$': any_news(c,args,options);
  104.     break;    case '!': see_dtables(c,args,options);
  105.     break;    case '*': do_fort(c,args,options);
  106.     break;    case '"': dup_file(c,args,options);
  107.     break;    case 'A': ask_sysop(c,args,options);
  108.     break;    case 'B': buzz_sysop(c,args,options);
  109.     break;    case 'C': create_file(c,args,options);
  110.     break;    case 'D': disp_dir(c,args,options);
  111.     break;    case 'E': erase_file(c,args,options);
  112.     break;    case 'F': fork_board(c,args,options);
  113.     break;    case 'G': go_dir(c,args,options);
  114.     break;    case 'H': yell_help(c,args,options);
  115.     break;    case 'I': help(SYSINFO,options);
  116.     break;    case 'J': jump_dir(c,args,options);
  117.     break;    case 'K': kill_board(c,args,options);
  118.     break;    case 'L': list_file(c,args,options);
  119.     break;    case 'M': msg_sys(c,args,options);
  120.     break;    case 'N': change_password(c,args,options);
  121.     break;    case 'O': other_systems(c,args,options);
  122.     break;    case 'P': mail(c,args,options);
  123.     break;    case 'Q':
  124.             if (find_option(options,'!')) c = 'Y';
  125.             else if (!(c=args[0])) {
  126.                 if (!quiet && beginner)
  127.                     printf("You are about to leave this system.\r\n");
  128.                 c = *disp_reply("Are you sure ? ",filename,4);
  129.                 }
  130.             c = toupper(c);
  131.             if (stpchr("YQX",c)==NULL) {
  132.                 nothing_done();
  133.                 break;
  134.                 }
  135.             if (c == 'Y') return c;
  136.             else if (c == 'Q') return c;
  137.             else if (c == 'X') {
  138.                 if (!strcmp(input_pw(9),exit_pw)) return c;
  139.                 }
  140.     break;    case 'R': rename_file(c,args,options);
  141.     break;    case 'S': smfile(c,args,options);
  142.     break;    case 'T': type_file(c,args,options);
  143.     break;    case 'U': user_data(c,args,options);
  144.     break;    case 'V': scan_events(c,args,options);
  145.     break;    case 'W': who_is(c,args,options);
  146.     break;    case 'X': beginner = !beginner;    expertstatus();
  147.     break;    case 'Y': systat(c,args,options);
  148.     break;    case 'Z': ask_sysop(c,args,"qf");
  149.     break;    default:  mputs("Type '?' for menu, type 'H' for help\r\n");
  150.         } /* END SWITCH */
  151.     sysdir(cur_dir,cur_root);    /* Make sure on current directory */
  152.     } /* FOR EVER */
  153. }
  154.  /* END MENU */
  155.  
  156. yell_help(c,args,opts)
  157. char c;
  158. char *args;
  159. char *opts;
  160. {
  161. int rc;
  162. strcat(opts,"u");        /* UPPERCASE; do not respect case for compare */
  163. rc = helplib(args,opts);
  164. if (rc==0) {
  165.     if (!*args) printf("Enter \"HELP name\" using name from above list\r\n");
  166.     }
  167. else if (rc==-1) printf("Nothing found.\r\n");
  168. else if (rc==1000) printf("* abort *\r\n");
  169. else printf("Help error: %d.\r\n",rc);
  170. }
  171.  
  172. char *prompt_str(level)
  173. int level;
  174. {
  175. if (level) return("Enter command or ? for menu");
  176.         else return(cur_dir);
  177. }
  178.  
  179. /* NOTE: If DEFLT true, then NULL command defaults to loading DOS */
  180. static int dossys(str,deflt)
  181. char *str;
  182. int deflt;
  183. {
  184. char *getenv();
  185. int rc;
  186. char doscmd[LINESIZE];
  187.  
  188. if (str==NULL) return(nothing_done());
  189. if (!*(str=stpblk(str))) {
  190.     if (!deflt) return(nothing_done());
  191.     if ((str=getenv(COMSPEC))==NULL) str = DEFLT_SYSCMD;
  192.     disp_msg(50);        /* Print default CMD instructions */
  193.     }
  194. stccpy(doscmd,str,sizeof(doscmd));  /* In case DOS plays with this */
  195. rc = system(doscmd);
  196. if (rc) printf("R(%d);\n",rc);
  197. return(rc);
  198. }
  199.  
  200. static int runfile(str,deflt)
  201. char *str;
  202. {
  203. char *offense;
  204.  
  205. if (*str && (offense=stpbrk(str,INVDOSCMDCHR))!=NULL) 
  206.     return(bad_fn(*offense));
  207. return(dossys(str,deflt));
  208. }
  209.  
  210. /* CHAT is called from bottom level routines MGETS and MPUTS */
  211. chat()
  212. {
  213. char line_buff[STR_SIZE+2];
  214.  
  215. cls();
  216. printf("\r\n\nChat mode - Control-Z gets out\r\n\n");
  217. line_buff[0]=STR_SIZE+1;
  218. do {
  219.     cgets(line_buff);
  220.     printf("\n");
  221.     } while(stpchr(&line_buff[2],CTLZ)==NULL);
  222. printf("\r\n\nReturning to previous task: \r\n\n");
  223. }
  224.  
  225.  
  226. eraselog() { if (!sysdir("",MAILBOX)) unlink(EVENT_LOG); }
  227.  
  228.  
  229. openlog()
  230. {
  231. if (nolog) event_fp=NULL;
  232. else {
  233.     if (sysdir("",MAILBOX)) event_fp=NULL;
  234.     else event_fp=fopen(EVENT_LOG,"a");
  235.     if (event_fp==NULL) printf("NOTE: Event log could not be opened.\r\n");
  236.     }
  237. }
  238.  
  239.  
  240. closelog() 
  241. if (event_fp!=NULL) {
  242.     sysdir("",MAILBOX);        /* DO WE NEED TO DO THIS ? */
  243.     fclose(event_fp);
  244.     event_fp = NULL;
  245.     }
  246. }
  247.  
  248.  
  249. logevent(event) 
  250. char event[]; 
  251. char timestr[32];
  252. strcpy(timestr,timedate());
  253. strcat(timestr," ");
  254. if (event_fp==NULL) return;
  255. writeln2(event_fp,timestr,event); 
  256. }
  257.  
  258.  
  259. /* These routines should later be removed to lower levels, such as -MD1 or MD2.
  260.    (MAYBE EVEN -BOT) 
  261. */
  262.  
  263. char *timedate()
  264. {
  265. static char timebuff[20];
  266. int hh,mm,ss,ms;
  267. int mo,dy,yr;
  268. sysdate(&mo,&dy,&yr);
  269. systime(&hh,&mm,&ss,&ms);
  270. sprintf(timebuff,"%02d/%02d %02d:%02d",mo,dy,hh,mm);
  271. return(timebuff);
  272. }
  273.  
  274.  
  275.  
  276. char *file_prompt(level,function)
  277. int level;
  278. char function[];
  279. {
  280. static char rb[LINESIZE+2];
  281. if (level) {
  282.     sprintf(rb,"Enter name of file to %s: ",function);
  283.     return(rb);
  284.     }
  285. else return("File? ");
  286. }
  287.  
  288.  
  289. int test_password()
  290. {
  291. char *string;
  292.  
  293. if (syspw[0]=='\0') return(TRUE);        /* No password - let him on */
  294. else {
  295.     printf("pw: ");
  296.     string = input_pw(9);
  297.     return(!strcmp(string,syspw));
  298.     }
  299. }
  300.  
  301.     
  302. int finput(filename,opts)
  303. char filename[];
  304. char *opts;
  305. {
  306. FILE *fp;
  307. char c;
  308. char line[STR_SIZE+4];
  309. int eof, linect, rc;
  310. int quiet;
  311.  
  312. quiet = find_option(opts,'q');
  313. if (*filename == '\0') return(nothing_done()&0+1);
  314. else if ((fp=fopen(filename,"r")) != NULL) {
  315.     fclose(fp);
  316.     if (find_option(opts,'a')) c = 'Y';
  317.     else {
  318.         printf("File already exists.\r\nWant to add lines to it ? ");
  319.         c = *ltrim(lineinput(2));
  320.         }
  321.     if (toupper(c)!='Y') {
  322.         printf("Use the ERASE command first.\r\n");
  323.         return(nothing_done()&0+1);
  324.         }
  325.     }
  326. if ((fp=fopen(filename,"a")) == NULL) {
  327.     not_open();
  328.     return(2);
  329.     }
  330. else {
  331.     if (!quiet) mputs("Enter input lines below, terminate with CTL-Z followed by <CR>.\r\n");
  332.     linect = 0;
  333.     rc = 0;
  334.     noforcenow = TRUE;
  335.     do {
  336.         strcpy(line,lineinput(STR_SIZE));
  337.         if (!dsr()) break;
  338.         eof = (stpchr(line,CTLZ)!=NULL);
  339.         del_crlf(line);
  340.         if (!eof) strcat(line,"\r\n");    /* Add text line end markers */
  341.         if (fputs(line,fp)==EOF) {
  342.              printf("DISK FULL or other error writing output file.\r\n");
  343.             rc = 3;
  344.             break;
  345.             }
  346.         else linect++;
  347.         } while (!eof);
  348.     noforcenow = FALSE;
  349.     if (fclose(fp)) {
  350.         printf("Error while closing file.\r\n");
  351.         return(4);
  352.         }
  353.     if (!quiet) printf("\r\n%d lines written to file '%s'\r\n",linect,filename);    
  354.     return(rc);
  355.     }
  356. }
  357.  
  358.  
  359. /* Parses normal "MM/DD/YY" form date into *PMO, *PDY, *PYR */
  360. extr_date(str,pmo,pdy,pyr)
  361. char *str;
  362. int *pmo, *pdy, *pyr;
  363. {
  364. char cmo[4], cdy[4], cyr[4];
  365.  
  366. *cmo = *cdy = *cyr = '\0';
  367. if (str) str = stptok(stpblk(str),cmo,sizeof(cmo),"/. ");
  368. if (str) str = stptok(++str,cdy,sizeof(cdy),"/. ");
  369. if (str) str = stptok(++str,cyr,sizeof(cyr),"/. ");
  370. *pmo = stoi(cmo);
  371. *pdy = stoi(cdy);
  372. *pyr = stoi(cyr);
  373. }
  374.  
  375.  
  376. /* CREATE_LIST:   We have already set up the DTA and issued a successsful
  377.           FIND_FIRST call to it.  We start allocating lines to it. */
  378. create_list()
  379. {
  380.     int rc;
  381.     rc = 0;
  382.     while(!rc) {
  383.         if (rc = add_list(&ftab_ptr->filedata)) ;
  384.         else rc = mdos(NEXT_FILE);
  385.         }
  386. }
  387.  
  388. /*****************************************************************************
  389.  *    add_list:  Add an 'element' (filedate/attrib/time/name info) to next *
  390.  *         (list_idx+1) element in string array.                 *
  391.  *****************************************************************************/
  392. int add_list(elemt)
  393. struct fileinfo *elemt;
  394. {
  395. struct dir_entry *newptr;
  396.  
  397. /* Obtain a memory for this elemt in the string array */
  398. if ((newptr=(struct dir_entry *)getmem(sizeof(**list)))==NULL) {
  399.     warn_omem();
  400.     return(100);
  401.     }
  402. /* Error if cannot get memory, else move the elemt into its place */
  403. /* (move elemt into fileinfo structure (FILEDATA)) */
  404. else movmem((char *)elemt,(char *)&newptr->filedata,sizeof(*elemt));
  405.  
  406. /* Put it in the array as next element, using LIST_IDX as index */
  407. list[list_idx++] = newptr;
  408.  
  409. /* Initialize value field to numeric value of filetype */
  410. /* Read extension of file, taking numeric value from it */
  411. newptr->value = stoi(file_ext(newptr->filedata.filename_ext));
  412.  
  413. /* Good return - element added to list */
  414. return(0);
  415. }
  416.  
  417. /*****************************************************************************
  418.  *    drop_list:  de-allocate the entire string array (use RLSMEM)         *
  419.  *****************************************************************************/
  420. drop_list()
  421. {
  422.     while(list_idx--) {
  423.         if (rlsmem((char *)list[list_idx],sizeof(**list)))
  424.             printf("Error during RLSMEM\r\n");
  425.         }
  426. list_idx = 0;
  427. }
  428.  
  429. /*****************************************************************************
  430.  *  SORT_LIST:  Sort the string array into ascending order, by VALUE.        *
  431.  *****************************************************************************/ 
  432. /* We have array in memory of pointers to structure type DIR_elemt.
  433.    we must sort this array according to ascending values found
  434.    in the 'value' variable in each structure. 
  435.    Note that since the data will be partially sorted, we will use the
  436.    bubble sort to take advantage of the partial sorted order.
  437.    (besides, its the only one i know by heart)    */
  438.  
  439. sort_list(list,n)
  440. struct dir_entry *list[];
  441. int n;
  442. {
  443.     int j;
  444.     int swapped;
  445.     struct dir_entry *temp;
  446.  
  447.     swapped = TRUE;
  448.     while(swapped) {
  449.         swapped = FALSE;
  450.         for (j=0;j<n-1;++j)
  451.             if (list[j]->value>list[j+1]->value) {
  452.                 swapped = TRUE;
  453.                 temp = list[j];
  454.                 list[j] = list[j+1];
  455.                 list[j+1] = temp;
  456.                 }
  457.         }
  458. }
  459.  
  460.  
  461. char *file_ext(fn)
  462. char *fn;
  463. {
  464. char *p;
  465.  
  466. if ((p=stpchr(fn,'.'))==NULL) return("");
  467. else return(p+1);
  468. }
  469.  
  470. char *get_name(dest,sourcefn)
  471. char *dest, *sourcefn;
  472. {
  473. char *p;
  474. int l;
  475.  
  476. if ((p=stpchr(sourcefn,'.'))==NULL) strcpy(dest,sourcefn);
  477. else {
  478.     l = p-sourcefn;                /* Get num chars in name */
  479.     if (l>0) {                /* I Don't trust myself */
  480.         movmem(sourcefn,dest,l);    /* Move in just filename */
  481.         }
  482.     else l = 0;
  483.     dest[l] = '\0';            /* Make printable string of it */
  484.     }
  485. return(dest);
  486. }
  487.  
  488. help(helpfile,opts)
  489. char helpfile[];
  490. char *opts;
  491. {
  492. char filename[DIRSIZE+17];
  493. prefix_dir(filename,helpfile,hpath);
  494. typefile(filename,opts);
  495. }
  496.  
  497. long cnct_seconds()
  498. {
  499. int hh,mm,ss,ms;
  500. systime(&hh,&mm,&ss,&ms);
  501. return(time_diff(login_hh,login_mm,login_ss,hh,mm,ss));
  502. }
  503.  
  504. print_cd()
  505. {
  506. printf("Current directory: %s\r\n",*cur_dir?cur_dir:"(node)");
  507. if (strcmp(user_root,cur_root) && *cur_root)
  508.     printf("Current root:      %s\r\n",cur_root);
  509. }
  510. 
  511.