Skip to content

add sasa.c #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions cat/sasa.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <unistd.h>
#include <fcntl.h>


int main(int argc, char * argv[]){
int fdescript;
ssize_t readbytes;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

どうでもいいけどタブとスペースが混ざっている気がする

ssize_t writebytes;
char buf[1024];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1024 みたいなマジックナンバーはstatic const#defineにしたいね

for (int i = 1; i < argc; i++) {
fdescript = open(argv[i], O_RDONLY);
if (fdescript == -1)
continue;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

インデントが揃っていなくてわかりにくいけど、"引数を複数指定したら最初に開けたファイルを出力する"という仕様になっているっぽい?
オリジナルのcatの仕様(となぜcatという名前がついているか)を確認してみるといいかも

while(1) {
readbytes = read(fdescript,buf,1024);
if (readbytes > 0 && readbytes <= 1024) {
writebytes = write(1,buf,readbytes);
} else if (readbytes == 0) {
close(fdescript);
writebytes = write(1,"\n",1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

writebytesは代入しているけど使っていないのかな?
書き込み失敗した時のエラーハンドリングもしたいところですね

break;
} else {
break;
}
}
}