home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume14 / pcomm / part04 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-05-17  |  4.7 KB  |  211 lines

  1. /*
  2.  * Pcomm is a public domain telecommunication program for Unix
  3.  * designed to operate similar to the popular MSDOS program, ProComm.
  4.  * ProComm (TM) is copyrighted by Datastorm Technologies, Inc.
  5.  *
  6.  * Emmet P. Gray            US Army, HQ III Corps & Fort Hood
  7.  * ...!ihnp4!uiucuxc!fthood!egray    Attn: AFZF-DE-ENV
  8.  *                    Directorate of Engineering & Housing
  9.  *                    Environmental Management Office
  10.  *                    Fort Hood, TX 76544-5057
  11.  *    Beta release     7 Feb 88
  12.  *    Release 1.0    12 Mar 88
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <signal.h>
  17. #include <curses.h>
  18. #ifndef OLDCURSES
  19. #include <term.h>
  20. #endif /* OLDCURSES */
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #define MAIN
  24. #include "dial_dir.h"
  25. #include "modem.h"
  26. #include "param.h"
  27. #include "status.h"
  28.  
  29. #ifdef OLDCURSES
  30. char bp[1024];
  31. #define cbreak crmode
  32. #endif /* OLDCURSES */
  33.  
  34. struct PARAM *param;
  35. struct DIAL_DIR *dir;
  36. struct STATUS *status;
  37. struct MODEM *modem;
  38. int xmc;
  39. char *null_ptr;
  40.  
  41. main(argc, argv)
  42. int argc;
  43. char *argv[];
  44. {
  45.     int c, ret_code, i, code, quit();
  46.     char *mytty, *ttyname(), *term, *getenv(), *index, *strdup();
  47.     char *extra_dir, buf[80], message[80];
  48.     struct PARAM *read_param();
  49.     struct DIAL_DIR *read_dir();
  50.     struct STATUS *init();
  51.     struct MODEM *read_modem();
  52.     struct stat stbuf;
  53.     void exit(), error_win(), input_on(), free_ptr();
  54.     extern char *optarg;
  55. #ifdef OLDCURSES
  56.     char *tgetstr(), ocbuf[20];
  57.     char *ocbufptr = ocbuf;
  58. #endif /* OLDCURSES */
  59.  
  60.     signal(SIGINT, SIG_IGN);
  61.     signal(SIGQUIT, SIG_IGN);
  62.     signal(SIGTERM, quit);
  63.     signal(SIGHUP, quit);
  64.  
  65.     null_ptr = "";
  66.     index = NULL;
  67.     extra_dir = NULL;
  68.                     /* the command line */
  69.     while ((c = getopt(argc, argv, "d:f:")) != EOF) {
  70.         switch (c) {
  71.             case 'd':    /* the extra directory to search */
  72.                 extra_dir = strdup(optarg);
  73.                 break;
  74.             case 'f':    /* the index into the dialing dir */
  75.                 index = strdup(optarg);
  76.                 break;
  77.             case '?':    /* default */
  78.                 fprintf(stderr, "Usage: pcomm [-d directory] [-f index]\n");
  79.                 exit(1);
  80.                 break;
  81.         }
  82.     }
  83.                     /* get terminal type */
  84.     term = getenv("TERM");
  85.     if (term == NULL || *term == NULL) {
  86.         fprintf(stderr, "Windows not supported (TERM not defined)\n");
  87.         exit(1);
  88.     }
  89.                     /* see if terminfo entry exists */
  90. #ifdef OLDCURSES
  91.     ret_code = tgetent(bp, term);
  92. #else /* OLDCURSES */
  93.     setupterm(term, 1, &ret_code);
  94. #endif /* OLDCURSES */
  95.     if (ret_code != 1) {
  96.         fprintf(stderr, "Windows not supported (no terminfo data for '%s')\n", term);
  97.         exit(1);
  98.     }
  99.                     /* minimum screen size */
  100. #ifdef OLDCURSES
  101.     if (tgetnum("co") < 80 || tgetnum("li") < 24) {
  102. #else /* OLDCURSES */
  103.     if (columns < 80 || lines < 24) {
  104. #endif /* OLDCURSES */
  105.         fprintf(stderr, "Windows not supported (minimum 80x24 screen required)\n");
  106.         exit(1);
  107.     }
  108.                     /* must have cursor movement */
  109. #ifdef OLDCURSES
  110.     if (tgetstr("cm", &ocbufptr) == NULL) {
  111. #else /* OLDCURSES */
  112.     if (cursor_address == NULL) {
  113. #endif /* OLDCURSES */
  114.         fprintf(stderr, "Windows not supported (terminal too dumb)\n");
  115.         exit(1);
  116.     }
  117.                     /* load magic cookie variable */
  118. #ifdef OLDCURSES
  119.     xmc = tgetnum("sg");
  120. #else /* OLDCURSES */
  121.     xmc = magic_cookie_glitch;
  122. #endif /* OLDCURSES */
  123.                     /* ok... now lets go! */
  124.     initscr();
  125.     nonl();
  126.     cbreak();
  127.     noecho();
  128.  
  129.     param = (struct PARAM *) NULL;
  130.     modem = (struct MODEM *) NULL;
  131.     dir = (struct DIAL_DIR *) NULL;
  132.                     /* show the herald, return status */
  133.     status = init(extra_dir, index);
  134.                     /* get 'msgs' status */
  135.     mytty = ttyname(0);
  136.     stat(mytty, &stbuf);
  137.     chmod(mytty, 0600);
  138.     status->msg = stbuf.st_mode & 0777;
  139.                     /* read the support files */
  140.     param = read_param();
  141.     dir = read_dir();
  142.     modem = read_modem();
  143.                     /* short-cut to dialing window ? */
  144.     code = 0;
  145.     if (index != NULL) {
  146.         for (i=1; i<dir->d_entries+1; i++) {
  147.             if (!strcmp(dir->index[i], index)) {
  148.                 dir->q_num[0] = i;
  149.                 dir->d_cur = i;
  150.                 break;
  151.             }
  152.         }
  153.                     /* if match is found */
  154.         if (dir->q_num[0] != -1)
  155.             code = dial_win();
  156.         else {
  157.             sprintf(buf, "Can't find index '%s' in dialing directory file", index);
  158.             sprintf(message, "'%s'", status->d_path);
  159.             error_win(0, buf, message);
  160.         }
  161.         free_ptr(index);
  162.     }
  163.                     /* start terminal dialogue */
  164.     terminal(code);
  165.     exit(0);
  166. }
  167.  
  168. /*
  169.  * Something dreadful happened...  Cleanup the mess we made with the
  170.  * tty driver and release the phone line.
  171.  */
  172.  
  173. int
  174. quit()
  175. {
  176.     void cleanup();
  177.  
  178.     cleanup(1);
  179.                     /* never returns... */
  180.     return(0);
  181. }
  182.  
  183. /*
  184.  * Check write permission with the real UID and GID.  Returns a 0 on
  185.  * permission denied, 1 on OK, and 2 on OK-but the file already exists.
  186.  */
  187.  
  188. int
  189. can_write(file)
  190. char *file;
  191. {
  192.     char *p, path[200], *strcpy(), *strrchr();
  193.  
  194.     p = strcpy(path, file);
  195.                     /* dissect the path component */
  196.     if (p = strrchr(path, '/'))
  197.         *(p++) = NULL;
  198.     else
  199.         strcpy(path, ".");
  200.                     /* if it already exists */
  201.     if (!access(file, 0)) {
  202.         if (!access(file, 2))
  203.             return(2);
  204.         return(0);
  205.     }
  206.                     /* if path is writable */
  207.     if (!access(path, 2))
  208.         return(1);
  209.     return(0);
  210. }
  211.