char *string = "";
setlocale(LC_ALL, "");
for(;c = *string;string++){
#ifdef DEBUG
printf("%c %d %d\n", *string, *string, c);
#endif
if(isprint(c)) printf("%c - \n", c);
}
return 0;
}
% a.out
-
-
. ???
''. '\307'.
c = *string;
c -57 (), .
/usr/include/ctype.h isprint :
#define isprint(c) ((_ctype + 1)[c] & (_P|_U|_L|_N|_B))
c ,
int (signed). -
; , _ctype.
. , 1992-95 - 129 - UNIX
isprint(c & 0xFF)
isprint((unsigned char) c)
unsigned char c;
signed unsigned ,
. - unsigned char unsigned int, -
. , .
3.12. , , char, unsigned char -
.
char c = '';
int x[256];
...x[c]... /* < 0 */
...x['']...
unsigned char, & 0xFF. -
:
/* :
tr abcd prst
xxxxdbcaxxxx -> xxxxtrspxxxx
. ..
*/
#include <stdio.h>
#define ASCII 256 /* ASCII */
/* BUFSIZ stdio.h */
char mt[ ASCII ]; /* */
/* */
void mtinit(){
register int i;
for( i=0; i < ASCII; i++ )
mt[i] = (char) i;
}
. , 1992-95 - 130 - UNIX
int main(int argc, char *argv[])
{
register char *tin, *tout; /* unsigned char */
char buffer[ BUFSIZ ];
if( argc != 3 ){
fprintf( stderr, ": %s \n", argv[0] );
return(1);
}
tin = argv[1]; tout = argv[2];
if( strlen(tin) != strlen(tout)){
fprintf( stderr, " \n" );
return(2);
}
mtinit();
do{
mt[ (*tin++) & 0xFF ] = *tout++;
/* *tin - char.
* & 0xFF
*/
} while( *tin );
tout = mt;
while( fgets( buffer, BUFSIZ, stdin ) != NULL ){
for( tin = buffer; *tin; tin++ )
*tin = tout[ *tin & 0xFF ];
fputs( buffer, stdout );
}
return(0);
}
3.13.
int main(int ac, char *av[]){
char c = '';
if('a' <= c && c < 256)
printf(" .\n");
return 0;
}
, . , signed char (
if) int. - .
(c & 0xFF),
unsigned char c. , , -
char.
:
if((ch1 & 0xFF) < (ch2 & 0xFF))...;
unsigned char .
3.14. :
. , 1992-95 - 131 - UNIX
#include <stdio.h>
main(){
char c;
while((c = getchar()) != EOF)
putchar(c);
}
c char, EOF - int (-1).
" " -8 '\377' (0xFF).
, signed char -
EOF, c -
. 0xFF (-1), , EOF. !!!
,
( ) . -
.
int c.
3.15.
#define TYPE char
void f(TYPE c){
if(c == '') printf(" \n");
printf("c=%c c=\\%03o c=%03d c=0x%0X\n", c, c, c, c);
}
int main(){
f(''); f('');
f('z'); f('Z');
return 0;
}
TYPE char, unsigned char, int. .
(int == 32 ):
c= c=\37777777707 c=-57 c=0xFFFFFFC7
c= c=\37777777712 c=-54 c=0xFFFFFFCA
c=z c=\172 c=122 c=0x7A
c=Z c=\132 c=090 c=0x5A
c= c=\307 c=199 c=0xC7
c= c=\312 c=202 c=0xCA
c=z c=\172 c=122 c=0x7A
c=Z c=\132 c=090 c=0x5A
1 .
if(c == (unsigned char) '') printf(" \n");
, c .
' ' char, int, -
c signed int ( 1).
!
if((unsigned char)c == (unsigned char)'') printf(" \n");
. , 1992-95 - 132 - UNIX
3.16. -
. va_args,
, -
. :
<varargs.h> <stdarg.h>.
poly(). , -
:
#include <stdio.h>
#include <stdarg.h>
void trace(char *fmt, ...) {
va_list args;
static FILE *fp = NULL;
if(fp == NULL){
if((fp = fopen("TRACE", "w")) == NULL) return;
}
va_start(args, fmt);
/* : -
* ... */
vfprintf(fp, fmt, args); /* - */
fflush(fp); /* */
va_end(args);
}
main(){ trace( "%s\n", "Go home.");
trace( "%d %d\n", 12, 34);
}
`...' () ( )
. ,
.
va_arg(args,type), `...'
type, . vfprintf
vsprintf ( - ):
int vfprintf(FILE *fp, const char *fmt, va_list args){
/*static*/ char buffer[1024]; int res;
res = vsprintf(buffer, fmt, args);
fputs(buffer, fp); return res;
}
vsprintf(str,fmt,args); sprintf(str,fmt,...) -
str, ,
. sprintf '\0'.
3.17. printf, %c (), %d (), %o (-
), %x (), %b (), %r (), %s (), %ld
( ). .
3.18. , -
( ), -
. - , -
" " :
#ifdef XX
... 1
#else
... 2
#endif
. , 1992-95 - 133 - UNIX
: XX
#define XX
1, - 2. #else -
- 2 . #ifndef, -
1 XX . #elif -
else if:
#ifdef 1
...
#elif 2
...
#else
...
#endif
#define, -
,
cc -DXX file.c ...
file.c
#define XX
main(){
#ifdef XX
printf( "XX = %d\n", XX);
#else
printf( "XX undefined\n");
#endif
}
cc -D"XX=2" file.c ...
#define XX 2
, -D ?
, -
:
cc -Dvoid=int ...
cc -Dstrchr=index ...
:
UNIX ( ):
-DM_UNIX
-DM_XENIX
-Dunix
-DM_SYSV
-D__SVR4
-DUSG
...
. , 1992-95 - 134 - UNIX
"", UNIX. -
cc.
3.19. #ifdef include-, -
. aa.h bb.h
aa.h bb.h
#include "cc.h" #include "cc.h"
typedef unsigned long ulong; typedef int cnt_t;
cc.h 00.c
cc.h 00.c
... #include "aa.h"
struct II { int x, y; }; #include "bb.h"
... main(){ ... }
cc.h 00.c : aa.h bb.h.
00.c " II". include-
, ,
- :
/* cc.h */
#ifndef _CC_H
# define _CC_H /* */
...
struct II { int x, y; };
...
#endif /* _CC_H */
,
. <sys/types.h>
_SYS_TYPES_H.
3.20. ,
#undef ߌ
:
#include <stdio.h>
#undef M_UNIX
#undef M_SYSV
main() {
putchar('!');
#undef putchar
#define putchar(c) printf( " '%c'\n", c);
putchar('?');
#if defined(M_UNIX) || defined(M_SYSV)
/* #if M_UNIX */
printf(" UNIX\n");
#else
printf(" UNIX\n");
#endif /* UNIX */
}
#undef , putchar
( , putchar - <stdio.h>).
#if, , #ifdef
:
. , 1992-95 - 135 - UNIX
#if defined(MACRO) /* #ifdef(MACRO) */
#if !defined(MACRO) /* #ifndef(MACRO) */
#if VALUE > 15 /*
#define VALUE 25
15 (==, !=, <=, ...) */
#if COND1 || COND2 /* */
#if COND1 && COND2 /* */
#if -
,
#if !defined(M1) && (defined(M2) || defined(M3))
3.21. :
#ifdef DEBUG
# define DEBUGF(body) \
{ \
body; \
}
#else
# define DEBUGF(body)
#endif
int f(int x){ return x*x; }
int main(int ac, char *av[]){
int x = 21;
DEBUGF(x = f(x); printf("%s equals to %d\n", "x", x));
printf("x=%d\n", x);
}
cc -DDEBUG file.c
.
-DDEBUG .
3.22. C++ ( ) class, delete, friend, new, operator,
overload, template, public, private, protected, this, virtual -
(). -
C++, :
#include <termio.h>
...
int fd_tty = 2; /* stderr */
struct termio old, new;
ioctl (fd_tty, TCGETA, &old);
new = old;
new.c_lflag |= ECHO | ICANON;
ioctl (fd_tty, TCSETAW, &new);
...
, ( ) new, C++.
( ).
,
define:
. , 1992-95 - 136 - UNIX
#define new new_modes
... ...
#undef new
C++ , C++
, (
, int
void).
. , 1992-95 - 137 - UNIX
4. .
( -
), :
- , (, ,
);
- ( ).
UNIX MS DOS
.
- .
,
- ;
- -
[], ;
- " /",
/, .. -
. , ,
.
.
UNIX.
4.1. - - -
.
"" file "
", :
f_offset:
/,
RWptr. long-,
/;
f_flag:
: , , ,
;
f_inode:
( UNIX - I- |-);
- .
-
"" |=.
____________________
|- I- (I-node, ) - "",
( ). :
- long di_size;
- int di_uid;
- ushort di_mode;
-
time_t di_ctime, di_mtime;
- char di_addr[...];
- short di_nlink;
...
stat(). I-
- I-. I-
, 1. ( "/")
I- 2.
|= UNIX "". -
, - "" ,
.
"u-area" user. , ,
. , 1992-95 - 138 - UNIX
, " " ,
-
" ".
, -
, .. .
. ..
-
( ). : () -
. :
. "" (
); /. -
, -
dup2.
fd u_ofile[] struct file
0 ## -------------
1---##---------------->| f_flag |
2 ## | f_count=3 |
3---##---------------->| f_inode---------*
... ## *-------------->| f_offset | |
1 | ------!------ |
| ! V
0 ## | struct file ! struct inode
1 ## | ------------- ! -------------
2---##-* | f_flag | ! | i_count=2 |
3---##--->| f_count=1 | ! | i_addr[]----*
... ## | f_inode----------!-->| ... | |
2 | f_offset | ! ------------- |
-------!----- *=========* |
! ! V
0 ! R/W ! i_size-1
@@@@@@@@@@@!@@@@@@@@@@@@@@@@@@@@@!@@@@@@
/* */
int fd = open(char _[], int _);
... /* - */
close(fd); /* */
_:
#include <fcntl.h>
O_RDONLY - .
O_WRONLY - .
O_RDWR - .
O_APPEND -
, "" :
O_WRONLY|O_APPEND, O_RDWR|O_APPEND
, : open (-1),
____________________
struct file *u_ofile[NOFILE];
I-
struct inode *u_cdir;
struct proc *u_procp;
. , 1992-95 - 139 - UNIX
. :
int fd = creat(char _[], int _);
fd .
, creat , ..
0L . _ .
9 ,
: 876 543 210
rwx rwx rwx
r -
w -
x -
- , - , -
. (
stat):
#include <sys/stat.h> /* : */
#define S_IREAD 0400
#define S_IWRITE 0200
#define S_IEXEC 0100
- UNIX. , open()
fd < 0 ,
(errno==ENOENT), ,
(errno==EACCES; errno . "
UNIX").
creat - open
fd = open( _,
O_WRONLY|O_TRUNC|O_CREAT, _);
O_TRUNC
, , -
. .
O_CREAT
, , (
, open fd < 0).
_|-. - ,
O_TRUNC.
O_EXCL
O_CREAT. :
, open (errno==EEXIST).
____________________
|- ,
di_mode = (_ & ~u_cmask) | IFREG;
( IFREG IFDIR), u_cmask
umask(u_cmask);
( ) -
( u-area ).
,
,
umask(0077); /* ???------ */
3 ( ).
.
mkdir.
. , 1992-95 - 140 - UNIX
- O_CREAT .
.
int unlink(char _[]);
,
0 - ( )
1 - ( )
2 - ( )
close(fd) fd ( -
) - .
:
( ) ( ), , -
( -
). , , -
, ! (. ), ""
- .
4.2. , ()
. read write. -
.
, !
, 512 .
. :
char buffer[512]; int n; int fd_inp, fd_outp;
...
while((n = read (fd_inp, buffer, sizeof buffer)) > 0)
write(fd_outp, buffer, n);
write:
char c = 'a';
int i = 13, j = 15;
char s[20] = "foobar";
char p[] = "FOOBAR";
struct { int x, y; } a = { 666, 999 };
/* rw-r--r-- */
int fd = creat("aFile", 0644);
write(fd, &c, 1);
write(fd, &i, sizeof i); write(fd, &j, sizeof(int));
write(fd, s, strlen(s)); write(fd, &a, sizeof a);
write(fd, p, sizeof(p) - 1);
close(fd);
:
- write() read() ,
(, ).
- read write /
( ,
; -
, ).
- read/write /
RWptr += ____;
: RWptr=0.
. , 1992-95 - 141 - UNIX
. -
, read " 0 " (.. -
).
- unsigned, int:
int n = read (int fd, char *, unsigned );
int n = write(int fd, char *, unsigned );
, -
( UNIX , -
):
4.2.1. m = write(fd, addr, n);
( [fd] ) (-1);
(n == 0) 0;
( [fd] O_APPEND )
RWptr = _; /* .. */
( RWptr > _ )
[fd][ _..RWptr-1 ] = '\0';
[fd][ RWptr..RWptr+n-1 ] = addr[ 0..n-1 ];
,
RWptr += n;
( RWptr > _ )
_ = RWptr;
n;
4.2.2. m = read(fd, addr, n);
( [fd] ) (-1);
( RWptr >= _ ) 0;
m = MIN( n, _ - RWptr );
addr[ 0..m-1 ] = [fd][ RWptr..RWptr+m-1 ];
RWptr += m;
m;
4.3. :
#define STDOUT 1 /* */
int i;
static char s[20] = "hi\n";
char c = '\n';
struct a{ int x,y; char ss[5]; } po;
scanf( "%d%d%d%s%s", i, po.x, po.y, s, po.ss);
write( STDOUT, s, strlen(s));
write( STDOUT, c, 1 ); /* 1 */
: scanf i "", &i.
&po.x &po.y. , s - , .. s ,
s & ; po.ss - & .
write ,
. &c ( write).
scanf - -
( -
scanf(char *fmt, ...), scanf -
. , 1992-95 - 142 - UNIX
).
!
4.4. , , ,
? :
#include <fcntl.h>
#include <stdio.h>
#include <sys/param.h> /* NOFILE */
#include <errno.h>
char *typeOfOpen(fd){
int flags;
if((flags=fcntl (fd, F_GETFL, NULL)) < 0 )
return NULL; /* fd */
flags &= O_RDONLY | O_WRONLY | O_RDWR;
switch(flags){
case O_RDONLY: return "r";
case O_WRONLY: return "w";
case O_RDWR: return "r+w";
default: return NULL;
}
}
char *type2OfOpen(fd){
extern errno; /* . " " */
int r=1, w=1;
errno = 0; read(fd, NULL, 0);
if( errno == EBADF ) r = 0;
errno = 0; write(fd, NULL, 0);
if( errno == EBADF ) w = 0;
return (w && r) ? "r+w" :
w ? "w" :
r ? "r" :
"closed";
}
main(){
int i; char *s, *p;
for(i=0; i < NOFILE; i++ ){
s = typeOfOpen(i); p = type2OfOpen(i);
printf("%d:%s %s\n", i, s? s: "closed", p);
}
}
NOFILE
( , ). -
fcntl (file control).
4.5. rename() . : -
link() unlink(). :
. , 1992-95 - 143 - UNIX
rename( from, to )
char *from, /* */
*to; /* */
{
unlink( to ); /* to */
if( link( from, to ) < 0 ) /* */
return (-1);
unlink( from ); /* */
return 0; /* OK */
}
link(_, _);
- UNIX :
- , "." .
, , -
, "." , ".." .
, _ ;
.
unlink(_)
. - .
:
int fd;
close(creat("/tmp/xyz", 0644)); /* */
fd = open("/tmp/xyz", O_RDWR);
unlink("/tmp/xyz");
...
close(fd);
.
. , ,
! fd.
- ( ).
.
,
"". .
UNIX rename,
, .
4.6. -
, ,
( ). -
zz.out ( , -
):
/* a.out */
main(){
int fd = creat("zz.out", 0644);
write(fd, "It's me\n", 8);
}
, . ,
zz.out /dev/tty (. ). -
ln:
$ rm zz.out ; ln /dev/tty zz.out
$ a.out
$ rm zz.out
:
. , 1992-95 - 144 - UNIX
/* start */
/* a.out */
#include <stdio.h>
main(){
unlink("zz.out");
link("/dev/tty", "zz.out");
if( !fork()){ execl("a.out", NULL); }
else wait(NULL);
unlink("zz.out");
}
( fork, exec, wait UNIX).
: a.out /usr/bin/vi (
system() ):
main(){
... system("/usr/bin/vi xx.c"); ...
}
vi /usr/local/bin/vi.
:
$ ln /usr/local/bin/vi /usr/bin/vi
, ,
. BSD |- ,
" "
symlink(link_to_filename,link_file_name_to_be_created);
- , ( ).
, ""
- . -
char linkbuf[ MAXPATHLEN + 1]; /* */
int len = readlink(pathname, linkbuf, sizeof linkbuf);
linkbuf[len] = '\0';
stat -
. lstat ( stat )
( S_IFLNK).
, -
.
:
. /opt/wawa.
wawa USR: /usr/wawa. /opt:
ln -s /usr/wawa /opt/wawa
/opt/wawa.
:
hard link
- , link, I-node (