-
Notifications
You must be signed in to change notification settings - Fork 0
/
kerncall.c
91 lines (80 loc) · 2.06 KB
/
kerncall.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
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
#ifndef KERNCALL
#define KERNCALL
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include "student.h"
#include "log.c"
struct kerncall {
char ch; // 'u' = updateFile 's' = searchFile;
int id; // id to update/search
int newRoomNo; // room number to update
};
void writeToPipe(int fd, char ch, int id, int newRoomNo)
{
struct kerncall k = {0};
k.ch = ch;
k.id = id;
k.newRoomNo = newRoomNo;
write(fd, &k, sizeof(k));
}
struct Student searchFile(int id, int flag)
{
FILE* db = fopen("disk", "r");
if (db == NULL)
{
perror("Error opening database file");
exit(0);
}
struct Student temp;
while(fread(&temp, 1, sizeof(struct Student), db) && sizeof(temp) == sizeof(struct Student))
{
if (temp.id == id)
{
if (flag == 0)
{
// printf("%d, %s, %s, %s, %d, %s, Year %d\n", temp.id, temp.name,
// temp.hostel, temp.course, temp.roomNumber, temp.dob, temp.yearOfStudy);
}
fclose(db);
return temp;
}
}
// printf("NOT FOUND!!\n");
fclose(db);
temp.id = -1;
return temp;
}
// Update the file.
void updateFile(int id, int newRoomNumber)
{
/*
*
* Implement the logic for updating here.
* First search file, but instead of returning a number, get the file pointer.
* Use fseek to reposition the file pointer. The number of bytes will be similar.
* And write the new struct there.
*
*/
FILE* db = fopen("disk", "r+");
if (db == NULL)
{
perror("Error opening database file");
exit(0);
}
struct Student temp;
while(fread(&temp, 1, sizeof(struct Student), db) && sizeof(temp) == sizeof(struct Student))
{
if (temp.id == id)
{
temp.roomNumber = newRoomNumber;
fseek(db, -sizeof(struct Student), SEEK_CUR);
fwrite(&temp, 1, sizeof(struct Student), db);
fclose(db);
return;
}
}
fclose(db);
return;
}
#endif