日記の練習です。
もしかしたら写経は Cosense でする方が良いかも知れない。
https://copyanddestroy.hatenablog.com/entry/2025/02/25/172747
ということで Cosense で写経してみる。
5.6 cat コマンドを作る - taizooo
gyazo.com
Cosense のコードブロック、code:cat.c
っていうふうに、同じファイル名を付加してコードブロックを分割しても(つまり、途中になんかコメントを記入しても)、コードブロックには別途 URL が割り当てられて、こんなふうにコード全文が取得できる。便利な気がする。どう使うかは思いつかない。
/api/code/taizooo/5.6 cat コマンドを作る/cat.c
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> static void do_cat(const char *path); static void die(const char *s); int main(int argc, char *argv[]) { int i; if (argc < 2) { fprintf(stderr, "%s: file name not given\n", argv[0]); exit(1); } for (i=1; i < argc; i++) { do_cat(argv[i]); } exit(0); } #define BUFFER_SIZE 2048 static void do_cat(const char *path) { int fd; unsigned char buf[BUFFER_SIZE]; int n; fd = open(path, O_RDONLY); if (fd < 0) die(path); for (;;) { n = read(fd, buf, sizeof buf); if (n < 0) die(path); if (n == 0) break; if (write(STDOUT_FILENO, buf, n) < 0) die(path); } if (close(fd) < 0) die(path); } static vod die(const char *s) { perror(s); exit(1); }
とか、わかったようなことを書いているけど、全然、進捗がない。そもそもまだこのソースコードはコンパイルしていない。コンパイルする前に読もうとしている。
まずはちゃんと C言語 を学習する必要があるのは、重々承知している。特にポインタ変数。