#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <limits.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdarg.h>
#define VERSION "1.0"
#define DEBUG_BUF_SIZE (1024*4)
static int g_iDebugLevel = 0;
static char const *gpcInputFile = NULL;
static char const *gpcOutputFile = NULL;
static unsigned long int giFileSize = 0;
static unsigned long int giSrcFileSize = 0;
inline int debug(const char *stFormat, ...)
{
if (g_iDebugLevel)
{
char acMsg[DEBUG_BUF_SIZE];
char acDebugHead[DEBUG_BUF_SIZE];
va_list vaMsg;
memset(acMsg, 0, DEBUG_BUF_SIZE);
memset(acDebugHead, 0, DEBUG_BUF_SIZE);
va_start(vaMsg, stFormat);
vsprintf(acMsg, stFormat, vaMsg);
va_end(vaMsg);
printf("\r%s%s\n\n", acDebugHead, acMsg);
fflush(stdout);
}
return 0;
}
/** 帮助命令显示内容 */
const char g_usage_longstr[] = "Usage: flashadd -i inputfile -o outputfile -s size [options]\n"
" flashadd [-h|--help] [-v|--version]\n\n"
" -i, --inputfile source file path\n"
" -o, --outputfile output file path\n"
" -s, --size add flash to [#MB/Mb/KB/Kb/B/b] size\n"
" -v, --version show version information and quit\n"
" -h, --help show this message and quit\n";
void
usage_long(FILE *f)
{
fprintf(f, g_usage_longstr);
}
int parse_arguments(int argc, char **argv)
{
static struct option longopts[] =
{
{"debug", no_argument, NULL, 'd'},
{"inputfile", required_argument, NULL, 'i'},
{"outputfile", required_argument, NULL, 'o'},
{"size", required_argument, NULL, 's'},
// {"char", required_argument, NULL, 'c'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
};
int flag;
char *pcEndstr;
long long int size;
while ((flag = getopt_long(argc, argv, "di:o:s:c:hv", longopts, NULL)) != -1)
{
switch (flag)
{
case 'd':
g_iDebugLevel = 1;
break;
case 'i':
gpcInputFile = optarg;
break;
case 'o':
gpcOutputFile = optarg;
break;
case 's':
size = strtoll(optarg, &pcEndstr, 10);
if (size <= 0)
{
printf("Size error!%lld\n", size);
exit(1);
}
giFileSize = (unsigned long int)size;
if (strlen(pcEndstr))
{
switch(*pcEndstr)
{
case 'M':
case 'm':
giFileSize = giFileSize * 1024;
case 'K':
case 'k':
giFileSize = giFileSize * 1024;
break;
case 'b':
case 'B':
pcEndstr--;
break;
default:
printf("Unresolvable size!%c\n", pcEndstr);
exit(1);
}
pcEndstr++;
if(strlen(pcEndstr))
{
switch(*pcEndstr)
{
case 'b':
giFileSize = giFileSize / 8;
case 'B':
break;
default:
printf("Unresolvable size!%c\n", pcEndstr);
exit(1);
}
pcEndstr++;
if(strlen(pcEndstr))
{
printf("Unresolvable size!%s\n", pcEndstr);
exit(1);
}
}
}
break;
case 'c':
printf("TODO USELESS\n");
break;
case 'v':
printf("%s:"VERSION"\n",argv[0]);
exit(0);
case 'h':
usage_long(stdout);
exit(0);
default:
usage_long(stderr);
exit(1);
}
}
return 0;
}
int check_arguments()
{
if (gpcInputFile == NULL || gpcOutputFile == NULL || giFileSize <=0)
{
usage_long(stderr);
exit(2);
}
debug("\rinput file:%s\n \routput file:%s\n \rfile size:%lu\n", gpcInputFile, gpcOutputFile, giFileSize);
if(access(gpcInputFile,F_OK))
{
printf("%s does not exist\n", gpcInputFile);
return -1;
}
fflush(stdout);
long long int filesize = -1;
FILE *fp;
fp = fopen(gpcInputFile, "r");
if(fp == NULL)
return -1;
fflush(stdout);
fseek(fp, 0L, SEEK_END);
fflush(stdout);
filesize = ftell(fp);
debug("source file size:%lld\n", filesize);
if (filesize > giFileSize)
{
printf("Source file to long!\n");
return -1;
}
giSrcFileSize = filesize;
if(!access(gpcOutputFile, F_OK))
{
char c;
do
{
printf("The file %s already exists. Is it overwritten?(y/n)", gpcOutputFile);
c = getchar();
if (c == 'y' || c == 'Y')
break;
else if(c == 'n' || c =='N')
exit (0);
printf("input error %c\n", c);
while ((c = getchar()) != EOF && c != '\n');
}while(1);
}
return 0;
}
int appendFile()
{
unsigned long int ulitmpSize = 0;
unsigned long int uliAddSize = 0;
unsigned char acTmpBuf[1024];
int iLoop = 0;
int fdInputFile, fdOutputFile;
memset(acTmpBuf, 0, sizeof(acTmpBuf));
if ((fdInputFile = open(gpcInputFile, O_RDONLY))== -1)
{
printf("Can not open file %s \n", gpcInputFile);
return -1;
}
if ((fdOutputFile = open(gpcOutputFile, O_RDWR|O_TRUNC|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH))== -1)
{
printf("Can not open file %s \n", gpcOutputFile);
return -1;
}
posix_fadvise(fdInputFile, 0, giSrcFileSize, POSIX_FADV_NOREUSE);
int len;
while((len=read(fdInputFile, acTmpBuf, sizeof acTmpBuf)) > 0)
{
write(fdOutputFile, acTmpBuf, len);//将缓冲区的数据写到目标文件中
ulitmpSize = ulitmpSize + len;
}
if (len < 0)
{
printf("read file error");
close(fdInputFile);
close(fdOutputFile);
return -1;
}
close(fdInputFile);
memset(acTmpBuf, 0xFF, sizeof(acTmpBuf));
uliAddSize = giFileSize - ulitmpSize;
while(uliAddSize > 0)
{
if (uliAddSize <= sizeof(acTmpBuf))
{
write(fdOutputFile, acTmpBuf, uliAddSize);
uliAddSize -= uliAddSize;
}
else
{
write(fdOutputFile, acTmpBuf, sizeof(acTmpBuf));
uliAddSize -= sizeof(acTmpBuf);
}
}
close(fdOutputFile);
return 0;
}
int main(int argc, char **argv)
{
if(parse_arguments(argc, argv))
{
printf("arguments error!\n");
return -1;
}
if(check_arguments())
{
printf("check arguments error!\n");
return -1;
}
appendFile();
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
fileadd_demo.zip
共3个文件
pro:1个
cpp:1个
user:1个
需积分: 3 0 下载量 166 浏览量
2022-01-13
19:00:57
上传
评论
收藏 6KB ZIP 举报
温馨提示
实现将bin文件自动补写1至指定大小,不影响bin文件程序实现
资源详情
资源评论
资源推荐
收起资源包目录
fileadd_demo.zip (3个子文件)
fileadd_demo
fileadd_demo.pro 113B
fileadd_demo.pro.user 47KB
fileadd.cpp 7KB
共 3 条
- 1
山下小僧
- 粉丝: 38
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0