/* M2WAT - Convert mTCP configuration file to WATTCP format. Copyright (C) 2011 by Cordata This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. This program uses the environment variable MTCPCFG to find an mTCP configuration file. This file is interesting to the WATTCP user because it may contain IP address information from DHCP. */ #include #include #include #include #include #include #ifdef __WATCOMC__ #define mktemp _mktemp #endif #define MAX_CFG 4 /* increase this if more data from mTCP goes to WATTCP */ #define MAX_ALIAS 2 /* increase if a WATTCP parameter has more names */ #define NO_ALIAS "" #define LINE_LEN 81 typedef struct cfg_dat { char *mtcp_name; /* how does mTCP refer to parameter */ char *wattcp_name[MAX_ALIAS]; /* how does WATTCP refer to parameter */ char mtcp_value[LINE_LEN]; /* what is the mTCP value for parameter */ int written_2_wat; /* has parameter been written to WATTCP file */ } cfg_dat_type; /* the following table can be used to add items from mTCP to push into the WATTCP file. To add new items just add entries to this table. We account for WATTCP having multiple names for parameters. In particular the IP address can be called "IP" or "MY_IP". */ cfg_dat_type cfg_data[MAX_CFG] = { { "IPADDR","MY_IP","IP","",0}, { "GATEWAY","GATEWAY",NO_ALIAS,"",0}, { "NETMASK","NETMASK",NO_ALIAS,"",0}, { "NAMESERVER","NAMESERVER",NO_ALIAS,"",0}, }; /* function prototypes */ void adjust_wat_line (char *wat_line, char *dst); void store_mtcp_line(char *); void strip(char *); void usage(void); void main(int argc, char *argv[]) { char *mtcp, *wattcp; /* environment variable pointers */ char str[LINE_LEN],w_fname[LINE_LEN],tmp_fname[LINE_LEN]; FILE *m_file, *w_file, *new_w_file; int l,p,new_file=0; if (argc != 1) usage(); if (!(mtcp=getenv("MTCPCFG"))) { fprintf(stderr,"ERROR: must set MTCPCFG environment variable\n"); exit(1); } m_file = fopen(mtcp,"r"); if (!m_file) { fprintf(stderr,"Error opening mTCP config file %s\n",mtcp); exit(1); } while (fgets(str,LINE_LEN -1,m_file)) { str[LINE_LEN -1]='\0'; strip(str); store_mtcp_line(str); } fclose(m_file); if (!(wattcp=getenv("WATTCP.CFG"))) { printf("No WATTCP.CFG Environment variable. Using current directory.\n"); wattcp="."; } strcpy(w_fname,wattcp); w_fname[LINE_LEN -1 ]='\0'; l=strlen(w_fname); if (l > (LINE_LEN - 12)) { fprintf(stderr,"ERROR: WATTCP directory too long.\n"); exit(1); } if ((w_fname[l-1] != '\\') && (w_fname[l-1] != '/')) strcat(w_fname,"\\"); strcpy(tmp_fname, w_fname); /* same directory for temp file */ strcat(tmp_fname,"XXXXXX"); strcat(w_fname,"WATTCP.CFG"); if (!(w_file=fopen(w_fname,"r"))) { char ch; printf("WATTCP config file %s does not exist.\n",w_fname); printf("Do you want to create it? (Y/N)"); do ch=toupper(getch()); while ((ch != 'Y') && (ch != 'N')); if (ch == 'N') exit(0); new_file=1; } if (!mktemp(tmp_fname)) { printf("Error: Unable to create temporary file.\n"); exit(1); } new_w_file= fopen(tmp_fname,"w"); if (!new_file) { while (fgets(str,LINE_LEN -1,w_file)) /* read lines from WATTCP file */ { char buf[LINE_LEN]; str[LINE_LEN -1]='\0'; strip(str); adjust_wat_line(str,buf); /* add mTCP info to line, comment out or do nothing*/ fprintf(new_w_file,"%s\n",buf); /* write to new file*/ } fclose(w_file); } /* now check to see if we missed anything */ for (p=0;p