Play MINIX

zhaozj2021-02-11  185

A few days ago, I bought this operating system in China-Pub: Design and Realization (Second Edition) ", then started to find Minix (huh, I will come to the underlying mechanism, I think of it. Bar). Who knows in the online search, discovering this thing, good! The body does slim, or open source :) Its official website just has a minix hard disk IMG download, give me back. The author of this minix img package is even brought to Bochsrc.txt, saves yourself:) (or foreigners can do practical things, this has to be admitted) I will imitate the DLXLinux built by Bochs. A folder called Minix, then copy the run.bat in dlxlinux to modify it (my Bochs is in C: / Program Files / Bochs): CD "c: / program files / bochs / minix" ../ Bochs -q -f bochsrc.txt will use Bochs to run minix to double-click this batch to get it. To this, it is solved with minix installation issues. In fact, virtual machines have VMware and Virtual PC, and exchange data between virtual machines and hosts is better than BOCHS, but I like it to the Bochs's body :)

Ok, you can go up soon! Start Bochs soon see:

MiniX boot monitor 2.16

Press Esc to Enter the Monitor

Hit a KEY As Follows:

= Start Minix

You can see the login screen after you press the equal sign:

MultiUser Startup in Progress.

Starting daemons: Update cron.

Minix Release 2 Version 0.3

Noname login:

The host name is Noname and can be changed in /etc/hostname.file after logging in. The login name here is ROOT, no password. After logging in, you can see the shell prompt "#", but only two console is available. It seems that there is no way to set the prompt to the current working directory (even if it is set, it is not convenient to use it, but it is a teaching system, not used to work, let it go: )

Probably turn, the basic order is there, but you can't alias. I used to "Alias ​​Dir 'ls -al'" in Linux and FreeBSD. I really feel that it is not convenient :) I saw it. Handbook, Minix's shell seems to be called ASH (have not heard of it before), it seems that the function is indeed simple. Vi is also, in fact, Elvis (with VI-compatible tools), actually read configuration files, is the .exrc file under the home directory (this img put the root home directory /, of course you can also Change / etc / passwd. The feeling of trial seems to be better than the VI below Solaris:) SET TS = 4SET SW = 4 These two options can also be used, that is, do not support syntax highlight (a joke :)). The most cool is that this img has also bringing CC and all source code. In this way, what we have, you can do it yourself, good, good! It seems that this Bochs Minix 2.0 IMG is really a learning operating system principle and a good thing in Minix! All source code is placed under / usr / src.

Looking at the hand itching, just try a little, there is no color when the LS column catalog is not colored, take this change :) What? How is the color come out? Take you look at the code below:

#include

int

Main (void)

{

Printf ("/ 033 [31M2ndBOY / 033 [0M / N");

Return (0);

}

Suppose the program is saved as color.c, compile: # cc -o color color.c running results as follows: # ./color2ndboy

how about it? See the colored! We can see other colors as long as we change the 31 in the format string to other values, and the other parts can be moved as long as you move. Let's take a look at the object we want to modify: # CD / usr / src # Find. -Name ls.c./commists/simple/ls.c

Ls.c is a 1134-row program, which is roughly looked at the source code, found a function called PrintName (), and it seems that it looks like the object we have to find without detailed analysis of the code. Try it, we refer to the above code to modify this function:

Void PrintName (Char * name)

{

INT C, Q = Present ('q');

PRINTF ("/ 033 [31m");

While ((c = (unsigned char) * Name )! = 0) {

IF (q && (c <= '|| c == 0177)) c ='? ';

PUTCHAR (C);

}

Printf ("/ 033 [0m"); / * Restore the default color, lestily affect subsequent content * /

}

The red code is I added, the storage exit, compile: # make lscc -i -d_minix -d_posix_source -o ls ls.cinstall -s 20kw ls # ./ls ​​Sure enough, all listed file names become red, It seems to find the door! We continue to modify.

Looking again, the program is used in a Struct File (ls.c: 248) to save the file information of each file, and this information is by calling Stat () (Status in the program () ), This is exactly what we need! Since the file type of the file is stored in ST_Mode in the STRUCT STAT structure, we can display different types of files with different colors by interpreting this member. But printname () has only one parameter, it seems that we have to add a parameter, change to this: void printname (char * name, struct file * f) Of course, the place to be called, a total of two Place, change to: PrintName (xxxx, f);

In this way, we have the file type and file name, the code is modified as follows:

Void PrintName (Char * Name, Struct File * F)

{

INT C, Q = Present ('q');

IF (S_ISDIR (F-> Mode))

Printf ("/ 033 [1; 34m"); / * directory with bright blue * /

Else if (f-> mode & s_ixusr || f-> mode & s_ixgrp || f-> mode & s_ixot)

Printf ("/ 033 [1; 32m"); / * Executable file with bright green * /

Else IF (f-> Mode & S_IFCHR || F-> Mode & S_IFBLK)

Printf ("/ 033 [1; 33m"); / * Character and block device yellow * /

#ifdef s_iflnk

Else IF (f-> Mode & S_IFLNK)

Printf ("/ 033 [1; 36m"); / * Symbolic link with cyan * /

#ENDIF

While ((c = (unsigned char) * Name )! = 0) {

IF (q && (c <= '|| c == 0177)) c ='? ';

PUTCHAR (C);

}

Printf ("/ 033 [0m");

}

After compiling, we used the modified LS to cover the original LS, which is convenient for normal: # CP LS / USR / BIN override, try there is no effect: # ls -al / Sure enough, the directory is displayed with bright blue. However, the file name displayed when the -L parameter is displayed is not color. It seems that there is a problem with the code to change under the premise of the detailed analysis process :) I want to think: I have to get the file type. Call Stat (), and search for this idea. Sure enough, the place to call Stat () in the program must require FIELD to be 0 (that is, set a parameter). So we change your ideas, let LS think that all of us add parameters to its calls, all the value of Field define macro in the program, from 0x0001 to 0x2000, then we use it yet not occupy 0x4000 :) MAIN () doing the following modification (red code is me plus): if (field & l_long) Field & = ~ l_extra; Field | = 0x4000; this is OK, our LS is successful! ! !

The above is said, Minix's shell cannot set the prompt to display the current directory instant display, then let's change :) Search I found out in the / usr / src / commands / below there are SH and ASH these two directories, compile Let's, the SH does not support the TAB key command to make up, then this shell we use is ASH. After / usr / src / commands / ASH / next GREP, the goal was determined: Parser.c. It seems that PS1val () is used to get the value of PS1 before the display prompt, then we rewrite this ps1val () to make a handle in advance not? ! Open the program, this ps1val () is a macro, which is defined in var.h into #define ps1val () (VPS1.TEXT 4), while the VPS1 is stored in the form of PS1 = XXX, so 4 It then gave it directly to its display. If you want to do your hands, you are unlikely to continue with macros. We look out this macro and change it into a function declaration: char * ps1val (void);

Now open Parser.c, press G to jump to the end of the file to join the following code:

#include

Char *

PS1val (Void)

{

Static char SZPS1 [128];

CHAR * P = VPS1.Text 4;

INT NPOS = 0;

MEMSET (SZPS1, 0, SIZEOF (SZPS1));

While (* p! = '/ 0')

{

IF (* p! = '//') / * is not an escape character to output * /

{

SZPS1 [NPOS] = * P;

NPOS ;

}

Else

{

P ;

IF (* p == 'w') / * Take the current work path * /

{

GetCWD (SzPS1 NPOS, SIZEOF (SZPS1) - NPOS;

}

Else if (* p == 'u') / * Take the user login name * /

{

UID_T uid = getuid (); struct passwd * ppasswd = getpwwuid (uid);

STRCAT (SZPS1 NPOS, PPASSWD-> PW_NAME);

}

NPOS = Strlen (SZPS1);

}

P ;

}

SZPS1 [NPOS] = '/ 0';

Return (SZPS1);

}

Here I imitate the BASH approach, you can use the escap to replace some things, such as using / u represents the current user name, / W represents the current working directory. Post-compilation: # Make

Here, MAKE time is longer, let's try again: # ./sh# ps1 = "[/ u / w] #" [root / usr / src / commists / ash] #

Haha, I am very good to tell, double, come ...

Copy the compiled SH to / bin, after we enter the system, use it, remember to change .profile set PS1, this minix environment is much better! ! !

Minix is ​​specifically designed to learn the operating system principle. I have changed here. Shell is not touched by OS theory. Oh, but use these two things to open a head. Nice.

转载请注明原文地址:https://www.9cbs.com/read-5363.html

New Post(0)