cgname - collect file names from {e,f,}grep output

Warren Tucker wht at tridom.uucp
Sat Jun 24 08:21:16 AEST 1989


This quickie collects the filenames produced by grep output
while passing the output oin to stdout.  I wrote it to help
me build a list of filenames to edit containing a certain string:

usage: {e,f,}grep <pattern> <flist> | cgname <cfile> [| ...]\n");
collects list of files reported by grep into <cfile>\n");
cgname reads grep output, passing it on to cgname's stdout\n");
and produces a sorted list of filenames into <cfile>\n");

i.e.:

    fgrep BLAH *.[ch] | cgname /tmp/123 
    vi `cat /tmp/123`

{awk,sed,perl}-fanatic flames > /dev/null

/*+-------------------------------------------------------------------------
    cgname.c - pass thru {e,f,}grep output, collecting filenames
    ...!gatech!emory!tridom!wht
--------------------------------------------------------------------------*/
/*+:EDITS:*/
/*:06-23-1989-17:34-wht-creation */

#include <stdio.h>

#define ff fprintf
#define se stderr

FILE *fptmp;

#if defined(M_XENIX) | defined(USG) | defined(SYSV) /* etc */
#define Strchr strchr
#else
#define Strchr index    /* BSD */
#endif

char *Strchr();

/*+-------------------------------------------------------------------------
    collect_name(grepstr)
--------------------------------------------------------------------------*/
int
collect_name(grepstr)
register char *grepstr;
{
register char *cptr;

    if(!(cptr = Strchr(grepstr,':')))
        return(1);
    fwrite(grepstr,1,(int)(cptr - grepstr),fptmp);
    fputc('\n',fptmp);
    return(0);

}   /* end of collect_name */

/*+-------------------------------------------------------------------------
    main(argc,argv,envp)
--------------------------------------------------------------------------*/
main(argc,argv,envp)
int argc;
char **argv;
char **envp;
{
register found_output = 0;
char tmpname[32];
char s256[256];

    if((argc == 1) || (*argv[1] == '-'))
    {
        ff(se,"usage: {e,f,}grep <pattern> <flist> | cgname <cfile> [| ...]\n");
        ff(se,"collects list of files reported by grep into <cfile>\n");
        ff(se,"cgname reads grep output, passing it on to cgname's stdout\n");
        ff(se,"and produces a sorted list of filenames into <cfile>\n");
        exit(1);
    }

    sprintf(tmpname,"/tmp/c%05d.tmp",getpid());
    if((fptmp = fopen(tmpname,"w")) == NULL)
    {
        perror("cgname: cannot open temp file");
        exit(1);
    }

    while(fgets(s256,sizeof(s256),stdin))
    {
        if(collect_name(s256))
        {
            fputs(
            "cgname: missing filename from grep output (only 1 file?)\n",se);
            found_output = 0;
            break;
        }
        found_output = 1;
        fputs(s256,stdout);
    }
    fclose(fptmp);

    if(found_output)
    {
        sprintf(s256,"sort -u %s > %s",tmpname,argv[1]);
        system(s256);
    }

    unlink(tmpfile);
    exit(!found_output);
}   /* end of main */

/* end of cgname.c */
-- 
-------------------------------------------------------------------
Warren Tucker, Tridom Corporation       ...!gatech!emory!tridom!wht 
Sforzando (It., sfohr-tsahn'-doh).  A direction to perform the tone
or chord with special stress, or marked and sudden emphasis.



More information about the Alt.sources mailing list