pretty code

顯示具有 DOS 標籤的文章。 顯示所有文章
顯示具有 DOS 標籤的文章。 顯示所有文章

2018年8月23日 星期四

Delete file in C

最近分別需要在 Windwos, UEFI, DOS 撰寫同樣的 tool
趁著寫完的空檔,整理一下之前沒有注意到的小細節

在這 3 個系統中,我共用了大部份的 code,只有針對系統的差異做 porting
其中我需要 1 個砍檔案的 function (unlink)

最早我是先在 UEFI 使用這個功能
原本以為只有 UEFI 有

後來在陸續 porting 到 Windows and DOS 時
才發現大家都有這個函數

上網查了一下
這個不是 standard c
而是 POSIX 標準
難怪我的 gcc 也會有 (Windows)

底下是 3 個環境 include header file 的位置
UEFI(UDK code) - <sys/EfiSysCall.h>
Windows(TDM GCC) - <io.h>
DOS(Open Watcom) - <io.h>

2018年3月1日 星期四

Rufus dos 冷知識

Rufus 是一個製作 dos 開機碟的工具。

原本以為 Rufus 是把開機相關檔案包進 .h 檔案裡,後來看到網路上的文章,才知道 Rufus 的 MS-DOS 是從 Windows 裡的檔案解壓縮出來的,﹝C:\Windows\system32\diskcopy.dll﹞。

這也解釋了為什麼在 Windows 10 下無法製作 MS-DOS 開機碟,因為 Windows 10 已經沒有這個檔案了。

src/dos.c

/*
 * Rufus: The Reliable USB Formatting Utility
 * DOS boot file extraction, from the FAT12 floppy image in diskcopy.dll
 * (MS WinME DOS) or from the embedded FreeDOS resource files
 * Copyright © 2011-2017 Pete Batard 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

/* Memory leaks detection - define _CRTDBG_MAP_ALLOC as preprocessor macro */

2017年6月27日 星期二

討厭的 DOS 程式


1. Enter Linux
2. make all
3. ./makedos.sh
4. make dos
5. copy exe to bootable disk

2017年6月9日 星期五

DOS network

https://www.lazybrowndog.net/freedos/virtualbox/

上面是教導如何在 DOS 使用網路並存取網路磁碟機的方法
經實測 DOS <-> Windows 7 確定是可以互通的

另外如果是想自己寫程式發送 TCP or UDP 封包
可以考慮  http://www.brutman.com/mTCP/ 提供的 mTCP-src_2013-05-23.zip
並參考 \APPS\HTGET 這支範例

編譯方式也很簡單
1. 開啟 Open Watcom Build Environment cmd windows
2. cd \APPS\HTGET
3. wmake -f MAKEFILE

據實測結果,在另一台電腦用 Go 寫的 TCPServer 確定可以收到封包
不過常常送了幾次後,DOS 端的 socket 就會關閉
目前還沒有時間研究這個,等以後再來查看


TCPServer - Golang

package main

import "net"
import "fmt"

func main() {
    port := 8080

    fmt.Printf("Launching TCP server port %d\n", port)

    ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
    if err != nil {
        fmt.Println(err)
        return
    }

    for {
        conn, err := ln.Accept()
        if err != nil {
            fmt.Println(err)
            continue
        }

        go handleRequest(conn)
    }
}

func handleRequest(conn net.Conn) {
    defer conn.Close()

    remoteIP := conn.RemoteAddr().String()

    buf := make([]byte, 1024)

    fmt.Println("--------------------")
    fmt.Println(remoteIP)

    for {
        reqLen, err := conn.Read(buf)
        if err != nil {
            //fmt.Println(err)
            break
        }

        if reqLen > 0 {
            fmt.Printf("%s(%d)\n", string(buf[:reqLen]), reqLen)
        }
    }

    conn.Close()

    fmt.Println("socket closed")
}


TCPSend - DOS (僅列出修改部份,其他都是原本的)

int sendTCP(char *msg, int len)
{
  int sendLen = 0;
  int rc;

  while (sendLen < len)
  {
    rc = sock->send( (uint8_t *)(msg+sendLen), len-sendLen);
    PACKET_PROCESS_SINGLE;
    Arp::driveArp( );
    Tcp::drivePackets( );

    if (rc > 0) {
      sendLen += rc;
    }
    else if ( rc == 0 ) {
      // Out of send buffers maybe?  Loop around to process packets
      printf("sock->send error ?\n");
      return -1;
    }
    else {
      return -1;
    }
  }

  return 0;
}

int main( int argc, char *argv[] ) {

  // Initialize TCP/IP
  if ( Utils::parseEnv( ) != 0 ) {
    exit(1);
  }

  if ( Utils::initStack( 1, TCP_SOCKET_RING_SIZE ) ) {
    fprintf( stderr, "\nFailed to initialize TCP/IP - exiting\n" );
    exit(1);
  }

  // From this point forward you have to call the shutdown( ) routine to
  // exit because we have the timer interrupt hooked.

  // Start
  Utils::Debugging |= 0xff;
  ServerPort = 8080;
  strcpy(Hostname, "192.168.11.9");

  fprintf( stderr, "Server: %s:%u\n", Hostname, ServerPort);

  if ( resolve(Hostname, HostAddr) ) shutdown( 1 );

  if ( connectSocket( ) ) shutdown( 1 );


  // send
  int rc;
  char data[64];
  int sent_bytes;
  int sendstrlen;

  printf("input message to send or 'q' to exit \n\n");

  while (1)
  {
    memset(data, 0x00, sizeof(data));
    fgets(data, sizeof(data) - 1, stdin);
    if (data[0] == 'q')
    {
      printf("ready to exit connection\n");
      break;
    }

    sendstrlen = strlen(data);
    if (sendstrlen > 0 && sendstrlen < 64)
    {
      rc = sendTCP(data, sendstrlen);
      printf("rc = %d \n", rc);
    }
  }

  shutdown( rc );
}