您的位置:首页 > 运维架构 > Linux

文件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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: