[prev in list] [next in list] [prev in thread] [next in thread] 

List:       openbsd-tech
Subject:    Re: [patch] file(1) determine file type of stdin
From:       Sebastien Marie <semarie-openbsd () latrappe ! fr>
Date:       2015-05-30 2:59:43
Message-ID: 20150530025943.GH1400 () local
[Download RAW message or body]


> On Fri, May 29, 2015 at 11:31:37PM +0100, Nicholas Marriott wrote:
> > Hi
> > 
> > We deliberately do not support FIFOs because they will block forever if
> > nothing is writing and using file(1) on FIFOs seems unnecessary, the old
> > file(1) had a hack to get around this with poll(2) but we're not going
> > to do that. So I think we should only allow FIFOs for stdin where people
> > presumably know what they're doing, not generally:
> > 

that makes sens. your diff (the corrected one) is ok. 
thanks.
-- 
Sebastien Marie

On Fri, May 29, 2015 at 11:35:01PM +0100, Nicholas Marriott wrote:
> Oops, this was wrong, sorry - try this instead:
> 
> Index: file.1
> ===================================================================
> RCS file: /cvs/src/usr.bin/file/file.1,v
> retrieving revision 1.41
> diff -u -p -r1.41 file.1
> --- file.1	27 Apr 2015 11:12:49 -0000	1.41
> +++ file.1	29 May 2015 22:34:41 -0000
> @@ -74,6 +74,14 @@ or
>  .Em data
>  meaning anything else.
>  .Pp
> +If
> +.Ar file
> +is a single dash
> +.Pq Sq -
> +,
> +.Nm
> +reads from the standard input.
> +.Pp
>  The options are as follows:
>  .Bl -tag -width indent
>  .It Fl b
> Index: file.c
> ===================================================================
> RCS file: /cvs/src/usr.bin/file/file.c,v
> retrieving revision 1.44
> diff -u -p -r1.44 file.c
> --- file.c	29 May 2015 15:58:34 -0000	1.44
> +++ file.c	29 May 2015 22:34:41 -0000
> @@ -209,7 +209,13 @@ main(int argc, char **argv)
>  		memset(&msg, 0, sizeof msg);
>  		msg.idx = idx;
>  
> -		if (lstat(argv[idx], &msg.sb) == -1) {
> +		if (strcmp(argv[idx], "-") == 0) {
> +			if (fstat(STDIN_FILENO, &msg.sb) == -1) {
> +				fd = -1;
> +				msg.error = errno;
> +			} else
> +				fd = STDIN_FILENO;
> +		} else if (lstat(argv[idx], &msg.sb) == -1) {
>  			fd = -1;
>  			msg.error = errno;
>  		} else {
> @@ -441,8 +447,11 @@ try_stat(struct input_file *inf)
>  		    strerror(inf->msg->error));
>  		return (1);
>  	}
> -	if (sflag) {
> +	if (sflag || strcmp(inf->path, "-") == 0) {
>  		switch (inf->msg->sb.st_mode & S_IFMT) {
> +		case S_IFIFO:
> +			if (strcmp(inf->path, "-") != 0)
> +				break;
>  		case S_IFBLK:
>  		case S_IFCHR:
>  		case S_IFREG:
> @@ -617,7 +626,10 @@ test_file(struct input_file *inf, size_t
>  	if (bflag)
>  		printf("%s\n", inf->result);
>  	else {
> -		xasprintf(&label, "%s:", inf->path);
> +		if (strcmp(inf->path, "-") == 0)
> +			xasprintf(&label, "/dev/stdin:");
> +		else
> +			xasprintf(&label, "%s:", inf->path);
>  		printf("%-*s %s\n", (int)width, label, inf->result);
>  		free(label);
>  	}
> 
> 
> 

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic