Title: Lecture 2 Stat system call
1Lecture 2Stat system call
CSCE 510 Systems Programming
- Topics
- Course Pragmatics
- What is systems software?
- Unix History
- C/C history
- Primitive Unix I/O
- Unix I/O system Calls
- Open File Structure
- String library
August 23, 2005
2Overview
- Last Time
- Standard I/O struct _iobuf
- I/O system calls
- Readings
- Unix I/O system Calls
- Open File Structure
- String library
- Next Time
- The Stat Structure
- Handouts
- C Reference Card
3Hole.c
- include ltsys/types.hgt
- include ltsys/stat.hgt
- include ltfcntl.hgt
- include "ourhdr.h"
- char buf1 "abcdefghij"
- char buf2 "ABCDEFGHIJ"
- int
- main(void)
-
- int fd
- if ( (fd creat("file.hole", FILE_MODE)) lt 0)
- err_sys("creat error")
- if (write(fd, buf1, 10) ! 10)
- err_sys("buf1 write error")
- / offset now 10 /
if (lseek(fd, 400000, SEEK_SET)
-1) err_sys("lseek error") / offset now
40000 / if (write(fd, buf2, 10) !
10) err_sys("buf2 write error") exit(0) vo
id err_sys(const char msg, ...)
fprintf(stderr, "ERROR s", msg) exit(1)
4The string library
- /usr/include/string.h
- strings in C
- strlen
- strcmp
- strncmp
- strcat
- strncat
- strcpy
- strchr
- strstr
- strpbrk
- strspn
- strcspan
- strtok
- strerror
5The string library
- Strings in C
- char p
- /usr/include/string.h
- strlen
- strcmp
- strncmp
- strcat
- strncat
- strcpy
- strchr
- strstr
- strpbrk
- strspn
- strcspan
- strtok
- strerror
6getchar macro
- define getc(p) \
- (--(p)-gt_cnt lt 0 ? __filbuf(p)
(int)(p)-gt_ptr)
7File Sharing
8Dup and dup2
- DUP(2) Linux Programmer's
Manual - NAME
- dup, dup2 - duplicate a file descriptor
- SYNOPSIS
- include ltunistd.hgt
- int dup(int oldfd)
- int dup2(int oldfd, int newfd)
9Lseek
- include lt sys/types gt
- include lt unistd.h gt
- off_t lseek(int filedes, off_t offset, int
start_flag) - SEEK_SET(0)
- SEEK_CUR(1)
- SEEK_END(2)
10Stat Structure
- include lt sys/stat.h gt
- struct stat
- dev_t st_dev / file system ID /
- ino_t st_ino / the inode number /
- mode_t st_mode / permissions and type
information/ - nlink_t st_nlink / number of links /
- uid_t st_uid / File owner user ID /
- gid_t st_gid / File group ID /
- dev_t st_rdev / Major and minor device
numbers / - off_t st_size / size in bytes /
- time_t st_atime / last access time /
- time_t st_mtime / last modification time /
- time_t st_ctime / last status change time /
- long st_blksize / best I/O block size /
- long st_blocks / number of 512-byte blocks'
/ -
11st_mode
- st_mode - information on file type and
permissions
12File types
-
- regular file
- directory
- symbolic link
- character device file
- block device file
- FIFO
- socket
13- mode 32 bits -
- define _S_IFMT 0170000 / Octal number type of
file / - define _S_IFREG 0100000 / regular sync with
S_IFREG /
14File type Testing macros
- File type Testing macros from stdio.h
- define S_ISDIR( mode ) (((mode) _S_IFMT)
_S_IFDIR)
15File Permissions
- File Permissions
- User, group, others
- 3 bits for each read, write, execute (rwx)
- ls -l
- chmod command
16Mode Testing definitions
- Mode Testing definitions / file modes /
- define S_IRWXU 00700 / read, write, execute
owner / - define S_IRUSR 00400 / read permission owner
/ - define S_IWUSR 00200 / write permission owner
/ - define S_IXUSR 00100 / execute permission
owner ... - define S_ISUID 0004000 / set user id on
execution / - define S_ISGID 0002000 / set group id on
execution /
17The stat, fstat lstat system calls
- int stat(const path, struct stat buf) - get
statistics on this file - int fstat(int fd, struct stat buf) - a version
of stat for open files - int lstat(const path, struct stat buf) - a
version of stat that does not follow symbolic
links - Command line arguments website/C-Intro/printargs.c
18filetype.c
- ...
- struct stat buf
- char ptr
- for (i 1 i lt argc i)
- printf("s ", argvi)
- if (lstat(argvi, buf) lt 0)
- err_ret("lstat error")
- continue
-
- if (S_ISREG(buf.st_mode)) ptr "regular"
-
19Inode information
- All of the stat structure info
- Disk block addresses
- Original Unix File system Disk Block Pointers
- 10 direct pointers
- single indirect pointer
- double indirect pointer
- triple indirect pointer
20HW 1 Due Thursday 500PM
- Create a file with a hole in it and check the
file size with "ls -s". - Get a copy of hole.c, compile and run it.
- cp /class/csci510/Examples/hole.c . // copy files
- cp /class/csci510/Examples/ourhdr.h .
- cc o genHole hole.c // Compile the program
- ./genHole // Create the file file.hole
- Use man to see what "ls -s" tells you.
- Use "ls -s file.hole.
- Use "cp file.hole file2" to create another copy
and then use "ls -s file2". - Login to a solaris machine (e.g. coosawhatchee)
and repeat compile, create and copy to create
file3 - Use "wc" (wordcount) on these files.