文件copy程序(linux)
2007-12-21 14:40
316 查看
linux中,将列表中的文件及文件的目录,原封不动地拷贝出来的程序。
参数:文件列表。
srccopy.c
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <stdarg.h>
int gettime(char *yyyymmdd);
int srccopy(char *src, char *dest);
void call_system(char* str_with_fmt, ...);
int main(int argc, char *argv[])
{
int ists;
char date[10];
char source_dir[1024];
char target_dir[1024];
char program_path[1024];
char result_path[1024];
char *p;
FILE *file;
char file_name[32];
ists = 0;
memset(date, 0x00, sizeof(date));
memset(source_dir, 0x00, sizeof(source_dir));
memset(target_dir, 0x00, sizeof(target_dir));
memset(program_path, 0x00, sizeof(program_path));
memset(result_path, 0x00, sizeof(result_path));
memset(file_name, 0x00, sizeof(file_name));
/* get time */
ists = gettime(date);
if(ists != 0) {
perror("gettime error");
return 1;
}
//printf("date=%s/n", date);
/* get program path */
p = getcwd(program_path, sizeof(program_path));
if(p == NULL) {
perror("getcwd error");
return 2;
}
//printf("%s||%s/n", program_path, p);
sprintf(result_path, "%s/%s", program_path, date);
printf("%s/n", result_path);
if(argc >= 2) {
strcpy(file_name, argv[1]);
} else {
sprintf(file_name, "./srclist");
}
printf("file_name=%s/n", file_name);
/* open file */
file = fopen(file_name, "r");
if(file == NULL) {
perror("file open error");
return 3;
}
while(fgets(source_dir, 1024, file)) {
source_dir[strlen(source_dir)-1] = '/0';
sprintf(target_dir, "%s%s", result_path, source_dir);
/* copy file */
ists = srccopy(source_dir, target_dir);
if(ists != 0) {
printf("%s copy error", source_dir);
}
memset(source_dir, 0x00, sizeof(source_dir));
}
ists = fclose(file);
if(ists != 0) {
perror("fclose error");
}
return 0;
}
int srccopy(char *src, char *dest)
{
char *pos;
char work_dir[1024];
//printf("src=%s,dest=%s/n", src, dest);
for (pos = dest; *pos != '/0'; pos++) {
if (*pos == '/') {
memset(work_dir, 0x00, sizeof(work_dir));
strncpy(work_dir, dest, pos - dest);
// work_dir[pos - target_filename + 1] = '/0';
if(access(work_dir, F_OK) < 0) {
if(strlen(work_dir)<=1) {
continue;
}
call_system("mkdir %s", work_dir);
}
}
}
call_system("cp %s %s", src, dest);
return 0;
}
void call_system(char* str_with_fmt, ...)
{
char buff[8*1024];
va_list ap;
va_start(ap, str_with_fmt);
vsprintf(buff, str_with_fmt, ap);
va_end(ap);
// printf("%s/n", buff);
system(buff);
}
int gettime(char *yyyymmdd) {
time_t t; /* time */
struct tm *tmr; /* struct time */
int i_date[8];
char c_date[10];
memset(i_date, 0x00, sizeof(i_date));
memset(c_date, 0x00, sizeof(c_date));
/* get location time */
tmr = NULL;
t = time(NULL);
if(t == -1){
return(-1);
}
tmr = localtime(&t);
if(tmr == NULL){
return(-1);
}
/* output to param */
i_date[0] = tmr->tm_year + 1900;
i_date[1] = tmr->tm_mon + 1;
i_date[2] = tmr->tm_mday ;
i_date[3] = tmr->tm_wday ;
i_date[4] = tmr->tm_hour;
i_date[5] = tmr->tm_min;
i_date[6] = tmr->tm_sec;
sprintf(c_date, "%4d%02d%02d", i_date[0], i_date[1], i_date[2]);
strcpy(yyyymmdd, c_date);
return 0;
}
Makefile
CC = gcc
CFLAGS = -c -g -Wall
PROGRAMS = ./srccopy
SOURCES = srccopy.c
INCLUDES =
OBJS = $(SOURCES:.c=.o)
.c.o:
${CC} ${CFLAGS} ${INCLUDES} $<
all : $(PROGRAMS)
$(PROGRAMS) : $(OBJS)
gcc -o $@ $(OBJS)
clean :
rm -f *.o $(TARGET)
debug :
$(CC) $(SOURCES) -o $(PROGRAMS) $(CFLAGS) -DDEBUG
运行方法:srcsopy srclist
参数:文件列表。
srccopy.c
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <stdarg.h>
int gettime(char *yyyymmdd);
int srccopy(char *src, char *dest);
void call_system(char* str_with_fmt, ...);
int main(int argc, char *argv[])
{
int ists;
char date[10];
char source_dir[1024];
char target_dir[1024];
char program_path[1024];
char result_path[1024];
char *p;
FILE *file;
char file_name[32];
ists = 0;
memset(date, 0x00, sizeof(date));
memset(source_dir, 0x00, sizeof(source_dir));
memset(target_dir, 0x00, sizeof(target_dir));
memset(program_path, 0x00, sizeof(program_path));
memset(result_path, 0x00, sizeof(result_path));
memset(file_name, 0x00, sizeof(file_name));
/* get time */
ists = gettime(date);
if(ists != 0) {
perror("gettime error");
return 1;
}
//printf("date=%s/n", date);
/* get program path */
p = getcwd(program_path, sizeof(program_path));
if(p == NULL) {
perror("getcwd error");
return 2;
}
//printf("%s||%s/n", program_path, p);
sprintf(result_path, "%s/%s", program_path, date);
printf("%s/n", result_path);
if(argc >= 2) {
strcpy(file_name, argv[1]);
} else {
sprintf(file_name, "./srclist");
}
printf("file_name=%s/n", file_name);
/* open file */
file = fopen(file_name, "r");
if(file == NULL) {
perror("file open error");
return 3;
}
while(fgets(source_dir, 1024, file)) {
source_dir[strlen(source_dir)-1] = '/0';
sprintf(target_dir, "%s%s", result_path, source_dir);
/* copy file */
ists = srccopy(source_dir, target_dir);
if(ists != 0) {
printf("%s copy error", source_dir);
}
memset(source_dir, 0x00, sizeof(source_dir));
}
ists = fclose(file);
if(ists != 0) {
perror("fclose error");
}
return 0;
}
int srccopy(char *src, char *dest)
{
char *pos;
char work_dir[1024];
//printf("src=%s,dest=%s/n", src, dest);
for (pos = dest; *pos != '/0'; pos++) {
if (*pos == '/') {
memset(work_dir, 0x00, sizeof(work_dir));
strncpy(work_dir, dest, pos - dest);
// work_dir[pos - target_filename + 1] = '/0';
if(access(work_dir, F_OK) < 0) {
if(strlen(work_dir)<=1) {
continue;
}
call_system("mkdir %s", work_dir);
}
}
}
call_system("cp %s %s", src, dest);
return 0;
}
void call_system(char* str_with_fmt, ...)
{
char buff[8*1024];
va_list ap;
va_start(ap, str_with_fmt);
vsprintf(buff, str_with_fmt, ap);
va_end(ap);
// printf("%s/n", buff);
system(buff);
}
int gettime(char *yyyymmdd) {
time_t t; /* time */
struct tm *tmr; /* struct time */
int i_date[8];
char c_date[10];
memset(i_date, 0x00, sizeof(i_date));
memset(c_date, 0x00, sizeof(c_date));
/* get location time */
tmr = NULL;
t = time(NULL);
if(t == -1){
return(-1);
}
tmr = localtime(&t);
if(tmr == NULL){
return(-1);
}
/* output to param */
i_date[0] = tmr->tm_year + 1900;
i_date[1] = tmr->tm_mon + 1;
i_date[2] = tmr->tm_mday ;
i_date[3] = tmr->tm_wday ;
i_date[4] = tmr->tm_hour;
i_date[5] = tmr->tm_min;
i_date[6] = tmr->tm_sec;
sprintf(c_date, "%4d%02d%02d", i_date[0], i_date[1], i_date[2]);
strcpy(yyyymmdd, c_date);
return 0;
}
Makefile
CC = gcc
CFLAGS = -c -g -Wall
PROGRAMS = ./srccopy
SOURCES = srccopy.c
INCLUDES =
OBJS = $(SOURCES:.c=.o)
.c.o:
${CC} ${CFLAGS} ${INCLUDES} $<
all : $(PROGRAMS)
$(PROGRAMS) : $(OBJS)
gcc -o $@ $(OBJS)
clean :
rm -f *.o $(TARGET)
debug :
$(CC) $(SOURCES) -o $(PROGRAMS) $(CFLAGS) -DDEBUG
运行方法:srcsopy srclist
相关文章推荐
- linux下写C程序,执行shell文件
- linux 服务器间copy 文件
- Linux 使用core file文件快速定位程序崩溃代码行
- Linux系统启动引导程序配置文件解析
- linux 驱动 编译加载 , 程序搬移到目标板,驱动源码和头文件位置
- 从网络上下载的Linux单文件程序,无法运行,用File查看出现 ELF 64-bit LSB executable
- linux中,运行java程序装载linux指定目录下的Properties文件
- 利用pscp程序实现Windows和Linux互传文件
- linux下可执行程序如何定位共享库文件以及如何让系统找到用户指定的库 .
- linux下编译程序使其处理超过2G的大文件
- 在linux中把汇编或c程序交叉编译成二进制文件烧录开发板过程详解
- uc笔记01---Unix,Linux,程序构建过程,gcc,头文件,预处理,环境变量配置
- linux下解压rar文件用rar程序
- linux下利用nohup后台运行jar文件包程序
- Linux下实现跟踪程序执行信息记录日志文件
- LINUX下目录遍历搜索文件程序,支持嵌套目录
- linux: 将32位.o文件和64位.o文件链接成64位的可执行程序
- Linux下静态库_库的基本概念;如何生成静态库动态库;nm查看库中包含那些函数、ar生成静态库,查看库中包含那些.o文件、ldd查看程序依赖的.so文件;gcc/g++与库相关的参数-L,-l,-f
- linux文件系统初始化过程(6)---执行init程序
- 在linux下利用程序崩溃后的core文件分析bug