/*
 *  linux/zBoot/xtract.c
 *
 *  Copyright (C) 1993  Hannu Savolainen
 *
 *    Extracts the system image and writes it to the stdout.
 *    based on tools/build.c by Linus Torvalds
 */

#include <stdio.h>    /* fprintf */
#include <string.h>
#include <stdlib.h>    /* contains exit */
#include <sys/types.h>    /* unistd.h needs this */
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <unistd.h>    /* contains read/write */
#include <fcntl.h>
#include <a.out.h>
#include <linux/config.h>

#define GCC_HEADER 1024

#define STRINGIFY(x) #x

//中断退出
void die(char * str)
{
    fprintf(stderr,"%s\n",str);
    exit(1);
}

//使用方法
void usage(void)
{
    die("Usage: xtract system [ | gzip | piggyback > piggy.s]");
}

//主函数
int main(int argc, char ** argv)
{
    int i,c,id, sz;
    char buf[1024];
    char major_root, minor_root;
    struct stat sb;

//可执行文件指向缓冲区
    struct exec *ex = (struct exec *)buf;

//参数一定是两个,否则提示使用方法
    if (argc  != 2)
        usage();
    //打开第二个参数指定的文件
    if ((id=open(argv[1],O_RDONLY,0))<0)
        die("Unable to open 'system'");
    //读取GCC文件头
    if (read(id,buf,GCC_HEADER) != GCC_HEADER)
        die("Unable to read header of 'system'");
    //校验
    if (N_MAGIC(*ex) != ZMAGIC)
        die("Non-GCC header of 'system'");

//计算系统长度
    sz = N_SYMOFF(*ex) - GCC_HEADER + 4;    /* +4 to get the same result than tools/build */

//输出系统长度
    fprintf(stderr, "System size is %d\n", sz);

//遍历System文件
    while (sz)
    {
        int l, n;

l = sz;
        //如果剩余长度大于buff长度,那么取buff长度
        if (l > sizeof(buf)) l = sizeof(buf);
        //读取一定长度
        if ((n=read(id, buf, l)) !=l)
        {
            if (n == -1)
               perror(argv[1]);
            else
               fprintf(stderr, "Unexpected EOF\n");

die("Can't read system");
        }
        //将内容写入到标准输出中
        write(1, buf, l);
        sz -= l;
    }

close(id);
    return(0);
}

zboot/xtract.c的更多相关文章

  1. Linux1.0源代码编译过程

    根据源代码包中的readme文件及http://chfj007.blog.163.com/blog/static/173145044201191195856806/?suggestedreading& ...

  2. zBoot/Makefile

    #上层makefile调用执行make命令,执行的应该是第一个目标allHEAD = head.oSYSTEM = ../tools/zSystem#LD = gcc#TEST = -DTEST_DR ...

  3. zboot/piggyback.c

    /* *    linux/zBoot/piggyback.c * *    (C) 1993 Hannu Savolainen */ /* *    This program reads the c ...

  4. boot/setup.S

    !!    setup.S        Copyright (C) 1991, 1992 Linus Torvalds!! setup.s is responsible for getting th ...

  5. 开源入侵检测系统OSSEC搭建之一:服务端安装

    OSSEC是一款开源的多平台的入侵检测系统,可以运行于Windows, Linux, OpenBSD/FreeBSD, 以及 MacOS等操作系统中.主要功能有日志分析.完整性检查.rootkit检测 ...

  6. Ossec常用命令

    启动并查看httpd服务 systemctl start httpd systemctl status httpd.service 启动并查看mysql服务 systemctl start maria ...

  7. OSSEC初探

    OSSEC初探 概念: OSSEC是一款开源的基于主机的入侵检测系统(HIDS),它可以执行日志分析.完整性检验.windows注册表监控.隐匿性检测和实时告警.它可以运行在各种不同的操作系统上,包括 ...

  8. 全网最详细的最新稳定OSSEC搭建部署(ossec-server(CentOS7.X)和ossec-agent(CentOS7.X))(图文详解)

    不多说,直接上干货! 前言 写在前面的话,网上能够找到一些关于ossec方面的资料,虽然很少,但是总比没有强,不过在实际的使用过程中还是会碰到许多稀奇古怪的问题.整理整理我的使用过程,就当做一篇笔记吧 ...

  9. 全网最详细的最新稳定OSSEC搭建部署(ossec-server(CentOS6.X)和ossec-agent(CentOS6.X))(图文详解)

    不多说,直接上干货! 前言 写在前面的话,网上能够找到一些关于ossec方面的资料,虽然很少,但是总比没有强,不过在实际的使用过程中还是会碰到许多稀奇古怪的问题.整理整理我的使用过程,就当做一篇笔记吧 ...

随机推荐

  1. CSS 垂直居中。

    1,display: table; display: table-cell <div style="border:solid red 1px ;height:200px;width:2 ...

  2. Servlet下

    HTTP简介 HTTP是 hypertext transfer protocol(超文本传输协议)的简写,它是 TCP/IP 协议集中的一个应用层协议,用于定义WEB浏览器与WEB服务器之间交换数据的 ...

  3. android button text属性中英文大小写问题

    Android版本升级的原因,需要手动添加属性android:textAllCaps="false"

  4. Ohlàlà

    Chap 1数数字 un 1 deux 2 trois 3 quatre 4 cinq 5 six 6 sept 7 huit 8 neuf 9 dix 10   Chap 2 讲地名 Paris 巴 ...

  5. hql语句理解2

    /* * this.getSession().createQuery("sdfdf").executeUpdate();这里面的query可以是delete,update,inse ...

  6. leetcode 239 Sliding Window Maximum

    这题是典型的堆排序算法,只是比一般的堆算法多了删除的操作,有两件事需要做: 1 用一个hash表存储从输入数组索引到堆数组(用于实现堆的那个数组)所以的映射,以便在需要删除一个元素的时候能迅速定位到堆 ...

  7. (spring-第8回【IoC基础篇】)BeanDefinition在IoC容器中的注册

    在spring中,所有的bean都是由BeanFactory进行管理的.下面是BeanFactory的类体系结构: 我们清楚的看到,DefaultListableBeanFactory继承了BeanF ...

  8. Python的魔法方法 .

    基本行为和属性 __init__(self[,....])构造函数 . 在实例化对象的时候会自动运行 __del__(self)析构函数 . 在对象被回收机制回收的时候会被调用 __str__(sel ...

  9. 获取本机IP非127.0.0.1

    protected function GetiP()    {    $preg="/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5] ...

  10. 爬虫再探实战(四)———爬取动态加载页面——请求json

    还是上次的那个网站,就是它.现在尝试用另一种办法——直接请求json文件,来获取要抓取的信息. 第一步,检查元素,看图如下: 过滤出JS文件,并找出包含要抓取信息的js文件,之后就是构造request ...