《Makefile》
***************
CC=gcc
TEXT=copy
SVC=file_copy.c debug.h
$(TEXT): $(SVC)
$(CC) -o $@ $^
clean:
rm -rf $(TEXT) *.o
******************************
《头文件》
/*Comment/uncomment the following line to disable/enable debugging,
OR define(or NOT) it in Makefile.
*/
//#define NDEBUG
#undef pr_debug /* undef it, just in case */
#ifndef NDEBUG
#ifdef __KERNEL__
/* This one if debugging is on, and kernel space */
#define pr_debug(fmt, args...) printk( KERN_ERR fmt, ## args)
#else
/* This one for user space */
#define pr_debug(fmt, args...) fprintf(stderr, fmt, ## args)
#endif
#else
#define pr_debug(fmt, args...) /* not debugging: nothing */
#endif
******************************
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include "debug.h"
/*usage: ./file_copy file1 file2 */
void usage (const char *s)
{
printf("usage:\n\t%s src_file dest_file\n", s);
}
int main(int argc, char **argv)
{
FILE * src_fp, *dst_fp;
if (argc != 3) {
usage(argv[0]);
exit(1);
}
src_fp = fopen(argv[1], "r");
dst_fp = fopen(argv[2], "w+");
assert( (src_fp !=NULL) && (dst_fp != NULL) );
if (!src_fp || !dst_fp ) {
usage(argv[0]);
exit(1);
}
pr_debug("Hello, copying start!\n");
while( !feof(src_fp)) {
int c,d;
c = fgetc(src_fp);
d = fputc(c, dst_fp);
if (d == EOF) {
pr_debug("fputc");
continue;
}
}
pr_debug("OK, copying finished!\n");
fclose(src_fp);
fclose(dst_fp);
return 0;
}