From 579b27fb8e16228f75cb6da50ac91ba83c08ef11 Mon Sep 17 00:00:00 2001 From: TriantaTV Date: Fri, 31 Mar 2023 00:05:28 -0500 Subject: [PATCH] Initial commit --- Memo | Bin 0 -> 16832 bytes Memo.c | 138 +++++++++++++++++++++++++++++++++++++++++++++ MemoOutputTest.txt | 0 TestMemo.sh | 9 +++ memo.history | Bin 0 -> 5200 bytes 5 files changed, 147 insertions(+) create mode 100755 Memo create mode 100755 Memo.c create mode 100644 MemoOutputTest.txt create mode 100755 TestMemo.sh create mode 100644 memo.history diff --git a/Memo b/Memo new file mode 100755 index 0000000000000000000000000000000000000000..18c1d7b916aaefe2445e9f2e15ca226f6f8dd83d GIT binary patch literal 16832 zcmeHOeQ;dWb-ycHpcsFw!4Ad**btk6q*^3nJ2H%8$@1FIjKA>5)M*RPEA5lCWj~bN zx3SFF#EfjJ9a&Rlnn^>_Ce92=n1&K3O(|sp$dFhelTdj=Q+L{Ql+<*jS`Z^bB7*Ve zckVss`R(&A$)x>@*{j(-_x#SsJ@?#u-@g0w?x$i~Hr9ngO3EztF-6??LW8u5;7+M1 z)v8+5Jbd4zZd6wQo{di{i-Yt`oe>CaV7*zOWVeJW73c>Htb|I0M9HpNS@J$35vsz5 zC%by8G9RRiIWVCj_u7k)F-k;#do;!U6Y964-0W>M`g_-w)2G;OgsIpOOLjeM*TZ%~ z&r?7sLM5M+6Z*7pK7+K0j8KFI?RwZQNH-WWC6wBrvg;K$@rSgxgYA|@v>O$q2Mw%* zN_+Rfj`H&R78&N9++MYDc#QKel+&wjPbE86ue?2#XiOzDg}%nVmeq}`S4Q*M=n5SI z#TCJa=G3NbdlcM^5XNa#b%zZv{z$fn<)r_GN3MHzJbV9m{_(Z{wEc+(T0Z|5f0-m5 zikoamhYI*;v^fy$&Z>@sU zG_K^wt%9$vf*-Ad{}gxxpJ2HcK&5tXu7clP1;3{XzPAcagHWm6Dc})&f+YliV!Eu@ zc4uGQ?n-9jspJEWvTaPvPCM_$bFQ6^Co`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gwJ&D)`sNM`Ip-bp~U?6ij$!$%dypTr&cJbS%#u*B2S$2uIhJSpZ( zNl=G_-IB~W?OEJN@R^e`H9CJ%2AH!aWsq&-w1~_tI!03QY)JV5nQ{j-Z}Ch!e<1C~ zJ3!r>p*>Q=^mB5(Dw@f zIFLaThPpW;`J|J}C$pI{#)d5Cq~c`2wceDgqB_hds?qK&SgPcmP8D^XKCrY-p`F!J zH|q58%IrzNn{dX5S)#^5>S!vSPQsZn#~7<9t_?J~yo=HQp+ftT5I(qE>O$8+Dneyn zVo;+ib)p}_hqeG>-@^7nn@Gu5`qwq3Djt>B=)jD5-Pvv62IsPeGvAv_Yz+AUqa=+B^bX1FoX)_RO}<9h$3`E zb8^=4^8WudYjLQe$j!(zvDObnTdozQ;{*vq+Zp|^U8F%HB|+&&ku zm-}j=@?1mogZZO<4CPP8Uqx7x>CMav3EKY%0>Aw-wi8PGIe!V-{|AJA`{NO#E%b*Z z^c9&u0=diz5;ZyeMYUg+zvUEs5q{ph|%fGDD8xJ$g???6C9B<;&SQO=Lc zIdNVVo&!^kM4)@j@@CFdtvPe8~{iWv`61&kf-@~g6 a-4zf@yP`|WG@znI|J`CVmIe$0toki99SU;* literal 0 HcmV?d00001 diff --git a/Memo.c b/Memo.c new file mode 100755 index 0000000..c532fc7 --- /dev/null +++ b/Memo.c @@ -0,0 +1,138 @@ +#include +#include +#include +#include + +typedef struct MemoHistory{ + int lineNumber; + char command[100]; +}MemoHistory; + +void AddHistoryData(char command[100]); +bool CheckArgumentSet(int argc, char* argv[], char argumentLetter); +void GetCommandNumber(MemoHistory* historyData, int commandNumber); +int GetHistoryLineCount(); +void Memo(int argc, char* argv[]); +FILE* OpenHistoryFile(char* openType); +void PrintHistory(); +void RunCommandNumber(char* command); +void TestHistoryData(); + +int main(int argc, char* argv[]) +{ + if (argc == 1) + PrintHistory(); + if (argc > 1) + Memo(argc, argv); + return 0; +} + +void AddHistoryData(char command[100]) +{ + FILE* historyFile = OpenHistoryFile("a"); + MemoHistory historyData; + strcpy(historyData.command, command); + historyData.lineNumber = GetHistoryLineCount() + 1; + fwrite(&historyData, sizeof(MemoHistory), 1, historyFile); + return; +} + + +// Check if an argument is set +// Returns true if found, false if argument not found +bool CheckArgumentSet(int argc, char* argv[], char argumentLetter) +{ + char argumentHyphen = '-'; + for (int i = 1; i < argc; i++) + if ((*(argv[i]) == argumentHyphen) && (*(argv[i]+1) == argumentLetter)) + return 1; + return 0; +} + +// Get numbered command from memo.history +void GetCommandNumber(MemoHistory* historyData, int commandNumber) +{ + FILE* historyFile = OpenHistoryFile("r"); + while(fread(historyData, sizeof(MemoHistory), 1, historyFile)) + if (historyData->lineNumber == commandNumber) + break; + fclose(historyFile); + return; +} + +// Get the highest line number in the history file and returns it +int GetHistoryLineCount() +{ + FILE* historyFile = OpenHistoryFile("r"); + MemoHistory historyData; + historyData.lineNumber = 0; + while(fread(&historyData, sizeof(MemoHistory), 1, historyFile)) + ; + fclose(historyFile); + return historyData.lineNumber; +} + +// Base Memo function +// Runs command then puts command in memo.history +void Memo(int argc, char* argv[]) +{ + if (CheckArgumentSet(argc, argv, 'e')) + { + RunCommandNumber(argv[2]); + return; + } + char commandInput[100]; + for (int i = 1; i < argc; i++) + { + strcat(commandInput, argv[i]); + strcat(commandInput, " "); + } + system(commandInput); + AddHistoryData(commandInput); +} + +// Safely opens memo.history +// Returns true if successful, false if unsuccessful +FILE* OpenHistoryFile(char* openType) +{ + FILE* historyFile; + if ((historyFile = fopen("memo.history", openType)) == NULL) + { + perror("memo.history"); + exit(EXIT_FAILURE); + } + return historyFile; +} + +// Prints data from memo.history file +void PrintHistory() +{ + FILE* historyFile = OpenHistoryFile("r"); + MemoHistory historyData; + while(fread(&historyData, sizeof(MemoHistory), 1, historyFile)) + printf("%2d %s\n", historyData.lineNumber, historyData.command); + fclose(historyFile); +} + +// Run numbered command from memo.history +void RunCommandNumber(char* command) +{ + MemoHistory historyData; + int commandNumber = 0; + sscanf(command, "%d", &commandNumber); + GetCommandNumber(&historyData, commandNumber); + system(historyData.command); +} + +void TestHistoryData() +{ + MemoHistory historyData; + FILE* historyFile = OpenHistoryFile("w"); + for (int i = 1; i < 11; i++) + { + historyData.lineNumber = i; + strcpy(historyData.command, "echo hello world"); + fwrite(&historyData, sizeof(MemoHistory), 1, historyFile); + } + fclose(historyFile); +} diff --git a/MemoOutputTest.txt b/MemoOutputTest.txt new file mode 100644 index 0000000..e69de29 diff --git a/TestMemo.sh b/TestMemo.sh new file mode 100755 index 0000000..fa6de49 --- /dev/null +++ b/TestMemo.sh @@ -0,0 +1,9 @@ +#!/bin/bash +: > MemoOutputTest.txt +gcc -o Memo Memo.c +echo Testing history printing... +./Memo +echo Testing command... +./Memo echo hello +echo Testing reusing number... +./Memo -e 3 diff --git a/memo.history b/memo.history new file mode 100644 index 0000000000000000000000000000000000000000..ad82e8c126269afa30112c23c6fb3d68d9e53ec5 GIT binary patch literal 5200 zcmbW*drVVT9Ki8wu}~ES0imKo#eoP4ZFLh}3>D{tY&M3+K;33n?5w=0M!?~OP6gZ` zU71iF(N;8QET%r zd1pl<&wNy%TO{q*lTO?4o3D_=9n)_wF#F%EfzDX?J}PHJt)_OFuZ(mILu2m^L+8mb zE@+5t*a00jt<$|cu{wK@OFELm&lAx+n!UK7C#dHLbR20Ni=hQ1My@9v>k^d@nkO(E z7qHTDGN9u`>vXhsz7@+eNQWQq&PMx9z7`j#w?A_HlwmluPI=2VTxc`3kUR_rf=OY zAcwOX7f1th7ei+Pt;37j@k*E%PCBlSLL$-eS@jqfs23aca346+I-&i&veJx9($W6? z$qKZun_uIC)53B$IL|~{=O>f0Ld!c$I_cV=U1*+TI$U7ZY$}5DOrmwRc_lcV+1W)p z{15W`(eaTl#09LPoll|TLhD=B=CfQ~Dz!xq0>SF(I2 z=`a(T&FJ`qIpYE)`%nRNCeu1~tubXYPE949bV01jex3?(LB!8jJmEfYqjef%)E#Mg z-lU^#PriWmL9+!Hs6)pN!g<_j9Zr?V_h#80(y1R}-$(nvHjE2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