我的学习之旅(7) build工具

49 篇文章 0 订阅
49 篇文章 0 订阅

build_boot.c源代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h> 
#include <sys/types.h> 
#include <sys/stat.h>
#include <linux/fs.h>
#include <unistd.h>
#include <fcntl.h>

#define MINIX_HEADER_LEN 32
#define BOOT_LENGTH 512

int main(int argc, char *argv[] )
{
 char buf[1024];
 int fd;
 int len;

 if( argc != 3 ) {
  printf("no input file\n");
  return 0;
 }
 sprintf(buf,"rm -f %s", argv[2]);
 system(buf);
 sprintf(buf,"touch %s", argv[2]);
 system(buf);

 fd = open(argv[1], O_RDONLY, 0 );
 if( fd < 0 ){
  printf("open file:%s failed\n", argv[1]);
  return 0;
 }
 if( read(fd, buf, MINIX_HEADER_LEN)!= MINIX_HEADER_LEN) {
  close(fd);
  printf("Invalid mini header len:%d\n", MINIX_HEADER_LEN);
  return 0;
 }
 memset(buf, 0x0, sizeof(buf));
 len = read(fd, buf, sizeof(buf));
 if( len != BOOT_LENGTH ) {
  printf("wrong boot length:%d, should be %d\n", len, BOOT_LENGTH);
  return 0;
 } 

 close(fd);
        fd = open(argv[2], O_WRONLY, O_CREAT | O_EXCL | O_SYNC );
        if( fd < 0 ){
                printf("open file:%s failed\n", argv[2]);
                return 0;
        }

 len = write(fd, buf, BOOT_LENGTH);
 if( len != BOOT_LENGTH ){
  printf("write failed with len:%d, should be:%d\n", len, BOOT_LENGTH);
  return 0;
 }


 close(fd);
 return 0;
}

build_setup.c和build_boot.c类似。

编译: gcc  -Wall -O2 -fomit-frame-pointer -o build_boot build_boot.c

              gcc  -Wall -O2 -fomit-frame-pointer -o build_boot build_setup.c

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值