grep for ascii nul

Kurt Hackenberg kh at panix.com
Fri Nov 1 04:56:26 UTC 2019


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;
}


More information about the freebsd-questions mailing list