← cproj

add systemcall for read open and close

8169183cb55cec5168e0b64b6ead5474e283fce5 · Ovidiu · Jun 28, 2019 17:07

+17 −9 · 3 files changed

  • mylib/include/myhash.h +1 −1
  • mylib/src/myhash.c +12 −6
  • mylib/test/t_hash.c +4 −2
diff --git a/mylib/include/myhash.h b/mylib/include/myhash.h
index b46942f..879ef49 100644
--- a/mylib/include/myhash.h
+++ b/mylib/include/myhash.h
@@ -23,5 +23,5 @@ extern HashTable *h_create ( unsigned int size );
 extern HashNode *h_search (HashTable *hTable, char *key);
 extern void h_delete (HashTable *hTable, char *key);
 extern int openFile(const char *fn);
-extern int readFile(int fd);
+void readFile(int fd);
 extern int closefile(int fd);
diff --git a/mylib/src/myhash.c b/mylib/src/myhash.c
index b398bba..0440e39 100644
--- a/mylib/src/myhash.c
+++ b/mylib/src/myhash.c
@@ -193,24 +193,30 @@ int openFile (const char *fn) {
 		return inputFd;
 }
 
-int readFile (int fd) {
+void readFile (int fd) {
 	// Read bytes from fd int numRead for read () 3rd arg
-	ssize_t countRead;
+	int countRead;
 
 	// Output file descriptor
 	int outputFd = 1; // stdout
 
 	// Set buffer for void *buf arg
 	char buf[BUFF];
+	// read (fd provided by openFile, buffer,  )
 
-	while ((countRead = read (fd, buf, BUFF)) > 0)
+	while ( (countRead = read (fd, buf, BUFF)) > 0)
 		//empty buffer
 			if (write(outputFd, buf, countRead) != countRead)
 				fprintf(stderr,"Could not write whole buffer.\n");
 }
 
-int closeFile(fd){
-	if ( close(fd) == -1)
+int closeFile(int fd){
+	int closeFd;
+	closeFd = close(fd);
+	if ( closeFd == -1)
 			perror("Error on close\n");
-	return 0;
+	else if(closeFd == 0) {
+		fprintf(stderr, "Closed file descriptior\n");
+		return closeFd;
+	}
 }
diff --git a/mylib/test/t_hash.c b/mylib/test/t_hash.c
index 391ea5c..540671c 100644
--- a/mylib/test/t_hash.c
+++ b/mylib/test/t_hash.c
@@ -65,8 +65,10 @@ int main(int argc, char** argv){
 	int x = openFile("../testdata");
 	fprintf(stderr,"%d\n",x);
 	// read the file
-	int r = closefile(x);
-	fprintf(stderr,"%d\n",r);
+	readFile(x);
+	// close the file
+	int r = closeFile(x);
+	printf(stderr,"%d\n",r);
 
 
 #if 0