Chapter 2 Input and Output System Calls Internal UNIX System Calls & Libraries Using C --- 1011 OBJECTIVES Upon completion of this unit, you will be able to: Describe the characteristics of a file Open and close a file Read and write data to and from a file Change the read/write position in a file Control a file Chapter 2-1 Chapter 2-1
WHAT IS A FILE? z A file is a contiguous sequence of bytes. z No format imposed by the operating system z Each byte is individually addressable in a disk file. z Automatic expansion as data is written to a disk file z End-of-file indication is not part of data. z A file is also a uniform interface to external devices. Chapter 2-2 OPENING A FILE open(2) NAME SYNOPSIS open --- open for reading or writing #include <fcntl.h> int open (path, oflag [, mode ] ) char *path; int oflag, mode; RETURN VALUE success --- nonnegative file descriptor failure --- -1 is returned and errno is set Chapter 2-3 Chapter 2-2
open(2) FLAGS O_RDONLY --- reading only O_WRONLY --- writing only O_RDWR --- reading and writing O_NDELAY --- non-blocking O_APPEND --- positions at the end for all writes O_CREAT --- creates a non-existent file O_TRUNC --- discard all data O_EXCL --- ensure creating non-existent file O_SYNC --- wait for physical write Chapter 2-4 OPENING A FILE --- EXAMPLES z open a file for reading acctfd = open ( account, O_RDONLY) ; z open a file for writing file = TMPFILE ; fd = open ( file, O_WRONLY O_CREAT O_TRUNC, 0600) ; z open a file for appending logfd = open ( /sys/log, O_WRONLY O_APPEND O_CREAT, 0600) ; z open a file for reading and writing fdin = open ( argv[1], O_RDWR) ; z create a new file for writing if ( ( fdout = open ( TMPFILE, O_WRONLY O_CREAT O_EXCL, 0600 ) ) = = -1 ) perror ( TMPFILE ) ; Chapter 2-5 Chapter 2-3
FILE TABLES file descriptor table (inside user area) system file table system inode table Chapter 2-6 CLOSING A FILE close(2) NAME SYNOPSIS close --- close a file descriptor int close ( fildes ) int fildes ; RETURN VALUE success --- 0 failure --- -1 and errno is set Chapter 2-7 Chapter 2-4
read(2) NAME READ FROM A FILE read --- read from a file SYNOPSIS int read ( fildes, buf, nbyte ) int fildes ; char *buf ; unsigned nbytes ; RETURN VALUE success --- the number of bytes read failure --- -1 and errno is set Chapter 2-8 write(2) NAME WRITE TO A FILE write --- write to file SYNOPSIS int write ( fildes, buf, nbyte ) int fildes ; char *buf ; unsigned nbyte ; RETURN VALUE success --- the number of bytes written. failure --- -1 and errno is set Chapter 2-9 Chapter 2-5
COPY INPUT TO OUTPUT --- EXAMPLE 1 #include <stdio.h> 2 3 main( ) 4 { 5 char buf [ BUFSIZ ] ; 6 int n ; 7 8 while ( ( n = read ( 0, buf, BUFSIZ ) ) > 0 ) 9 write ( 1, buf, n ) ; 10 exit ( 0 ) ; 11 } Chapter 2-10 COPY FILE --- EXAMPLE 1 #include <stdio.h> 2 #include <fcntl.h> 3 #define PMODE 0644 4 5 main ( argc, argv ) 6 int argc ; 7 char *argv [ ] ; 8 { 9 int fdin, fdout, n ; 10 char buf [ BUFSIZ ] ; 11 12 if ( argc!= 3 ) { 13 fprintf ( stderr, Usage : %s filein fileout \n, 14 argv [0] ) ; 15 exit (1) ; 16 } Chapter 2-11 Chapter 2-6
17 if ( ( fdin = open ( argv [1], O_RDONLY ) ) = = -1 ) { 18 perror ( argv [1] ) ; 19 exit (2) ; 20 } 21 if ( ( fdout = open ( argv [2], O_WRONLY O_CREAT 22 O_TRUNC, PMODE ) ) = = -1 ) { 23 perror ( argv [2] ) ; 24 exit (3) ; 25 } 26 27 while ( ( n = read ( fdin, buf, BUFSIZ ) ) > 0 ) 28 write ( fdout, buf, n ) ; 29 30 exit (0) ; 31 } Chapter 2-12 MOVE READ/WRITE FILE POSITION lseek(2) NAME failure --- lseek --- move read/write file position SYNOPSIS long lseek ( fildes, offset, whence ) int fildes ; long offset ; int whence; RETURN VALUE success --- location in bytes from beginning of the file -1 and errno is set Chapter 2-13 Chapter 2-7
lseek(2) --- EXAMPLE z rewind z append lseek ( fd, 0L, 0 ) ; lseek ( fd, 0L, 2 ) ; z update read ( fd, (char *) &record, sizeof (record) ) ; /* update */ lseek ( fd, (long) -sizeof (record), 1 ) ; write ( fd, (char *) &record, sizeof (record) ) ; z record position loc = lseek ( fd, 0L, 1 ) ; z increase file lseek ( fd, (long) MAX * sizeof (record), 2 ) ; write ( fd, (char *) &record, sizeof (record) ) ; Chapter 2-14 CREATING AN EMPLOYEE FILE --- EXAMPLE 1 #include <fcntl.h> 2 #include <stdio.h> 3 #include employee.h 4 5 main ( argc, argv ) /* create employee file */ 6 int argc ; 7 char *argv [ ] ; 8 { 9 int fd, open ( ), getpid ( ) ; 10 struct employee record ; 11 12 if ( argc < 2 ) { 13 fprintf ( stderr, Usage : %s file\n, argv [0] ) ; 14 exit (1); 15 } Chapter 2-15 Chapter 2-8
16 if ( ( fd = open ( argv [1], O_WRONLY O_CREAT 17 O_EXCL, 0640 ) ) = = -1 ) { 18 perror ( argv [1] ) ; 19 exit (2) ; 20 } 21 for ( ; ; ) { 22 printf ( Enter employee name <SPACE> salary : ); 23 scanf ( %s, record.name ) ; 24 if ( record.name [0] = =. ) 25 break ; 26 scanf ( %d, &record.salary ) ; 27 record.pid = getpid ( ) ; 28 write ( fd, (char *) &record, sizeof ( record ) ) ; 29 } 30 close ( fd ) ; 31 exit (0) ; 32 } Chapter 2-16 QUERYING THE EMPLOYEE FILE --- EXAMPLE 1 #include <fcntl.h> 2 #include employee.h 3 4 main ( argc, argv ) 5 int argc ; 6 char *argv [ ] ; 7 { 8 int fd, recnum ; 9 struct employee record ; 10 11 if ( ( fd = open ( argv [1], O_RDONLY ) ) = = -1 ) { 12 perror ( argv [1] ) ; 13 exit (2); 14 } Chapter 2-17 Chapter 2-9
15 for ( ; ; ) { 16 printf ( \n Enter record number : ) ; 17 scanf ( %d, &recnum ) ; 18 if ( recnum < 0 ) 19 break ; 20 lseek ( fd, (long) recnum*sizeof ( record ), 0 ) ; 21 if ( read ( fd, (char *) &record, 22 sizeof ( record ) ) > 0 ) 23 printf ( Employee : %s \t Salary : %d \n, 24 record.name, record.salary ) ; 25 else 26 printf ( Record %d not found \n, recnum ) ; 27 } 28 close ( fd ) ; 29 exit (0) ; 30 } Chapter 2-18 DUPLICATE A FILE DESCRIPTOR dup(2) NAME SYNOPSIS dup --- duplicate an open file descriptor int dup ( fildes ) int fildes ; RETURN VALUE success --- a nonnegative file descriptor is returned failure --- -1 is returned and errno is set Chapter 2-19 Chapter 2-10
dup(2) --- FILE TABLES file descriptor table system file table system inode table Chapter 2-20 INPUT/OUTPUT REDIRECTION --- EXAMPLE 1 #include <fcntl.h> 2 #include <stdio.h> 3 4 main ( argc, argv ) /* demonstrate dup (2) */ 5 int argc ; 6 char *argv [ ] ; 7 { 8 9 close (1); 10 if ( open (argv [1], O_WRONLY 11 O_CREAT O_TRUNC, 0644 ) = = -1 ) { 12 perror ( argv [1] ) ; 13 exit (1) ; 14 } 15 Chapter 2-21 Chapter 2-11
16 close (2) ; 17 if ( dup (1) = = -1 ) { 18 perror ( argv [0] ) ; 19 exit (2) ; 20 } 21 22 printf ( first line to stdout ( uses fd 1 ) \n ) ; 23 fprintf ( stderr, first line to srderr ( uses fd 2 ) \n ) ; 24 printf ( second line to stdout \n ) ; 25 fprintf ( stderr, second line to stderr \n ) ; 26 27 } Chapter 2-22 FILE CONTROL fcntl(2) NAME fcntl --- file control SYNOPSIS #include <fcntl.h> int fcntl ( fildes, cmd, arg ) int fildes, cmd ; int arg ; or struct flock *arg ; RETURN VALUE success --- depends on cmd failure --- -1 and errno is set Chapter 2-23 Chapter 2-12
fcntl(2) COMMANDS z int arg ; F_DUPFD --- duplicate a file descriptor F_GETFD --- get the close-on-exec flag F_SETFD --- set the close-on-exec flag F_GETFL --- get the file status flags F_SETFL --- set the file status flags Chapter 2-24 ADDING EMPLOYEE RECORDS --- EXAMPLE 1 #include <fcntl.h> 2 #include employee 3 #define DUMMY 0 4 5 main ( argc, argv ) 6 int argc ; 7 char *argv [ ] ; 8 { 9 struct employee record ; 10 int fd, open( ), flags, fcntl( ), pid, getpid( ) ; 11 12 if ( ( fd = open ( argv[1], O_RDWR ) ) = = -1 ) { 13 perror ( argv[1] ) ; 14 exit (1) ; 15 } 16 /* 17 * other data processing here žöof 18 fj jš æbú*/ Chapter 2-25 Chapter 2-13
19 if ( ( flags = fcntl ( fd, F_GETFL, DUMMY ) ) = = -1 ) { 20 perror (argv[1]) ; 21 exit (2) ; 22 } 23 flags!= O_APPEND ; 24 fcntl ( fd, F_SETFL, flags ) ; 25 pid = getpid( ) ; 26 for ( ; ; ) { 27 printf ( /nenter employee name : ) ; 28 scanf ( %s, record.name) ; 29 if ( record.name[0] = =. ) 30 break ; 31 printf ( Enter employee salary : ) ; 32 scanf ( %d, &record.salary) ; 33 record.pid = pid ; 34 write ( fd, (char *) &record, sizeof (record) ) ; 35 } 36 close (fd) ; 37 } Chapter 2-26 OPENING TERMINAL FILE --- EXAMPLE O_NDELAY flag 1 #include <stdio.h> 2 #include <fcntl.h> 3 4 main ( ) 5 { 6 int fd, flags, open( ), fcntl( ) ; 7 int i, n ; 8 char line[bufsiz] ; 9 10 if ( ( fd = open ( /dev/tty, O_RDONLY 11 O_NDELAY ) ) = = -1 ) { 12 perror ( /dev/tty ) ; 13 exit (2); 14 } Chapter 2-27 Chapter 2-14
15 printf ( Enter your PIN within five seconds : \n> ) ; 16 sleep (5) ; 17 if (read (fd, line, BUFSIZ) = = 0) { 18 printf ( \nsorry\n ) ; 19 exit (1) ; 20 } 21 22 flags = fcntl ( fd, F_GETFL, 0 ) ; 23 flags &= ~ O_NDELAY ; /* turn off delay flag */ 24 fcntl ( fd, F_SETFL, flags ) ; 25 printf ( Enter your bank account number : \n> ) ; 26 read ( fd, line, BUFSIZ ) ; 27 28 /* 29 *.... data processing is performed here.. 30 */ 31 } Chapter 2-28 SUMMARY open (2) --- open for reading or writing close (2) --- close a file descriptor read (2) --- read from a file write (2) --- write to file lseek (2) --- move read / write file position fcntl (2) --- file control Chapter 2-29 Chapter 2-15
STANDARD INPUT/OUTPUT LIBRARY FUNCTIONS z Functions are easier to use z Functions provide more services z Increases efficiency of programs z Increases portability of programs Chapter 2-30 STANDARD INPUT/OUTPUT INCLUDE FILE < stdio.h >... 2 #ifndef _NFILE 3 #define _NFILE 20 4 5 #if u370 6 #define BUFSIZ 4096 7 #endif 8 #if vax u3b u3b5 9 #define BUFSIZ 1024 10 #endif 11 #if pdp11 12 #define BUFSIZ 512 13 #endif... Chapter 2-31 Chapter 2-16
... 18 typedef struct { 19 #if vax u3b u3b5 20 int _cnt ; 21 unsigned char *_ptr ; 22 #else 23 unsigned char *_ptr ; 24 int _cnt ; 25 #endif 26 unsigned char *_base ; 27 char _flag ; 28 char _file ; 29 } FILE ;... 61 #define getc (p) ( - - (p) -> _cnt < 0? _filbuf (p) : (int) *(p) -> _ptr++ ) 62 #define putc (x, p) ( - - (p) -> _cnt < 0? 63 _flsbuf ( (unsigned char) (x), (p) ) : 64 (int) (*(p) -> _ptr++ = (unsigned char) (x) ) )... 73 extern FILE _iob [ _NFILE ] ; Chapter 2-32 STANDARD I/O PACKAGE Text fpute ( X, fd ) ; Data fp UNI FILE _file = 3 _base = 107604 _cnt = 1021 _ptr = 407607 BUFSIZ User Area Fd [3] System I/O Cont Chapter 2-33 Chapter 2-17