Re: read() returns positive numbers in BSD systems, which make: bash / valid.

2015-04-19 Thread Chet Ramey
On 4/17/15 2:43 PM, Eduardo A. Bustamante López wrote:
> In the BSDs, doing a read on a directory doesn't return a negative value.

Thanks for the investigation and report.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



read() returns positive numbers in BSD systems, which make: bash / valid.

2015-04-17 Thread Eduardo A . Bustamante López
In the BSDs, doing a read on a directory doesn't return a negative value.

Bash assumes that you can't do reads on directories here:

1501   /* Only do this with non-tty file descriptors we can seek on. */
1502   if (fd_is_tty == 0 && (lseek (fd, 0L, 1) != -1))
1503 {
1504   /* Check to see if the `file' in `bash file' is a binary file
1505  according to the same tests done by execute_simple_command (),
1506  and report an error and exit if it is. */
1507   sample_len = read (fd, sample, sizeof (sample));
1508   if (sample_len < 0)
1509 {
1510   e = errno;
1511   if ((fstat (fd, &sb) == 0) && S_ISDIR (sb.st_mode))
1512 internal_error (_("%s: is a directory"), filename);
1513   else
1514 {
1515   errno = e;
1516   file_error (filename);
1517 }
"shell.c" 1925 lines --78%--

It needs to check if the argument passed is a directory before this part.

Please see:

Linux

dualbus@yaqui ~ % cat > readtest.c
#include 
#include 
#include 
#include 
int main() {
ssize_t nbytes;
int fd;
char buffer[16];
fd = open("/", O_RDONLY);
nbytes = read(fd, buffer, 16);
printf("%d %d\n", fd, nbytes);
return 0;
}
dualbus@yaqui ~ % gcc -o readtest readtest.c
dualbus@yaqui ~ % ./readtest 
3 -1

NetBSD

[dual...@faeroes.sdf.org]$ cat readtest.c   
#include   
#include   
#include   
#include   
int main() {  
ssize_t nbytes;  
int fd;  
char buffer[16];  
fd = open("/", O_RDONLY);  
nbytes = read(fd, buffer, 16);  
printf("%d %d\n", fd, nbytes);  
return 0;  
}  
[dual...@faeroes.sdf.org]$ gcc -o readtest readtest.c
[dual...@faeroes.sdf.org]$ ./readtest  
3 16  

execute_cmd.c doesn't seem to be affected by this, only shell.c. This breaks
one of the tests that specify that:

dualbus@yaqui ~ % bash /
/: /: is a directory

Should be returned in this case.

-- 
Eduardo Bustamante
https://dualbus.me/