Files and Directories Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 1 of 60.

Slides:



Advertisements
Similar presentations
Operating system services Program execution I/O operations File-system manipulation Communications Error detection Resource allocation Accounting Protection.
Advertisements

1 Files and Directories Hua LiSystems ProgrammingCS2690Files and Directories.
Process Control Hua LiSystems ProgrammingCS2690Process Control Page 1 of 41.
System-Level I/O Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
Files. System Calls for File System Accessing files –Open, read, write, lseek, close Creating files –Create, mknod.
1 Advanced programming in UNIX 1 File I/O Hua LiSystems ProgrammingCS2690File I/O.
Linux+ Guide to Linux Certification, Second Edition
6/24/2015B.RamamurthyPage 1 File System B. Ramamurthy.
Kernel Directory Interface. DirectoriesDirectories Recall that in Unix your hard drive is organized into a hierarchical system of directories and files.
7/15/2015B.RamamurthyPage 1 File System B. Ramamurthy.
CS 311 – Lecture 12 Outline File management system calls Stat() Directory Information  Opendir()  Readdir()  Closedir() Truncate() and remove() Lecture.
1 Unix File System API Operating System Hebrew University Spring 2007.
1 UNIX Systems Programming Interprocess communication.
Filesystem Hierarchy Standard (FHS) –Standard of outlining the location of set files and directories on a Linux system –Gives Linux software developers.
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
Chapter 39 Virtsualization of Storage: File and Directory Chien-Chung Shen CIS, UD
Files & Directories Objectives –to be able to describe and use the Unix file system model and concepts Contents –directory structure –file system concepts.
Input and Output Topics I/O hardware Unix file abstraction Robust I/O File sharing io.ppt CS 105 “Tour of the Black Holes of Computing”
1 System-Level I/O Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem  Low level file I/O.
Chapter 5 Files and Directories Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
Linux+ Guide to Linux Certification, Second Edition
1 UNIX System Programming v Objectives –look at how to program with directories –briefly describe the UNIX file system Directories and File System.
1 UNIX System Programming Files and Directories. 2 File Pointer Both read() and write() will change the file pointer. The pointer will be incremented.
File Systems CSCI What is a file? A file is information that is stored on disks or other external media.
Adv. UNIX: dirs/181 Advanced UNIX v Objectives –look at how to program with directories –briefly describe the UNIX file system Special.
Directory structure. Slide 2 Directory Structure  A directory ‘file’ is a sequence of lines; each line holds an i-node number and a file name.  The.
UNIX Files File organization and a few primitives.
Files & File system. A Possible File System Layout Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
Files and Directories File types stat functions for file information
Project 6 Unix File System. Administrative No Design Review – A design document instead 2-3 pages max No collaboration with peers – Piazza is for clarifications.
Navigating Directories
Linux+ Guide to Linux Certification, Third Edition
Linux+ Guide to Linux Certification, Third Edition
Advanced Programming in the Unix Environment
Manage Directories and Files in Linux Part 2. 2 Identify File Types in the Linux System The file types in Linux referred to as normal files and directories.
Today’s topic Access and manipulate meta data for files –File type, ownership, access permissions, access time, etc How to determine if a file is not there?
File Systems. 2 What is a file? A repository for data Is long lasting (until explicitly deleted).
Laface 2007 File system 2.1 Operating System Design Filesystem system calls buffer allocation algorithms getblk brelse bread breada bwrite iget iput bmap.
Programming with UNIX File Systems (Chap 3, 4. in the book “Advanced Programming in the UNIX Environment”) Acknowledgement : Prof. Y. Moon at Kangwon Nat’l.
1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data.
NCHU System & Network Lab Lab 14 File and Directory.
CSE333 SECTION 3. Important Dates Jan 27 th – Homework 1 Due Feb 6 th – Midterm.
January 7, 2003Serguei Mokhov, 1 File I/O System Calls Reference COMP 229, 444, 5201 Revision 1.2 Date: July 21, 2004.
CS 241 Section Week #8 (10/29/09). Outline MP5 Overview Files & I/O UNIX File Systems inodes Directories Links.
Library Functions The UNIX system provides a large number of C functions as libraries. Some of these implement frequently used operations, while others.
Files & Directories 2 CS 360 dir2. Slide 2 dir2 CS 360, WSU Vancouver Course Topics Program development review Files & Directories Tool Building Processes.
1 System Programming Chapter 4 Files and Directories.
CSCI 330 UNIX and Network Programming Unit VIII: I/O Management II.
The Unix File System R Bigelow. The UNIX File System The file system refers to the way in which UNIX implements files and directories. The UNIX file system.
Adv. UNIX: FileAgain/171 Advanced UNIX v Objectives –to look at the low-level manipulation of files and their properties (e.g. permissions)
File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Files Oct. 28, 2008 Topics Mapping file offsets to disk blocks File system buffering and you The directory hierarchy lecture-18.ppt “The course.
CSC 271 – Software I: Utilities and Internals An Introduction to File I/O in Linux Credited to Dr. Robert Siegfried and Beginning Linux Programming by.
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem.
CSS430 File System1 CSS430 File-System Interface Textbook Ch11 These slides were compiled from the OSC textbook slides (Silberschatz, Galvin, and Gagne)
CHAPTER 4. FILES AND DIRECTORIES System Programming 本份投影片大量參考熊博安教授的系統程式投影片 羅習五 國立中正大學資訊工程學系 EA-001 (05)
Linux Filesystem Management
Week 12 - Thursday CS222.
Unix Files Course code: 10CS62 Prepared by Department of CSE
Directory Directory is a file containing list of entries,where each entry represents inode number and name of the file stored in that directory.
LINUX programming Unit-3 PPT Slides
Operating System Hebrew University Spring 2004
An overview of the kernel structure
File System B. Ramamurthy B.Ramamurthy 11/27/2018.
File Structure Related system calls
“The course that gives CMU its Zip!”
File I/O (1) Prof. Ikjun Yeom TA – Hoyoun
Programming with UNIX File Systems (Chap 3, 4
Chien-Chung Shen CIS, UD
Presentation transcript:

Files and Directories Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 1 of 60

Main Topics Additional features of the filesystem. Properties of a file. stat functions. Each member of the stat structure. All attributes of a file. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 2 of 60

Describe functions that modify the attributes of a file. Structure of Unix filesystem in more detail. Symbolic links in more detail. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 3 of 60

stat, fstat and lstat Functions Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 #include int stat (const char *pathname, struct stat *buf); int fstat (int filedes, struct stat *buf); int lstat (const char *pathname, struct stat *buf); All three return: 0 if OK, -1 on error Page 4 of 60

Given a pathname, the stat function returns a structure of information about the named file. The fstat function obtains information about the file that is already open on the descriptor filedes. The lstat function is similar to stat, but when the named file is a symbolic link, lstat returns information about the symbolic link, not the file referenced by the symbolic link. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 5 of 60

The second argument of the stat functions is a pointer to a structure that we must supply. The function fills in the structure pointed to by buf. The structure could look like: struct stat { mode_t st_mode; /* file type & mode (permissions) */ ino_t st_ino; /* i-node number (serial number) */ dev_t st_dev; /* device number (filesystem) */ dev_t st_rdev; /* device number for special files */ nlink_t st_nlink; /* number of links */ Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 6 of 60

uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ off_t st_size; /* size in bytes, for regular files */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last file status change */ long st_blksize; /* best I/O block size */ long st_blocks; /* number of 512-byte blocks allocated */ }; Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 7 of 60

File Types Regular File It is the most common type of file, which contains data of some form. There is no distinction to the Unix kernel whether this data is text or binary. Any interpretation of the contents of a regular file is left to the application processing the file. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 8 of 60

Directory File It is a file that contains the names of other files and pointers to information on these files. Any process that has read permission for a directory file can read the contents of the directory. Character Special File It is a type of file used for certain types of devices on a system. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 9 of 60

Block Special File It is a type of file typically used for disk devices. All devices on a system are either character special files or block special files. FIFO It is a type of file used for interprocess communication between processes. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 10 of 60

Socket It is a type of file used for network communication between processes. A socket can also be used for nonnetwork communication between processes on a single host. Symbolic Link It is a type of file that points to another file. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 11 of 60

The type of a file is encoded in the st_mode member of the stat structure. The various file type macros in are as follows: Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 MacroType of file S_ISREG ( ) S_ISDIR ( ) S_ISCHR ( ) S_ISBLK ( ) S_ISFIFO ( ) regular file directory file character special file block special file pipe or FIFO Page 12 of 60

The argument to each of these macros is the st_mode member from the stat structure. Example The following program takes its command-line arguments and prints the type of file for each command-line argument. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 MacroType of file S_ISLNK ( ) S_ISSOCK ( ) symbolic link (not in POSIX.1 or SVR4) socket (not in POSIX.1 or SVR4) Page 13 of 60

Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 #include #include “ourhdr.h” int main (int argc, char *argv[]) { int i; struct stat buf; char *ptr; Page 14 of 60

Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 for (i = 1; i < argc; i++) { printf (“%s: ”, argv[i]); if (lstat(argv[i], &buf) < 0) { err_ret (“lstat error”); continue; } if (S_ISREG(buf.st_mode)) ptr = “regular”; else if (S_ISDIR(buf.st_mode)) ptr = “directory”; else if (S_ISCHR(buf.st_mode)) ptr = “character special”; Page 15 of 60

Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 else if (S_ISBLK(buf.st_mode)) ptr = “block special”; else if (S_ISFIFO(buf.st_mode)) ptr = “fifo”; #ifdef S_ISLNK else if (S_ISLNK(buf.st_mode)) ptr = “symbolic link”; #endif #ifdef S_ISSOCK else if (S_ISSOCK(buf.st_mode)) ptr = “socket”; #endif else ptr = “** unknown mode ** printf (“%s\n”, ptr); Page 16 of 60

Sample output from the program is as follows: $ a.out /vmunix /etc /dev/ttya /dev/sd0a /var/spool/cron/FIFO \ > /bin /dev/printer /vmunix: regular /etc: directory /dev/ttya: character special /dev/sd0a: block special Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 } exit (0); } Page 17 of 60

/var/spool/cron/FIFO: fifo /bin: symbolic link /dev/printer: socket Set-User-ID and Set-Group-ID Every process has six or more IDs associated with it. They are shown as follows: Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 real user ID who we really are real group ID Page 18 of 60

The saved set-user-ID and saved set-group-ID contain copies of the effective user ID and the effective group ID when a program is executed. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 effective user ID effective group ID used for file access permission checks supplementary group Ids saved set-user-ID saved by exec functions saved set-group-ID Page 19 of 60

File Access Permissions The st_mode value also encodes the access permission bits for the file. All the file types (directories, character special files, etc.) have permissions. There are nine permission bits for each file, divided into three categories. These nine file access permission bits, from are as follows: Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 20 of 60

Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 st_mode maskMeaning S_IRUSR S_IWUSR S_IXUSR user-read user-write user-execute S_IRGRP S_IWGRP S_IXGRP group-read group-write group-execute S_IROTH S_IWOTH S_IXOTH other-read other-write other-execute Page 21 of 60

The chmod(1) command, which is typically used to modify these nine permission bits, allows us to specify u for user (owner), g for group, and o for other. Ownership of New Files and Directories The user ID of a new file is set to the effective user ID of the process. The group ID of a new file can be the effective group ID of the process. The group ID of a new file can be the group ID of the directory in which the file is being created. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 22 of 60

In the above case, all the files and directories created in that directory will have the group ID belonging to the directory. This group ownership of files and directories will then propagate down the hierarchy from that point. access Function The access function bases its tests on the real user ID and the real group ID. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 #include Page 23 of 60

The mode is the bitwise OR of any of the constants shown below, for access function, from Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 int access (const char *pathname, int mode); Returns: 0 if OK, -1 on error. modeDescription R_OK W_OK test for read permission test for write permission Page 24 of 60

Example The program which is an example of an access function is as follows: Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 modeDescription X_OK F_OK test for execute permission test for existence of file #include Page 25 of 60

Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 #include #include “ourhdr.h” int main (int argc, char *argv[]) { if (argc != 2) err_quit (“usage: a.out ”); if (access (argv[1], R_OK) < 0) err_ret (“access error for %s”, argv[1]); else Page 26 of 60

chmod and fchmod Functions Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 printf (“read access OK\n”); if (open (argv[1], O_RDONLY) < 0) err_ret (“open error for %s”, argv[1]); else printf (“open for reading OK\n”); exit(0); } Page 27 of 60

These two functions allow us to change the file access permissions for an existing file. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 #include int chmod (const char *pathname, mode_t mode); int fchmod (int filedes, mode_t mode); Both return: 0 if OK, -1 on error Page 28 of 60

The chmod function operates on the specified file while the fchmod function operates on a file that has already been opened. To change the permission bits of a file, the effective user ID of the process must equal the owner of the file, or the process must have superuser permissions. The mode is specified as the bitwise OR of the constants shown in the figure below: Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 modeDescription S_ISUIDset-user-ID on execution Page 29 of 60

Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 modeDescription S_ISGIDset-group-ID on execution S_IRWXU S_IRUSR S_IWUSR S_IXUSR read, write, and execute by user (owner) read by user (owner) write by user (owner) execute by user (owner) S_IRWXG S_IRGRP read, write, and execute by group read by group Page 30 of 60

The mode constants for chmod functions are from Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 modeDescription S_IWGRP S_IXGRP write by group execute by group S_IRWXO S_IROTH S_IWOTH S_IXOTH read, write, and execute by other (world) read by other (world) write by other (world) execute by other (world) Page 31 of 60

Example The example of a chmod function is as follows: Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 #include #include “ourhdr.h” int main(void) { Page 32 of 60

Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 struct stat statbuf; /* turn on set-group-ID and turn off group-execute */ if (stat(“foo”, &statbuf) < 0) err_sys(“stat error for foo”); if(chmod(“foo”, (statbuf.st_mode & ~S_IXGRP) | S_ISGID) < 0) err_sys(“chmod error for foo”); /* set absolute mode to “rw-r--r—” */ Page 33 of 60

chown, fchown, and lchown Functions The chown functions allow us to change the user ID of a file and the group ID of a file. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 if (chmod(“bar”, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) err_sys(“chmod error for bar”); exit(0); } Page 34 of 60

Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 #include int chown (const char *pathname, uid_t owner, gid_t group); int fchown (int filedes, uid_t owner, gid_t group); int lchown(const char *pathname, uid_t owner, gid_t group); All three return: 0 if OK, -1 on error Page 35 of 60

If the referenced file is a symbolic link, lchown changes the owners of the symbolic link itself, not the file pointed to by the symbolic link. Filesystems A disk drive can be divided into one or more partitions. Each partition can contain a filesystem. The figure representing this is as follows: Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 36 of 60

INODE Structure Administrative information and physical data addresses of the file: –file mode –count of hard links –owner id –group id –time of last file access –time of last file modification –time of last inode modification –file size –file addresses

Internal View of a File System Boot Block Super Block i-list Blocks …… Last i-list Blocks First Data Block Data Blocks Total Blocks in File System Block FD-1 T...

Internal View of a File System Boot BlockBoot Block: the first block in a UNIX file system, contains the boot program and other initialization information or unused. Super BlockSuper Block: always the second block, contains the complete catalog of specific information about the file system i-list Blocksi-list Blocks: list of inodes for the file system, contiguous and always follows the super block. The number of inodes is specified by the system administrator Data BlocksData Blocks: immediately follow the i-list and consume the rest of the blocks.

Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 37 of 60

i-nodes are fixed-length entries that contain most of the information about the file. inode: index node representing a file. Every access to the file must make use of the information of the inode. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 38 of 60

Every i-node has a link count that contains the number of directory entries that point to the i-node. Only when the link count goes to 0 can the file be deleted. The other type of link is called a symbolic link. With a symbolic link, the actual contents of the file (the data blocks) contains the name of the file that the symbolic link points to. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 40 of 60

The file type in the i-node would be S_IFLNK so that the system knows that this is a symbolic link. The i-node contains all the information about the file: the file type, the file’s access permission bits, the size of the file, pointers to the data blocks for the file, and so on. When renaming a file without changing filesystems, the actual contents of the file need not be moved – all that needs to be done is to have a new directory entry point to the existing i-node and have the old directory entry removed. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 41 of 60

link, unlink, remove, and rename Functions Any file can have multiple directory entries pointing to its i- node. link Function Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 #include int link (const char *existingpath, const char *newpath); Returns: 0 if OK, -1 on error Page 42 of 60

We create a link to an existing file with the link function. This function creates a new directory entry, newpath, that references the existing file existingpath. If the newpath already exists an error is returned. Only a superuser process can create a new link that points to a directory. unlink Function To remove an existing directory entry we call the unlink function. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 43 of 60

This function removes the directory entry and decrements the link count of the file referenced by pathname. Only when the link count reaches 0 can the contents of the file be deleted. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 #include int unlink (const char *pathname); Returns: 0 if OK, -1 on error Page 44 of 60

Question? A process creates a file using either open or creat and then immediately calls unlink. Is the file deleted? Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 45 of 60

The file is not deleted, however, because it is still open. Only when the process either closes the file or terminates, is the file deleted. remove Function We can also unlink a file or directory with the remove function. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 45 of 60

For a file, remove is identical to unlink. For a directory, remove is identical to rmdir. rename Function A file or directory is renamed with the rename function. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 #include int remove (const char *pathname); Returns: 0 if OK, -1 on error Page 46 of 60

Symbolic Links A symbolic link is an indirect pointer to a file, unlike the hard links from the previous section, which pointed directly to the i- node of the file. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 #include int rename (const char *oldname, const char *newname); Returns: 0 if OK, -1 on error Page 47 of 60

Symbolic links were introduced to get around the limitations of hard links: (a) hard links normally require that the link and the file reside in the same filesystem. (b) Only the superuser can create a hard link to a directory. There are no filesystem limitations on a symbolic link and what it points to. Anyone can create a symbolic link to a directory. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 48 of 60

Symbolic links are typically used to move a file or an entire directory hierarchy to some other location on a system. Example The following commands create a directory foo that contains the file a and a symbolic link that points to foo: $ mkdir foo make a new directory $ touch foo/a create a 0-length file $ ln -s../foo foo/testdir create a symbolic link $ ls -l foo Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 49 of 60

This same example is shown in the following figure, by drawing a directory as a circle and a file as a square, where the symbolic link testdir that creates a loop is shown. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 50 of 60

symlink Function A symbolic link is created with the symlink function. A new directory entry, sympath, is created that points to actualpath. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 #include int symlink (const char *actualpath, const char *sympath); Returns: 0 if OK, -1 on error Page 51 of 60

Also, actualpath and sympath need not reside in the same filesystem. mkdir and rmdir Functions Directories are created with the mkdir function and deleted with the rmdir function. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 #include int mkdir (const char *pathname, mode_t mode); Returns: 0 if OK, -1 on error Page 52 of 60

This function creates a new, empty directory. The entries for dot and dot-dot are automatically created. The specified file access permissions, mode, are modified by the file mode creation mask of the process. For a directory, we normally want at least one of the execute bits enabled, to allow access to filenames within the directory. An empty directory is deleted with the rmdir function. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 53 of 60

If the link count of the directory becomes 0 with this call, and no other process has the directory open, then the space occupied by the directory is freed. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 #include int rmdir (const char *pathname); Returns: 0 if OK, -1 on error Page 54 of 60

Reading Directories Directories can be read by anyone who has access permission to read the directory. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 #include DIR *opendir (const char *pathname); Returns: pointer if OK, NULL on error Page 55 of 60

The dirent structure is defined in the file. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 struct dirent *readdir (DIR *dp); Returns: pointer if OK, NULL at end of directory or error void rewinddir (DIR *dp); int closedir (DIR *dp); Returns: 0 if OK, -1 on error Page 56 of 60

One of the implementation of the dirent structure is as follows: struct dirent { ino_t d_ino; /* i-node number */ char d_name[NAME_MAX + 1]; /* null-terminated filename */ } Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 57 of 60

chdir, fchdir, and getcwd Functions Every process has a current working directory. This directory is where the search for all relative pathnames starts (all pathnames that do not begin with a slash). We can change the current working directory of the calling process by calling the chdir or fchdir functions. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 58 of 60

We can specify the new current working directory as either a pathname or through an open file descriptor. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 #include int chdir (const char *pathname); int fchdir (int filedes); Both return: 0 if OK, -1 on error Page 59 of 60

We must pass this function the address of a buffer, buf, and its size. The buffer must be large enough to accommodate the absolute pathname plus a terminating null byte, or an error is returned. Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003 #include char *getcwd (char *buf, size_t size); Returns: buf if OK, NULL on error Page 60 of 60