Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 1 Nov 2019 00:56:18 -0400
From:      Kurt Hackenberg <kh@panix.com>
To:        freebsd-questions@freebsd.org
Subject:   Re: grep for ascii nul
Message-ID:  <61f96c80-7965-9c80-30dc-b153e418b668@panix.com>
In-Reply-To: <558fd145-ad3e-90dc-5930-c01ca0c27d3c@panix.com>
References:  <20191101024817.GA60134@admin.sibptus.ru> <558fd145-ad3e-90dc-5930-c01ca0c27d3c@panix.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On 2019-11-01 00:44, Kurt Hackenberg wrote:

> You could use the attached program, which I wrote some time ago for this 
> purpose.

Huh. The attachment didn't come through. Does this list forbid attachments?

Anyway, here's the program inline. It's C.

----------------------------

#include <stdio.h>
#include <errno.h>
#include <string.h>

typedef int BOOL;
#define TRUE 1
#define FALSE 0


static void qerror(char *me, char *thing)
{
     char tmp[1024 + 255 + 2 + 1]; /* max pathname + max filename + junk 
+ NUL */

     sprintf(tmp, "%s: %s", me, thing);
     perror(tmp);
}

static BOOL doit(FILE *fin)
{
     int c;

     while ((c = getc(fin)) != EOF)
	if (c == '\0')
	    return TRUE;
     return FALSE;
}

int main(int argc, char *argv[], char *envp[])
{
     FILE *fin;
     char *me;

     if ((me = strrchr(*argv, '/')) != NULL)
	me++;
     else
	me = *argv;
     argc--, argv++;
     if (argc > 0)
	do
	{
	    if ((fin = fopen(*argv, "r")) != NULL)
	    {
		if (doit(fin))
		    printf("%s\n", *argv);
		if (fclose(fin) != 0)
		    qerror(me, *argv);
	    }
	    else
		qerror(me, *argv);
	} while (++argv, --argc > 0);
     else
	if (doit(stdin))
	    printf("<stdin>\n");

     return errno;
}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?61f96c80-7965-9c80-30dc-b153e418b668>