home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume16 / pcomm2 / part05 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-09-14  |  6.0 KB  |  260 lines

  1. /*
  2.  * Pcomm is a public domain telecommunication program for Unix that
  3.  * is designed to operate similar to the 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.  * ...!uunet!uiucuxc!fthood!egray    Attn: AFZF-DE-ENV
  8.  *                    Directorate of Engineering & Housing
  9.  *                    Environmental Management Office
  10.  *                    Fort Hood, TX 76544-5057
  11.  *
  12.  *    Release v1.0    12 Mar 88
  13.  *      patch #1    22 Mar 88
  14.  *      patch #2    26 Mar 88
  15.  *      patch #3     3 Apr 88
  16.  *      patch #4    14 Apr 88
  17.  *      patch #5    25 May 88
  18.  *    Release v1.1    21 Aug 88
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <signal.h>
  23. #include <curses.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include "config.h"
  27. #ifndef OLDCURSES
  28. #include <term.h>
  29. #endif /* OLDCURSES */
  30. #define MAIN
  31. #include "dial_dir.h"
  32. #include "modem.h"
  33. #include "param.h"
  34. #include "status.h"
  35.  
  36. #ifdef OLDCURSES
  37. char tcbuf[1024], *TI, *VS;
  38. #define cbreak crmode
  39. #endif /* OLDCURSES */
  40.  
  41. #ifdef SHAREDMEM
  42. int shm_id;
  43. #endif /* SHAREDMEM */
  44.  
  45. struct PARAM *param;
  46. struct DIAL_DIR *dir;
  47. struct STATUS *status;
  48. struct MODEM *modem;
  49.  
  50. int fd = -1;                /* file descriptor for port */
  51. int xmc;                /* magic cookie terminal */
  52. int msg_status;                /* read/write permissions on TTY */
  53. char *null_ptr = "";            /* generic null pointer */
  54.  
  55. main(argc, argv)
  56. int argc;
  57. char *argv[];
  58. {
  59.     extern char *optarg;
  60.     int c, ret_code, i, code, quit();
  61.     char *mytty, *ttyname(), *term, *getenv(), *short_cut, *strdup();
  62.     char *extra_dir, buf[80], message[80];
  63.     struct PARAM *read_param();
  64.     struct DIAL_DIR *read_dir();
  65.     struct STATUS *init();
  66.     struct MODEM *read_modem();
  67.     struct stat stbuf;
  68.     void exit(), error_win(), free_ptr();
  69. #ifdef OLDCURSES
  70.     char *tgetstr(), *t, tb[1024];
  71.     t = tcbuf;
  72. #endif /* OLDCURSES */
  73.  
  74.     signal(SIGINT, SIG_IGN);
  75.     signal(SIGQUIT, SIG_IGN);
  76.     signal(SIGTERM, quit);
  77.     signal(SIGHUP, quit);
  78.  
  79.     short_cut = NULL;
  80.     extra_dir = NULL;
  81.                     /* the command line */
  82.     while ((c = getopt(argc, argv, "d:f:")) != EOF) {
  83.         switch (c) {
  84.             case 'd':    /* the extra directory to search */
  85.                 extra_dir = strdup(optarg);
  86.                 break;
  87.             case 'f':    /* the index into the dialing dir */
  88.                 short_cut = strdup(optarg);
  89.                 break;
  90.             case '?':    /* default */
  91.                 fprintf(stderr, "Usage: pcomm [-d directory] [-f index]\n");
  92.                 exit(1);
  93.                 break;
  94.         }
  95.     }
  96.                     /* get terminal type */
  97.     term = getenv("TERM");
  98.     if (term == NULL || *term == NULL) {
  99.         fprintf(stderr, "Windows not supported (TERM not defined)\n");
  100.         exit(1);
  101.     }
  102.                     /* see if terminfo entry exists */
  103. #ifdef OLDCURSES
  104.     ret_code = tgetent(tb, term);
  105. #else /* OLDCURSES */
  106.     setupterm(term, 1, &ret_code);
  107. #endif /* OLDCURSES */
  108.     if (ret_code != 1) {
  109.         fprintf(stderr, "Windows not supported (no terminfo data for '%s')\n", term);
  110.         exit(1);
  111.     }
  112.                     /* minimum screen size */
  113. #ifdef OLDCURSES
  114.     if (tgetnum("co") < 80 || tgetnum("li") < 24) {
  115. #else /* OLDCURSES */
  116.     if (columns < 80 || lines < 24) {
  117. #endif /* OLDCURSES */
  118.         fprintf(stderr, "Windows not supported (minimum 80x24 screen required)\n");
  119.         exit(1);
  120.     }
  121.                     /* must have cursor movement */
  122. #ifdef OLDCURSES
  123.     if (tgetstr("cm", &t) == NULL) {
  124. #else /* OLDCURSES */
  125.     if (cursor_address == NULL) {
  126. #endif /* OLDCURSES */
  127.         fprintf(stderr, "Windows not supported (terminal too dumb)\n");
  128.         exit(1);
  129.     }
  130.                     /* load magic cookie variable */
  131. #ifdef OLDCURSES
  132.     xmc = tgetnum("sg");
  133.     TI = tgetstr("ti", &t);
  134.     VS = tgetstr("vs", &t);
  135. #else /* OLDCURSES */
  136.     xmc = magic_cookie_glitch;
  137. #endif /* OLDCURSES */
  138.                     /* ok... now lets go! */
  139.     initscr();
  140.     nonl();
  141.     cbreak();
  142.     noecho();
  143.  
  144.     param = (struct PARAM *) NULL;
  145.     modem = (struct MODEM *) NULL;
  146.     dir = (struct DIAL_DIR *) NULL;
  147.                     /* show the herald, return status */
  148.     status = init(short_cut);
  149.                     /* get "msgs" status */
  150.     mytty = ttyname(0);
  151.     stat(mytty, &stbuf);
  152.     chmod(mytty, 0600);
  153.     msg_status = stbuf.st_mode & 0777;
  154.                     /* read the support files */
  155.     param = read_param(extra_dir);
  156.     dir = read_dir(extra_dir);
  157.     modem = read_modem(extra_dir);
  158.     free_ptr(extra_dir);
  159.                     /* warning about screen size */
  160.     if (LINES > MAX_ROW || COLS > MAX_COL)
  161.         error_win(0, "Your screen size exceeds an internal Pcomm limit",
  162.          "The edges of the screen may contain garbage");
  163.  
  164.                     /* short-cut to dialing window? */
  165.     code = 0;
  166.     if (short_cut != NULL) {
  167.         for (i=1; i<dir->d_entries+1; i++) {
  168.             if (!strcmp(dir->index[i], short_cut)) {
  169.                 dir->q_num[0] = i;
  170.                 dir->d_cur = i;
  171.                 break;
  172.             }
  173.         }
  174.                     /* if match is found */
  175.         if (dir->q_num[0] != -1)
  176.             code = dial_win();
  177.         else {
  178.             sprintf(buf, "Can't find index '%s' in dialing directory", short_cut);
  179.             sprintf(message, "file '%s'", dir->d_path);
  180.             error_win(0, buf, message);
  181.         }
  182.         free_ptr(short_cut);
  183.     }
  184.                     /* start terminal dialogue */
  185.     terminal(code);
  186.     exit(0);
  187. }
  188.  
  189. /*
  190.  * Something dreadful happened...  Clean up the mess we made with the
  191.  * TTY driver and release the phone line.
  192.  */
  193.  
  194. int
  195. quit()
  196. {
  197.     void cleanup();
  198.  
  199.     cleanup(1);
  200.                     /* never returns... */
  201.     return(0);
  202. }
  203.  
  204. /*
  205.  * Check write permission with the real UID and GID.  Returns a 0 on
  206.  * permission denied, 1 on OK, and 2 on OK-but the file already exists.
  207.  */
  208.  
  209. int
  210. can_write(file)
  211. char *file;
  212. {
  213.     char *p, path[200], *strcpy(), *strrchr();
  214.  
  215.     p = strcpy(path, file);
  216.                     /* dissect the path component */
  217.     if (p = strrchr(path, '/'))
  218.         *(p++) = NULL;
  219.     else
  220.         strcpy(path, ".");
  221.                     /* if it already exists */
  222.     if (!access(file, 0)) {
  223.         if (!access(file, 2))
  224.             return(2);
  225.         return(0);
  226.     }
  227.                     /* if path is writable */
  228.     if (!access(path, 2))
  229.         return(1);
  230.     return(0);
  231. }
  232.  
  233. /*
  234.  * Check the read and write permissions before opening a file.  This
  235.  * is a horrible kludge to work around that fact that a lot of systems
  236.  * that claim to be SVID compatible don't treat setuid(2) and setgid(2)
  237.  * properly.  For example, on a Masscomp, you can't flip-flop back and
  238.  * forth between the real and effective UID/GID.
  239.  */
  240.  
  241. FILE *
  242. my_fopen(file, mode)
  243. char *file, *mode;
  244. {
  245. #ifdef SETUGID
  246.     switch (*mode) {
  247.         case 'a':
  248.         case 'w':
  249.             if (!can_write(file))
  250.                 return(NULL);
  251.             break;
  252.         case 'r':
  253.             if (access(file, 4))
  254.                 return(NULL);
  255.             break;
  256.     }
  257. #endif /* SETUGID */
  258.     return ((FILE *) fopen(file, mode));
  259. }
  260.