-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyscalls.c
More file actions
133 lines (116 loc) · 4.81 KB
/
syscalls.c
File metadata and controls
133 lines (116 loc) · 4.81 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* hashTab Supporting Functions
* Program 2 assignment
* CSE109 Fall 2021
* student ID (e.g., idk321): jwk324
* student Last name: Keane
* student First name: Jack
* gitHub ID: @jackwkeane
*/
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h> /* for fork, pipe */
/* Defining the debug macro from the make file */
#ifdef DEBUG
#define DEBUG 1
#else
#define DEBUG 0
#endif
/* Malloc Function in the form of Steven-styled wrap */
/* in: bytes to allocate; return: ptr of type void to allocated memory */
void *Malloc(size_t numBytes) {
void *ptr = NULL;
if ((ptr = malloc(numBytes)) == NULL) {
fprintf(stderr, "Could not allocate space - %s", strerror(errno));
exit(errno);
}
return ptr; /* Functions of type void * call for a pointer to be returned */
}
/* Fopen Function in the form of Steven-styled wrap */
/* in: path/file, mode; return: file ptr */
FILE *Fopen(const char *path, const char *mode) {
FILE *fp = NULL;
if((fp = fopen(path, mode)) == NULL){
fprintf(stderr, "fopen error (%d): %s\n", errno, strerror(errno)); /* Formatted print taken from program directions */
exit(errno);
}
return fp;
}
/* Fread Function in the form of Steven-styled wrap */
/* in: buffer ptr, buffer size, num buffers, input file ptr; return: bytes read */
size_t Fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t num_items = 0;
if((num_items = fread(ptr, size, nmemb, stream)) != nmemb) { /* If statement to check to see if the return value differs from the nmemb parameter, therefore causing an error or meaning the function has reached the end of file. */
if(!feof(stream) && ferror(stream)) { /* If statement that if true means that fread returned an error */
fprintf(stderr, "fread error (%d): %s\n", errno, strerror(errno)); /* Formatted print taken from program directions */
exit(errno);
}
}
return num_items; /* Returns the totla number of elements successfully read */
}
/* Fclose Function in the form of Steven-styled wrap */
/* in: file ptr */
void Fclose(FILE *stream) {
if(fclose(stream)) { /* Checks to see if the stream returns EOF */
fprintf(stderr, "fclose error (%d): %s\n", errno, strerror(errno)); /* Formatted print taken from program directions */
exit(errno);
}
}
/* Fwrite Function in the form of Steven-styled wrap */
/* in: buffer ptr, buffer size, num buffers, output file ptr; return: bytes written */
size_t Fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t num_items;
if((num_items = fwrite(ptr, size, nmemb, stream)) != nmemb) { /* If statement to check to see if the return value differs from the nmemb parameter, therefore causing an error */
fprintf(stderr, "fwrite error (%d): %s\n", errno, strerror(errno)); /* Formatted print taken from program directions */
exit(errno);
}
return num_items; /* Returns the totla number of elements successfully returned as size_t object */
}
/* in: int array, index 0 will be read end, index 1 will be write end out: returns 0 on success, returns -1 on error.*/
int Pipe(int filedes[]) {
if (pipe(filedes) == -1) {
fprintf(stderr, "pipe error (%d): %s\n", errno, strerror(errno));
exit(errno);
} else {
return pipe(filedes);
}
}
/* in: void out: returns a new process */
// pid_t Fork() {
// pid_t pid;
// if ((pid = fork()) == -1) { /* create process */
// fprintf(stderr, "fork error (%d): %s\n", errno, strerror(errno));
// exit(errno);
// } else {
// return pid;
// }
// }
/* FUNCTIONS FROM THE LAB */
ssize_t Read(int fildes, void *buf, size_t nbyte) {
ssize_t numBytes;
if ((numBytes = read(fildes, buf, nbyte)) == 0) { /* read in a string from the pipe */
fprintf(stderr, "PARENT: read error (%d): %s\n", errno, strerror(errno));
exit(errno);
} else {
return numBytes; /* If no error, normal return. */
}
}
ssize_t Write(int fildes, const void *buf, size_t nbyte) {
ssize_t numBytes;
if ((numBytes = write(fildes, buf, nbyte)) == -1) { /* send "string" down the pipe */
fprintf(stderr, "CHILD: write error (%d): %s\n", errno, strerror(errno));
exit(errno);
} else {
return numBytes; /* If no error, normal return. */
}
}
int Close(int fildes) {
if (close(fildes) == -1) { /* close unneeded "write" end of pipe */
fprintf(stderr, "PARENT: close error (%d): %s\n", errno, strerror(errno));
exit(errno);
} else {
return close(fildes); /* If no error, normal return. */
}
}