C I/O Library. stdout, stderr, stdin

Similar documents
I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

CSC 271 Software I: Utilities and Internals

System Calls. Library Functions Vs. System Calls. Library Functions Vs. System Calls

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

System Calls and I/O Appendix. Copyright : University of Illinois CS 241 Staff 1

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

File I/O. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CSI 402 Lecture 11 (Unix Discussion on Files continued) 11 1 / 19

File I/O. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

UNIX System Calls. Sys Calls versus Library Func

SYSTEM INFORMATION. UNIX Programming 2015 Fall by Euiseong Seo

Important Dates. October 27 th Homework 2 Due. October 29 th Midterm

UNIX System Programming

Contents. Programming Assignment 0 review & NOTICE. File IO & File IO exercise. What will be next project?

CMPSC 311- Introduction to Systems Programming Module: Input/Output

CS 201. Files and I/O. Gerson Robboy Portland State University

System Calls and I/O. CS 241 January 27, Copyright : University of Illinois CS 241 Staff 1

All the scoring jobs will be done by script

structs as arguments

The link() System Call. Preview. The link() System Call. The link() System Call. The unlink() System Call 9/25/2017

All the scoring jobs will be done by script

Lecture 23: System-Level I/O

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

CSCI-E28 Lecture 3 Outline. Directories, File Attributes, Bits, File Operations. Write our own versions of Unix programs

CSci 4061 Introduction to Operating Systems. File Systems: Basics

Preview. System Call. System Call. System Call. System Call. Library Functions 9/20/2018. System Call

Contents. NOTICE & Programming Assignment #1. QnA about last exercise. File IO exercise

Interacting with Unix

Operating System Labs. Yuanbin Wu

Naked C Lecture 6. File Operations and System Calls

Contents. NOTICE & Programming Assignment 0 review. What will be next project? File IO & File IO exercise

System- Level I/O. Andrew Case. Slides adapted from Jinyang Li, Randy Bryant and Dave O Hallaron

Lecture 21 Systems Programming in C

System-Level I/O. Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O

Input/Output and the Operating Systems

CSE 333 SECTION 3. POSIX I/O Functions

Lecture files in /home/hwang/cs375/lecture05 on csserver.

Preview. Review. System Data Files (Password File) System Data Files (Password File) System Data Files (Password File)

How do we define pointers? Memory allocation. Syntax. Notes. Pointers to variables. Pointers to structures. Pointers to functions. Notes.

Lecture 3. Introduction to Unix Systems Programming: Unix File I/O System Calls

25.2 Opening and Closing a File

PROGRAMMAZIONE I A.A. 2017/2018

What Is Operating System? Operating Systems, System Calls, and Buffered I/O. Academic Computers in 1983 and Operating System

Files and Directories Filesystems from a user s perspective

Operating System Labs. Yuanbin Wu

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

The course that gives CMU its Zip! I/O Nov 15, 2001

CSE 410: Systems Programming

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

File Access. FILE * fopen(const char *name, const char * mode);

CMSC 216 Introduction to Computer Systems Lecture 17 Process Control and System-Level I/O

CS , Spring Sample Exam 3

UNIX input and output

Systems Programming. COSC Software Tools. Systems Programming. High-Level vs. Low-Level. High-Level vs. Low-Level.

Files and Directories

Outline. OS Interface to Devices. System Input/Output. CSCI 4061 Introduction to Operating Systems. System I/O and Files. Instructor: Abhishek Chandra

Files and Directories Filesystems from a user s perspective

CS240: Programming in C

CSE 333 SECTION 3. POSIX I/O Functions

CS240: Programming in C

Process Management! Goals of this Lecture!

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

CS631 - Advanced Programming in the UNIX Environment

Thesis, antithesis, synthesis

Goals of this Lecture

Summer June 15, 2010

which maintain a name to inode mapping which is convenient for people to use. All le objects are

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

Introduction. Files. 3. UNIX provides a simple and consistent interface to operating system services and to devices. Directories

Fall 2017 :: CSE 306. File Systems Basics. Nima Honarmand

Operating System Labs. Yuanbin Wu

Operating systems. Lecture 7

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Layers in a UNIX System. Create a new process. Processes in UNIX. fildescriptors streams pipe(2) labinstructions

Physical Files and Logical Files. Opening Files. Chap 2. Fundamental File Processing Operations. File Structures. Physical file.

2009 S2 COMP File Operations

Operating System Labs. Yuanbin Wu

Memory Mapped I/O. Michael Jantz. Prasad Kulkarni. EECS 678 Memory Mapped I/O Lab 1

File Systems. q Files and directories q Sharing and protection q File & directory implementation

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING SOFTWARE ENGINEERING DEPARTMENT

File Input and Output (I/O)

File Systems. Today. Next. Files and directories File & directory implementation Sharing and protection. File system management & examples

OS COMPONENTS OVERVIEW OF UNIX FILE I/O. CS124 Operating Systems Fall , Lecture 2

Input / Output Functions

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

we are here Page 1 Recall: How do we Hide I/O Latency? I/O & Storage Layers Recall: C Low level I/O

File Descriptors and Piping

CptS 360 (System Programming) Unit 6: Files and Directories

System-Level I/O Nov 14, 2002

POSIX Shared Memory. Linux/UNIX IPC Programming. Outline. Michael Kerrisk, man7.org c 2017 November 2017

Preview. Interprocess Communication with Pipe. Pipe from the Parent to the child Pipe from the child to the parent FIFO popen() with r Popen() with w

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

This document gives a general overview of the work done by an operating system and gives specific examples from UNIX.

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Chp1 Introduction. Introduction. Objective. Logging In. Shell. Briefly describe services provided by various versions of the UNIX operating system.

File IO and command line input CSE 2451

This manual is for Libffi, a portable foreign-function interface library. Copyright c 2008, 2010, 2011 Red Hat, Inc. Permission is granted to copy,

Transcription:

C I/O Library Defined in <stdio.h> scanf, printf scanf( %d, &num); printf( Hello World\n ); fscanf, fprintf fprintf(file, Hello World\n ); fscanf(file, %d, &num); file is a FILE* pointer stdout, stderr, stdin Not required, but highly recommended more suitable and efficient than cout and cin in our problems Header files Usually define interfaces between separately compiled modules System header files in /usr/include May contain macro definitions preprocessor directives declarations of types function prototypes Should not contain variable definitions or executable statements Watch out Improper header file use can cause problems Including a header file multiple times may cause redefinition errors Fall 2013 CS4350 Husain Gholoom Page 1

Conditional Code in Headers Preprocessor directives are used to prevent the body of a header file from being used multiple times #ifndef MYHEADER #define MYHEADER /* the body of the header file */ #endif system() Header file Prototype Call #include <stdlib.h> int system(const char *string); system( echo hello world ); Works as if string is typed at the command prompt Can separate multiple commands within the string using semicolons or newlines The executed commands have access to current environment variables Return value Returns the exit status of the last command executed The value does not always match the exit status you see in the shell Returns -1 if the system() function call fails to execute Fall 2013 CS4350 Husain Gholoom Page 2

System Calls in C A large collection of pre-defined system functions are available to each C program When we call one of these functions from our C source file it is commonly referred to as a system call Some functions have equivalent unix commands getenv(), chmod() Some functions do not have equivalent unix commands fread(), lseek() Some functions behave differently than their corresponding unix commands umask() Some low-level functions are written in assembly language but provide C-like interface When a process invokes a system call the process switches from user mode to kernel mode privileged mode can access memory outside own address space does not make the user root! there is some overhead in switching back and forth between the two modes should use system calls judiciously Fall 2013 CS4350 Husain Gholoom Page 3

Why Use System Calls? Many system functions are heavily used in system-level programming Low-level file I/O Client-server programming Network programming System functions provide easy access to system information that would otherwise be difficult to find System functions often take care of the dirty work underneath to provide a clean and efficient interface to user programs sockets, signals, threads Library Functions vs System Calls Standard library functions are associated with C/C++ compilers Bundled with gcc and other commercial compilers Provide a convenient wrapper for low-level system calls fread(), fgets(), fgetc() all call read() internally Do not provide enough fine-grain control fopen() does not support chmod-like permissions No equivalent library calls for some functions fork(), chmod() etc. Information man -s 2 for system calls man -s 3 for library functions Fall 2013 CS4350 Husain Gholoom Page 4

Locating System Functions Difficult to provide an exhaustive list list is quite large (over 1000 s in most systems) may vary from one unix installation to another Usual location of header files /usr/include /usr/include/linux /usr/include/sys /usr/local/include Some header files we will be looking at <unistd.h> ( unistd.h is the name of the header file that provides access to the Portable Operating System Interface API. Typically, It is made up largely of system call wrapper functions such as fork, pipe and I/O primitives such as read, write, close, etc. ) <time.h> <sys/types.h> Using System Functions 1. Include the appropriate header file e.g. #include<unistd.h> Could do a grep in /usr/include if we have some idea about the function name 2. Call the function $ grep exit* /usr/include/*/* int returnval = sysfoo(arg1, arg2); 3. Link library, if needed e.g., need it for math, not for stdio Fall 2013 CS4350 Husain Gholoom Page 5

Handling Return Value It is very important to check for return values when making system calls functions may fail but not generate an error message functions may fail for a variety of reasons Frequently Used System Calls Getting information about the environment getenv(), putenv(), getcwd() Directory and File Operation chdir(), chmod(), umask() File I/O Beyond fprintf() and fscanf() read binary data open(), close(), read(), write(), lseek(), System Calls for the Environment System calls to access information about the environment User System Process Time We already know how to get this information in shell Cannot use shell for everything Fall 2013 CS4350 Husain Gholoom Page 6

Why Use These Functions? When programming in Unix, it is important to make the program aware of the environment Portability Conditional compilation with #ifdef #ifdef REDHAT // then use boost for multithreading #endif Security If the user is not root then I really shouldn t be executing this code Robustness If PWD is / then Performance Same strategy as portability, but different goal Fall 2013 CS4350 Husain Gholoom Page 7

getenv() function searches the environment list to find the environment variable name, and returns a pointer to the corresponding value string Header file Prototype #include<stdlib.h> char *getenv(const char *name); Call char *envvar; envvar = getenv( HOME ); Return value returns null if it fails or there is no match returns a pointer to the location of the value held in var Fall 2013 CS4350 Husain Gholoom Page 8

Puten v() The putenv() function adds or changes the value of environment variables. The argument string is of the form name=value. If name does not already exist in the environment, then string is added to the environment. If name does exist, then the value of name in the environment is changed to value. The string pointed to by string becomes part of the environment, so altering the string changes the environment ENOMEM Insufficient space to allocate new environment Header file Prototype Call #include<stdlib.h> int putenv(const char *str); putenv( HOME=/home/zz01 ); Return value returns -1 if it fails returns 0 when it succeeds Fall 2013 CS4350 Husain Gholoom Page 9

/* * Program illustrates usage of getenv() and putenv() * */ #include<stdio.h> #include<stdlib.h> int main() { /* no allocation necessary */ char *home; char *path; path = getenv("path"); home = getenv("home"); printf("home = %s\n", home); printf("path = %s\n", path); } /* getenv pointer is in shared location home + 20 points to different env var */ printf("where = %s\n", home + 20); /* since HOME is pre-defined, modification is legal */ *(home + 1) = 'D'; printf("home = %s\n", home); return 0; HOME = /home/faculty/hag10 PATH = /usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/lib/oracle/1 1.2/client64/bin:/usr/local/MATLAB/R2012a/bin:/home/Faculty/hag10/bin Where = LOGNAME=hag10 Home = /Dome/Faculty/hag10 Fall 2013 CS4350 Husain Gholoom Page 10

getcwd() get pathname of current working directory The getcwd() function may fail if A parent directory cannot be read to get its name. /* The following example prints the current working directory. */ #include <unistd.h> #include <stdio.h> int main() { char cwd[1024]; if (chdir("/tmp")!= 0) perror("chdir() error()"); else { if (getcwd(cwd, sizeof(cwd)) == NULL) perror("getcwd() error"); else printf("current working directory is: %s\n", cwd); } } current working directory is: /tmp Fall 2013 CS4350 Husain Gholoom Page 11

Accessing User Information getuid() getgid() getlogin() getpwuid() getpwnam() getuid() Header Prototype Call #include<sys/types.h> #include<unistd.h> uid_t getuid(); uid_t uid; uid = getuid(); On a unix system every user has a name and a numeric id getuid() returns the numeric user id associated with the program user id of a process is the user id of user who runs the program Example printf("the process ID is %d\n", getuid()) Fall 2013 CS4350 Husain Gholoom Page 12

getgid() Header #include<sys/types.h> #include<unistd.h> Prototype gid_t getgid(); Call gid_t gid; gid = getgid(); On a unix system every user belongs to a group Every group has a name and numeric id getgid() returns the numeric group id associated with the program usually the primary group of the user who runs the program Fall 2013 CS4350 Husain Gholoom Page 13

getlogin(); Header #include<sys/types.h> #include<unistd.h> Prototype char* getlogin(); Call printf( The user is %s\n, getlogin()); We can also get the login name Not used a whole lot other than informational purposes Example #include <stdio.h> #include <unistd.h> int main() { char *name; name = getlogin(); if (!name) perror("getlogin() error"); else printf("this is the login info: %s\n", name); return 0; } This is the login info: hag10 Fall 2013 CS4350 Husain Gholoom Page 14

getpwuid() Header Prototype #include<sys/types.h> #include<pwd.h> struct passwd *getpwuid(); struct passwd *getpwnam(); Uses information from /etc/passwd file Becoming non-standard for security issues Example : Getting a User Name from a UID #include <pwd.h> struct passwd *getpwuid(uid_t uid ) // The passwd structure: struct passwd { char *pw_name; /* user name */ char *pw_passwd; uid_t pw_uid; gid_t pw_gid; char *pw_gecos ; char *pw_dir; char *pw_shell ; }; Fall 2013 CS4350 Husain Gholoom Page 15

Example: struct passwd *pwptr ;... pwptr = getpwuid(uid); printf("the user name is %s\n", pwptr->pw_name); Modifier Functions for user ID When a process is running it enjoys the privileges of the user who invoked the process There are system calls that can change the user id of the running process setuid() setgid() Does it make sense to have these? Yes, but only for root allow the program to do stuff as root write to a system location execute a privileged command (useradd?) Be careful when using these functions as root, may not be able to come back to root Fall 2013 CS4350 Husain Gholoom Page 16

Examples: /* * This process sets its userid to 0 (root) */ #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> int main( void ) { uid_t ouid; } ouid = getuid(); if( setuid( 0 ) == -1 ) { perror( "setuid" ); return EXIT_FAILURE; } printf( "userid %d switched to 0\n", ouid ); return EXIT_SUCCESS; Fall 2013 CS4350 Husain Gholoom Page 17

Effective User ID Sometimes a user may want to write a program that other users on the system can execute In these situations a specific user may want to allow extra privileges to the users of the program This can be achieved by modifying the effective user ID of the process In addition to the real user ID, every process also has an effective user id associated with it The real user id is always set to the user who invokes the program By default, the effective user ID has the same value as the real user ID geteuid() Returns the effective user ID of the calling process. Header Prototype Call #include<sys/types.h> #include<unistd.h> uid_t geteuid(); int euid = geteuid(); Fall 2013 CS4350 Husain Gholoom Page 18

EXAMPLE The following code fragment illustrates the use of geteuid to determine the effective user ID of a calling process: #include <sys/types.h> #include <unistd.h> #include <stdio.h> main() { uid_t uid, euid; uid = getuid(); euid = geteuid(); printf("the effective user id is %dn", (int) geteuid()); printf("the real user id is %dn", (int) getuid()); if (uid == euid) printf("effective user id and Real user id are the samen"); } Sample Run : The effective user id is 9812n The real user id is 9812n Effective user id and Real user id are the samen Fall 2013 CS4350 Husain Gholoom Page 19

seteuid() Set the effective user ID Header Prototype Call #include<sys/types.h> #include<unistd.h> int seteuid(uid_t euid); int error = seteuid(getuid()); Fall 2013 CS4350 Husain Gholoom Page 20

Examples: /* * This process sets its effective userid to 0 (root). */ #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> int main( void ) { uid_t oeuid; oeuid = geteuid(); if( seteuid( 0 ) == -1 ) { perror( "seteuid" ); return EXIT_FAILURE; } printf( "effective userid now 0, was %d\n", oeuid ); return EXIT_SUCCESS; } Fall 2013 CS4350 Husain Gholoom Page 21

Providing Extra Privilege with EUID Set the set-user-bit $ chmod u+s prog when prog is executed its effective user id is going to be set to the owner of prog prog will have same privileges as the owner of prog not the caller of prog Use seteuid() to re-set the effective user ID back to real user ID types.h A collection of typedefs Definitions may vary across systems Determines storage requirements for different types uid_t -> short size_t -> int id_t -> int gid_t -> int pid_t -> int mode_t -> uint time_t -> long Should use these defined types even if program compiles when using primitive data types Fall 2013 CS4350 Husain Gholoom Page 22

printf() and fprintf() Header Prototype Call #include<stdio.h> printf(const char *format,...); fprintf(file *stream, const char *format,...); printf( Hello %s\n, name); fprintf(stderr, Hello %s\n, name); displays data to a stream notion of streams similar to C++ for printf() it s always stdout for fprintf() it s stdout, stderr or any regular file (FILE) Conversion specifiers Works with family of printf() and scanf() functions %d print argument as an integer %f print argument as a floating-point value formatting specifiers also available %c print argument as a char value argument should generally be between 32 and 126 %s print argument as an array of char argument is expected to be a pointer to a char string is expected to be null terminated Fall 2013 CS4350 Husain Gholoom Page 23

scanf() and fscanf() Header Prototype Call #include<stdio.h> scanf(const char *format,...); fscanf(file *stream, const char *format,...); scanf( Hello no %d\n, &num); fscanf(stdin, Hello %s\n, name); reads formatted data from a stream for scanf() stream is stdin for fscanf() stream is stdin or any regular file all arguments should be pointers or memory addresses will add null-termination if value is read into a string will discard CRLF if value read from keyboard Fall 2013 CS4350 Husain Gholoom Page 24

sprintf() Header Prototype Call #include<stdio.h> int sprintf(char *str, const char *format,...); char filename[256]; sprintf(filename, bar%d, i); writes formatted data to a string can use for concatenation of data that are not strings need to ensure that data fits in output string sscanf() Header Prototype Call #include<stdio.h> int sscanf(char *str, const char *format,...); char *str = John 17 M ; sscanf(str, %s %d %c, name, age, gender); reads formatted data from a string can use for extracting different types of data values from a string need to ensure that data fits in output string Fall 2013 CS4350 Husain Gholoom Page 25

Pointers int *x; declare a pointer variable allocate memory for a pointer variable how much memory is allocated? not guaranteed to be initialized to 0 (or null) no matter what type of pointer variable we declare the value stored inside the pointer variable is always an unsigned integer Like all other variables the pointer variable has an address malloc() and calloc() Header Prototype Call #include<stdlib.h> void *malloc (size_t size); void *calloc (size_t elem, size_t size); int *p = (int *) malloc(sizeof(int) * SIZE); char *q = (char *) calloc(sizeof(int) * SIZE); Allocate dynamic memory in heap calloc() gives memory with all zero bits, equivalent to malloc() and memset() Fall 2013 CS4350 Husain Gholoom Page 26

free() Header #include<stdlib.h> Prototype void *free (void *ptr); Call int *p = malloc();... free(p); Gives memory back to the system Can t de-allocate same memory twice Shouldn t de-allocate memory that we may later use Fall 2013 CS4350 Husain Gholoom Page 27

Things to pay attention to Header files Return Values and Handling error codes Special data structures and Calls that return information in a struct e.g. passwd struct Acessing System Information Two commonly used system calls are :- o gethostname() o uname() gethostname() Header #include<unistd.h> Prototype Call int gethostname(char *name, size_t namelen); char host[255]; gethostname(host, 255); Copies the hostname of the machine in the first argument Second argument specifies the number of characters that are copied Similar to uname -n or hostname Returns -1 if there is an error, 0 if successful Fall 2013 CS4350 Husain Gholoom Page 28

uname() Header #include<sys/utsname.h> Prototype int uname(struct utsname *name); Call struct utsname uts; // allocation for struct uname(&uts); printf( %s\n, uts.sysname); Provides system information in a struct called utsname Need to allocate struct and send address to function struct utsname struct utsname { char sysname[] char nodename[] char release[] char version[] char machine[] } Fall 2013 CS4350 Husain Gholoom Page 29

Example #include<sys/utsname.h> #include<stdio.h> main() { int uname(struct utsname *name); } struct utsname uts; // allocation for struct uname(&uts); printf("system Name %s\n", uts.sysname); printf("node Name %s\n", uts.nodename); printf("release %s\n", uts.release); printf("version %s\n", uts.version); Sample Run System Name Darwin Node Name 192.168.1.69 Release 11.4.2 Version Darwin Kernel Version 11.4.2: Thu Aug 23 16:26:45 PDT 2012; root:xnu-1699.32.7~1/release_i386 192:C_Programs husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 30

Time related system calls Time is handled with the defined type time_t Size of time_t may vary from system to system Should be more than 32 bits, otherwise we run into problems after 2038 Will probably transition to 64-bits completely by then Safe for another 293 billion years time() Header #include<time.h> Prototype time_t time(time_t *tloc); Call time_t thistime; thistime = time((time_t *) 0); printf( %d\n, thistime); Returns time elapsed since epoch in seconds ( from January 1 st 1970 ) Can use this to measure running time for code segments Fall 2013 CS4350 Husain Gholoom Page 31

Example #include<time.h> #include<stdio.h> main() { } time_t time(time_t *tloc); time_t thistime; thistime = time((time_t *) 0); printf("%d\n", thistime); Sample Run 192:C_Programs husaingholoom$./time.o 1381898362 192:C_Programs husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 32

gettimeofday() Header #include<sys/time.h> Prototype int gettimeofday(struct timeval *tv, struct timezone *tz); Call struct timeval curtime; struct timezone zone; int errcode = gettimeofday(curtime, zone); printf( %d\n, curtime.tv_sec); printf( %d\n, curtime.tv_usec); time() has been deprecated by gettimeofday() provides both seconds and microseconds Better accuracy for performance measurement also provides time zone info Fall 2013 CS4350 Husain Gholoom Page 33

gmtime() Header #include<time.h> Prototype Call struct tm* gmtime(const time_t *timeval); struct tm* tm_ptr; time_t thistime; thistime = time((time_t *) 0); tm_ptr = gmtime(&thistime); Returns time in years, months, days etc Returns GMT Use localtime() to get local time The time struct The tm structure contains the following members: int tm_sec; /* seconds after the minute [0, 60] */ int tm_min; /* minutes after the hour [0, 59] */ int tm_hour; /* hour since midnight [0, 23] */ int tm_mday; /* day of the month [1, 31] */ int tm_mon; /* months since January [0, 11] */ int tm_year; /* years since 1900 */ int tm_wday; /* days since Sunday [0, 6] */ int tm_yday; /* days since January 1 [0, 365] */ int tm_isdst; /* flag for daylight savings time */ Fall 2013 CS4350 Husain Gholoom Page 34

struct tm { int tm_sec int tm_min int tm_hour int tm_mday // starts at 1 int tm_mon // starts at 0 int tm_year } ; mktime() Header Prototype Call #include<sys/time.h> time_t mktime(struct *tm timeptr); struct tm* tm_ptr; time_t thistime; tm_ptr = gmtime(&thistime); time_t rawtime = mktime(tm_ptr); converts a tm structure to a calendar time Fall 2013 CS4350 Husain Gholoom Page 35

Example /* mktime example: weekday calculator */ #include <stdio.h> /* printf, scanf */ #include <time.h> /* time_t, struct tm, time, mktime */ int main () { time_t rawtime; struct tm * timeinfo; int year, month,day; const char * weekday[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; /* prompt user for date */ printf ("Enter year: "); scanf ("%d",&year); printf ("Enter month: "); scanf ("%d",&month); printf ("Enter day: "); scanf ("%d",&day); /* get current timeinfo and modify it to the user's choice */ time ( &rawtime ); timeinfo = localtime ( &rawtime ); timeinfo->tm_year = year - 1900; timeinfo->tm_mon = month - 1; timeinfo->tm_mday = day; /* call mktime: timeinfo->tm_wday will be set */ mktime ( timeinfo ); printf ("That day is a %s.\n", weekday[timeinfo->tm_wday]); return 0; } Fall 2013 CS4350 Husain Gholoom Page 36

Sample Run 192:C_Programs husaingholoom$./maketime.o Enter year: 2013 Enter month: 5 Enter day: 16 That day is a Thursday. 192:C_Programs husaingholoom$ Accessing Process Information Frequently used getpid(); getpriority(); setpriority(); Less frequently used setpgid(); getpgid(); Fall 2013 CS4350 Husain Gholoom Page 37

getpid() Header Prototype Call #include<sys/types.h> #include<unistd.h> pid_t getpid(); pid_t pid; pid = getpid(); On a unix system every process has a numeric id getpid() returns the id associated with the current process At any point in time, process ids are unique Fall 2013 CS4350 Husain Gholoom Page 38

getpriority() Header Prototype Call #include<sys/resource.h> int getpriority(int which, id_t who); getpriority(prio_user, getuid()); Returns the priority level of a process Each process on a Linux system runs with a priority value ranges between -20 to 20 the higher the numeric value the lower the priority getpriority() int getpriority(int which, id_t who); First argument says what the second argument is If first argument is PRIO_PROCESS then second argument is treated as a pid If first argument is PRIO_PGRP then second argument is treated as a pgid If first argument is PRIO_USER then second argument is treated as uid Examples int priority; priority = getpriority(prio_user, getuid()); priority = getpriority(prio_process, 17); Fall 2013 CS4350 Husain Gholoom Page 39

setpriority() Header #include<sys/resource.h> Prototype int setpriority(int which, id_t who, int prio); Call setpriority(prio_user, getuid(), 0); Sets the priority level of a process it is a program scheduling priority If you are not root you can only decrease the priority of your process Fall 2013 CS4350 Husain Gholoom Page 40

Files and Filesystem Files are bytes of data stored on the disk Divided in blocks Blocks may not be contiguous Almost never have to worry about that Filesystem is an organization of data and metadata on a storage device Where should files be stored? How do we find a file? Inode a key component of the unix filesystem Stores basic information about files File Types A file can be a regular file Text Binary If we look at a random byte, we can map the contents to a character in the ASCII table e.g., value 17 stored using 8 bytes, first 4 bytes has ASCII value for 1, second 4 bytes has ASCII value of 7 00000000 00000000 00000000 00000001 00000000 00000000 00000000 00000111 Contents are encoded Random byte may or not map to an ASCII character Even if it does map to a character does not represent the data stored in that byte e.g., value 17 stored in 4 bytes 00000000 00000000 00000000 00010001 Fall 2013 CS4350 Husain Gholoom Page 41

Directory On a unix system a directory is also a file bytes of data stored on the disk A directory is a special file holds data about files contained within the directory I/O interface is different from a regular file Cannot use the same functions as we would to read and write from a regular file Some commands will distinguish between directories and regular files (cat), others will not (less) Devices Devices are often mapped to files cdrom, printer, terminal does not contain data $ ls -l /dev/ can write to devices reading is less common A special device /dev/null : eats up any data sent to it useful when you don t want output of a command to appear on the screen $ find / -name hello 2> /dev/null Fall 2013 CS4350 Husain Gholoom Page 42

Example : [hag10@zeus ~]$ find / -name "hello" find: `/usr/share/cracklib': Permission denied /usr/share/tk8.5/demos/hello find: `/usr/lib64/audit': Permission denied /usr/packages/linux/tars/qt/examples/hello /usr/packages/linux/tars/qt/examples/hello/hello ^Xfind: `/lost+found': Permission denied find: `/etc/sudoers.d': Permission denied find: `/etc/cups/ssl': Permission denied find: `/etc/modprobe.d': Permission denied find: `/etc/audisp': Permission denied... Or [hag10@zeus ~]$ find / -name "hello" 2> /dev/null /usr/share/tk8.5/demos/hello /usr/packages/linux/tars/qt/examples/hello /usr/packages/linux/tars/qt/examples/hello/hello [hag10@zeus ~]$ Fall 2013 CS4350 Husain Gholoom Page 43

Links A special file that contains a reference to another file or directory in the form of an absolute or relative path Unix supports two types of links Soft links or symbolic links shares the same data but has different inodes deleting original makes link a dangling pointer $ ln s <file> <linkname> Hard links shares the same inode, same data deleting original makes the link the owner of the file $ ln <file> <linkname> Hard links cannot point to directories or files in other volumes Links do not store any data other than path information The path information is not stored on disk but rather in the inode ( index node ) Links can lead to some security issues Example ( Soft / Hard Links ) : [hag10@zeus ~]$ ln -s hello.c h.c // Soft link [hag10@zeus ~]$ ln h.c h2.c // hard link [hag10@zeus ~]$ cat h.c [hag10@zeus ~]$ cat h2.c Fall 2013 CS4350 Husain Gholoom Page 44

inode A data structure that stores file metadata The main component in a unix filesystem The filesystem contains an array of inodes An inode for each file on the filesystem Directories and device files also have an inode The inode array resides on disk Loaded to memory by kernel struct inode inode Entry An inode contains file type file access permission file size pointers to file's data blocks number of hard links timestamp inode does not contain the file name File name is stored in the directory To view inode numbers of files, type $ ls i Example : $ ls -i test.c 97566733 test.c Fall 2013 CS4350 Husain Gholoom Page 45

Advantages and Disadvantages of inode This underlying organization of files and directories provides the N-ary tree-like directory structure on unix systems Using a tree data structure, although more intuitive, would be much more expensive O(logn) <-> O(1) Drawback : number of files on the system limited by the size of the inode number theoretically possible to exceed this limit while disk is not full File Descriptor Mechanism for the kernel to keep track of all open files Each open file on the system has a file descriptor associated with it The kernel maintains a data structure for each file descriptor that includes File status (read, write etc.) File offset File size Pointer to the i-node Streams Streams allow us to do buffered I/O on files When we open a file using the standard I/O library we associate a stream with the file Every stream has a buffer (some amount of memory) When we do a write to a file the data is first written to the buffer in memory and then after an unspecified amount of time, is written to the file File reads are also buffered Fall 2013 CS4350 Husain Gholoom Page 46

Examining File Attributes Header Prototype #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> Call int stat (const char *filename, struct stat *fileinfo) ; struct stat fileinfo; stat( foo, &fileinfo); The stat function returns information about the attributes of the file named by filename in the structure pointed to by fileinfo If filename is the name of a symbolic link, the attributes you get describe the file that the link points to If the link points to a nonexistent file name, then stat fails reporting a nonexistent file Fall 2013 CS4350 Husain Gholoom Page 47

File Attributes When you read the attributes of a file, they come back in a structure called struct stat names of the attributes data types their meanings #include <sys/stat.h> struct stat {... }; mode_t st_mode File type information and the file permission bits Information is encoded uid_t st_uid User ID of the file's owner Note difference from getuid() gid_t st_gid Group ID of the file Note difference from getgid() ino_t st_ino File i-node number time_t st_atime Last access time for the file time_t st_mtime Last modification time to the contents of the file time_t st_ctime Last modification time to the attributes of the file nlink_t st_nlink Number of hard links to the file Symbolic links (soft links) are not counted here off_t st_size Size of a regular file in bytes. blkcnt_t st_blocks Amount of disk space that the file occupies, measured in units of (512 byte) blocks. Fall 2013 CS4350 Husain Gholoom Page 48

Retrieving Info from st_mode st_mode stores multiple pieces of information File type and access information for user, group and other Need to use bit operations to retrieve the encoded information Pre-defined macros available for checking file type int S_ISREG (mode_t m) Returns non-zero if the file is a regular file. int S_ISDIR (mode_t m) Return non-zero if the file is a directory. int S_ISLNK (mode_t m) Returns non-zero if the file is a symbolic link. int S_ISSOCK (mode_t m) Returns non-zero if the file is a socket Fall 2013 CS4350 Husain Gholoom Page 49

Checking File Type in st_mode Example struct stat fileinfo; stat( foo, &fileinfo); if (S_ISDIR(fileinfo.st_mode)) printf( directory ); Checking File Permission in st_mode File permission masks S_IRWXU /* user read-write-execute mask */ S_IRWXG /* group read-write-execute mask */ S_IRWXO /* other read-write-execute mask */ Used to interpret file permission info in st_mode Symbolic names for file permission bits S_IRUSR S_IWUSR S_IXUSR S_IRGRP S_IWGRP S_IXGRP S_IROTH S_IWOTH S_IXOTH user read user write user exec group read group write group exec other read other write other exec Fall 2013 CS4350 Husain Gholoom Page 50

General procedure for checking file permission Extract current mode and perform bitwise and (&) with mask Compare result with permission bits Can do bitwise or ( ) to combine bits or takes on the meaning of and Check if user has read and write permission on file if ((mode & S_IRWXU) == (S_IWUSR S_IRUSR)) Check if group has only execute permission on file if ((mode & S_IRWXG) == (S_IXGRP)) Fall 2013 CS4350 Husain Gholoom Page 51

Example : #include<stdio.h> #include<stdlib.h> #include<sys/stat.h> #include<sys/types.h> int main(int argc, char *argv[]) { char *filetype; struct stat fileinfo; int ret; if (argc < 2) { printf("usage: filename\n"); return 0; } /* construct mode : -rw-rw--- */ /* implies and */ mode_t chmode = S_IRUSR S_IWUSR S_IRGRP S_IWGRP; CHECK(chmod(argv[1], chmode)); CHECK(ret = stat(argv[1], &fileinfo)); /* check file permissions */ mode_t mode; mode = fileinfo.st_mode; /* check if user has read permission (and only read permission) on file */ if ((mode & S_IRWXU) == S_IRUSR) printf("user has read permission on %s\n", argv[1]); Fall 2013 CS4350 Husain Gholoom Page 52

/* check if user has read and write permission on file */ if ((mode & S_IRWXU) == (S_IRUSR S_IWUSR)) printf("user has read and write permission on %s\n", argv[1]); /* check if user has read, write and execute permission on file */ if ((mode & S_IRWXU) == (S_IRUSR S_IWUSR S_IXUSR)) printf("user has read, write and execute permission on %s\n", argv[1]); /* masking with group mask will produce incorrect results for user */ if ((mode & S_IRWXG) == (S_IRUSR S_IWUSR S_IXUSR)) printf("user has read, write and execute permission on %s\n", argv[1]); /* mask with group mask to check group permissions */ if ((mode & S_IRWXG) == (S_IRUSR S_IWUSR S_IXUSR)) printf("group has read, write and execute permission on %s\n", argv[1]); return 0; } Sample Run 192:c_programs husaingholoom$./a.out test.c user has read and write permission on test.c Fall 2013 CS4350 Husain Gholoom Page 53

Examining File Attributes Header Prototype #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> Call int fstat (int filedes, struct stat *fileinfo) ; struct stat fileinfo; fstat(stdout_fileno, &fileinfo); The fstat() function is like stat(), except that it takes an open file descriptor as an argument instead of a file name Examining File Attributes Header #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> Prototype int lstat (const char *filename, struct stat *fileinfo) ; Call struct stat fileinfo; lstat( foo, &fileinfo); Fall 2013 CS4350 Husain Gholoom Page 54

The lstat() function is like stat, except that it does not follow symbolic links If filename is the name of a symbolic link, lstat() returns information about the link itself; otherwise lstat() works like stat() Information returned for symbolic links Type, size and link count Permission and access info is derived from parent directory Fall 2013 CS4350 Husain Gholoom Page 55

Example // A Program to Print some general file info #include <time.h> #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <sys/types.h> int main(int argc, char *argv[]) { struct stat file_stats; if(argc!= 2) fprintf(stderr, "Usage: fstat FILE...\n"), exit(exit_failure); if((stat(argv[1], &file_stats)) == -1) { perror("fstat"); return 1; } printf("information for %s\n",argv[1]); printf("---------------------------\n"); printf("filename: %s\n", argv[1]); printf(" device: %lld\n", file_stats.st_dev); printf(" inode: %ld\n", file_stats.st_ino); printf(" protection: %o\n", file_stats.st_mode); printf(" number of hard links: %d\n", file_stats.st_nlink); printf(" user ID of owner: %d\n", file_stats.st_uid); printf(" group ID of owner: %d\n", file_stats.st_gid); printf(" device type (if inode device): %lld\n",file_stats.st_rdev); printf(" total size, in bytes: %ld\n", file_stats.st_size); printf(" blocksize for filesystem I/O: %ld\n", file_stats.st_blksize); printf(" number of blocks allocated: %ld\n", file_stats.st_blocks); printf(" time of last access: %ld : %s", file_stats.st_atime, ctime(&file_stats.st_atime)); printf(" time of last modification: %ld : %s", file_stats.st_mtime, ctime(&file_stats.st_mtime)); printf(" time of last change: %ld : %s", file_stats.st_ctime, ctime(&file_stats.st_ctime)); return 0; } Fall 2013 CS4350 Husain Gholoom Page 56

Sample Run 192:c_programs husaingholoom$./a.out chmode.c Information for chmode.c --------------------------- filename: chmode.c device: 234881026 inode: 5711936 protection: 100755 number of hard links: 1 user ID of owner: 501 group ID of owner: 20 device type (if inode device): 0 total size, in bytes: 1561 blocksize for filesystem I/O: 4096 number of blocks allocated: 8 time of last access: 1382492926 : Tue Oct 22 20:48:46 2013 time of last modification: 1382492908 : Tue Oct 22 20:48:28 2013 time of last change: 1382492920 : Tue Oct 22 20:48:40 2013 192:c_programs husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 57

Modifying File Permissions Header Prototype #include <sys/types.h> #include <sys/stat.h> Call int chmod (const char *filename, mode_t mode) ; mode_t mode = S_IRUSR S_IWUSR; chmod( foo, mode); chmod() allows us to modify permission bits for a file to change file permissions the effective user id must be the same as the owner of the file can set file permissions using absolute mode value or through bit manipulation of current st_mode Fall 2013 CS4350 Husain Gholoom Page 58

Modifying File Permissions Header Prototype #include <sys/types.h> #include <sys/stat.h> Call int fchmod (int filedes, int mode) ; mode_t mode = S_IRUSR S_IWUSR; fchmod(stdout_fileno, mode); Functionally exactly like chmod() Takes a file descriptor as an argument rather than a file name Passing on a file descriptor implies the file is currently open Fall 2013 CS4350 Husain Gholoom Page 59

Checking File Permission Header #include <unistd.h> Prototype int access (const char *filename, int how) ; Check whether the file can be accessed in the way specified by the how argument. Uses the real user and group IDs of the calling process to check for access permission The how argument can be a bitwise OR of the flags R_OK W_OK X_OK Can also do an existence test with F_OK Useful as a check before calling open() Fall 2013 CS4350 Husain Gholoom Page 60

File Ownership Header #include <sys/types.h> #include <sys/unistd.h> Prototype int chown (const char *filename, uid_t owner, gid_t group); int fchown (int filedes, int owner, int group) ; The chown() function changes the owner of the file filename to owner, and its group owner to group Usually useful only for root Privileged processes can use chown() to give ownership of files to other users Can t get them back fchown()is like chown(), except that it changes the owner of the open file with descriptor filedes Fall 2013 CS4350 Husain Gholoom Page 61

umask() Header #include <sys/types.h> #include <sys/stat.h> Prototype mode_t umask (mode_t mask) The umask() function sets the file creation mask of the current process to mask, and returns the previous value of the file creation mask umask()is needed when calling functions such as creat() and open() getumask() Header Prototype #include <sys/types.h> #include <sys/stat.h> mode_t getumask (void) Return the current value of the file creation mask for the current process Fall 2013 CS4350 Husain Gholoom Page 62

Example #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> main() { mode_t oldmask, newmask; /* Get old mask, temporarily setting the mask to 0. */ oldmask = umask((mode_t) 0); /* Print old mask. Octal values are used by mask. */ printf("old mask = %on\n", (int) oldmask); /* Make sure group read permission is allowed. */ if (oldmask & S_IRGRP) { printf("changing group read permission from MASKED to UNMASKED.n\n"); oldmask = (oldmask ^ S_IRGRP); } /* Make sure group write and execute permission is not allowed. */ newmask = (oldmask S_IWGRP S_IXGRP); umask(newmask); /* Update mask. */ printf("new mask = %onn\n", (int) newmask); /* Print new mask. */ printf("the file mode creation mask now specifies:nn\n"); printf(" Group read permission UNMASKEDn\n"); printf(" Group write permission MASKEDn\n"); printf(" Group execute permission MASKEDn\n"); } Fall 2013 CS4350 Husain Gholoom Page 63

Sample Run 192:c_programs husaingholoom$./a.out Old mask = 22n New mask = 32nn The file mode creation mask now specifies:nn Group read permission UNMASKEDn Group write permission MASKEDn Group execute permission MASKEDn File Size Header Prototype #include <unistd.h> int truncate (const char *filename, off_t length) ; int ftruncate (int fd, off_t length) ; The function changes the size of filename to length. If length is shorter than the previous length (typically the case), data at the end will be lost On some systems can use truncate to extend the file size empty blocks added to end of file Fall 2013 CS4350 Husain Gholoom Page 64

Example #include <stdio.h> #include <sys/stat.h> int main() { struct stat stat_result; char filename[]= "foo2"; if ( stat(filename, &stat_result) == -1 ) return 1; printf("size of \"%s\" is %ld bytes\n", filename, stat_result.st_size); return 0; } Sample Run 192:c_programs husaingholoom$./a.out Size of "foo2" is 0 bytes 192:c_programs husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 65

Rename File Header #include<stdio.h> Prototype int rename (const char *oldname, const char *newname) The rename function renames the file oldname to newname If oldname is not a directory, then any existing file named newname is removed during the renaming operation if newname is the name of a directory, rename fails If oldname is a directory, then either newname must not exist or it must name a directory that is empty Fall 2013 CS4350 Husain Gholoom Page 66

Example #include <stdio.h> int main () { int ret; char oldname[] = "foo2"; char newname[] = "foo3"; ret = rename(oldname, newname); if(ret == 0) { printf("file renamed successfully"); } else { printf("error: unable to rename the file"); } } return(0); Sample Run 192:c_programs husaingholoom$./a.out File renamed successfully192:c_programs husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 67

Delete File Header #include<stdio.h> Prototype int remove (const char *filename) Delete a file from the system Fall 2013 CS4350 Husain Gholoom Page 68

Example #include<stdio.h> main() { int status; char file_name[25]; printf("enter the name of file you wish to delete\n"); gets(file_name); status = remove(file_name); if( status == 0 ) printf("%s file deleted successfully.\n",file_name); else { printf("unable to delete the file\n"); perror("error"); } return 0; } Sample Run 192:c_programs husaingholoom$./a.out Enter the name of file you wish to delete warning: this program uses gets(), which is unsafe. foo2 Unable to delete the file Error: No such file or directory 192:c_programs husaingholoom$ Fall 2013 CS4350 Husain Gholoom Page 69

What's File IO Before you can read or write the contents of a file, you must establish a connection or communications channel to the file. This process is called opening the file. The connection to an open file is represented either as a stream or as a file descriptor. A file descriptor within an application is represented with an integer value - When a file is opened the kernel always returns the lowest unused value - Applications cannot choose values for file descriptors - When you have finished reading to or writing from the file, you can terminate the connection by closing the file. - When a file descriptor is closed the value goes back into the available pool - Every system has a maximum number of open file descriptors for a process File Descriptor Handler Represented as a non-negative integer 0 (STDIN_FILENO) : standard input 1 (STDOUT_FILENO) : standard output 2 (STDERR_FILENO) : standard error When used, you should refer to these special file descriptors using symbolic names not the integer values Fall 2013 CS4350 Husain Gholoom Page 70

Stream Streams allow us to do buffered I/O on files When we open a file using the standard I/O library we associate a stream with the file Every stream has a buffer (some amount of memory) When we do a write to a file the data is first written to the buffer in memory and then after an unspecified amount of time, is written to the file File reads are also buffered Within a C program a stream is represented as a pointer to a FILE object - represented as FILE * objects Special streams - stdin, stdout, stderr Comparison Streams provide a higher-level interface, provides powerful formatted input and output. File descriptor provides only simple functions for transferring blocks of characters. Fall 2013 CS4350 Husain Gholoom Page 71

Summary : Two Modes of I/O Operations Fall 2013 CS4350 Husain Gholoom Page 72

Three Steps in File I/O Open - tell the kernel we want to do I/O on a file Read/Write - do the I/O (with the kernel as a mediator) Close - tell the kernel we are done Need to follow these steps if we are using file descriptors or streams Use different functions File I/O System Calls Stream I/O - fopen() - fclose() - fprintf(), fscanf() - File Descriptor I/O - open() - close() - read(), write() - lseek() Fall 2013 CS4350 Husain Gholoom Page 73

Opening a Stream Header #include <stdio.h> Prototype FILE *fopen(const char *filename, const char *mode) ; Call FILE *myfile = fopen( foo, r ); Open a file with name filename and mode mode - Can specify absolute or relative path as filename Return a FILE pointer, NULL if unsuccessful Important to check for return value before attempting I/O - Will fail if file doesn t exist or doesn t have the right permissions - Will fail if there is no room to allocate a FILE pointer FILE * Access Modes r : open for reading w : open for writing a : append, open for writing at end of file r+ : open for reading and writing a+ : open for reading and writing at end of file Fall 2013 CS4350 Husain Gholoom Page 74

Closing a Stream Header #include <stdio.h> Prototype int fclose(file *fp) ; Call fclose(fp); Close a file stream pointed to by the FILE pointer fp Return 0 if successful Can open and close a file stream multiple times Each close() operation flushes the buffer Can also use fflush() to flush the buffer Reading and writing starts from beginning of file when we reopen the strea I/O on File Streams Should be already familiar with the following Input int scanf(const char *format,...) ; int fscanf(file *stream, const char *format,...) ; int sscanf(const char *str, const char *format,...) ; Output int printf(const char *format,...) int fprintf(file *stream, const char *format,...) int sprintf(char *str, const char *format,...) Fall 2013 CS4350 Husain Gholoom Page 75

I/O on File Streams ( cont. ) Header #include <stdio.h> Prototype fgets(char *s, int n, FILE *stream); reads a line of input from stream reads characters until end of line is encountered n - 1 characters have been read EOF is reached need this if we want to read strings with whitespaces fgetc(), getc() Reads a character from a stream fputc(), putc() Writes a character to a stream fputs() Write a line to stream Fall 2013 CS4350 Husain Gholoom Page 76

Using File Descriptors Ask kernel to allocate memory to load part of the file The kernel provides a handle on the file descriptor in memory to Perform I/O on file descriptor Close file descriptor open() Header #include <fcntl.h> Prototype int open (const char *filename, int flags) ; int open (const char *filename, int flags, mode_t mode) ; Call int fd = open ( foo, O_RDONLY); The open() function creates and returns a new file descriptor for the file named by filename. Initially, the file position indicator for the file is at the beginning of the file. On failure returns -1 The argument mode is used when a file is created. Fall 2013 CS4350 Husain Gholoom Page 77

Access Modes Flags Allow a file descriptor to be used for reading, writing, or both. The access modes are chosen when the file is opened, and never change. Access modes remain in effect until file descriptor is closed int O_RDONLY Open the file for read access. int O_WRONLY Open the file for write access. int O_RDWR Open the file for both reading and writing. Open-time Flags Specify options affecting how open will behave. These options are not preserved once the file is open. There are two sorts of options specified by open-time flags. File name translation flags affect how open looks up the file name to locate the file, and whether the file can be created. Open-time action flags specify extra operations that open will perform on the file once it is open. int O_CREAT If set, the file will be created if it doesn't already exist. int O_EXCL If both O_CREAT and O_EXCL are set, then open fails if the specified file already exists. int O_NONBLOCK This prevents open from blocking for a "long time" to open the file. int O_NOLINK If the named file is a symbolic link, open the link itself instead of the file it refers to Fall 2013 CS4350 Husain Gholoom Page 78

int O_TRUNC Truncate the file to zero length. int O_EXLOCK Acquire an exclusive lock on the file. close() Header #include <unistd.h> Prototype int close (int fd) ; Call close(fd); Close the file descriptor filedes. Consequences: The file descriptor is deallocated. Any record locks owned by the process on the file are unlocked. Example #include <stdio.h> #include <fcntl.h> #include <unistd.h> int main(int argc, char** argv) { int fd; fd=open("afile", O_WRONLY O_CREAT O_TRUNC, 438); close(fd); printf("%d\n", fd); return (0); } // the above example truncates the file to zero length Fall 2013 CS4350 Husain Gholoom Page 79

read() Header #include <unistd.h> Prototype ssize_t read (int fd, void *buf, size_t size) Call char buf[256]; read(fd, buf, 128); The read function reads up to size bytes from the file with descriptor filedes, storing the results in the buffer. The buffer is not necessarily a character string, and no terminating null character is added. The return value is the number of bytes actually read. This might be less than size argument A value of zero indicates end-of-file If you keep calling read() while at end-of-file, it will keep returning zero and doing nothing else. If read() fails it will return -1 Fall 2013 CS4350 Husain Gholoom Page 80

pread() Header #include <unistd.h> Prototype Call ssize_t pread (int fd, void *buf, size_t size, off_t offset); char buf[256]; pread(fd, buf, 16, 64); pread() reads from the file starting at position offset Similar lseek() + read(), but position of the file descriptor itself is not affected by the operation write() Header #include <unistd.h> Prototype ssize_t write (int fd, const void *buf, size_t size) ; Call write(fd, foobar, 6); The write function writes up to size bytes from buffer to the file with descriptor filedes. The return value is the number of bytes actually written. This may be size, but can always be smaller. Once write returns, the data is en-queued to be written and can be read back right away, but it is not necessarily written out to permanent storage immediately. You can use fsync() when you need to be sure your data has been permanently stored before continuing. You can use the O_FSYNC open mode to make write always store the data to disk before returning Fall 2013 CS4350 Husain Gholoom Page 81

pwrite() Header #include <unistd.h> Prototype ssize_t pwrite (int fd, const void *buf, size_t size, off_t offset) Call pwrite (fd, foobar, 6, 32); The difference is the fourth argument and its handling. The data block is not written to the current position of the file descriptor filedes. Instead the data is written to the file starting at position offset. The position of the file descriptor itself is not affected by the operation. lseek() Header #include <unistd.h> Prototype off_t lseek (int fd, off_t offset, int whence) ; Call lseek(fd, 100, SEEK_CUR); lseek() sets the read/write pointer for the file descriptor The whence argument specifies how the offset should be interpreted and it must be one of the symbolic constants SEEK_SET, SEEK_CUR, or SEEK_END. SEEK_SET - Specifies that whence is a count of characters from the beginning of the file. Fall 2013 CS4350 Husain Gholoom Page 82