📄 fileopt.c
字号:
/*
* Some tools
* Copyright (C) Bank of communication & Fujian Start Ltd 1997
* All Rights Reserved
* Last Modified by Lcm. 1997.11.15 ShangHai
*/
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "mytools.h"
/*
* Move source_file to target_file.
*/
int _move(char *source_file, char *target_file)
{
unlink(target_file);
if (link(source_file, target_file) < 0) {
errcall(ERROR, "Link file error! [%s]->[%s]", source_file, target_file);
return(-1);
}
if (unlink(source_file) < 0) {
errcall(ERROR, "Unlink file error! [%s]", source_file);
return(-1);
}
return(0);
}
/*
* Copy source_file to target_file.
*/
int _copy(char *source_file, char *target_file)
{
unlink(target_file);
if (link(source_file, target_file) < 0) {
errcall(ERROR, "Link file error! [%s]->[%s]", source_file, target_file);
return(-1);
}
return(0);
}
/*
* Move all file in source directory to target directory.
*/
int _move_dir(char *source_dir, char *target_dir)
{
DIR *DirectoryPointer;
struct dirent *dirp;
char source_file[80], target_file[80];
DirectoryPointer = opendir(source_dir);
while ((dirp = readdir(DirectoryPointer)) != NULL) {
if (!memcmp(dirp->d_name,".",1)||!memcmp(dirp->d_name,"..",2))
continue;
sprintf(source_file, "%s/%s", source_dir, dirp->d_name);
sprintf(target_file, "%s/%s", target_dir, dirp->d_name);
if (_move(source_file, target_file) < 0) {
closedir(DirectoryPointer);
return(-1);
}
}
closedir(DirectoryPointer);
return(0);
}
/*
* Copy all file in source directory to target directory.
*/
int _copy_dir(char *source_dir, char *target_dir)
{
DIR *DirectoryPointer;
struct dirent *dirp;
char source_file[80], target_file[80];
DirectoryPointer = opendir(source_dir);
while ((dirp = readdir(DirectoryPointer)) != NULL) {
if (!memcmp(dirp->d_name,".",1)||!memcmp(dirp->d_name,"..",2))
continue;
sprintf(source_file, "%s/%s", source_dir, dirp->d_name);
sprintf(target_file, "%s/%s", target_dir, dirp->d_name);
if (_copy(source_file, target_file) < 0) {
closedir(DirectoryPointer);
return(-1);
}
}
closedir(DirectoryPointer);
return(0);
}
int _creat_new_file(char *f_name)
{
int fd;
if ((fd = creat(f_name, 0666)) < 0) {
printf("Creat file %s error!", f_name);
return(-1);
}
close(fd);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -