x_cp.c,v

来自「TCP-IP红宝书源代码」· C,V 代码 · 共 71 行

C,V
71
字号
head	1.1;
access;
symbols;
locks
	dls:1.1; strict;
comment	@ * @;


1.1
date	97.09.21.19.29.03;	author dls;	state Dist;
branches;
next	;


desc
@@


1.1
log
@pre-3e code
@
text
@/* x_cp.c - x_cp */

#include <conf.h>
#include <kernel.h>

LOCAL	char	errfmt[] = "Cannot open %s\n";

/*------------------------------------------------------------------------
 *  x_cp  -  (copy command) copy one file to another
 *------------------------------------------------------------------------
 */
COMMAND	x_cp(stdin, stdout, stderr, nargs, args)
int	stdin, stdout, stderr, nargs;
char	*args[];
{
	char	*buf;
	int	from, to;
	int	ret;
	int	len;

	if (nargs != 3) {
		fprintf(stderr, "usage: cp file1 file2\n");
		return(SYSERR);
	}
	if ( (from=open(NAMESPACE, args[1], "ro")) == SYSERR) {
		fprintf(stderr, errfmt, args[1]);
		return(SYSERR);
	}
	if ( (to=open(NAMESPACE, args[2], "w")) == SYSERR) {
		close(from);
		fprintf(stderr, errfmt, args[2]);
		return(SYSERR);
	}
	if ( ((int) (buf = (char *)getmem(512)) ) == SYSERR) {
		fprintf(stderr, "no memory\n");
		ret = SYSERR;
	} else {
		while ( (len = read(from, buf, 512)) > 0 )
			write(to, buf, len);
		freemem(buf, 512);
		ret = OK;
	}
	close(from);
	close(to);
	return(ret);
}
@

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?