3
UNIX
UNIX, , : , , , , .
1. UNIX .
2. .
3. , .
4. , UNIX.
5. , .
UNIX : , , , , .
, UNIX. .
, : , , : , , , .. , , , "." "..". , ". ", , . , " .. ", , . , , /. " ", , , "" . UNIX , - .
- , , UNIX . , . , : , / .. , , - , .
. UNIX. / , / . , , : , , , . , , . , , , . , , - .
|
|
UNIX .
1
inode | / | |||||
Open, creat, dup, pipe, close | Open, creat, chdir, chmod, stat, mkfifo, mound, mknod, link, unmount, unlink, chown | Creat, link, unlink, mknod | Chown, chmod, stat | Read, write, lseek | Mount, unmount | Chmod, chown |
, . stat() (fstat()). stat() (fstat()):
#include <sys/types.h>
#include <sys/stat.h>
int stat(const char *name, struct stat *stbuf)
int fstat(int fd, struct stat *stbuf)
( name, - fd) , stbuf. ; 0, -1 errno. stat <sys/stat.h>. :
struct stat
{
dev_t st_dev; /* device file */
ino_t st_ino; /* file serial inode */
ushort st_mode; /* file mode */
short st_nlink; /* number of links */
ushort st_uid; /* user ID */
ushort st_gid; /* group ID */
dev_t st_rdev; /* device ident */
off_t st_size; /* size of file */
time_t st_atime; /* last access time */
time_t st_mtime; /* last modify time */
time_t st_ctime; /* last status change */
}
st_mode stat , . :
S_IFMT 0170000 -
S_IFDIR 0040000 -
S_IFCHR 0020000 - -
S_IFBLK 0060000 - -
S_IFREG 0100000 -
S_IFFIFO 0010000 - FIFO
S_ISUID 04000 -
S_ISGID 02000 -
S_ISVTX 01000 -
S_ISREAD 00400 -
S_IWRITE 00200 -
S_IEXEC 00100 - .
, S_IF, .
, , dirent, <dirent.h>
struct dirent
{
ino_t d_ino; /* */
|
|
char d_name[DIRSIZ]; /* */
}
, d_ino 0 ( I- 1, 0). () , I- , , : d_ino=0; d_ino==0 - .
(creat, link, mknod) ( ) d_ino==0, ( ). - . : "." - ( I-node), ".." - . "/" (.. d_ino==2). . "..".mkdir():
#include <sys/types.h>
#include <sys/stat.h>
int mkdir (char *pathname, mode_t mode);
rmdir().
#include <unistd.h>
int rmdir (char *pathname);
opendir() closedir():
#include <sys/types.h>
#include <dirent.h>
DIR *opendir (char *dirname);
DIR, , <dirent.h> . NULL.
#include <dirent.h>
int closedir (DIR *dirptr); dirptr - .
chdir():
#include <unistd.h>
int chdir (char *pathname);
readdir():
#include <sys/types.h>
#include <dirent.h>
struct dirent *readdir (DIR *dirptr);
readdir() dirent, . , .
void rewinddir (DIR *dirptr);
.
getcwd pathbuf. n . , , , n. #include <direct.h> char *getcwd(pathbuf,n); char *pathbuf - path-, int n - . pathbuf NULL; n malloc path-. getcwd pathbuf. NULL errno : ENOMEM n ( NULL pathbuf). ERANGE Path- , n .
GCC (GNU Compiler Collection), UNIX. (ee, vi ..). .c, gcc 1.c ( 1. ). a.out, , , :./a.out. g a.out. -, -o , : g 1.c o primer. primer, ./primer.
|
|
, :
//
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
//
#define DIRNAME "."
#define OFNAME "out.log"
int main(void)
{
FILE *ofp;
DIR *dp;
struct dirent *dent;
if((dp = opendir(DIRNAME)) == NULL) {
// , 1
fprintf(stderr, "opendir: %s: %s\n", DIRNAME, strerror(errno));
return 1;
}
if((ofp = fopen(OFNAME, "w")) == NULL) {
fprintf(stderr, "fopen: %s: %s\n", OFNAME, strerror(errno));
return 1;
}
//
while(dent = readdir(dp))
// , . ..
// strcmp 0,
if(strcmp(".", dent->d_name) && strcmp("..", dent->d_name))
//
fprintf(ofp, "%s\n", dent->d_name);
closedir(dp);
fclose(ofp);
return 0; // 0,
}
1. , . , , , .
2. , . , , , .
3. , . .
4. , , . .
5. , . .
6. ls, ( ".") . "." ".." . , .
1. ?
2. , ?
3. ?
4. stat?
5. UNIX?
6. , (r, w, x), - ?
- UNIX?