# dos2unix 程式碼

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

```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: 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:

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

The question should be specific, self-contained, and written in natural language.
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.
