[英]How to copy the directory from one place to another using C language?
I have searched online but didn't a solution.我在网上搜索过,但没有解决方案。 I want to copy the directories/folders from one place to another using C language.
我想使用 C 语言将目录/文件夹从一个地方复制到另一个地方。 Although it could be possible using
cp
in command line but I want to implement that in C language.虽然可以在命令行中使用
cp
但我想用 C 语言来实现它。
To copy a folder, including all files and subfolders inside, you need to iterate over the content of the folder, and for each "entry" check if it's a file or folder要复制文件夹,包括其中的所有文件和子文件夹,您需要遍历文件夹的内容,并为每个“条目”检查它是文件还是文件夹
To iterate over the content of the folder you can use readdir
from dirent.h
, to check if an entry is a folder or file lstat
and S_ISDIR
from sys/stat.h
, and to create folder mkdir
from sys/stat.h
.要遍历文件夹的内容,您可以使用
dirent.h
中的readdir
,检查条目是否是文件夹或文件lstat
和sys/stat.h
S_ISDIR
sys/stat.h
夹mkdir
。
This needs some polishing (it ought to handle a full path for a target dir, should think about edge cases, etc.), but this is a reasonable start:这需要一些修饰(它应该处理目标目录的完整路径,应该考虑边缘情况等),但这是一个合理的开始:
/* Recursively duplicate a directory tree */
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
static DIR * xopendir(const char *);
static void xclosedir(DIR *, const char *);
static void xstat(const char *, struct stat *);
static void xrealpath(const char *s, char *resolved);
static void pathcat(char *p, char *n);
struct dir { DIR *d; char *path, *end; };
static void
build_dir(char *spath, char *dpath, char *name, mode_t mod)
{
struct dirent *f;
struct dir src;
struct dir dst;
src.end = strchr(spath, '\0');
dst.end = strchr(dpath, '\0');
pathcat(dpath, name);
if( mkdir(dpath, mod) == -1 && errno != EEXIST ){
err(EXIT_FAILURE, "%s", name);
}
if( getenv("V") ){
printf("%s -> %s\n", spath, dpath);
}
src.d = xopendir(src.path = spath);
while( (f = readdir(src.d)) != NULL ) {
if( !strcmp(f->d_name, ".") || !strcmp(f->d_name, "..") ){
continue;
}
if( f->d_type == DT_DIR ){
struct stat b;
pathcat(spath, f->d_name);
xstat(spath, &b);
build_dir(spath, dpath, f->d_name, b.st_mode);
*src.end = '\0';
}
}
*src.end = '\0';
*dst.end = '\0';
xclosedir(src.d, spath);
}
int
main(int argc, char **argv)
{
if( argc < 2 ){
errx(EXIT_FAILURE, "src directory missing");
}
if( argc < 3 ){
errx(EXIT_FAILURE, "target directory missing");
}
char spath[PATH_MAX];
char dpath[PATH_MAX];
xrealpath(argv[1], spath);
for( argv += 2; *argv; argv++ ){
char *name = *argv;
if( *name == '/' ){
dpath[0] = '\0';
} else {
getcwd(dpath, sizeof dpath);
}
build_dir(spath, dpath, name, 0777);
}
return 0;
}
static void
xrealpath(const char *s, char *resolved)
{
if( realpath(s, resolved) == NULL ){
err(EXIT_FAILURE, "%s", s);
}
}
static DIR *
xopendir(const char *path)
{
DIR *d = opendir(path);
if( d == NULL ) {
err(EXIT_FAILURE, "%s", path);
}
return d;
}
static void
xstat(const char *path, struct stat *buf)
{
if( stat(path, buf) ){
err(EXIT_FAILURE, "stat %s", path);
}
}
static void
xclosedir(DIR *d, const char *path)
{
if( closedir(d) ) {
err(EXIT_FAILURE, "%s", path);
}
}
static void
pathcat(char *p, char *n)
{
if( snprintf(p, PATH_MAX, "%s/%s", p, n) > PATH_MAX - 1 ){
errx(1, "path too long");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.