/*
 *  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. ASP.NET MVC学习之路由篇(1)

    1.基本路由 RouteConfig.cs: 1 public class RouteConfig 2 { 3 public static void RegisterRoutes(RouteColle ...

  2. TOP 10开源的推荐系统简介

    最近这两年推荐系统特别火,本文搜集整理了一些比较好的开源推荐系统,即有轻量级的适用于做研究的SVDFeature.LibMF.LibFM等,也有重量级的适用于工业系统的 Mahout.Oryx.Eas ...

  3. bzoj 2820 YY的GCD 莫比乌斯反演

    题目大意: 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对 这里就抄一下别人的推断过程了 后面这个g(x) 算的方法就是在线性 ...

  4. POJ 3648 2-sat

    题目大意: 有一对新人结婚,邀请n对夫妇去参加婚礼. 有一张很长的桌子,人只能坐在桌子的两边,还要满 足下面的要求:1.每对夫妇不能坐在同一侧 2.n对夫妇 之中可能有通奸关系(包括男男,男女,女女) ...

  5. 如何对Linux的grub进行加密

    一.加密 设置grub密码: 众所周知,通过编辑GRUB启动参数可以轻松的进入单用户模式从而修改root密码,这对于一台多用户的计算机或服务器来说,无疑增加了安全隐患.大家一定很像为自己的GRUB加一 ...

  6. What am I missing out in life if I don't have a girlfriend?

    http://www.quora.com/What-am-I-missing-out-in-life-if-I-dont-have-a-girlfriend/answer/Kelly-Erickson ...

  7. 网络数据的XML解析

    网络应用中的数据解析,因为最近的应用,无论是Android的和ios平台的,一直用也是建议用的都是Json解析, xml解析都有点被遗忘了. 然后最近自己在做着玩一个ios的小应用,涉及网络数据的抓取 ...

  8. poj1141 区间dp+路径

    //Accepted 176 KB 47 ms //感谢大神们为我们这群渣渣铺平前进的道路!! //用scanf("%s",s)!=EOF WA到死 #include <cs ...

  9. poj1179

    //Accepted 244 KB 0 ms //区间dp //石子合并模型 #include <cstdio> #include <cstring> #include < ...

  10. JS中阻止默认事件与事件冒泡

    JQuery 提供了两种方式来阻止事件冒泡. 方式一:event.stopPropagation(); $("#div1").mousedown(function(event){ ...