1. ----------------------------------- getc is for FILE pointers, not file descriptors, putc is for I/O, not copy. grade = 0 int readline (int fd, char * buf, int nbytes) { int c; int numbytes = 0; while (c = getc(&fd)){ putc(c, buf); numbytes++; if (c == -1){ /* error occurred */ return -1; } if ((numbytes == nbytes - 1) && (c != '\n')){ return -1; /* Max bytes read w/o newline */ } if (c == EOF){ /* End-of-File occurred */ if (numbytes == 0){ return 0; } else{ return -1; } } if (c == '\n'){ /* Newline occurred */ putc('\0', buf); return numbytes; } } } 2. ------------------------------- c should be an int, not a char It is not really needed in this program. grade = 9 int readline(int fd, char *buf, int nbytes){ int i; int c; int n; for(i=0;i 0)) { if(*current == '\n') { *(current+1)=0x0; /* Insert zero terminator */ return nbytes - remaining_bytes; /* bytes read */ } current++; remaining_bytes--; } if(result < 1) { /* EOF or error */ if(result == -1) /* error */ return -1; /* EOF */ if(remaining_bytes == nbytes-1) /* Nothing read yet */ return 0; return -1; /* last line not terminated with \n */ } /* otherwise, we've read in nbytes-1, and have not gotten * a \n yet. This is an error. */ return -1; } 5. -------------------------- OK, but p really isn't necessary. grade = 10 int readline(int fd, char *buf, int nbytes){ int n, bytesRead = 0; char *p = buf; while(bytesRead < nbytes && (n = read(fd, p, sizeof(char))) > 0){ bytesRead += n; if(*p == '\n'){ buf[bytesRead] = '\0'; /*putting in a null terminator*/ return bytesRead; } p++; } /*Check if there was a read error*/ if(n < 0) return -1; /*EOF and no bytes read*/ if(bytesRead == 0) return 0; /*EOF or read nbytes - 1 and no newline char*/ return -1; } 6. ------------------------- OK grade = 10 int readline(int fd,char *buf,int nbytes) { char ch; int read_stat; int bytes_read=0; while(bytes_read 1) break; /* A newline symbol has been read. */ else if (c == '\n') break; *buf++ = c; } *buf=0; return i; } 9. ---------------------------- OK. Do not use C++ commnet syntax grade = 10 int readline(int fd,char *buf,int nbytes) { int i,j; for(i= 0;i< nbytes-1;i++) //read upto nbytes-1 { if((j=read(fd, buf+i ,1))==1) { if(*(buf+i)=='\n') //new-line character { *(buf+i+1)='\0'; return i+1; //since we started from i=0 } } else if(j == -1) //An error occured return -1; else if(j==0) //end of file { if(i==0) return 0; else return -1; } } //end of for loop return -1; //nbytes-1 has read } 10. -------------------------------- Do not use perror in this &buf[i] is buf+i always returns 0 on EOF returns value 1 too small on normal lines grade = 0 int readline(int fd, char *buf, int nbytes) { int count = 0, i; if(fd < 0) { perror("read error\n"); return -1; } else { for(i = 0; i < nbytes; i++) { count = read(fd,&buf[i],sizeof(char)); if(buf[i] == '\n'){ buf[i++] = '\0'; return i; } else if(count == 0){ return 0;} } } return -1; } 11. -------------------------------- OK, do not use C++ commnet syntax grade = 10 int readline(int fd, char * buf, int nbytes){ int bytes_read = 0; // number of bytes read and buf index int read_value; // read return value //read upto nbytes - 1, error, or eof from fd while( bytes_read < nbytes - 1 && (read_value = read(fd,&buf[bytes_read],1)) > 0){ bytes_read++; //handle newline if(buf[bytes_read - 1] == '\n'){ buf[bytes_read] = '\0'; return bytes_read; } } // read error if(read_value == -1) return -1; // eof and zero bytes read if(bytes_read == 0) return 0; // eof with bytes read or no newline return -1; } 12. ------------------------------- Same as 11 int readline(int fd, char * buf, int nbytes){ int bytes_read = 0; // number of bytes read and buf index int read_value; // read return value //read upto nbytes - 1, error, or eof from fd while( bytes_read < nbytes - 1 && (read_value = read(fd,&buf[bytes_read],1)) > 0){ bytes_read++; //handle newline if(buf[bytes_read - 1] == '\n'){ buf[bytes_read] = '\0'; return bytes_read; } } // read error if(read_value == -1) return -1; // eof and zero bytes read if(bytes_read == 0) return 0; // eof with bytes read or no newline return -1; } 13. -------------------------------- Returns value one too small on normal line. grade = 5 int readline(int fd, char *buf, int nbytes){ int i; int c; int n; for(i=0;i 0) return -1; if(ctr ==0) return 0; } // End of if temp == 0 if (*(buf1+ctr) == '\n'){ *(buf1+ctr+1) = '\0'; return ctr; } ctr++; } // End of while loop } // End of readline 15. ----------------------------- Braces do not match up Do not call perror from readline grade = 0 int readline(int fd, char *buf, int nbytes){ char tempbuf; int bytesread = 0; int seennl = 0; int read_result; //note, bytes read starts at 0 for indexing puroses... //bytesread+1 is how many bytes have actually be read for other uses while(bytesread < nbytes){ read_result = read(fd, &tempbuf, 1); //check for read error if(read_result == -1){ perror("Unable to read file"); return -1; } //check for EOF if(read_result == 0){ printf("EOF FOUND\n"); if(bytesread){ return -1; } else{ return 0; } } //check for newline character if(tempbuf == '\n'){ //if we see a newline buf[bytesread] = tempbuf; buf[bytesread+1] = '\0'; printf("found newline\n"); seennl = 1; break; } // we havent seen a newline yet due to the previous if statement if((bytesread+1) == (nbytes-1)){ printf("ERROR: Read too much\n"); break; } printf("Successful read: %c\n", tempbuf); buf[bytesread] = tempbuf; bytesread++; } if(!seennl){ printf("Last char read: %c\n", tempbuf); buf[bytesread] = '\0'; //fix the last character return -1; //read too much, prevent buffer overflow } else{ buf[bytesread+1] = '\0'; printf("buf: %s", buf); return bytesread; } } 16. ------------------------ OK grade = 10 int readline(int fd, char *buf, int nbytes) { int echeck, counter = 0; while (counter < nbytes - 1) { if ((echeck = read(fd, buf + counter, sizeof(char))) > 0) { if (*(buf + counter) == '\n') { *(buf + counter + 1) = '\0'; return ++counter; } } else /* echeck = 0 or -1 */ { if ((echeck == 0) && (counter == 0)) { return 0; } return -1; } counter ++; }/* End of While Loop */ return -1; } 17. ------------------------- Put only on declartion per line Does not handle EOF before newline grade = 5 int readline(int fd, char * buf, int nbytes){ int bytesRead, i = 0, totalBytesRead = 0, nl = 0; while((bytesRead = read(fd, buf+i, sizeof(char))) > 0){ i++; if((totalBytesRead += bytesRead) >= nbytes - 1) return -1; if(buf[i - 1] == '\n'){ buf[i] = '\0'; nl = 1; break; } } if(bytesRead == -1 && !nl) return -1; else return totalBytesRead; } 18. ------------------------- You were asked to write readline, not readLine grade = 10 int readLine(int fd, char *buf, int nbytes) { int i, bytesread, val; bytesread = 0; /* goes through for nbytes - 1, if not encountered \n by nbytes - 1 it will return a - 1 */ for (i=0; i < nbytes; i++) { val = read(fd, buf+i, 1); /* If read returns an error return -1 */ if (val == -1) { return -1; } /* Check for EOF */ if (val == 0) { /* Check to see if any bytes have been read yet */ if (bytesread == 0) { return 0; } else { return -1; } } /* This increments before a check for \n. So bytesread includes \n as a byte */ if (val == 1) { bytesread = bytesread + 1; } if (buf[i] == '\n') { buf[i+1] = '\0'; return bytesread; } } return -1; } 19. -------------------------- Falls off end without returning a value grade = 9 int readline(int fd, char *buf, int nbytes) { int i=0, ret; char temp[1]; while(i < (nbytes-1) && (ret=read(fd,temp,sizeof(char))) > 0 ) { buf[i]=temp[0]; if(temp[0] == '\n') { buf[++i]='\0'; return(i);/* returns the number of bytes read*/ } i=i+1; } /* if either error or (EOF and no bytes read)*/ if(ret == -1 || (ret == 0 && i == 0)) return(ret); /* if EOF and bytes have been read or nbytes-1 bytes have been read without receiving a newline*/ if((ret == 0 && i > 0) || i == (nbytes-1) ) return(-1); } 20. ------------------------------------ ferror is for FILE pointers, not file descriptors Compares a string to a character in buf[n++]=="\n" grade = 0 int readline(int fd,char *buf,int nbytes){ int n=0; char x; if(ferror(fd)) return -1; while(!feof(fd)&&n 0) { /* If a newline is read, insert the newline in the buffer, followed by a \0 and return the number of bytes read. */ if (buf[i] == '\n') { buf[i+1] = '\0'; return readbytes; } } } /* If nbytes-1 bytes have been read without receiving a \n, return -1. */ printf("\nnbytes-1\n"); return -1; } 22. ---------------------------- Returns value one too small on normal lines grade = 5 int readline (int fd, char *buf, int nbytes){ char ch; int noofbytes = 0; int retvalue; char *chptr; chptr = &ch; while(noofbytes < (nbytes - 1)){ retvalue = read(fd, chptr, 1); if(retvalue == -1){ return (-1); } else if(retvalue == 0){ if(noofbytes == 0){ return (0); } else{ return (-1); } } else if(*chptr == '\n'){ *buf = *chptr; buf++; *buf = '\0'; return (noofbytes); } else{ *buf = *chptr; buf++; noofbytes++; } } return (-1); } 23. ------------------------------- Missing final brace Do not use C++ comment syntax grade = 9 int readline(int fd, char *buf, int nbytes){ int mybytesread=0; int check=0; while(mybytesread n) { buf[n] = '\0'; return n-1; } /* nbytes-1 bytes read without newline or string overflow */ if ( (nbytes-1) == n ) return -1; } } 26. ---------------------------------- Returns value one too large on normal line. grade = 5 int readline(int fd, char *buf, int nbytes) { int bytes, byteCt = 0; char temp; while( (bytes = read(fd, &temp, sizeof(char))) ) { if (bytes == -1) { perror("Read error"); return -1; } if (temp == '\n') { if ( byteCt < (nbytes-1) ) { buf[byteCt++] = '\n'; buf[byteCt++] = '\0'; return byteCt; } } if ( byteCt == (nbytes-1) ) { buf[byteCt++] = temp; return -1; } buf[byteCt++] = temp; } if (byteCt == 0) return 0; else return -1; } 27. -------------------------------- temp is not initialized returns wrong value on normal line How can read_bytes by < 0? grade = 0 int readline(int fd, char *buf, int nbytes){ int read_bytes = 0; char *temp; int n, num_bytes = 0; while((n = read(fd, temp, 1)) >= 0){ if(n == 0 && num_bytes == 0){ return 0; } if(n == 0 && num_bytes != 0){ return -1; } if(*temp == '\n'){ buf[read_bytes] = '\n'; read_bytes++; buf[read_bytes] = '\0'; return read_bytes -1; } if(num_bytes == nbytes -1){ return -1; } if(*temp != '\n'){ buf[read_bytes] = temp[0]; read_bytes++; num_bytes += n; } } if(read_bytes < 0){ return -1; } return read_bytes; } 28. ------------------------------ Always returns 0 on end of file char temp[1] may show lack of understanding grade = 5 int readline( int fd, char *buf, int nbytes ){ int i = 0; int c = 0; char temp[1]; while( i-1 != nbytes ){ c = read(fd, temp, 1); buf[i] = temp[0]; if((c == -1)||(c == 0)){ printf("Reached an EOF or EORROR\n"); return c; } if(buf[i++] == '\n'){ buf[i] = '\0'; return i; } } printf("Reached the nbytes-1 without a \\n\n"); return -1; } 29. ----------------------------- Need extra set of () in while fgets is for FILE pointers, not file descriptors Do not use C++ comment syntax grade = 0 int readline(int fd, char *buf, int nbytes) { int i=0; // Current position in buf int c; // Character read in from file /* Loop and read characters until we get an EOF or exceed nbytes-1 */ while (c=fgetc(fd)!=EOF && i0) return -1; /*If an error occurs, return -1 */ else return -1; } 30. ------------------------------ Must read one bytes at a timeA returns wrong value on normal line Always returns 0 on EOF grade = 0 int readline(int fd, char * buf, int nbytes) { int bytesread; while (bytesread = read(fd, buf, (nbytes - 1))) { if (bytesread == -1) { return -1; } if (bytesread > 0) { if (buf[bytesread] == '\n') { buf[bytesread + 1] = '\0'; return bytesread; } } } if (bytesread == 0) return 0; return -1; } 31. ---------------------------- Always returns 0 on EOF grade = 5 int readline( int fd, char *buf, int nbytes ){ int i = 0; int c = 0; char temp[1]; while( i-1 != nbytes ){ c = read(fd, temp, 1); buf[i] = temp[0]; if((c == -1)||(c == 0)){ return c; } if(buf[i++] == '\n'){ buf[i] = '\0'; return i; } } return -1; } #include 32. -------------------------- Does not check return value on read Compares a pointer, buf, to an integer, EOF grade = 0 int readline(int fd, char *buf, int nbytes){ int n=0; if ( read(fd,buf,1)){ n++; if (*buf == '\n'){ buf++; *buf='\0'; return n; } if (buf == EOF) { if (n == 0) return 0; else return -1; } if (n == (nbytes-1)) return -1; buf++; } else return -1; } 33. ------------------------------ Always returns 0 on EOF grade = 5 int readline( int fd, char *buf, int nbytes ){ int i = 0; int c = 0; char temp[1]; while( i-1 != nbytes ){ c = read(fd, temp, 1); if((c == -1)||(c == 0)){ return c; } buf[i] = temp[0]; if(buf[i++] == '\n'){ buf[i] = '\0'; return i; } } return -1; } 34. -------------------------- Do not compare an integer (fd) to a pointer (NULL) Do not use fgetc with file descriptors Return value of fgetc is an int not a char grade = 0 int readline(int fd, char *buf, int nbytes) { int count=0; char temp; if (fd==NULL) /* File not found */ return (-1); else if ((temp=fgetc(fd))==EOF) /* EOF read as 1st char */ return(0); else { buf[0]=temp; while (((temp=fgetc(fd)) != EOF) && (temp!='\n') && (count!=(nbytes-1))) { count++; buf[count]=temp; } if ( count==(nbytes-1) ) /* Read more than nbytes-1 bytes */ return (-1); else if ( temp == EOF ) /* EOF read without newline */ return(-1); else if ( temp == '\n' ) { buf[count+1]='\0'; /* \n read returning number of bytes read */ return (count+1); } } } 35. -------------------------- Uses file descriptor 0 instead of fd Do not do other I/O in readline, no write statements. Always reads into the smae location at the start of buf. grade = 0 int readline(int fd, char *buf, int nbytes){ int n, bytesRead =0; while((n = read(0,buf,nbytes))>0){ write(1,buf,n); if(buf[bytesRead]=='\n'){ bytesRead++; buf[bytesRead]='\0'; return bytesRead; } bytesRead++; } if(n==-1) return -1; if(bytesRead==0) return 0; return -1; } 36. -------------------------- Use the name readline, instead of readLine. More complicated than necessary Fall off end without returning a value Too much indentation grade = 9 int readLine(int fd, char * buf, int nbytes) { int bytesread; /* bytes read */ int tbytesread; /* total bytes read */ bytesread = 0; tbytesread = 0; while(tbytesread != nbytes) { bytesread = read(fd, buf, 1); tbytesread += bytesread; /* checks for error */ if(bytesread < 0) { perror("read error"); close(fd); return -1; } /* checks for EOF - End Of File */ if(bytesread == 0) { if(tbytesread == 0) return 0; else return -1; } /* inserts string terminator after a newline character read and return number of bytes read */ if(bytesread != 0 && * buf == '\n') { * (buf + 1) = '\0'; return tbytesread; } /* checks for size of buffer equals to nbytes - 2 before inserting newline character and string terminator */ if(tbytesread == nbytes - 2 && buf[nbytes - 2] != '\n') return -1; /* increments pointer to buffer */ buf++; } } 37. ------------------------------- Returns a value one too small on normal lines Falls off end without returning a value grade = 5 int readline(int fd, char *buf, int nbytes){ int k,bytesread; for(k=0;k 0 && c == 'EOF') return -1; if(c == '\n') { putc(('\0'), stdout); return n_read; } if((n_read == nbytes - 1) && c != '\n') return -1; putc((c), buf); } return -1; } 39. ------------------------------- Declaration of buffer is invalid in C Do not do output in readline grade = 0 int readline(int fd, char *buf, int nbytes) { int bytes; char buffer[nbytes]; int sum=0; while ((bytes=read(fd, buffer,1)) >0) { sum=sum+1; if (buffer[0] =='\n' && sum <=nbytes) { buf[sum-1]=buffer[0]; buf[sum]='\0'; printf("buf= %s\n",buf); return sum; } else if (sum==nbytes-1 && buffer[0] != '\n') return -1; else buf[sum-1]=buffer[0]; } if (bytes == 0 && sum == 0) return 0; else return -1; } 40. ----------------------------- readbytes is not initialized. Cannot determine if tests are correct. grade = 0 int readline(int fd, char *buf, int nbytes) { int readbytes; /* bytes read so far */ int n, i; for (i = 0; i < nbytes-1; i++) { readbytes = readbytes + 1; n = read(fd, buf + i, 1); if (n == -1) { return -1; } if (n == 0) { if (readbytes == 0) { return 0; } else return -1; } if (n > 0) { if (buf[i] == '\n') { buf[i+1] = '\0'; return readbytes; } } } /* If nbytes-1 bytes have been read without receiving \n, return -1.*/ return -1; } 41. ------------------------------ i is not used bytesRead, exit=0 is dubious bytesread is not initialized Does not stop reading at nbytes-1 Do not do output from readline grade = 0 int readline(int fd, char *buf, int nbytes){ int bytesRead, exit, i; bytesRead, exit = 0; while( read(fd,buf,1) > 0 || !exit || bytesRead!=(nbytes-1) ){ bytesRead++; if( buf[bytesRead] == '\n' ) exit ++; } if(exit){ buf[bytesRead] = '\n'; buf[bytesRead+1] = '\0'; printf("%i\n", bytesRead); } switch(bytesRead){ case -1: return -1; case 0: return 0; default: return -1; } } 42. ------------------------------ Keep non-UNIX includes out of the programs grade = 10 #ifdef WIN32 // Visual Studio.NET uses WIN32 and NOT __WIN32__ #include #else #include #endif int readline(int fd, char *buf, int nbytes){ char * curBufPtr = buf; int numBytes = 0; while( numBytes < nbytes ){ switch( read(fd,curBufPtr,1) ){ case -1: return -1; case 0: if(numBytes == 0) return 0; else return -1; default: numBytes++; if(curBufPtr[0] == '\n'){ curBufPtr[1] = '\0'; return numBytes; } curBufPtr++; } } return -1; } 43. ------------------------------- c is not initialized EOF is not a character There is not end-of-file character in UNIX grade = 0 int readline(int fd, char *buf, int nbytes){ int n=0; char *c; if ( read(fd,c,1)){ n++; if (*c == '\n'){ buf[n]=*c; buf[n+1]='\0'; return n; } if (c == EOF) { if (n == 0) return 0; else return -1; } if (n == (nbytes-1)) return -1; buf[n]=*c; } else return -1; } 44. ------------------------------------ Returns a value one too small for normal lines grade = 5 int readline(int fd, char *buf, int nbytes) { int bytesread = 0; int bytecounter = 0; char *bufindex = buf; while((bytesread = read(fd, bufindex, 1))>0) { if(*bufindex == '\n') { bufindex++; *bufindex = '\0'; return bytecounter; } if(bytecounter == nbytes-1) { return -1; } bytecounter += bytesread; bufindex++; } /********************************************* * * If the file is empty, then return 0. * If a newline occurs before end of file, * or if there was a read error, return -1 * ********************************************/ if(bytecounter == 0) { return 0; } return -1; } 45. ---------------------------------- if ('\n') and if(EOF) are always true Must read only one byte at a time grade = 0 int readline(int fd, char *buf, int nbytes){ int n; while((n = read(fd, buf, nbytes)) > 0 ){ if(n == -1){ return -1; //return -1 on error } else if('\n'){ buf = strcat(buf,"\n\0"); printf(buf); return n + 1; //return bytes read after seeing a newline } else if(EOF){ if(n == 0) return 0; //return 0 if no bytes read after EOF else return -1; } else if(n == nbytes - 1){ return -1; } } } 46. ---------------------------------- Do not use C++ comment syntax Falls off end without returning a value grade = 9 int readline(int fd, char *buf, int nbytes){ int numByte = 0; //----> used to keep track of number of bytes read int readCheck = 0; int i = 0; for(i = 0; i <= nbytes; i++){ readCheck = read(fd, buf+i, 1); if ( readCheck <= 0 && numByte == 0) // no bytes have been read // if an end of file occurs // -if no bytes have been read yet, return 0 // -otherwise, return -1 return readCheck; numByte++; //if a newline is the first charcacter read if (buf[0] == '\n'){ buf[1] = '\0'; return numByte; } // if an error occurs return -1 if (readCheck == -1){ return -1; } // if an end of file occurs // -otherwise, return -1 if (readCheck == 0){ return -1; } // if a newline is read, // 1. insert the newline into the buffer // 2. followed by a string terminator // return number of bytes read if (buf[i] == '\n'){ if (numByte <= nbytes - 1){ // leave room for ... buf[i] = '\n'; // ... newline ... buf[i+1] = '\0'; // ... string terminator return numByte; } } // if nbytes-1 bytes have been read without // receiving a newline // return -1 if (numByte == nbytes-1){ return -1; } } } 47. ----------------------------------- Sometimes uses readbyte and sometimes readbytes Do not do output from readline There is no OEF character in UNIX grade = 0 int readline(int fd, char *buf, int nbytes) { int i,byteread; char temp[1]; for (i=0;i=0) { if(n==0) return 0; else if(n==nbytes-1 && buf[nbytes-1]!='\0') return -1; else return n; } return -1; } 51. ----------------------------------- MAXBYTES not defined Do not exit from a function Do not output from readline grade = 0 int readline(int fd,char *buf, int nbytes) { int numbytes =0; int count =0; char *bufpointer =buf; if(MAXBYTES < nbytes) { perror("buf0) { if(*bufpointer == '\n') { bufpointer++; *bufpointer = '\0'; return count; } if(count == nbytes-1) { perror("nbytes-1 bytes read without receiving a newline\n"); return -1; } count += numbytes; bufpointer++; } if(count == 0) { printf("EOF no bytes read yet\n"); return 0; } if(count > 0) { printf("EOF bytes occured and bytes have been read \n"); return -1; } if(numbytes<0) { perror("error/read"); return -1; } return -1; } 52. --------------------------------- flagNewLine is not used Do not output from readline Has buffer overflaw grade = 0 int readline(int fd, char *buf, int nbytes){ int i, n, temp=-1; int flagNewline = 0; while((n=read(fd, buf, nbytes))>0){ if (n<0){ perror("Read error\n"); return(-1); } else temp = n; } for (i=0; i <= nbytes; i++){ if (i == (nbytes-1)) return (-1); if (buf[i] == '\n'){ buf[i+1] = '\0'; break; } } if (temp==1 && buf[0]=='\n') return (0); else if(temp==1 && buf[0]!='\n') return (-1); else return (temp); } 53. ----------------------------------- 54. ----------------------------------- Return value is one too small on normal line grade = 5 int readline(int fd, char *buf, int nbytes) { int bytecount = 0; int n; char *bufptr = buf; while ( (n = read(fd, bufptr, 1)) > 0 ){ if (*bufptr == '\n') { /* newline */ bufptr++; *bufptr = '\0'; return bytecount; } if (bytecount == nbytes-1) { return -1; } bytecount += n; bufptr++; } if (n < 0) { /* read error */ return -1; } if (bytecount == 0) { /* empty file */ return 0; } if (bytecount > 0) { /* eof */ return -1; } return -1; /* default */ } 55. ------------------------------- Not a readline function while(!EOF||!'\n'); is meaningless grade = 0 main() { int fd; int r; int c; char buf[nbytes]; fd=open("hello.c",O_RDONLY); if(fd == -1) { printf("file does not exist"); exit(1); } do { r=read (fd,buf,nbytes); c++; } while(!EOF||!'\n'); close(fd); printf("%s\n",buf); } 56. ----------------------------- '\n' is not a string so cannot be used with strncmp Why use strncmp to compare two characters What is the meaning of if(buf)? grade = 0 int readline(int fd, char *buf, int nbytes){ int n, numbytes; char *c; numbytes = 0; c = buf; while(numbytes < nbytes-1){ while((n = read(fd, buf, 1)) > 0){ numbytes++; if(strncmp(c,'\n',1)==0){ c++; *c = '\0'; return numbytes; } c++; } if(n==0){ if(buf) return -1; return 0; } } if(buf) return -1; return 0; } 57. ------------------------------- No necessary to put anything in buf if returning 0 or -1 grade = 10 int readline(int fd, char *buf, int nbytes) { char byte; int point, numread; for ( point = 0; point < nbytes-1; point++) { if ((numread = read(fd,&byte,1)) < 0) return -1; if ( numread == 0 ) { if (point == 0) { buf[point] = '\0'; return 0; } return -1; } buf[point] = byte; if (byte == '\n') { buf[++point] = '\0'; return point; } } buf[point] = '\0'; return -1; } 58. ----------------------------- Do not convert file descriptors to file pointers. File pointer I/O has side effects Does not put in the newline grade = 0 int readline(int fd, char *buf, int nbytes) { int n = 0, c; FILE *f = fdopen(fd, "r"); if(!f) return -1; while( (c=fgetc(f)) != EOF && c != '\n' && n < nbytes-1) { buf[n++] = c; } if(c==EOF && !n) /* EOF but no chars read */ { fclose(f); return 0; } else if(c=='\n') /* newline found */ { buf[n] = 0; fclose(f); return n; } else /* EOF with chars read or no newline */ { fclose(f); return -1; } } 59. ---------------------------------- Must read one byte at a time grade = 0 int readline(int fin, char *buff, int nbytes) { int ch; int bytesread =3D 0; int count =3D 0; while ((ch =3D read(fin, buff, 1) >0) && (count < nbytes)) { count++; /* putchar(ch); */ } } 60. --------------------------------- Must read one byte at a time grade = 0 int readline(int fin, char *buff, int nbytes) { int ch; int bytesread = 0; int count = 0; while ((ch = read(fin, buff, 1) >0) && (count < nbytes)) { count++; /* putchar(ch); */ } } 61. ----------------------------------- Two break statements are never reached Returns a value one too small for normal lines grade = 5 int readline(int fd,char *buf,int nbytes){ int k; for (k=0;k