NetSim Source Code Help
Loading...
Searching...
No Matches
Command.c
Go to the documentation of this file.
1#define _CRT_SECURE_NO_WARNINGS
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <Ws2tcpip.h>
6#include <windows.h>
7#include <stdbool.h>
8#include <signal.h>
9#include "main.h"
10#include "CLI.h"
11#include "CLIInterface.h"
12
14{
15
16 int count = 0;
17 int index = 0;
18 char **words = NULL;
19 char word[BUFSIZ];
20 memset(word, 0, BUFSIZ);
21
22 ptrCOMMANDARRAY array = calloc(1, sizeof* array);
23 array->originalCommand = _strdup(text);
24
25 while (*text)
26 {
27 if (*text == ' ')
28 {
29 count++;
30 index = 0;
31 if (words)
32 words = realloc(words, count * sizeof* words);
33 else
34 words = calloc(1, sizeof* words);
35 words[count - 1] = _strdup(word);
36 memset(word, 0, BUFSIZ);
37 }
38 else
39 {
40 word[index] = *text;
41 index++;
42 }
43 text++;
44 }
45
46 count++;
47 index = 0;
48 if (words)
49 words = realloc(words, count * sizeof* words);
50 else
51 words = calloc(1, sizeof* words);
52 words[count - 1] = _strdup(word);
53 memset(word, 0, BUFSIZ);
54
55 array->length = count;
56 array->commands = words;
57
58 return array;
59}
60
62{
63 for (int i = 0; i < c->length; i++)
64 free(c->commands[i]);
65 free(c->commands);
66 free(c->originalCommand);
67 free(c);
68}
69
71{
72 int i;
73 for (i = 0; i < c->length - 1; i++)
74 {
75 c->commands[i] = _strdup(c->commands[i + 1]);
76 }
77 free(c->commands[c->length - 1]);
78 c->commands[c->length - 1] = NULL;
79 c->length--;
80 return c;
81}
ptrCOMMANDARRAY get_commandArray(char *text)
Definition: Command.c:13
ptrCOMMANDARRAY remove_first_word_from_commandArray(ptrCOMMANDARRAY c)
Definition: Command.c:70
void free_commandArray(ptrCOMMANDARRAY c)
Definition: Command.c:61
#define c
#define _strdup(x)
Definition: Linux.h:126
#define _declspec(dllexport)
This function is used to trigger the update.
Definition: Linux.h:41
#define realloc(p, s)
Definition: Memory.h:32
#define free(p)
Definition: Memory.h:31
#define calloc(c, s)
Definition: Memory.h:29
char * originalCommand
Definition: CLIInterface.h:59