h07280
s 00068/00000/00000
d D 1.1 82/08/02 16:19:24 cecily 1 0
e
u
U
t
T
I 1
#include <stdio.h>


main(argc,argv)
int argc;
char *argv[];
{
	int i;
	static int force = 1;
	static int nerr = 0;
	
	for(i = 1 ; i < argc ; ++i)
		if( strcmp(argv[i], "-c") )
			nerr += touch(force, argv[i]);
		else
			force = 0;
	return(nerr);
}


#include <sys/types.h>
#include <sys/stat.h>


touch(force, name)
int force;
char *name;
{
	struct stat stbuff;
	char junk[1];
	int fd;
	
	if( stat(name,&stbuff) < 0)
		if(force)
			goto create;
		else {
			fprintf(stderr, "touch: file %s does not exist.\n", name);
			return(1);
		}

	if(stbuff.st_size == 0)
		goto create;
	
	if( (fd = open(name, 2)) < 0)
		goto bad;
	
	if( read(fd, junk, 1) < 1){
		close(fd);
		goto bad;
	}
	lseek(fd, 0L, 0);
	if( write(fd, junk, 1) < 1 ){
		close(fd);
		goto bad;
	}
	close(fd);
	return(0);
	
create:
	if( (fd = creat(name, 0666)) < 0)
		goto bad;
	close(fd);
	return(0);

bad:
	fprintf(stderr, "Cannot touch %s\n", name);
	return(1);
}
E 1
