/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*    mod [files]                                                       *
*                                                                      *
* This program "modifies" each of its args in  such  a  way  that  all *
* known  tests will say that they are different than they were before, *
* although the actual changes are (hopefully ;-) insignificant.        *
*                                                                      *
* At present, the technique used is to append white stuff to  the  end *
* of the file.  This should fool diff and relink.                      *
*                                                                      *
* If  there  are  no files named on the command line, we copy stdin to *
* stdout and append the new stuff.                                     *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "V.h"
#include "sys_fcntl.h"

char buf[] = "\t\n";	/* What to add */
int  l   = 2;
char*file  = 0;	/* File name */
int  files = 0;	/* Files processed */
int  mods  = 0;	/* Files modified */

main(ac,av)
	char**av;
{	int   r=0;	/* Return value */
	int   a, c, f, i;
	int   c0, c1;
	ac = Vinit(ac,av);
	for (a=1; a<ac; a++) {
		switch(c0=av[a][0]) {
		  case '-':
		  case '+':
			switch (c1=av[a][1]) {
			  case 'd':
			  case 'D':
				Vopt(av[a]+2);
				break;
			  default:
				P1 "Option \"%s\" unknown, ignored.",av[a] D;
				break;
			}
			break;
		  default:
			++files;		/* Count even the files that fail */
			file = av[a];
			V3 "File %d \"%s\"",files,file D;
			if ((f = Open(file,O_RDWR,0)) >= 0) {
				V3 "File %d=\"%s\" opened.",f,file D;
				if ((i=Seek(f,0,2)) >= 0) {	/* Is the file seekable? */
					V4 "Seek %d=EOF",i D;
					if ((i = Write(f,buf,l)) > 0) {	/* Can we append? */
						V3 "%d bytes appended to \"%s\"",i,file D;
						++mods;
						V2 "Modified: \"%s\"",file D;
					} else {
						V3 "Can't write \"%s\" [Err %d=%s]",file,Erreason D;
						++r;
					}
				} else {
					V3 "Can't seek \"%s\" [Err %d=%s]",file,Erreason D;
					++r;
				}
				Close(f);
			} else {
				P1 "%s: Can't open \"%s\" [Err %d=%s]",pname,file,Erreason D;
				++r;
			}
			file = 0;
			break;
		}
		V3 "Files: %d mods: %d",files,mods D;
	}
	if (files <= 0) {	/* No files on command line */
		V3 "Copy stdin to stdout ..." D;
		while ((c = getchar()) != EOF)
			putchar(c);
		puts(buf);
		++files;
		++mods;
	}
	V2 "Files: %d mods: %d at end.",files,mods D;
	exit(r);
}
