{"id":178,"date":"2023-11-23T00:48:11","date_gmt":"2023-11-23T00:48:11","guid":{"rendered":"http:\/\/10.1.250.135\/ozus\/?page_id=178"},"modified":"2023-11-23T16:43:26","modified_gmt":"2023-11-23T16:43:26","slug":"bash-linux-historyconv-c","status":"publish","type":"page","link":"https:\/\/ozus.com\/index.php\/bash-linux-historyconv-c\/","title":{"rendered":"BASH (LINUX)-HistoryConv.c"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\/*\n * File Name: HistoryConv.c\n * File Creation Date: 19980104\n * Copyright 1998 by OZUS LLC       Auth0r: Lance Lenz\n * Copyright 2009 by TriParadigm LLC\n * Copyright 2009 by MtXia Inc.     Author: Dana French\n * Copyright 2009 by Edgetec Inc.   Author: Joseph Stephens\n * \n * Permission to use, copy, modify, distribute, and sell this software and its\n * documentation for any purpose is hereby Restricted,\n * the above copyright notice must appear in all copies and that both the\n * copyright notice and this permission notice appear in supporting\n * documentation, and that the names of \n * OZUS LLC, TriParadigm LLC, MtXia Inc., Edgetec Inc.\n * not be used in advertising or publicity pertaining to distribution of the\n * software without specific, written prior permission.  \n *  makes no representations about the OZUS LLC, TriParadigm LLC, \n *  MtXia Inc., Edgetec Inc. suitability of this software for any purpose.\n * It is provided \"as is\" without express or implied warranty.\n * \n * Users Restricted Rights - Use, duplication or disclosure restricted\n * Restricted License: 2000      Company Name Here\n * \n * Update Log:\n * 20091231: Lance Lenz:\n *\n * Compile cc user.c -lpq\n*\/\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n#include &lt;time.h&gt;\n\n#define NUL '\\x00'\t\/\/ Start of Line\n#define SOH '\\x01'\t\/\/ Start of Record\n#define DE '\\xde'\t\/\/ Start of Time\n#define DF '\\xdf'\t\/\/ End of Time\n#define LF '\\x0a'\t\/\/ Line Feed  \n#define CR '\\x0d'\t\/\/ Line Feed  \n#define MAXDATA 4096\n\n\n\/\/ Global Counts\nint usCnt = 0, secCnt = 0;\nint ServerNumber = 0;\n\n\/\/ Time Conversion\nconvertTime(char *timeEpoch, char *rtnBuf)\n{\n\tstruct tm *t;\n\ttime_t seconds;\n\tsscanf (timeEpoch, \"%ld\", &amp;seconds);\n\tt = localtime(&amp;seconds);\n\tsprintf(rtnBuf,\"%4d-%02d-%02d %02d:%02d:%02d\"\n\t\t, t-&gt;tm_year+1900\n\t\t, t-&gt;tm_mon+1\n\t\t, t-&gt;tm_mday\n\t\t, t-&gt;tm_hour\n\t\t, t-&gt;tm_min\n\t\t, t-&gt;tm_sec\n\t);\n}\n\nint main(int argc, char * *argv)\n{\n\tFILE *in_file, *out_file;  \/* input data file            *\/\n\t\/\/struct tm *t;\n\t\/\/time_t seconds;\n\tint i = 0;\n\tint vl_1 = 0;             \/* Used in for loop read in from OCC         *\/\n\tint vl_2 = 0;             \/* Used in for loop read in from OCC         *\/\n\tint c;                    \/* Used as a char to read in from OCC        *\/\n\t\/\/ Epoch is a sliding window. Once secondEpochEnd is set copy out the data \n\t\/\/ for the firstEpoch and commandText then change it to the firstEpoch\n\t\/\/ and look for the secondEpoch again.\n\t\/\/ #1234567890;text;#1234567890;\n\tint firstEpochStart = 0;\n\tint firstEpochEnd = 0;\n\tint secondEpochStart = 0;\n\tint secondEpochEnd = 0;\n\n\tchar buf1&#91;MAXDATA];\n\tchar hold_buf&#91;MAXDATA];\n\tchar commandText&#91;MAXDATA];\n\tchar commandTextBuf&#91;MAXDATA];\n\tchar epoch&#91;20];\n\tchar convertedEpoch&#91;20];\n\n\n\tif (argc != 2)\n\t{\n\t\tfprintf(stderr,\"Usage: HistoryConv FileName\\n\");\n\t\tfprintf(stderr,\"Usage: HistoryConv \/OZUS\/History\/odcapmdm00-bmcaddm-sh_history.20120415.015741.bmcaddm.bmcaddm.146.61.182.59.pts-3\\n\");\n\t\tfprintf(stderr,\"Usage: HistoryConv \/root\/.bash_history\\n\");\n\t\texit(1);\n\t}\n\n\t\/* Open the password file and read into x array the user names *\/\n\t\/\/strcpy(buf1,\"\/home\/infosec\/OZUS_Infosec\/TestText\");\n\t\/\/strcat(buf1,\"\/odcapmdm00-bmcaddm-sh_history.20120415.015741.bmcaddm.bmcaddm.146.61.182.59.pts-3\");\n\n\tmemset(&amp;buf1,'\\0', sizeof(buf1));\n\tstrcat(buf1,argv&#91;1]);\n\tif (!(in_file = fopen(buf1, \"r\"))) {\n\t\tprintf(\"error opening input file; %s\\n\", buf1);\n\t\texit(1);\n\t}\n\tprintf(\"\\n&lt;TABLE Border=\\\"2\\\" Width=\\\"100%\\\" Cellspacing=\\\"2\\\" Cellpadding=\\\"2\\\"&gt;&lt;TR&gt;&lt;TD width=\\\"15%\\\"&gt;******History_File******&lt;\/TD&gt; &lt;TD width=\\\"75%\\\"&gt; %s&lt;\/TD&gt;&lt;\/TR&gt;\\n\",buf1);\n\t\/*\n \t* Read in char-x-char to look for CTRL and NewLine to determin where the line breaks are\n \t* A line ends just after the EPOCH with a 0x0A or something like #df#\\n\n \t*\/\n\tvl_1 = 0;\n\tmemset(&amp;commandText,'\\0', sizeof(commandText));\n\tmemset(&amp;hold_buf,'\\0', sizeof(hold_buf));\n\n\t\/\/ Linux is based on Time on one Line then data on the Next\n\t\/\/ We need to trap from Epoch to Epoch and cut from the first Ephoc to the start of the second Epoch\n\t\/\/ and not loose any of the second Epoch\n\twhile (( c = getc(in_file)) != EOF)  \/* read in record *\/\n\t{\n\t\t\/\/ if for some reason we miss a counter reset everyting to keep the buffer from over flowing\n\t\tif((vl_1 &gt;= MAXDATA) || (vl_2 &gt;= MAXDATA))\n\t\t{\n\t\t\tprintf(\"Error MAXDATA: read exceeded %d chars for vl_1 %d or vl_2 %d.\\n File Name: %s\\n\",MAXDATA, vl_1, vl_2, buf1);\n\t\t\tfclose(in_file); \/\/ Close the input file\n\t\t\texit(2);\n\t\t}\n\n\t\t\/\/ Load the buffer on char at a time\n\t\thold_buf&#91;vl_1] = c; \n\t\t\/\/printf(\"VL= %d %s\\n\",vl_1, hold_buf);\n\n\t\t\/\/ Setting the buffer to NULL and the vl_1 counter to -1 for the else statment that does a vl_1++\n\t\t\/\/ this is a port way to make it Zero after the while loop\n\t\t\/\/ Conver to lower ascii value on the chart\n\t\t\/\/\n\t\t\/\/ Conver to a different char to test with\n\t\t\/\/if(hold_buf&#91;vl_1] == LF)\n\t\t\/\/{\n\t\t\/\/\t hold_buf&#91;vl_1] = ';'; \n\t\t\/\/}\n\n\t\t\/\/ Setting the buffer to NULL and the vl_1 counter to -1 for the else statment that does a vl_1++\n\t\t\/\/ this is a port way to make it Zero after the while loop\n\t\tif(hold_buf&#91;vl_1] == NUL)\n\t\t{\n\t\t\tmemset(&amp;hold_buf,'\\0', sizeof(hold_buf));\n\t\t\tvl_1 = -1;\n\t\t}\n\n\t\t\/\/ Linux Test for the start location of epoch Linux\n\t\t\/\/ Test for an Epoch. Should be on the first line read in if not clean up the file\n\t\t\/\/ #1234567890\\n\n\t\t\/\/ DATA DATA DATA DATA DATA \\n\n\t\t\/\/ #1234567890\\n\n\t\t\/\/ DATA DATA DATA DATA DATA \\n\n\t\t\/\/\n\t\t\/\/ Buff loaded from the while(c = getc(....\n\t\t\/\/ #   EPOCH  \\n    SOME DATA         \\n#   EPOCH  \\n\n\t\t\/\/ #1234567890;DATA DATA DATA DATA DATA;#1234567890;\n\t\t\/\/ |          |                        ||          |\n\t\t\/\/ 0         11                       x 0         11\n\t\t\/\/\n\t\t\/\/if(((strncmp(&amp;hold_buf&#91;0],\"#\", 1)) == 0) &amp;&amp; ((strncmp(&amp;hold_buf&#91;vl_1],\";\", 1)) == 0) &amp;&amp; (vl_1 &lt; 12))\n\t\tif(((strncmp(&amp;hold_buf&#91;0],\"#\", 1)) == 0) &amp;&amp; ((strncmp(&amp;hold_buf&#91;vl_1],\"\\n\", 1)) == 0) &amp;&amp; (vl_1 &lt; 12))\n\t\t{\n\t\t\tfirstEpochStart=vl_1 -11;\n\t\t\tfirstEpochEnd= vl_1 ;\n\t\t}\n\n\t\t\/\/ Linux Test for the end location of epoch and end of the record we want to capture\n\t\t\/\/if(((strncmp(&amp;hold_buf&#91;vl_1 -12],\";#\", 2)) == 0) &amp;&amp; ((strncmp(&amp;hold_buf&#91;vl_1],\";\", 1)) == 0) &amp;&amp; (vl_1 &gt; 13))\n\t\tif(((strncmp(&amp;hold_buf&#91;vl_1 -12],\"\\n#\", 2)) == 0) &amp;&amp; ((strncmp(&amp;hold_buf&#91;vl_1],\"\\n\", 1)) == 0) &amp;&amp; (vl_1 &gt; 13))\n\t\t{\n\t\t\tmemset(&amp;commandText,'\\0', sizeof(commandText));\n\t\t\tsecondEpochStart = vl_1 - 12;\n\t\t\tsecondEpochEnd = vl_1 -1;\n\t\t\tstrncpy(epoch,&amp;hold_buf&#91;firstEpochStart +1],firstEpochEnd);\n\t\t\tconvertTime(epoch,convertedEpoch);\n\t\t\tstrncpy(commandText,&amp;hold_buf&#91;firstEpochEnd + 1],secondEpochStart - firstEpochEnd - 1);\n\n\t\t\t\/\/ Setup the Epoch Window\n\t\t\tstrncpy(epoch,&amp;hold_buf&#91;secondEpochStart + 1],secondEpochEnd);\n\n\t\t\t\/\/ This is a good place to put a function call to parce the commandText for chars that cannot go into the database\n\t\t\t\/\/ or wrap escape chars around before the insert into the DB\n\t\t\t\/\/\n\t\t\t\/\/ Print to HTML\n\t\t\tprintf(\"&lt;TR&gt;&lt;TD&gt;%s&lt;\/TD&gt;&lt;TD&gt;%s&lt;\/TD&gt;&lt;\/TR&gt;\\n\",convertedEpoch,commandText);\n\n\t\t\t\/\/ Setup for DataBase by wrapping chars before Insert \n\t\t\tvl_2 = 0;\n\t\t\tmemset(&amp;commandTextBuf,'\\0', sizeof(commandTextBuf));\n\t\t\t for(i = 0; i&lt;= sizeof(commandText);i++)\n\t\t\t {\n\t\t\t\tcommandTextBuf&#91;vl_2] =  commandText&#91;i]; \n\n\t\t\t\tif(commandTextBuf&#91;vl_2] == '\\'')\n\t\t\t\t{\n\t\t\t\t\tcommandTextBuf&#91;vl_2] = '\\\\'; \n\t\t\t\t\tvl_2++;\n\t\t\t\t\tcommandTextBuf&#91;vl_2] = '\\''; \n\t\t\t\t\tvl_2++;\n\t\t\t\t}\n\t\t\t\telse if(commandTextBuf&#91;vl_2] == '\\\"')\n\t\t\t\t{\n\t\t\t\t\tcommandTextBuf&#91;vl_2] = '\\\\'; \n\t\t\t\t\tvl_2++;\n\t\t\t\t\tcommandTextBuf&#91;vl_2] = '\\\"'; \n\t\t\t\t\tvl_2++;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t\tvl_2++;\n\t\t\t}\n\t\t\t\/\/ Print to HTML\n\t\t\t\/\/printf(\"&lt;TR&gt;&lt;TD&gt;%s&lt;\/TD&gt;&lt;TD&gt;%s&lt;\/TD&gt;&lt;\/TR&gt;\\n\",convertedEpoch,commandTextBuf);\n\t\t\t\/\/ Insert into the DataBase\n\n\t\t\t\/\/ Reset all the buffers back to zero and keep reading in the next X chars.\n\t\t\tvl_2 = 0;\n\t\t\tmemset(&amp;commandText,'\\0', sizeof(commandText));\n\t\t\tmemset(&amp;hold_buf,'\\0', sizeof(hold_buf));\n\t\t\t\/\/ Now copy in the new Epoch Time from the window\n\t\t\tstrcpy(hold_buf,epoch);\n\t\t\tvl_1 = strlen(epoch);\n\t\t}\n\t\telse\n\t\t\tvl_1++;\n\t}\n\n\tfclose(in_file); \/\/ Close the input file\n\tprintf(\"&lt;\/TABLE&gt;\\n\");\n\texit(0);\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-178","page","type-page","status-publish"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/ozus.com\/index.php\/wp-json\/wp\/v2\/pages\/178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ozus.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/ozus.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/ozus.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ozus.com\/index.php\/wp-json\/wp\/v2\/comments?post=178"}],"version-history":[{"count":3,"href":"https:\/\/ozus.com\/index.php\/wp-json\/wp\/v2\/pages\/178\/revisions"}],"predecessor-version":[{"id":203,"href":"https:\/\/ozus.com\/index.php\/wp-json\/wp\/v2\/pages\/178\/revisions\/203"}],"wp:attachment":[{"href":"https:\/\/ozus.com\/index.php\/wp-json\/wp\/v2\/media?parent=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}