forked from piuma/limit-fs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
shellfunctions.c
46 lines (38 loc) · 1.3 KB
/
shellfunctions.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <bits/types/FILE.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "shellfunctions.h"
char* getShellCommandOutput(char command[])
{
//This functions sends a command to the system shell and returns the output
FILE *file; // Pointer to get the output channel of the command
char *buff; //Buffer to get the output
buff = (char *) malloc(sizeof(char) *1024);
file = popen(command, "r");
char *output;
output = (char *) malloc(sizeof(char) * 1024);
memset(output, '\0', strlen(output)); //Reset the string
while (fgets(buff, sizeof(buff)-1, file) != NULL)
strcat(output, buff); //Keep getting from the bugger while it isn't null
//And append it to the output string
pclose(file);
free(buff);
return output;
}
void removeLogDuplicates(char * filepath)
{
//This function removes the duplicate lines from the Log.txt file
char *command = malloc(sizeof(char)*1024);
/*
* The full command is oldSortedFile=`uniq "Log.txt"`; echo "$oldSortedFile" > "Log.txt"
* strcat is used to add the filepath
*/
strcat(command, "oldSortedFile=`uniq \"");
strcat(command, filepath);
strcat(command, "\"`; echo \"$oldSortedFile\" > \"");
strcat(command, filepath);
strcat(command, "\"");
system(command);
free(command);
}