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

  1. /*
  2.  * Display the welcome screen and find the Pcomm support files.  Returns a
  3.  * pointer to the STATUS structure.  All errors are fatal.
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <curses.h>
  8. #include "config.h"
  9. #ifdef SHAREDMEM
  10. #include <sys/types.h>
  11. #include <sys/ipc.h>
  12. #include <sys/shm.h>
  13. #endif /* SHAREDMEM */
  14. #include "misc.h"
  15. #include "status.h"
  16.  
  17. struct STATUS *
  18. init(short_cut)
  19. char *short_cut;
  20. {
  21.     char *mktemp(), *strcpy();
  22.     struct STATUS *s_ptr;
  23.     void info();
  24. #ifdef SHAREDMEM
  25.     int i, j, mode;
  26.     extern int shm_id;
  27.     char *shmat();
  28.     void exit();
  29.  
  30.     /*
  31.      * Since the "pcomm_input" program does not run set-user/group-id
  32.      * the mode must be set so the effective ID can read/write to the
  33.      * shared memory segment.  Kinda strange... real ID's aren't used.
  34.      */
  35. #ifdef SETUGID
  36.     mode = 0666;
  37. #else /* SETUGID */
  38.     mode = 0600;
  39. #endif /* SETUGID */
  40.                     /* create a shared memory segment */
  41.     shm_id = shmget(IPC_PRIVATE, sizeof (struct STATUS),  mode|IPC_CREAT|IPC_EXCL|IPC_NOWAIT);
  42.     if (shm_id < 0) {
  43.         endwin();
  44.         perror("shmget");
  45.         exit(1);
  46.     }
  47.     s_ptr = (struct STATUS *) shmat(shm_id, (char *) 0, 0);
  48.     if ((int) s_ptr == -1) {
  49.         endwin();
  50.         perror("shmat");
  51.         exit(1);
  52.     }
  53. #else /* SHAREDMEM */
  54.     static struct STATUS s;
  55.     s_ptr = &s;
  56. #endif /* SHAREDMEM */
  57.                     /* some defaults */
  58.     s_ptr->fd = -1;
  59.     s_ptr->add_lf = 0;
  60.     s_ptr->log = 0;
  61.     s_ptr->print = 0;
  62.     strcpy(s_ptr->log_path, "NOT_DEFINED");
  63. #ifdef SHAREDMEM
  64.     s_ptr->clr = 0;
  65.     s_ptr->row = 0;
  66.     s_ptr->col = 0;
  67.     for (i=0; i<LINES; i++) {
  68.         for (j=0; j<COLS; j++)
  69.             s_ptr->vs[i][j] = ' ';
  70.         s_ptr->vs[i][COLS] = NULL;
  71.     }
  72. #else /* SHAREDMEM */
  73.     strcpy(s_ptr->vs_path, mktemp("/tmp/pcommXXXXXX"));
  74. #endif /* SHAREDMEM */
  75.                     /* display herald if no short-cut */
  76.     if (short_cut == NULL)
  77.         info(1);
  78.  
  79.     erase();
  80.     refresh();
  81.     return(s_ptr);
  82. }
  83.  
  84. /*
  85.  * Search the extra directory (supplied on the command line), then the
  86.  * directory in the PCOMM environmental variable, then the current working
  87.  * directory, and lastly, the default directory.
  88.  */
  89.  
  90. char *
  91. findfile(extra, name)
  92. char *extra, *name;
  93. {
  94.     int i;
  95.     char *pcomm, *getenv(), *path, pbuf[200], *getcwd(), *strdup();
  96.     char temp[200];
  97.  
  98.                     /* see if PCOMM variable is set */
  99.     pcomm = getenv("PCOMM");
  100.     if (pcomm == NULL || *pcomm == NULL)
  101.         pcomm = NULL;
  102.     else {
  103.                     /* zap the trailing separator */
  104.         if (pcomm[strlen(pcomm)-1] == '/')
  105.             pcomm[strlen(pcomm)-1] = NULL;
  106.     }
  107.  
  108.     for (i=0; i<4; i++) {
  109.                     /* directory search order */
  110.         switch (i) {
  111.             case 0:        /* extra directory from command line */
  112.                 path = extra;
  113.                 break;
  114.             case 1:        /* PCOMM environmental variable */
  115.                 path = pcomm;
  116.                 break;
  117.             case 2:        /* current working directory */
  118.                 path = getcwd(pbuf, 200);
  119.                 break;
  120.             case 3:        /* Pcomm's default directory */
  121.                 path = DEFAULT_DIR;
  122.                 break;
  123.         }
  124.         if (path == NULL)
  125.             continue;
  126.  
  127.         sprintf(temp, "%s/%s", path, name);
  128.                     /* read permission checked */
  129.         if (!access(temp, 4))
  130.             return(strdup(temp));
  131.     }
  132.     return(NULL);
  133. }
  134.