「C 低水準ファイルアクセス」の版間の差分
(ページの作成:「==C 低水準ファイルアクセス== [Programming C] ====file descriptor==== {|class="wikitable" !file descriptor !関連付け |- |0 |標準入力 |- |1…」) |
|||
| (同じ利用者による、間の3版が非表示) | |||
| 1行目: | 1行目: | ||
| − | ==C 低水準ファイルアクセス== | + | ==[[C 低水準ファイルアクセス]]== |
| − | [Programming C] | + | [[Programming C]] | |
====file descriptor==== | ====file descriptor==== | ||
| 19行目: | 19行目: | ||
====write==== | ====write==== | ||
| − | #include | + | #include<unistd.h> |
ssize_t write(int filedes, const void *buf, size_t nbytes); | ssize_t write(int filedes, const void *buf, size_t nbytes); | ||
| 27行目: | 27行目: | ||
*global変数errnoにエラーの内容を示す値が設定される。 | *global変数errnoにエラーの内容を示す値が設定される。 | ||
| − | #include | + | #include <unistd.h> |
int main() | int main() | ||
| 37行目: | 37行目: | ||
} | } | ||
====read==== | ====read==== | ||
| − | #include | + | #include <unistd.h> |
ssize_t read(int filedes, void *buf, size_t nbytes); | ssize_t read(int filedes, void *buf, size_t nbytes); | ||
| 45行目: | 45行目: | ||
*errorが発生した場合、-1を返す | *errorが発生した場合、-1を返す | ||
| − | #include | + | #include <unistd.h> |
int main() | int main() | ||
| 64行目: | 64行目: | ||
====open==== | ====open==== | ||
| − | #include | + | #include <fcntl.h> |
| − | #include | + | #include <sys/types.h> |
| − | #include | + | #include <sys/stat.h> |
int open(const char *path, int oflags); | int open(const char *path, int oflags); | ||
int open(const char *path, int oflags, mode_t mode); | int open(const char *path, int oflags, mode_t mode); | ||
| 73行目: | 73行目: | ||
*失敗すると、-1をかえし、global変数のerrnoに値を設定 | *失敗すると、-1をかえし、global変数のerrnoに値を設定 | ||
*返された file descriptor は、read や write で利用できる。 | *返された file descriptor は、read や write で利用できる。 | ||
| − | * | + | *Openするfileまたは、de[[vi]]ce は、'path' parameterに指定する。 |
*Parameter 'oflags'には、file を open するときの動作を指定する。 | *Parameter 'oflags'には、file を open するときの動作を指定する。 | ||
| 81行目: | 81行目: | ||
!説明 | !説明 | ||
|- | |- | ||
| − | | | + | |O_[[R]]DONLY |
|読み取り専用 | |読み取り専用 | ||
|- | |- | ||
| − | | | + | |O_W[[R]]ONLY |
|書き込み専用 | |書き込み専用 | ||
|- | |- | ||
| − | | | + | |O_[[R]]DW[[R]] |
|読み書き用 | |読み書き用 | ||
|- | |- | ||
| 100行目: | 100行目: | ||
|file の最後に追加 | |file の最後に追加 | ||
|- | |- | ||
| − | | | + | |O_T[[R]]UNC |
|file の長さを 0 に切り詰め、既存の内容を破棄 | |file の長さを 0 に切り詰め、既存の内容を破棄 | ||
|- | |- | ||
| − | | | + | |O_C[[R]]EAT |
|必要に応じて、mode で指定された permission で file を作成 | |必要に応じて、mode で指定された permission で file を作成 | ||
|- | |- | ||
|O_EXCL | |O_EXCL | ||
| − | | | + | |O_C[[R]]ETとともに指定された場合、既にfileが存在していればopenは失敗。 |
|- | |- | ||
|} | |} | ||
=====初期permission===== | =====初期permission===== | ||
| − | open に | + | open に O_C[[R]]EAT flag を指定して file を作成する場合には、parameterを3つ指令する必要がある。3つ目の parameter の mode には、sys/stat.h header file で定義されている flag(以下参照) の bit 単位の論理和を指定。 |
{|class="wikitable" | {|class="wikitable" | ||
| 118行目: | 118行目: | ||
!内容 | !内容 | ||
|- | |- | ||
| − | | | + | |S_I[[R]]US[[R]] |
|owner の読み取り permission | |owner の読み取り permission | ||
|- | |- | ||
| − | | | + | |S_IWUS[[R]] |
|owner の書き込み permission | |owner の書き込み permission | ||
|- | |- | ||
| − | | | + | |S_IXUS[[R]] |
|owner の実行 permission | |owner の実行 permission | ||
|- | |- | ||
| − | | | + | |S_I[[R]]G[[R]]P |
|group の読み取り permission | |group の読み取り permission | ||
|- | |- | ||
| − | | | + | |S_IWG[[R]]P |
|group の書き込み permission | |group の書き込み permission | ||
|- | |- | ||
| − | | | + | |S_IXG[[R]]P |
|group の実行 permission | |group の実行 permission | ||
|- | |- | ||
|S_IROTH | |S_IROTH | ||
| − | |その他 user の読み取り permission | + | |[[その他]] user の読み取り permission |
|- | |- | ||
|S_IWOTH | |S_IWOTH | ||
| − | |その他 user の書き込み permission | + | |[[その他]] user の書き込み permission |
|- | |- | ||
|S_IXOTH | |S_IXOTH | ||
| − | |その他 user の実行 permission | + | |[[その他]] user の実行 permission |
|- | |- | ||
|} | |} | ||
| − | #include | + | #include <fcntl.h> |
| − | #include | + | #include <sys/types.h> |
| − | #include | + | #include <sys/stat.h> |
int main() { | int main() { | ||
int out; | int out; | ||
| − | out = open("file.out", | + | out = open("file.out", O_C[[R]]EAT, S_I[[R]]US[[R]]|S_IWUS[[R]]|S_I[[R]]OTH); |
} | } | ||
| 197行目: | 197行目: | ||
====close==== | ====close==== | ||
| − | #include | + | #include <unistd.h> |
int close(int filedes); | int close(int filedes); | ||
| 204行目: | 204行目: | ||
====ioctl==== | ====ioctl==== | ||
| − | #include | + | #include <unistd.h> |
int ioctl(int filedes, int cmd, ...); | int ioctl(int filedes, int cmd, ...); | ||
| − | + | ioctlは何でも屋的な関数で、de[[vi]]ce と その descriptor の振る舞いを制御したり、de[[vi]]ce が提供する ser[[vi]]ce を設定するための interface を提供する。 | |
| − | * | + | *さまざまなde[[vi]]ceでそれぞれ独自の呼び出しが定義されている。 |
| − | ===file | + | ===file 操作のための[[その他]]の system call=== |
====lseek==== | ====lseek==== | ||
| − | #include | + | #include <unistd.h> |
| − | #include | + | #include <sys/types.h> |
out_t lseek(int filedes, off_t offset, int whence); | out_t lseek(int filedes, off_t offset, int whence); | ||
| 220行目: | 220行目: | ||
*parameter offset には位置を指定し、whenceには offsetの以下の意味を指定 | *parameter offset には位置を指定し、whenceには offsetの以下の意味を指定 | ||
**SEEK_SET offset は絶対位置 | **SEEK_SET offset は絶対位置 | ||
| − | ** | + | **SEEK_CU[[R]] offset は現在位置から相対 |
**SEEK_END offset はfileの末尾から相対 | **SEEK_END offset はfileの末尾から相対 | ||
| − | #include | + | #include <unistd.h> |
| − | #include | + | #include <sys/stat.h> |
| − | #include | + | #include <sys/types.h> |
| − | #include | + | #include <fcntl.h> |
int main() | int main() | ||
| 233行目: | 233行目: | ||
int in, out; | int in, out; | ||
| − | in = open("file.in", | + | in = open("file.in", O_[[R]]DONLY); |
while(read(in, &c, 1) ==1) { | while(read(in, &c, 1) ==1) { | ||
write(1, &c, 1); | write(1, &c, 1); | ||
| − | lseek(in, 1, | + | lseek(in, 1, SEEK_CU[[R]]); |
} | } | ||
exit(0); | exit(0); | ||
| 242行目: | 242行目: | ||
====fstat, stat, lstat==== | ====fstat, stat, lstat==== | ||
| − | #include | + | #include <unistd.h> |
| − | #include | + | #include <sys/stat.h> |
| − | #include | + | #include <sys/types.h> |
int fstat(int filedes, struct stat *buf); | int fstat(int filedes, struct stat *buf); | ||
int stat(const char *path, struct stat *buf); | int stat(const char *path, struct stat *buf); | ||
| 265行目: | 265行目: | ||
|- | |- | ||
|st_dev | |st_dev | ||
| − | | | + | |fileが存在するde[[vi]]ce |
|- | |- | ||
|st_uid | |st_uid | ||
| 288行目: | 288行目: | ||
====dup, dup2==== | ====dup, dup2==== | ||
| − | #include | + | #include <unistd.h> |
int dup(int filedes); | int dup(int filedes); | ||
int dup2(int filedes, int filedis2); | int dup2(int filedes, int filedis2); | ||
2020年2月16日 (日) 04:23時点における最新版
目次
C 低水準ファイルアクセス
file descriptor
| file descriptor | 関連付け |
|---|---|
| 0 | 標準入力 |
| 1 | 標準出力 |
| 2 | 標準エラー |
write
#include<unistd.h> ssize_t write(int filedes, const void *buf, size_t nbytes);
file descriptor 'filedes' に関連付けられた File に buf から最初の'nbytes' byteを書き込み、実際に書き込まれたbyte数を返す。
- 戻り値が0の場合には、データが書き込まれなかったことをあらわす。
- 戻り値が-1の場合には、エラーが発生したことを表す。
- global変数errnoにエラーの内容を示す値が設定される。
#include <unistd.h>
int main()
{
if (write(1, "Here is some data\n",18) != 18) {
write(2, "A write error has occured on file descriptor 1\n", 48);
}
exit(0);
}
read
#include <unistd.h> ssize_t read(int filedes, void *buf, size_t nbytes);
file descriptor 'filedes'に関連付けられた file から、'nbytes' byte までのdataを data area 'buf' に読み込み、実際に読み込んだbyte数を返す。
- read が 0 を返す場合には、読み取るものがないことを意味する。
- errorが発生した場合、-1を返す
#include <unistd.h>
int main()
{
char buf[128];
int nread;
nread = read(0, buf, 128);
if (nread == -1) {
write(2, "A read error has occured\n", 26);
}
if ((write(1, buf, nread)) != nread) {
write(2, "A write error has occured\n", 27);
}
exit(0);
}
open
#include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> int open(const char *path, int oflags); int open(const char *path, int oflags, mode_t mode);
新しい file descriptor を作成するには、open system call を使う必要がある。
- 呼び出しが成功した場合には、file descriptor を返す
- 失敗すると、-1をかえし、global変数のerrnoに値を設定
- 返された file descriptor は、read や write で利用できる。
- Openするfileまたは、device は、'path' parameterに指定する。
- Parameter 'oflags'には、file を open するときの動作を指定する。
file access mode 必須
| mode | 説明 |
|---|---|
| O_RDONLY | 読み取り専用 |
| O_WRONLY | 書き込み専用 |
| O_RDWR | 読み書き用 |
file access mode との bit 単位の論理和によって oflags に指定できる省略可能なmode
| mode | 説明 |
|---|---|
| O_APPEND | file の最後に追加 |
| O_TRUNC | file の長さを 0 に切り詰め、既存の内容を破棄 |
| O_CREAT | 必要に応じて、mode で指定された permission で file を作成 |
| O_EXCL | O_CRETとともに指定された場合、既にfileが存在していればopenは失敗。 |
初期permission
open に O_CREAT flag を指定して file を作成する場合には、parameterを3つ指令する必要がある。3つ目の parameter の mode には、sys/stat.h header file で定義されている flag(以下参照) の bit 単位の論理和を指定。
| flag | 内容 |
|---|---|
| S_IRUSR | owner の読み取り permission |
| S_IWUSR | owner の書き込み permission |
| S_IXUSR | owner の実行 permission |
| S_IRGRP | group の読み取り permission |
| S_IWGRP | group の書き込み permission |
| S_IXGRP | group の実行 permission |
| S_IROTH | その他 user の読み取り permission |
| S_IWOTH | その他 user の書き込み permission |
| S_IXOTH | その他 user の実行 permission |
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int main() {
int out;
out = open("file.out", O_CREAT, S_IRUSR|S_IWUSR|S_IROTH);
}
-rw----r-- 1 root root 0 9月 9 20:10 file.out
umask
file 作成時に使用される system 変数で、file permission の mask をあらわす。
- open または create を呼び出して file を作成する場合、mode parameter と umask の値が比較され、mode で指定されているbitのうち、umask でもセットされているbitがclearされる。
- 3桁の8進数を指定する
| 桁 | 内容 |
|---|---|
| 1桁目 | userのpermission |
| 2桁目 | groupのpermission |
| 3桁目 | 他のuserのpermission |
| 値 | 内容 |
|---|---|
| 0 | permissionを許可 |
| 4 | 読み取りpermissionを不許可 |
| 2 | 書き込みpermissionを不許可 |
| 1 | 実行permissionを不許可 |
close
#include <unistd.h> int close(int filedes);
close system call は、file descriptor filedes と file との関連付けを終了する。
- 成功した場合、0を返し、エラーが発生した場合、-1を返す。
ioctl
#include <unistd.h> int ioctl(int filedes, int cmd, ...);
ioctlは何でも屋的な関数で、device と その descriptor の振る舞いを制御したり、device が提供する service を設定するための interface を提供する。
- さまざまなdeviceでそれぞれ独自の呼び出しが定義されている。
file 操作のためのその他の system call
lseek
#include <unistd.h> #include <sys/types.h> out_t lseek(int filedes, off_t offset, int whence);
file descriptor の読み取り/書き込みpointerをsetします。
- 次にfileの読み取り/書き込み位置を指定することができる。
- pointerはfaile内の絶対一か、現在の位置またはfileの最後からの相対位置を設定できる。
- parameter offset には位置を指定し、whenceには offsetの以下の意味を指定
- SEEK_SET offset は絶対位置
- SEEK_CUR offset は現在位置から相対
- SEEK_END offset はfileの末尾から相対
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
int main()
{
char c;
int in, out;
in = open("file.in", O_RDONLY);
while(read(in, &c, 1) ==1) {
write(1, &c, 1);
lseek(in, 1, SEEK_CUR);
}
exit(0);
}
fstat, stat, lstat
#include <unistd.h> #include <sys/stat.h> #include <sys/types.h> int fstat(int filedes, struct stat *buf); int stat(const char *path, struct stat *buf); int lstat(const char *path, struct stat *buf);
- fstatは openされたfile descriptorに関連付けられているfileのstatus情報を返す。
- statとlstatは、指定された名前のfileに関するstatus情報を返す。
- file が symbolic linkの場合、lstatはlinkそれ自体、statはlinkが指し示すfileの情報を返す。
構造体statのmember
| member | 説明 |
|---|---|
| st_mod | fileのpermission情報 |
| st_ino | fileに関連付けられたi-node |
| st_dev | fileが存在するdevice |
| st_uid | fileのownerのuser id |
| st_gid | fileのownerのgroup id |
| st_atime | 最終access時刻 |
| st_ctime | mode、owner、group、または内容の最終変更時刻 |
| st_mtime | 内容の最終修正時刻 |
| st_nlink | fileへのhard linkの数 |
dup, dup2
#include <unistd.h> int dup(int filedes); int dup2(int filedes, int filedis2);
- dup は file descriptor を複製し、同じfileに異なる複数のfile descriptor で access できるようにする。
- dup2 は file descriptor を copy する。
この本からの覚書。
© 2006 矢木浩人