add systemcall for read open and close
+17 −9 · 3 files changed
-
mylib/include/myhash.h+1 −1 -
mylib/src/myhash.c+12 −6 -
mylib/test/t_hash.c+4 −2
@@ -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); @@ -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; + } } @@ -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