-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.c
More file actions
82 lines (66 loc) · 1.6 KB
/
Copy pathutilities.c
File metadata and controls
82 lines (66 loc) · 1.6 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
int myStrLen(char *input)
{
int count = 0;
int i = 0;
while (input[i] != '\0')
{
count++;
i++;
}
return count;
}
void insertPID(char* buffer, char* input)
{
char target[3] = "$$";
// get process id
int pidNum= getpid();
// convert pid to string
char* pidStr = malloc(sizeof(char) * 15);
memset(pidStr, '\0', 15);
sprintf(pidStr, "%d", pidNum);
// length of pid
int pidLen = myStrLen(pidStr);
char *foundPtr;
foundPtr = strstr(input, target);
// while $$ is still found in input string
int i = 0; // index for input string
int b = 0; // index for buffer string
while (foundPtr != NULL)
{
// copy chars from input to buffer until start of target is found
while (foundPtr != &input[i])
{
buffer[b] = input[i];
b++;
i++;
}
// target found, copy pid onto end of buffer
strcat(buffer, pidStr);
b += pidLen; // increment buffer ptr by length of pid
i += 2; // increment input ptr past the current $$
foundPtr = strstr(&input[i], target);
}
// no more $$ found, copy rest of input to buffer
strcat(buffer, &input[i]);
free(pidStr);
}
void freeArray(char* array[], int num)
{
int i;
for (i=0; i < num; i++)
{
if (array[i] != NULL)
{
free(array[i]);
array[i] = NULL;
}
}
}