> For the complete documentation index, see [llms.txt](https://applezulab.netdpi.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://applezulab.netdpi.net/linux-prog/dos2unix-sample-code.md).

# dos2unix 程式碼

格式 DOS(windows) 與 UNIX 的差異是，\
\
DOS 以 '\r' 作為換行符號，而 UNIX 系統以 '\n' 作為換行符號，\
\
一般 Linux 系統都會有 dos2unix 與 unix2dos 的指令，\
\
如果要自行安裝的話，該指令是位於 sysutils 套件中。\
\
因為只有更換 '\r' -> '\n' 或 '\n' -> '\r' 而已，\
\
以前就練習一下，寫一個 dos2unix 的 sample.<br>

```c
/* Author : AppleZu Lab (http://applezu.netdpi.net)
 * Only translate '\r'(ASCII code: 13) to '\n'.
 * License: GNU
 */

#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<stdio.h>

int main(int argc, char *argv[])
{
  FILE *fp;
  int i;
  char buf[1024];
  char buf2[65535];

  if (argc <2) {
    printf("%s filename\n", argv[0]);
    return -2;
  }

  memset(buf, '\0', sizeof(buf));
  memset(buf2, '\0', sizeof(buf2));

  fp=fopen(argv[1], "r");

  if(fp == NULL){
    printf("Can't open %s\n", argv[1]);
      return -1;
  }

  while( fgets( buf, sizeof(buf), fp) != NULL ) {

    for(i=0; i < sizeof (buf); i++) {
      if(buf[i] == 13)
        buf[i] = '\n';
    }

    strncat(buf2, buf, strlen(buf));
  }

  fclose(fp);

  fp=fopen(argv[1], "w+");
  fputs(buf2, fp);
  fclose(fp);
  return 0;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://applezulab.netdpi.net/linux-prog/dos2unix-sample-code.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
