Unix程序设计:实现cp命令
2008-04-04 17:35
429 查看
最近苦读《Unix系统编程》便写了一些实例,逐步增加自己Unix程序设计的能力。
首先来实现一个Unix下常用命令:cp
先看代码:
#include
#include
#include
#define BUFSIZE 512
#define PERM 0755
/* copy file function */
int copyfile(const char *name1, const char *name2)
{
int infile, outfile;
ssize_t nread;
char buffer[BUFSIZE];
/* 打开源文件 */
if ((infile = open(name1, O_RDONLY)) == -1)
return (-1);
/* 打开目标文件 */
if ((outfile = open(name2, O_WRONLY|O_CREAT|O_TRUNC, PERM)) == -1)
{
close(infile);
return (-2);
}
/* 循环的把源文件写入目标文件 */
while ((nread = read(infile, buffer, BUFSIZE)) > 0)
{
if (write(outfile, buffer, nread) < nread)
{
close(infile);
close(outfile);
return (-3);
}
}
/* 关闭资源 */
close(infile);
close(outfile);
if (nread == -1)
return (-4);
else
return (0);
}
main(int argc, char *argv[])
{
/* 判断提交的参数 */
if (argc != 3) {
printf("Usage: copyfile /n");
exit(1);
}
char *file1, *file2;
file1 = argv[1];
file2 = argv[2];
int retcode;
/* 进行复制 */
retcode = copyfile(file1, file2);
/* 错误信息控制 */
if (retcode == -1) {
printf("Open %s failed/n", file1);
exit(1);
}
if (retcode == -2) {
printf("Open %s failed/n", file2);
exit(1);
}
if (retcode == -3) {
printf("Read %s buffer failed/n", file1);
exit(1);
}
if (retcode == -4) {
printf("Write %s buffer failed/n", file2);
exit(1);
}
if (retcode == 0) {
printf("Copy file succeed!/n");
}
}
保存为copyfile.c,然后使用gcc来编译:gcc -o copyfile copyfile.c
使用命令的格式是:copyfile
能够复制任何文件,不管是ASC还是二进制的。其实根本原理就是调用了三个Unix下的系统调用:open, read, write,完成基本的IO操作,既然不复杂,我就不解释了。
本代码再FreeBSD5.3下编译通过。
Author: heiyeluren
Date: 2005-08-02
首先来实现一个Unix下常用命令:cp
先看代码:
#include
#include
#include
#define BUFSIZE 512
#define PERM 0755
/* copy file function */
int copyfile(const char *name1, const char *name2)
{
int infile, outfile;
ssize_t nread;
char buffer[BUFSIZE];
/* 打开源文件 */
if ((infile = open(name1, O_RDONLY)) == -1)
return (-1);
/* 打开目标文件 */
if ((outfile = open(name2, O_WRONLY|O_CREAT|O_TRUNC, PERM)) == -1)
{
close(infile);
return (-2);
}
/* 循环的把源文件写入目标文件 */
while ((nread = read(infile, buffer, BUFSIZE)) > 0)
{
if (write(outfile, buffer, nread) < nread)
{
close(infile);
close(outfile);
return (-3);
}
}
/* 关闭资源 */
close(infile);
close(outfile);
if (nread == -1)
return (-4);
else
return (0);
}
main(int argc, char *argv[])
{
/* 判断提交的参数 */
if (argc != 3) {
printf("Usage: copyfile /n");
exit(1);
}
char *file1, *file2;
file1 = argv[1];
file2 = argv[2];
int retcode;
/* 进行复制 */
retcode = copyfile(file1, file2);
/* 错误信息控制 */
if (retcode == -1) {
printf("Open %s failed/n", file1);
exit(1);
}
if (retcode == -2) {
printf("Open %s failed/n", file2);
exit(1);
}
if (retcode == -3) {
printf("Read %s buffer failed/n", file1);
exit(1);
}
if (retcode == -4) {
printf("Write %s buffer failed/n", file2);
exit(1);
}
if (retcode == 0) {
printf("Copy file succeed!/n");
}
}
保存为copyfile.c,然后使用gcc来编译:gcc -o copyfile copyfile.c
使用命令的格式是:copyfile
能够复制任何文件,不管是ASC还是二进制的。其实根本原理就是调用了三个Unix下的系统调用:open, read, write,完成基本的IO操作,既然不复杂,我就不解释了。
本代码再FreeBSD5.3下编译通过。
Author: heiyeluren
Date: 2005-08-02
相关文章推荐
- Unix程序设计:实现cp命令
- Unix程序设计:实现cp命令
- Unix程序设计:实现wc命令
- Unix程序设计:实现wc命令
- 线程池与cp命令的实现
- linux cp 命令的实现
- C语言实现Linux cp命令
- linux下实现我自己简单的 cp 命令
- mmap 用mmap实现cp命令
- APUE习题4.6源代码----实现自己的简易 cp 命令
- 动手实现linux中的cp命令(可自行拓展)
- unix/linux more命令的实现
- 系统调用实现Linux命令 cp
- linux 的cp命令实现
- unix/linux who命令的实现
- Linux系统利用cp命令实现强制覆盖功能的方法
- unix/linux ls命令的实现
- 用Linux_C语言模拟CP命令,实现文件和文件夹的拷贝
- cp(1)命令的简单实现(一)
- 【Unix/Linux】【命令】【文件,目录操作工具】复制文件或目录——cp命令