home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / trn_12.zip / src / os2patch.c < prev    next >
C/C++ Source or Header  |  1993-12-04  |  18KB  |  446 lines

  1. /************************************************************
  2.  *** OS2: This file contains additional routines          ***
  3.  ***      only needed in the OS/2-version.                ***
  4.  ***                                                      ***
  5.  ***   last modified: 12/01/92                            ***
  6.  ***   Authors:  Thilo Schuster (term@godot.stgt.sub.org) ***
  7.  ***         Herbert Neugebauer (haen@veces.stgt.sub.org) ***
  8.  ***                                                      ***
  9.  ***  please send bug reports to term@godot or haen@veces ***
  10.  ***                                                      ***
  11.  ***  Remark: the things in os2patch.c are used in every  ***
  12.  ***          executable, os2trn.c only in trn            ***
  13.  ************************************************************/
  14.  
  15. #define OS2_PATCHES_C
  16.  
  17. #define EABUF 0L
  18.  
  19. #include <os2.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <strings.h>
  23. #include "os2patch.h"
  24.  
  25.  
  26. /*****************************************************************
  27.  *****                    global variables                    ****
  28.  *****************************************************************/
  29.  
  30. INFO uupc_rc_settings;
  31.  
  32. /*****************************************************************
  33.  *****                        functions                       ****
  34.  *****************************************************************/
  35.  
  36.  
  37. /***********************************************
  38.  *** the fopen function of EMX/GCC has some  ***
  39.  *** restrictions, (e.g. you cannot open     ***
  40.  ***  one file two times simultaneously).    ***
  41.  *** So we replace the fopen-calls in the    ***
  42.  *** the source by the fos2open-call so we   ***
  43.  *** can do the enhancements here.           ***
  44.  ***********************************************/
  45.  
  46. /*FILE *fos2open(const char *filename, const char *mode) */
  47. FILE *fblaopen(const char *filename, const char *mode)
  48. {   char newmode[3+1];
  49.     HFILE   FileHandle;
  50.     ULONG   Action;
  51.     unsigned long  rc;              /* Return code */
  52.     ULONG   openflags, shareflags;
  53.  
  54.     Action = 2;
  55.  
  56.     if (*mode == 'r') {
  57.         if (*(mode+1) == '+') {
  58.             openflags = OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
  59.             shareflags = OPEN_ACCESS_READWRITE;
  60.         }
  61.         else {
  62.             openflags = OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
  63.             shareflags = OPEN_ACCESS_READONLY;
  64.         }
  65.     }
  66.     else
  67.     if (*mode == 'w') {
  68.         if (*(mode+1) == '+') {
  69.             openflags = OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
  70.             shareflags = OPEN_ACCESS_READWRITE;
  71.         }
  72.         else {
  73.             openflags = OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_REPLACE_IF_EXISTS;
  74.             shareflags = OPEN_ACCESS_WRITEONLY;
  75.         }
  76.     }
  77.     else
  78.     if (*mode == 'a') {
  79.         openflags = OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS;
  80.         shareflags = OPEN_SHARE_DENYNONE;
  81.     }
  82.     else
  83.     return NULL;   /*** Error in mode-flags ***/
  84.  
  85.     shareflags |= OPEN_FLAGS_FAIL_ON_ERROR | OPEN_SHARE_DENYNONE;
  86.  
  87.     rc = DosOpen(filename,                    /* File path name */
  88.                  &FileHandle,                 /* File handle */
  89.                  &Action,                     /* Action taken */
  90.                  0,                           /* File primary allocation */
  91.                  FILE_NORMAL,                 /* File attribute */
  92.                  openflags,                   /* Open function type */
  93.                  shareflags,                  /* Open mode of the file */
  94.                  EABUF);                      /* No extended attributes */
  95.  
  96.     if (rc != 0) return NULL;
  97.  
  98.     *newmode = '\0';
  99.     strcpy(newmode,mode);
  100.     strcat(newmode,"b");
  101.  
  102.     return fdopen(FileHandle,mode);
  103. }
  104.  
  105. FILE *fos2open(const char *filename, const char *mode)
  106. {   char newmode[3+1];
  107.  
  108.     *newmode = '\0';
  109.     strcpy(newmode,mode);
  110.     strcat(newmode,"b");
  111.  
  112.     return fopen(filename,newmode);
  113. }
  114.  
  115.  
  116. /****************************************************
  117.  **** This function is used to change all '\'    ****
  118.  **** characters to the '/' character. This must ****
  119.  **** must be done to prevent the translation    ****
  120.  **** when sprintf is used to prepare a filename ****
  121.  ****************************************************/
  122.  
  123. void change_bsl2sl(char *changeString)
  124. {   char *tmpptr;
  125.  
  126.     if (changeString == NULL) return;
  127.     if (strlen(changeString) == 0) return;
  128.  
  129.     tmpptr = &changeString[0];
  130.     while(*tmpptr != '\0')
  131.     {   if (*tmpptr == '\\') *tmpptr = '/';
  132.         ++tmpptr;
  133.     }
  134. }
  135.  
  136.  
  137. /*****************************************************
  138.  **** This function is used to change all '/'     ****
  139.  **** characters to the '\' character. This must  ****
  140.  **** must be done when using a prepared filename ****
  141.  **** together with the OS/2 shell cmd or 4os2,   ****
  142.  **** because these programs do not accept        **** 
  143.  **** with slashes in all conditions.             **** 
  144.  *****************************************************/
  145.  
  146. void change_sl2bsl(char *changeString)
  147. {   char *tmpptr;
  148.  
  149.     if (changeString == NULL) return;
  150.     if (strlen(changeString) == 0) return;
  151.  
  152.     tmpptr = &changeString[0];
  153.     while(*tmpptr != '\0')
  154.     {   if (*tmpptr == '/') *tmpptr = '\\';
  155.         ++tmpptr;
  156.     }
  157. }
  158.  
  159.  
  160. /*******************************************************
  161.  **  this function was picked out of the SNews        **
  162.  **  source written by John McCombs, New Zealand      **
  163.  **   <john@ahuriri.gen.nz>. Since the first version  **
  164.  **  most a lot has been rewritten to meet our        **
  165.  **  requirements. The strategy how the information   **
  166.  **  is extracted, is still the same.                 **
  167.  *******************************************************/
  168.  
  169. int load_uupc_rc(void)
  170. {
  171.     /*
  172.      *  Trawl the UUPC files to get the stuff we need - return TRUE
  173.      *  if completed ok
  174.      */
  175.  
  176.     int  res = 0;
  177.     int  i;
  178.     char buf[256];
  179.     char *fn, *p, *v;
  180.     FILE *tmp;
  181.  
  182.  
  183.     memset(uupc_rc_settings.temp_name, '\0', 80);
  184.     memset(uupc_rc_settings.news_dir, '\0', 80);
  185.     memset(uupc_rc_settings.mail_dir, '\0', 80);
  186.     memset(uupc_rc_settings.config_dir, '\0', 80);
  187.     memset(uupc_rc_settings.mailsent, '\0', 80);
  188.     memset(uupc_rc_settings.rmailpath, '\0', 80);
  189.     memset(uupc_rc_settings.active, '\0', 80);
  190.     memset(uupc_rc_settings.active_opt, '\0', 80);
  191.     memset(uupc_rc_settings.active_times, '\0', 80);
  192.     memset(uupc_rc_settings.user, '\0', 80);
  193.     memset(uupc_rc_settings.mailfile, '\0', 160);
  194.     memset(uupc_rc_settings.name, '\0', 80);
  195.     memset(uupc_rc_settings.domain, '\0', 80);
  196.     memset(uupc_rc_settings.site, '\0', 80);
  197.     memset(uupc_rc_settings.organization, '\0', 80);
  198.     memset(uupc_rc_settings.mail_server, '\0', 80);
  199.     memset(uupc_rc_settings.editor, '\0', 80);
  200.     memset(uupc_rc_settings.home, '\0', 80);
  201.     memset(uupc_rc_settings.signature, '\0', 80);
  202.     memset(uupc_rc_settings.newsadmin, '\0', 80);
  203.     memset(uupc_rc_settings.trnlib, '\0', 80);
  204.     memset(uupc_rc_settings.prefshell, '\0', 160);
  205.     memset(uupc_rc_settings.locdist, '\0', 80);
  206.     memset(uupc_rc_settings.orgdist, '\0', 80);
  207.     memset(uupc_rc_settings.citydist, '\0', 80);
  208.     memset(uupc_rc_settings.statedist, '\0', 80);
  209.     memset(uupc_rc_settings.countrydist, '\0', 80);
  210.     memset(uupc_rc_settings.continentdist, '\0', 80);
  211.  
  212.     if ((fn = getenv("COMSPEC")) == NULL) {
  213.         fprintf(stderr, "COMSPEC environment variable undefined\n");
  214.         return(0);
  215.     }
  216.     strcpy(uupc_rc_settings.prefshell, fn);
  217.     change_sl2bsl(uupc_rc_settings.prefshell);
  218.  
  219.  
  220.     /* read the system file first */
  221.     for (i = 0; i < 3; i++) {
  222.  
  223.         /* choose the file to open */
  224.         if (i == 0) {
  225.             fn = getenv("UUPCSYSRC");
  226.             if (fn == NULL) {
  227.                 fprintf(stderr, "Enviroment variable UUPCSYSRC not defined\n");
  228.             }
  229.         } else {
  230.             if (i == 1) {
  231.                fn = getenv("UUPCUSRRC");
  232.                if (fn == NULL) {
  233.                    fprintf(stderr, "Enviroment variable UUPCUSRRC not defined\n");
  234.                }
  235.             }
  236.             else {
  237.                fn = getenv("UUPCTRNRC");
  238.                if (fn == NULL) {
  239.                    fprintf(stderr, "Enviroment variable UUPCTRNRC not defined\n");
  240.                }
  241.             }
  242.         }
  243.  
  244.         if (((fn != NULL) && (strlen(fn) > 1)) &&
  245.             ((tmp = fopen(fn, "rt")) != NULL))
  246.         {   while (fgets(buf, 83, tmp))
  247.             {   p = strtok(buf, " =\r\n");
  248.                 if ((p == NULL) ? 0 : *p != '#')
  249.                 {   v = strtok(NULL, " =\r\n");
  250.                     if (stricmp(p, "MailServ") == 0) {
  251.                         strcpy(uupc_rc_settings.mail_server, v);
  252.                         change_bsl2sl(uupc_rc_settings.mail_server);
  253.                         res++;
  254.                     }
  255.                     if (stricmp(p, "FileSent") == 0) {
  256.                         /** This setting is not required, but optional **/
  257.                         strcpy(uupc_rc_settings.mailsent, v);
  258.                         change_bsl2sl(uupc_rc_settings.mailsent);
  259.                     }
  260.                     if (stricmp(p, "NodeName") == 0) {
  261.                         strcpy(uupc_rc_settings.site, v);
  262.                         change_bsl2sl(uupc_rc_settings.site);
  263.                         res++;
  264.                     }
  265.                     if (stricmp(p, "NewsDir") == 0) {
  266.                         strcpy(uupc_rc_settings.news_dir, v);
  267.                         strcpy(uupc_rc_settings.active,
  268.                                uupc_rc_settings.news_dir);
  269.                         strcpy(uupc_rc_settings.active_times,
  270.                                uupc_rc_settings.news_dir);
  271.                         strcat(uupc_rc_settings.active,"/active");
  272.                         strcat(uupc_rc_settings.active_times,"/active.times");
  273.                         change_bsl2sl(uupc_rc_settings.news_dir);
  274.                         change_bsl2sl(uupc_rc_settings.active);
  275.                         change_bsl2sl(uupc_rc_settings.active_times);
  276.                         res++;
  277.                     }
  278.                     if (stricmp(p, "Domain") == 0) {
  279.                         strcpy(uupc_rc_settings.domain, v);
  280.                         res++;
  281.                     }
  282.                     if (stricmp(p, "TempDir") == 0) {
  283.                         strcpy(uupc_rc_settings.temp_name, v);
  284.                         change_bsl2sl(uupc_rc_settings.temp_name);
  285.                         res++;
  286.                     }
  287.                     if (stricmp(p, "MailDir") == 0) {
  288.                         strcpy(uupc_rc_settings.mail_dir, v);
  289.                         change_bsl2sl(uupc_rc_settings.mail_dir);
  290.                         res++;
  291.                     }
  292.                     if (stricmp(p, "ConfDir") == 0) {
  293.                         strcpy(uupc_rc_settings.config_dir, v);
  294.                         strcpy(uupc_rc_settings.active_opt,
  295.                                uupc_rc_settings.config_dir);
  296.                         strcat(uupc_rc_settings.active_opt,"/active");
  297.                         change_bsl2sl(uupc_rc_settings.config_dir);
  298.                         change_bsl2sl(uupc_rc_settings.active_opt);
  299.                         res++;
  300.                     }
  301.             /*****  rmail no longer required **/
  302.                     if (stricmp(p, "rmail") == 0) {
  303.                         strcpy(uupc_rc_settings.rmailpath, v);
  304.                         change_bsl2sl(uupc_rc_settings.rmailpath);
  305.                     }
  306.                     if (stricmp(p, "Mailbox") == 0) {
  307.                         strcpy(uupc_rc_settings.user, v);
  308.                         res++;
  309.                     }
  310.                     if (stricmp(p, "Signature") == 0) {
  311.                         strcpy(uupc_rc_settings.signature, v);
  312.                         change_bsl2sl(uupc_rc_settings.signature);
  313.                         res++;
  314.                     }
  315.                     if (stricmp(p, "Name") == 0) {
  316.                         strcpy(uupc_rc_settings.name, v);
  317.                         v = strtok(NULL, " =\r\n");
  318.                         while (v != NULL) {
  319.                             strcat(uupc_rc_settings.name, " ");
  320.                             strcat(uupc_rc_settings.name, v);
  321.                             v = strtok(NULL, " =\r\n");
  322.                         }
  323.                         res++;
  324.                     }
  325.                     if (stricmp(p, "Organization") == 0) {
  326.                         strcpy(uupc_rc_settings.organization, v);
  327.                         v = strtok(NULL, " =\r\n");
  328.                         while (v != NULL) {
  329.                             strcat(uupc_rc_settings.organization, " ");
  330.                             strcat(uupc_rc_settings.organization, v);
  331.                             v = strtok(NULL, " =\r\n");
  332.                         }
  333.                         res++;
  334.                     }
  335.                     if (stricmp(p, "Editor") == 0) {
  336.                         strcpy(uupc_rc_settings.editor, v);
  337.                         res++;
  338.                         v = strtok(NULL, " =\r\n");
  339.                         while (v != NULL) {
  340.                             strcat(uupc_rc_settings.editor, " ");
  341.                             strcat(uupc_rc_settings.editor, v);
  342.                             v = strtok(NULL, " =\r\n");
  343.                         }
  344.                         change_bsl2sl(uupc_rc_settings.editor);
  345.                     }
  346.                     if (stricmp(p, "Home") == 0) {
  347.                         strcpy(uupc_rc_settings.home, v);
  348.                         change_bsl2sl(uupc_rc_settings.home);
  349.                         res++;
  350.                     }
  351.                     if (stricmp(p, "Newsadmin") == 0) {
  352.                         strcpy(uupc_rc_settings.newsadmin, v);
  353.                         res++;
  354.                     }
  355.                     if (stricmp(p, "TrnLib") == 0) {
  356.                         strcpy(uupc_rc_settings.trnlib, v);
  357.                         change_bsl2sl(uupc_rc_settings.trnlib);
  358.                         res++;
  359.                     }
  360.                     if (stricmp(p, "LocalDist") == 0) {
  361.                         strcpy(uupc_rc_settings.locdist, v);
  362.                         res++;
  363.                     }
  364.                     if (stricmp(p, "OrganizationDist") == 0) {
  365.                         strcpy(uupc_rc_settings.orgdist, v);
  366.                         res++;
  367.                     }
  368.                     if (stricmp(p, "CityDist") == 0) {
  369.                         strcpy(uupc_rc_settings.citydist, v);
  370.                         res++;
  371.                     }
  372.                     if (stricmp(p, "StateDist") == 0) {
  373.                         strcpy(uupc_rc_settings.statedist, v);
  374.                         res++;
  375.                     }
  376.                     if (stricmp(p, "CountryDist") == 0) {
  377.                         strcpy(uupc_rc_settings.countrydist, v);
  378.                         res++;
  379.                     }
  380.                     if (stricmp(p, "ContinentDist") == 0) {
  381.                         strcpy(uupc_rc_settings.continentdist, v);
  382.                         res++;
  383.                     }
  384.                 }
  385.             } fclose (tmp);
  386.         }
  387.     }
  388.     
  389.     if (strlen(uupc_rc_settings.rmailpath) < 2)
  390.     {   strcpy(uupc_rc_settings.rmailpath, "rmail.exe");
  391.     }
  392.     
  393.  
  394.     if (strlen(uupc_rc_settings.mail_server) < 1)
  395.         fprintf(stderr, "Missing settings <MailServ>!\n");
  396.     if (strlen(uupc_rc_settings.site) < 1)
  397.         fprintf(stderr, "Missing setting <NodeName>!\n");
  398.     if (strlen(uupc_rc_settings.news_dir) < 1)
  399.         fprintf(stderr, "Missing setting <NewsDir>!\n");
  400.     if (strlen(uupc_rc_settings.config_dir) < 1)
  401.         fprintf(stderr, "Missing setting <ConfDir>\n");
  402.     if (strlen(uupc_rc_settings.domain) < 1)
  403.         fprintf(stderr, "Missing setting <Domain>\n");
  404.     if (strlen(uupc_rc_settings.temp_name) < 1)
  405.         fprintf(stderr, "Missing setting <TempDir>\n");
  406.     if (strlen(uupc_rc_settings.mail_dir) < 1)
  407.         fprintf(stderr, "Missing setting <MailDir>\n");
  408.     if (strlen(uupc_rc_settings.user) < 1)
  409.         fprintf(stderr, "Missing setting <Mailbox>\n");
  410.     if (strlen(uupc_rc_settings.signature) < 1)
  411.         fprintf(stderr, "Missing setting <Signature>\n");
  412.     if (strlen(uupc_rc_settings.name) < 1)
  413.         fprintf(stderr, "Missing setting <Name>\n");
  414.     if (strlen(uupc_rc_settings.organization) < 1)
  415.         fprintf(stderr, "Missing setting <Organization>\n");
  416.     if (strlen(uupc_rc_settings.editor) < 1)
  417.         fprintf(stderr, "Missing setting <Editor>\n");
  418.     if (strlen(uupc_rc_settings.home) < 1)
  419.         fprintf(stderr, "Missing setting <Home>\n");
  420.     if (strlen(uupc_rc_settings.newsadmin) < 1)
  421.         fprintf(stderr, "Missing setting <Newsadmin>\n");
  422.     if (strlen(uupc_rc_settings.trnlib) < 1)
  423.         fprintf(stderr, "Missing setting <TrnLib>\n");
  424.     if (strlen(uupc_rc_settings.locdist) < 1)
  425.         fprintf(stderr, "Missing setting <LocalDist>\n");
  426.     if (strlen(uupc_rc_settings.orgdist) < 1)
  427.         fprintf(stderr, "Missing setting <OrganizationDist>\n");
  428.     if (strlen(uupc_rc_settings.citydist) < 1)
  429.         fprintf(stderr, "Missing setting <CityDist>\n");
  430.     if (strlen(uupc_rc_settings.statedist) < 1)
  431.         fprintf(stderr, "Missing setting <StateDist>\n");
  432.     if (strlen(uupc_rc_settings.countrydist) < 1)
  433.         fprintf(stderr, "Missing setting <CountryDist>\n");
  434.     if (strlen(uupc_rc_settings.continentdist) < 1)
  435.         fprintf(stderr, "Missing setting <ContinentDist>\n");
  436.  
  437.     if (res == 21)
  438.     {  strcpy(uupc_rc_settings.mailfile, uupc_rc_settings.mail_dir);
  439.        strcat(uupc_rc_settings.mailfile, "/");
  440.        strcat(uupc_rc_settings.mailfile, uupc_rc_settings.user);
  441.     }
  442.  
  443.     return(res == 21);
  444. }
  445.  
  446.