, , - - "" ! x  S_IEXEC.  . , / , (chdir), ( namei() ): - ; I- d_ino; I- . "" - . - (.. ls); "" - ( , , , stat, chdir). .. "" read, "" - namei. "" " "; - I-nod . t sticky bit S_ISVTX - , : , , - . . . : : chmod o-w,+t  BSD , , . , : opendir, closedir, readdir. , ls . . , 1992-95 - 192 - UNIX #include <stdio.h> #include <sys/types.h> #include <dirent.h> int listdir(char *dirname){ register struct dirent *dirbuf; DIR *fddir; ino_t dot_ino = 0, dotdot_ino = 0; if((fddir = opendir (dirname)) == NULL){ fprintf(stderr, "Can't read %s\n", dirname); return 1; } /* */ while ((dirbuf = readdir (fddir)) != NULL ) { if (dirbuf->d_ino == 0) continue; if (strcmp (dirbuf->d_name, "." ) == 0){ dot_ino = dirbuf->d_ino; continue; } else if(strcmp (dirbuf->d_name, "..") == 0){ dotdot_ino = dirbuf->d_ino; continue; } else printf("%s\n", dirbuf->d_name); } closedir (fddir); if(dot_ino == 0) printf(" : \".\"\n"); if(dotdot_ino == 0) printf(" : \"..\"\n"); if(dot_ino && dot_ino == dotdot_ino) printf(" \n"); return 0; } int main(int ac, char *av[]){ int i; if(ac > 1) for(i=1; i < ac; i++) listdir(av[i]); else listdir("."); return 0; } , '\0' d_name, - readdir(). 6.1.4. , argv. stat, (/). - . ( , "." "..") - rmdir(_); ( - errno EEXIST); ( ) unlink(_); , , , " ?". 6.1.5. . U42 (BSD 4.2). . , 1992-95 - 193 - UNIX /*#!/bin/cc -DFIND -DU42 -DMATCHONLY treemk.c match.c -o tree -lx * ( & ). * : * BSD-4.2 BSD-4.3 -DU42 * XENIX .. * XENIX -lx -DU42 * -DFIND * -DRM_REC * _ */ #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/param.h> /* MAXPATHLEN */ #if defined(M_XENIX) && defined(U42) # include <sys/ndir.h> /* XENIX + U42 */ #else # include <dirent.h> # define stat(f,s) lstat(f,s) /* */ # define d_namlen d_reclen #endif /* : */ #define isdir(st) ((st.st_mode & S_IFMT) == S_IFDIR) struct stat st; /* stat() */ char buf[MAXPATHLEN+1]; /* */ #define FAILURE (-1) /* */ #define SUCCESS 1 /* */ #define WARNING 0 /* */ /* : */ #ifndef ERR_CANT_READ # define ERR_CANT_READ(name) \ fprintf( stderr, "\t \"%s\"\n", name), WARNING # define ERR_NAME_TOO_LONG() \ fprintf( stderr, "\t \n" ), WARNING #endif /* . */ extern char *strrchr(char *, char); int directory (char *name, int level, int (*enter)(char *full, int level, struct stat *st), int (*leave)(char *full, int level), int (*touch)(char *full, int level, struct stat *st)); /* - enter, leave, touch * (-1) , * >= 0 . */ . , 1992-95 - 194 - UNIX /* rootdir */ int walktree ( char *rootdir, /* */ int (*enter)(char *full, int level, struct stat *st), int (*leave)(char *full, int level), int (*touch)(char *full, int level, struct stat *st) ){ /* */ if( stat(rootdir, &st) < 0 || !isdir(st)){ fprintf( stderr, "\t \"%s\"\n", rootdir ); return FAILURE; /* */ } strcpy (buf, rootdir); return act (buf, 0, enter, leave, touch); } /* name. */ int act (char *name, int level, int (*enter)(char *full, int level, struct stat *st), int (*leave)(char *full, int level), int (*touch)(char *full, int level, struct stat *st)) { if (stat (name, &st) < 0) return WARNING; /* , */ if(isdir(st)){ /* */ if(enter) if( enter(name, level, &st) == FAILURE ) return FAILURE; return directory (name, level+1, enter, leave, touch); } else { /* */ if(touch) return touch (name, level, &st); else return SUCCESS; } } . , 1992-95 - 195 - UNIX /* : */ int directory (char *name, int level, int (*enter)(char *full, int level, struct stat *st), int (*leave)(char *full, int level), int (*touch)(char *full, int level, struct stat *st)) { #ifndef U42 struct direct dirbuf; int fd; #else register struct dirent *dirbuf; DIR *fd; extern DIR *opendir(); #endif char *nbp, *tail, *nep; int i, retcode = SUCCESS; #ifndef U42 if ((fd = open (name, 0)) < 0) { #else if ((fd = opendir (name)) == NULL) { #endif return ERR_CANT_READ(name); } tail = nbp = name + strlen (name); /* \0 */ if( strcmp( name, "/" )) /* "/" */ *nbp++ = '/'; *nbp = '\0'; #ifndef U42 if (nbp + DIRSIZ + 2 >= name + MAXPATHLEN) { *tail = '\0'; return ERR_NAME_TOO_LONG(); } #endif #ifndef U42 while (read(fd, (char *) &dirbuf, sizeof(dirbuf)) == sizeof(dirbuf)){ if (dirbuf.d_ino == 0) /* */ continue; if (strcmp (dirbuf.d_name, "." ) == 0 || strcmp (dirbuf.d_name, "..") == 0) /* */ continue; for (i = 0, nep = nbp; i < DIRSIZ; i++) *nep++ = dirbuf.d_name[i]; # else /*U42*/ while ((dirbuf = readdir (fd)) != NULL ) { if (dirbuf->d_ino == 0) continue; if (strcmp (dirbuf->d_name, "." ) == 0 || strcmp (dirbuf->d_name, "..") == 0) continue; for (i = 0, nep = nbp; i < dirbuf->d_namlen ; i++) *nep++ = dirbuf->d_name[i]; #endif /*U42*/ *nep = '\0'; if( act(name, level, enter, leave, touch) == FAILURE) { retcode = FAILURE; break; } } . , 1992-95 - 196 - UNIX #ifndef U42 close (fd); #else closedir(fd); #endif *tail = '\0'; /* name */ if(retcode != FAILURE && leave) if( leave(name, level) == FAILURE) retcode = FAILURE; return retcode; } /* -------------------------------------------------------------- */ /* Disk Usage -- , */ /* -------------------------------------------------------------- */ /* */ #define KB(s) (((s)/1024L) + ((s)%1024L ? 1L:0L)) /* #define KB(s) (((s) + 1024L - 1) / 1024L) */ long size; /* */ long nfiles; /* */ long ndirs; /* */ #define WARNING_LIMIT 150L /* */ static int du_touch (char *name, int level, struct stat *st){ long sz; size += (sz = KB(st->st_size)); /* . */ nfiles++; #ifndef TREEONLY if( sz >= WARNING_LIMIT ) fprintf(stderr,"\t! \"%s\" : %ld .\n", name, sz); #endif /*TREEONLY*/ return SUCCESS; } static int du_enter (char *name, int level, struct stat *st){ #ifndef TREEONLY fprintf( stderr, " \"%s\"\n", name ); #endif size += KB(st->st_size); /* . */ nfiles++; ++ndirs; return SUCCESS; } long du (char *name){ size = nfiles = ndirs = 0L; walktree(name, du_enter, NULL, du_touch ); return size; } . , 1992-95 - 197 - UNIX /* -------------------------------------------------------------- */ /* */ /* -------------------------------------------------------------- */ int deleted; /* */ static int recrm_dir (char *name, int level){ if( rmdir(name) >= 0){ deleted++; return SUCCESS; } fprintf(stderr, " rmdir '%s'\n", name); return WARNING; } static int recrm_file(char *name, int level, struct stat *st){ if( unlink(name) >= 0){ deleted++; return SUCCESS; } fprintf(stderr, " rm '%s'\n", name); return WARNING; } int recrmdir(char *name){ int ok_code; deleted = 0; ok_code = walktree(name, NULL, recrm_dir, recrm_file); printf(" %d %s\n", deleted, name); return ok_code; } /* -------------------------------------------------------------- */ /* ( ) */ /* -------------------------------------------------------------- */ char *find_PATTERN; static int find_check(char *fullname, int level, struct stat *st){ char *basename = strrchr(fullname, '/'); if(basename) basename++; else basename = fullname; if( match(basename, find_PATTERN)) printf("Level#%02d %s\n", level, fullname); if( !strcmp( basename, "core")){ printf(" %s, .\n", fullname); return FAILURE; } return SUCCESS; } void find (char *root, char *pattern){ find_PATTERN = pattern; walktree(root, find_check, NULL, find_check); } . , 1992-95 - 198 - UNIX /* -------------------------------------------------------------- */ #ifndef TREEONLY void main(int argc, char *argv[]){ #ifdef FIND if(argc != 3){ fprintf(stderr, "Arg count\n"); exit(1); } find(argv[1], argv[2]); #else # ifdef RM_REC for(argv++; *argv; argv++) recrmdir(*argv); # else du( argc == 1 ? "." : argv[1] ); printf( "%ld %ld .\n", size, nfiles ); printf( "%ld .\n", ndirs ); # endif #endif exit(0); } #endif /*TREEONLY*/ 6.1.6. , . - mkdir(_, _); 6.1.7. , , - , , . , . UNIX rm -r ___ 6.1.8. , find, : - , ( - match() " "); - : S_IFREG, (st.st_mode & 0111) != 0 , stat . 6.2. UNIX. 6.2.1. , , , , , , 00 00 00 1 1970 . : long ( time_t). , - "" , long , , , - , - , ...; - . UNIX ; #include <sys/types.h> #include <time.h> time_t t = time(NULL); /* time(&t); */ struct tm *tm = localtime( &t ); . , 1992-95 - 199 - UNIX , int- : tm_year ( 1900) tm_yday 0..365 tm_mon 0..11 (0 - ) tm_mday 1..31 tm_wday 0..6 (0 - ) tm_hour 0..23 tm_min 0..59 tm_sec 0..59 , : char *months[] = { "", "", ..., "" }; printf( "%s\n", months[ tm->tm_mon ] ); . stime(&t); 6.2.2. :: --. time() localtime(). ctime(), : /* Mon Mar 25 18:56:36 1991 */ #include <stdio.h> #include <time.h> main(){ /* date */ time_t t = time(NULL); char *s = ctime(&t); printf("%s", s); } , s '\n'. 6.2.3. stat, stat(), time_t st_ctime, st_mtime st_atime - I- , . - st_ctime ( - ) creat, chmod, chown, link, unlink, mknod, utime|-, write (.. ); ; - st_mtime - write, creat, mknod, utime; (); - st_atime - read, creat, mknod, utime; (). typeOf(), . ____________________ |- . utime(ߔ, NULL); make - touch. . . , 1992-95 - 200 - UNIX 6.2.4. ls -tm, - , st_mtime, . stat; , - . 6.2.5. , (st_size). 6.2.6. ls -l, - rwxrw-r--. stat stat( ߔ, &st);  = st.st_mode & 0777; chmod(_, _); , 0777 | S_ISUID | S_ISGID | S_ISVTX ( <sys/stat.h>).  (. typeOf) . - . I- : d_ino st_ino stat. 6.2.7. , 2 - - : #include <sys/types.h> #include <sys/stat.h> extern char *ctime(); main(){ time_t last; struct stat st; for( stat(".", &st), last=st.st_mtime; ; sleep(2)){ stat(".", &st); if(last != st.st_mtime){ last = st.st_mtime; printf(" - : %s", ctime(&last)); } } } ,  () ( , - ). 6.2.8. , , - , . st_atime, st_mtime stat() < 0 . - . 6.2.9. UNIX- ( ) . "" : ; , ; - - , . . , 1992-95 - 201 - UNIX , , ( ). : sleep alarm ( , ). #include <sys/types.h> #include <signal.h> #include <sys/time.h> void do_nothing() {} /* usec () */ void usleep(unsigned int usec) { struct itimerval new, old; /* struct itimerval : struct timeval it_interval; struct timeval it_value; struct timeval : long tv_sec; -- long tv_usec; -- */ struct sigaction new_vec, old_vec; if (usec == 0) return; /* tv_sec . tv_usec . it_value - , "", SIGALRM. , , . it_interval - , "" ( ). , , "". */ new.it_interval.tv_sec = 0; new.it_interval.tv_usec = 0; new.it_value.tv_sec = usec / 1000000; new.it_value.tv_usec = usec % 1000000; . , 1992-95 - 202 - UNIX /* SIGALRM old_vec, do_nothing() */ new_vec.sa_handler = do_nothing; sigemptyset(&new_vec.sa_mask); new_vec.sa_flags = 0; sighold(SIGALRM); sigaction(SIGALRM, &new_vec, &old_vec); /* new, . * old. * &old NULL - . */ setitimer(ITIMER_REAL, &new, &old); /* SIGALRM */ sigpause(SIGALRM); /* SIGALRM */ sigaction(SIGALRM, &old_vec, (struct sigaction *) 0); sigrelse(SIGALRM); /* */ setitimer(ITIMER_REAL, &old, (struct itimerval *) 0); } 6.2.10. - , ( ). gettimeofday #include <time.h> void main(){ struct timeval timenow; gettimeofday(&timenow, NULL); printf("%u sec, %u msec\n", timenow.tv_sec, timenow.tv_usec ); printf("%s", ctime(&timenow.tv_sec)); exit(0); } tv_sec , 1 1970 ; time. tv_usec ( 1000000). 6.2.11. , fork() exit(). : ( - ) ( ). , , times(). , , ( fork). . , 1992-95 - 203 - UNIX struct tms { clock_t tms_utime; clock_t tms_stime; clock_t tms_cutime; clock_t tms_cstime; }; #include <sys/times.h> struct tms time_buf; clock_t real_time = times(&time_buf); "" - . ( Solaris): #include <unistd.h> clock_t HZ = sysconf(_SC_CLK_TCK); , , 60 (60 - ). 100. : tms_utime , . tms_stime , . tms_cutime , : tms_utime tms_cutime ( - ). tms_cstime , : tms_stime tms_cstime ( ). real_time , . . : #include <stdio.h> #include <unistd.h> /* _SC_CLK_TCK */ #include <signal.h> /* SIGALRM */ #include <sys/time.h> /* */ #include <sys/times.h> /* struct tms */ struct tms tms_stop, tms_start; clock_t real_stop, real_start; clock_t HZ; /* ticks */ . , 1992-95 - 204 - UNIX /* */ void hello(void){ real_start = times(&tms_start); } /* */ void bye(int n){ real_stop = times(&tms_stop); #ifdef CRONO /* */ tms_stop.tms_utime -= tms_start.tms_utime; tms_stop.tms_stime -= tms_start.tms_stime; #endif /* */ printf("User time = %g seconds [%lu ticks]\n", tms_stop.tms_utime / (double)HZ, tms_stop.tms_utime); printf("System time = %g seconds [%lu ticks]\n", tms_stop.tms_stime / (double)HZ, tms_stop.tms_stime); printf("Children user time = %g seconds [%lu ticks]\n", tms_stop.tms_cutime / (double)HZ, tms_stop.tms_cutime); printf("Children system time = %g seconds [%lu ticks]\n", tms_stop.tms_cstime / (double)HZ, tms_stop.tms_cstime); printf("Real time = %g seconds [%lu ticks]\n", (real_stop - real_start) / (double)HZ, real_stop - real_start); exit(n); } /* SIGALRM - */ void onalarm(int nsig){ printf(" #%d ================\n", getpid()); bye(0); } /* */ void dochild(int n){ hello(); printf(" #%d ================\n", getpid()); signal(SIGALRM, onalarm); /* SIGALRM 1 + n*3 */ alarm(1 + n*3); for(;;){} /* user mode */ } . , 1992-95 - 205 - UNIX #define NCHLD 4 int main(int ac, char *av[]){ int i; /* */ HZ = sysconf(_SC_CLK_TCK); setbuf(stdout, NULL); hello(); for(i=0; i < NCHLD; i++) if(fork() == 0) dochild(i); while(wait(NULL) > 0); printf(" MAIN =================\n"); bye(0); return 0; } : #3883 ================ #3884 ================ #3885 ================ #3886 ================ #3883 ================ User time = 0.72 seconds [72 ticks] System time = 0.01 seconds [1 ticks] Children user time = 0 seconds [0 ticks] Children system time = 0 seconds [0 ticks] Real time = 1.01 seconds [101 ticks] #3884 ================ User time = 1.88 seconds [188 ticks] System time = 0.01 seconds [1 ticks] Children user time = 0 seconds [0 ticks] Children system time = 0 seconds [0 ticks] Real time = 4.09 seconds [409 ticks] #3885 ================ User time = 4.41 seconds [441 ticks] System time = 0.01 seconds [1 ticks] Children user time = 0 seconds [0 ticks] Children system time = 0 seconds [0 ticks] Real time = 7.01 seconds [701 ticks] #3886 ================ User time = 8.9 seconds [890 ticks] System time = 0 seconds [0 ticks] Children user time = 0 seconds [0 ticks] Children system time = 0 seconds [0 ticks] Real time = 10.01 seconds [1001 ticks] MAIN ================= User time = 0.01 seconds [1 ticks] System time = 0.04 seconds [4 ticks] Children user time = 15.91 seconds [1591 ticks] Children system time = 0.03 seconds [3 ticks] Real time = 10.41 seconds [1041 ticks] , 72+188+441+890=1591 ( tms_cutime main). 6.2.12. : . : timer ls -l . , 1992-95 - 206 - UNIX /* */ #include <stdio.h> #include <unistd.h> #include <sys/times.h> extern errno; typedef struct _timeStamp { clock_t real_time; clock_t cpu_time; clock_t child_time; clock_t child_sys, child_user; } TimeStamp; TimeStamp TIME(){ struct tms tms; TimeStamp st; st.real_time = times(&tms); st.cpu_time = tms.tms_utime + tms.tms_stime + tms.tms_cutime + tms.tms_cstime; st.child_time = tms.tms_cutime + tms.tms_cstime; st.child_sys = tms.tms_cstime; st.child_user = tms.tms_cutime; return st; } void PRTIME(TimeStamp start, TimeStamp stop){ clock_t HZ = sysconf(_SC_CLK_TCK); clock_t real_time = stop.real_time - start.real_time; clock_t cpu_time = stop.cpu_time - start.cpu_time; clock_t child_time = stop.child_time - start.child_time; printf("%g real, %g cpu, %g child (%g user, %g sys), %ld%%\n", real_time / (double)HZ, cpu_time / (double)HZ, child_time / (double)HZ, stop.child_user / (double)HZ, stop.child_sys / (double)HZ, (child_time * 100L) / (real_time ? real_time : 1) ); } . , 1992-95 - 207 - UNIX TimeStamp start, stop; int main(int ac, char *av[]){ char *prog = *av++; if(*av == NULL){ fprintf(stderr, "Usage: %s command [args...]\n", prog); return(1); } start = TIME(); if(fork() == 0){ execvp(av[0], av); perror(av[0]); exit(errno); } while(wait(NULL) > 0); stop = TIME(); PRTIME(start, stop); return(0); } 6.3. . 6.3.1. ustat() - , ( - ): #include <sys/types.h> #include <sys/stat.h> #include <ustat.h> struct stat st; struct ustat ust; void main(int ac, char *av[]){ char *file = (ac==1 ? "." : av[1]); if( stat(file, &st) < 0) exit(1); ustat(st.st_dev, &ust); printf(" %*.*s\n" "%ld (%ld )\n" "%d I-\n", sizeof ust.f_fname, sizeof ust.f_fname, ust.f_fname, /* () */ ust.f_tfree, /* 512 */ (ust.f_tfree * 512L) / 1024, ust.f_tinode ); } printf: , - , ANSI C : char s[] = "This is" " a line " "of words"; char s[] = "This is a line of words"; 6.3.2. , , statvfs - - . : . . , 1992-95 - 208 - UNIX #include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdarg.h> #include <fcntl.h> /* O_RDONLY */ #include <sys/types.h> #include <sys/stat.h> #include <sys/statvfs.h> #include <sys/param.h> /* MAXPATHLEN */ char *progname; /* */ void error(char *fmt, ...){ va_list args; va_start(args, fmt); fprintf(stderr, "%s: ", progname); vfprintf(stderr, fmt, args); fputc('\n', stderr); va_end(args); } int copyFile(char *to, char *from){ /* , */ char newname[MAXPATHLEN+1]; char answer[20]; struct stat stf, stt; int fdin, fdout; int n, code = 0; char iobuf[64 * 1024]; char *dirname = NULL, *s; if((fdin = open(from, O_RDONLY)) < 0){ error("Cannot read %s", from); return (-1); } fstat(fdin, &stf); if((stf.st_mode & S_IFMT) == S_IFDIR){ close(fdin); error("%s is a directory", from); return (-2); } . , 1992-95 - 209 - UNIX if(stat(to, &stt) >= 0){ /* */ if((stt.st_mode & S_IFMT) == S_IFDIR){ /* */ /* from */ if((s = strrchr(from, '/')) && s[1]) s++; else s = from; dirname = to; /* - */ sprintf(newname, "%s/%s", to, s); to = newname; if(stat(to, &stt) < 0) goto not_exist; } if(stt.st_dev == stf.st_dev && stt.st_ino == stf.st_ino){ error("%s: cannot copy file to itself", from); return (-3); } switch(stt.st_mode & S_IFMT){ case S_IFBLK: case S_IFCHR: case S_IFIFO: break; default: printf("%s already exists, overwrite ? ", to); fflush(stdout); *answer = '\0'; gets(answer); if(*answer != 'y'){ /* NO */ close(fdin); return (-4); } break; } } . , 1992-95 - 210 - UNIX not_exist: printf("COPY %s TO %s\n", from, to);