Usual tiny skills & solutions

Ubuntu and Win10 - double OS

  • 2016-02-21

Yesterday I helped my friend install Ubuntu (14.04 LTS) on his PC where there has been a MS Win10.
I used UltraISO to create a U-disk installation and installed it.
However, then we could only start up Ubuntu and there was no Win10.
Then we use LaoMaoTao software to rescue Win10's loader, while the Ubuntu disappeared.
Then we use EasyBCD on Win10 to add an Ubuntu loader.
Bingo!

Modify a binary file in Linux

  • 2016-02-28

Here is a helloworld.c:

#include <stdio.h>

int main()
{
    printf("Hello, world\n");
    return 0;
}

gcc -o helloworld helloworld.c

Then I want to change the string "Hello, world" to "hello, world".

Here is :

vim helloworld -b      # -b means binary
:%!xxd                 # observe in hex mode
# move cursor to letter 'H' and change it to 'h'
:%!xxd -r              # recover from text to binary
:wq

bingo!

Get file-size on Linux

  • 2016-02-29

Here is an interesting question that how can I get the file size in Linux environment.

Method.1

#include <stdio.h>

FILE *fp = fopen(filename, "r");
if(!fp)
    return -1;
fseek(fp, 0L, SEEK_END);
int size=ftell(fp);
printf("size is %d\n", size);
fclose(fp);

However, this method must load the whole file into memory. If the file is very large, its speed will be ridiculous!

Method.2

#include <sys/stat.h>
#include <stdio.h>

struct stat statbuf;
int state = stat(filename, &statbuf);
if(state < 0)
    return -1;
int size = statbuf.st_size;
printf("size is %d", size);

Bingo!

Win7 installation - GPT & MBR - Problems

  • 2016-02.29

Today my friend want to install Win7. I use a U-disk with LaoMaotao software and Win7 GHO. However, his disk is GPT mode.
The Win7 should be installed in MBR mode by default.
So we use LaoMaotao to change the disk into MBR mode.

P.S. Maybe we should also study how to install Win7 in GPT mode for the reason that GPT will be in fashion from now on.

bingo!

Editors in Linux automatically add '\n' to a file

  • 2016-03-01

Today I use Vim to create a text file and store it. Then I use 'xxd' to observe that file. Here is a '\x0a' at the end of the text.
And gedit will do so. Somebody says that is because of the canonical mode in Linux.

<Backspace> makes no effect in Vim & Vi

  • 2016-03-01

Today my friend uses Vi to edit a text. However he can't use to delete letters.
Here is the solution(Copy from Internet):

set nocompatible
set backspace=indent,eol,start

UTF-8 & Unicode & ASCII

  • 2016-03-02

Usual tiny skills & solutions_第1张图片

execve - C function

  • 2016-03-03

Today I read CSAPP Chapter-8 and follow it to make a simple shell.
Here's the code:

        if(pid == 0){ // child runs user job
            if(execve(argv[0], argv, environ) < 0){
                printf("evecve error: %s\n", strerror(errno));
                printf("%s: Command not found.\n", argv[0]);
            }
        }

However, I can't use [echo "hello,world"] to execute [echo] instruction.
I must use [/bin/echo "hello,world"]. I have deliver the environment variables into the sub-process.
So, how can that thing happen?
After searching on the Internet, I know it.
[execve] belongs to a [exec] family. And there are some suffixes:

  • l // means that you must deliver the argv[0], argv[1]... one by one as variables and arguments end up with a NULL.
  • v // means that you should give a two-dimension pointer [**argv] as a variable. So you can't use suffix [v] and [l] at the same time.
  • p // means that you can use the $PATH environment variable to search for the argv[0] (filename).
  • e // means that you can deliver a [**envp] to give an environment to sub-process. Otherwise, the sub-process will use parent-process' environment.

Here are some functions:

  • execl
  • execv
  • execle
  • execlp
  • execvp
  • execve
  • execvpe

For the small shell above, we should use [execvpe] to replace [execve].
P.S.

for more details, please refer to [man execl] in Linux.

bingo!

你可能感兴趣的:(Usual tiny skills & solutions)