-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsHelper.c
More file actions
69 lines (61 loc) · 1.74 KB
/
sHelper.c
File metadata and controls
69 lines (61 loc) · 1.74 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
/***********************************************************************
* FILENAME: sHelper.c
*
* DESCRIPTION:
* This file contains the sHelper function.
*
* AUTHORS: Matthew Derzay, CS Login: derzay
* Ian Mark, CS Login: imark
*
***********************************************************************/
#include "sHelper.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define BUF_SIZE 100
/**
* This function reteives the state of the process based on the pid
* given. Then concatenates the information onto the result string.
*/
void sHelper(char* pid, char* result) {
//Create file object.
FILE *file;
//Start of the filepath.
char filePath[] = "/proc/";
//Next word in the file.
char nextWord[BUF_SIZE];
//Index of the elements in the file.
int infoLine = 0;
//First letter of a file output.
char firstLetter;
//Last letter of a file output.
char lastLetter;
//Concatenate filepath with pid and status file to get full filepath.
strncat(filePath, pid, BUF_SIZE);
strncat(filePath, "/stat", BUF_SIZE);
//Open file, r means read only.
file = fopen(filePath, "r");
//Check if the file is open.
if(file == NULL) {
printf("File cannot be opened\n");
exit(0);
}
//Read through the stat file and find the state data.
while(fscanf(file, "%s", nextWord) != EOF) {
if(infoLine == 2) {
//Concatenate the data onto the result;
strncat(result, nextWord, BUF_SIZE);
strncat(result, " ", BUF_SIZE);
break;
}
//Set variable to chack if its a name with two words.
firstLetter = nextWord[0];
lastLetter = nextWord[strlen(nextWord) - 1];
if(firstLetter == '(' && lastLetter != ')') {
} else {
infoLine++;
}
}
fclose(file);
}