首页 > C/C++ > linux/freebsd 复制文件保存原有目录结构
2014
04-04

linux/freebsd 复制文件保存原有目录结构

由于有个需求复制文件保存原有目录结构
源文件:/usr/local/bin/python
目标文件:/tmp/usr/local/bin/python
而/tmp/usr/local/bin 这个目录是不在存在
cp 命令不存在这个功能

于是参考 stdc-file-copy.c 修改了一个版本可以达到这个需求。
http://www.csc.villanova.edu/~mdamian/C/stdc-file-copy.c

代码如下:
文件名:wcp.c

/*
 * stdc-file-copy.c - copy one file to a new location, possibly under a 
 * different name.
 */

#include <stdio.h>              /* standard input/output routines.    */
#include <libgen.h>             /* 64 bit system must include this file for dirname or core dump*/
#include <unistd.h>

#define MAX_LINE_LEN 1000	/* maximal line length supported.     */

int createdir(const char *filepath); /* create a multi-level directory */
/*
 * function: main. copy the given source file to the given target file.
 * input:    path to source file and path to target file.
 * output:   target file is being created with identical contents to
 *           source file.
 */
void
main(int argc, char* argv[])
{
    char* file_path_from;	/* path to source file.   */
    char* file_path_to;		/* path to target file.   */
    FILE* f_from;		/* stream of source file. */
    FILE* f_to;			/* stream of target file. */
    char buf[MAX_LINE_LEN+1];   /* input buffer.          */
    char *dirpath = NULL;

    /* read command line arguments */
    if (argc != 3 || !argv[1] || !argv[2]) {
	fprintf(stderr, "Usage: %s <source file path> <target file path>\n",
		argv[0]);
	exit(1);
    }
    file_path_from = argv[1];
    file_path_to = argv[2];

    /* create a multi-level directory */
    dirpath = dirname(file_path_to);
    if(createdir( dirpath ) == -1)
    {
        fprintf(stderr, "Cannot create a multi-level directory !! \n");
        perror("");
        exit(1);
    }

    /* open the source and the target files. */
    f_from = fopen(file_path_from, "r");
    if (!f_from) {
	fprintf(stderr, "Cannot open source file: ");
	perror("");
	exit(1);
    }
    f_to = fopen(file_path_to, "w+");
    if (!f_to) {
	fprintf(stderr, "Cannot open target file: ");
	perror("");
	exit(1);
    }

    /* copy source file to target file, line by line. */
    while (fgets(buf, MAX_LINE_LEN+1, f_from)) {
	if (fputs(buf, f_to) == EOF) {  /* error writing data */
	    fprintf(stderr, "Error writing to target file: ");
	    perror("");
	    exit(1);
	}
    }
    if (!feof(f_from)) { /* fgets failed _not_ due to encountering EOF */
        fprintf(stderr, "Error reading from source file: ");
        perror("");
        exit(1);
    }

    /* close source and target file streams. */
    if (fclose(f_from) == EOF) {
	fprintf(stderr, "Error when closing source file: ");
	perror("");
    }
    if (fclose(f_to) == EOF) {
	fprintf(stderr, "Error when closing target file: ");
	perror("");
    }
}

int createdir(const char *filepath)
{
    char buf[256];
    int i,len;    
    strcpy(buf, filepath);
    len = strlen(buf);
    if(buf[len-1]!='/')
            strcat(buf, "/");

    len = strlen(buf);

    for(i=1; i<len; i++)
    {
            if(buf[i]=='/')
            {
                    buf[i] = 0;
                    if( access(buf, F_OK)!=0 )
                    {
                            if(mkdir(buf, 0755)==-1)
                            {
                                    printf("mkdir error");
                                    return -1;
                            }
                    }
                    buf[i] = '/';
            }
    }

    return 0;
}

 

文件名:Makefile

PROG=   wcp
MAN=

NO_SHARED=

WARNS=  0

.include <bsd.prog.mk>

 

运行 make 命令就可以编译成功了.

使用

root@test:/tmp # ./wcp /usr/local/bin/python /tmp/usr/local/bin/python

 

最后编辑:
作者:dnybz
这个作者貌似有点懒,什么都没有留下。