Making Minimalist Hex Editor in C on Linux

Поделиться
HTML-код
  • Опубликовано: 27 окт 2024

Комментарии • 64

  • @nirlichtman
    @nirlichtman  10 месяцев назад +10

    Interesting points about the behavior of the program:
    - Reminding that the code is just for fun and not suitable for production and skips many error checking and other checks
    - If you open a file that is larger than 1024 bytes and save it will discard all the bytes after 1024 since it truncates the file when saving and saves the 1024 byte long buffer
    - You can use the p and e commands to go beyond the buffer limits and access memory outside of the buffer

    • @ItsCOMMANDer_
      @ItsCOMMANDer_ 10 месяцев назад +1

      For production it would be better to
      ```
      fseek(f, 0,SEEK_END);
      long lenght = ftell(f);
      char* buffer = calloc(lenght, sizeof(char));
      fseek(f,0,SEEK_SET);
      ```

  • @d9h34nu0df
    @d9h34nu0df 10 месяцев назад +4

    Never stop posting videos, your videos are great inspirations and help me come up with ideas for cool projects to do, as well as already helping with a foundation of how logic works

  • @benhetland576
    @benhetland576 10 месяцев назад +2

    To be pedantic, the scanf on line 31 contains a memory overwrite bug. It should be given a (unsigned int*) but you give it an (unsigned char*).

  • @metamirasi
    @metamirasi 10 месяцев назад

    Very clear and concise purpose und workflow! I look forward to the next videos👋🙌

  • @jannatgaoshiqqalb3598
    @jannatgaoshiqqalb3598 10 месяцев назад +4

    What kind if books do you recommend us to have a solid understanding of C if we are beginner for C?

    • @nirlichtman
      @nirlichtman  10 месяцев назад +4

      I enjoyed reading the first edition of "Writing Solid Code" by Steve Maguire, it does require some prior knowledge in C and is a little old but generally has great tips. But the best way to learn in my opinion is to just practically write C for projects and work with the manuals as much as you can. (I recommend trying both Linux and Windows programming and learning to work with the man pages and Microsoft win32 api docs)

    • @AnalogDude_
      @AnalogDude_ 10 месяцев назад +2

      @@nirlichtman win32/64 is death, but doesn't know it yet.

    • @baranjan6969
      @baranjan6969 4 месяца назад

      For absolute beginners K&R is also good one.

  • @N7TWL
    @N7TWL 10 месяцев назад

    Nicely done!

  • @silme9417
    @silme9417 10 месяцев назад +3

    suggestion : make a terminal text editor like nano or vim

    • @N7TWL
      @N7TWL 10 месяцев назад

      Have a look at Nir Lichtman's "Minimalist Text Editor" put up about 2 weeks ago.

    • @benhetland576
      @benhetland576 10 месяцев назад

      He's doing the terminal editor the true old style, the line editor! Like MS-DOS's edlin or Unix's ed. It will probably work with Curious Marc's teletype as its "user interface" too 😊

  • @juliovata9194
    @juliovata9194 10 месяцев назад +7

    What terminal do you use?

    • @greybrunix
      @greybrunix 10 месяцев назад +6

      looks like the windows terminal xD
      Unironically I think this is Windows with a DWM port for the layout

    • @thatonemailbox
      @thatonemailbox 10 месяцев назад

      ​@@greybrunix What is a DWN port?

    • @nirlichtman
      @nirlichtman  10 месяцев назад +4

      @@thatonemailbox DWM is a minimalist tiling window manager for X Window System and is one of the projects of the Suckless group, I use a port of this for Windows called dwm-win32

    • @juliovata9194
      @juliovata9194 10 месяцев назад

      @@nirlichtmanOk I thought it looked like Windows Terminal lol

  • @naturebc
    @naturebc 5 месяцев назад

    Even if you’re writing code just for fun and not for production you should do it right.

  • @mattiasevstedt9340
    @mattiasevstedt9340 10 месяцев назад

    Since no one else has mentioned it, the reason for the reversed order on the numbers is because of intel based computers being "little endian".

  • @sanjaybalnad4180
    @sanjaybalnad4180 10 месяцев назад

    I am a high level programmer, usually i work with javascript and python. When i see mid and low level code my head gets burst. But i am really interested in c language 😂

    • @AnalogDude_
      @AnalogDude_ 10 месяцев назад +2

      pretty much every things is based on C/C++, java, perl, etc.

  • @NullCyan
    @NullCyan 10 месяцев назад

    I have always wondered why argv is a pointer of a pointer, why, just why does that "magically" turn into an array of strings?

    • @ahmedsat4780
      @ahmedsat4780 10 месяцев назад +2

      actually array is just a pointer , so when you create a pointer off characters it's a string ( string is an array of characters )
      pointer off string is array of strings

    • @Zeutomehr
      @Zeutomehr 10 месяцев назад +1

      arrays decay into pointers when passed as function arguments.
      within a function they were not declared in, they are identical.
      could you explain what exactly bugs you, maybe I can help you?

    • @NullCyan
      @NullCyan 10 месяцев назад +2

      @@Zeutomehr so an array is a pointer to a location of the memory with sequential data?

    • @StefanoTrevisani
      @StefanoTrevisani 10 месяцев назад +5

      well because, in the C point of view, an array is simply a contiguos block of memory, and a string is just an array with additional semantics for humans.
      A CPU does not atomically manipulate entire blocks of memory, but only small "words" (usually 8, 16, 32 or 64 bits): then, to represent a full block of memory, you can use two words, e.g. one representing the starting location of your block of memory (aka a pointer) and one representing its length. You can also use the single word (pointer) that represents its initial location, if you know that the block is ended by a special delimiter value which is not allowed to appear anywhere else inside the block.
      And in C, sequences (arrays) of characters (strings) are supposed to always terminate with the non-printable NUL character (i.e. the byte 0).
      Similarly, the NULL pointer (i.e. the word 0) is assumed to be an invalid memory address, hence it can be used to delimit an arrray of pointers.
      And this is what argv is: just a machine word telling you "hey, I am a pointer to a block of memory, and each word in the block of memory I point to is also a pointer, and the last of them is the NULL pointer. By the way, they are all pointers to blocks of memory where each byte in those blocks is a character, and the last character in them is the NUL character"

    • @StefanoTrevisani
      @StefanoTrevisani 10 месяцев назад +1

      If you wonder why argc exists, I think it is beacuse it is allowed for arguments other than the last one to be actually NULL pointers...

  • @rafaeldasilvasena9332
    @rafaeldasilvasena9332 10 месяцев назад

    In (buffer, 1, 1024, f), the "1" would be the size of what exactly?

    • @nirlichtman
      @nirlichtman  10 месяцев назад +1

      The size of each item to write/read, for example if you were writing an array of 4 byte integers you could specify the size to be 4 and the count to be the length of the array. In this video I am working with an array of 1 byte characters and so I decided to use 1 in the count - this also causes the function to return the number of bytes written/read.

    • @arta6183
      @arta6183 10 месяцев назад

      @@nirlichtman I know sizeof(char) is a bit redundant but maybe it could make it more readable, also what if a char's size is different on another platform?

    • @blacklistnr1
      @blacklistnr1 10 месяцев назад

      ​@@arta6183Then you add a disclaimer that this editor does not support quantum computers :)) All computing devices have the notion of a byte(char).
      It only gets complicated when you want unicode support and run into wchar and multi byte sequences.
      or if you go above 1 byte(short, int, long) and run into endiannes(byte order) and platform-dependent sizes.

    • @rafaeldasilvasena9332
      @rafaeldasilvasena9332 10 месяцев назад

      ​@@nirlichtmanoh, I get it now, thank you

    • @benhetland576
      @benhetland576 10 месяцев назад

      @@arta6183 sizeof(char) is always 1 -- by definition -- regardless of how many bits it contains. The sizeof operator reports the size in number of chars, not in octets or "bytes" that is commonly assumed.

  • @gabriellevesque2185
    @gabriellevesque2185 10 месяцев назад

    How can you can call a file VIEWER an Editor?

    • @nirlichtman
      @nirlichtman  10 месяцев назад

      Since it is also an editor, it has an edit command ('e') that can modify the bytes

  • @NullCyan
    @NullCyan 10 месяцев назад

    also what if the file has more than 1024 characters?

    • @ahmedsat4780
      @ahmedsat4780 10 месяцев назад +2

      it will only deal with the first 1024 of the file and ignore the rest

    • @NullCyan
      @NullCyan 10 месяцев назад

      @@ahmedsat4780 oh ok thanks

    • @xblxckxpxny1005
      @xblxckxpxny1005 10 месяцев назад

      @@ahmedsat4780 That is not totally correct.
      Yes you can only modify the first 1024 chars, but what is more important os that when you save, the characters that come after the initial 1024 will be discarded.
      In simple terms:
      You lose all characters after 1024.
      Your file: 1024 * 'A' (1024 'A' in a row)
      and then 10 * 'b' for example
      So you have 1024 AAAA... and then 10 bbbbbbbbbb
      When you execute his program and save your hex-edits. you keep your edits in 1024 'A' region. BUT you lose all the 10 bbbbbbbbbb at the end!
      If I am wrong here please correct me :) But this should be the result, I also tested.

    • @nirlichtman
      @nirlichtman  10 месяцев назад

      @@xblxckxpxny1005That is correct, part of why the code is just for fun and not for production :)

    • @ahmedsat4780
      @ahmedsat4780 10 месяцев назад

      @@xblxckxpxny1005
      i know 😊😊 but I might not be able to express it correctly because English is not my first language
      thanks for correcting me.

  • @cozyfog
    @cozyfog 10 месяцев назад +1

    bro uses windows terminal on linux

    • @myrix_dev
      @myrix_dev 10 месяцев назад

      he uses wsl, windows sumsystem for linux
      and yes, there are tiling wms for windows

    • @pavlovidankmemeovi
      @pavlovidankmemeovi 10 месяцев назад

      He's using a windows port of dwm

    • @cozyfog
      @cozyfog 10 месяцев назад

      @@myrix_dev sound cool ig, I knew of the dwm port but lately its been everywhere idk

  • @richardgrosman5798
    @richardgrosman5798 10 месяцев назад

    Better than a bot 😋

  • @ItsCOMMANDer_
    @ItsCOMMANDer_ 10 месяцев назад

    still waiting for minimalist https server 😅

    • @ZsomborBerki
      @ZsomborBerki 10 месяцев назад

      he already did that

    • @ItsCOMMANDer_
      @ItsCOMMANDer_ 10 месяцев назад

      no he didnt, he did an https CLIENT and an HTTP server.@@ZsomborBerki

  • @kamertonaudiophileplayer847
    @kamertonaudiophileplayer847 10 месяцев назад

    I duplicated the video in Rust. Is anyone interested in?

    • @nirlichtman
      @nirlichtman  10 месяцев назад +1

      That could be interesting, I haven't programmed in Rust, but I can't find the video on your channel, did you upload it?

    • @silakanveli
      @silakanveli 10 месяцев назад +2

      Please just keep going with C. This content is fantastic a
      When it is minimalistic.

    • @kamertonaudiophileplayer847
      @kamertonaudiophileplayer847 10 месяцев назад

      @@nirlichtman I didn't do a video yet, just created a similar program.