Lecture 7 Log into Linux Copy files from csserver: /home/hwang/cs375/lecture07/

advertisement
Lecture 7



Log into Linux
Copy files from csserver:
/home/hwang/cs375/lecture07/
Questions?
Tuesday, February 4
CS 375 UNIX System Programming - Lecture 7
1
Outline

getstat exercise

Directories

Formatted I/O

Error handling

The /proc filesystem

Handling program arguments
Tuesday, February 4
CS 375 UNIX System Programming - Lecture 7
2
getstat Exercises

Modify getstat.cpp to display all the
information shown by the stat command.
$ stat /etc/passwd
File: `/etc/passwd'
Size: 32889 Blocks: 72 IO Block: 4096 regular file
Device: 801h/2049d Inode: 1712752 Links: 1
Access: (0644/­rw­r­­r­­) Uid: (0/root) Gid: (0/root)
Access: 2010­09­08 08:17:16.000000000 ­0500
Modify: 2010­08­27 08:16:42.000000000 ­0500
Change: 2010­08­27 08:16:42.000000000 ­0500
Birth: ­
Tuesday, February 4
CS 375 UNIX System Programming - Lecture 7
3
Processing Times


Times in the stat struct are of type time_t, which
is the elapsed time in seconds since the Epoch
began (January 1, 1970, 00:00 UTC).
Information about (some) time routines can be
found on the ctime man page. In particular,
there are routines for converting between
time_t values and a tm struct of "broken down
time" values. E.g.,
struct tm *bt = localtime(sb­>atime);
Tuesday, February 4
CS 375 UNIX System Programming - Lecture 7
4
Directory Scanning

Use opendir to open a directory. Calls to
readdir will return successive directory entries.
closedir closes the directory.
DIR *dp;
if ((dp = opendir(".")) == NULL)
error_exit();
struct dirent *entry;
while((entry = readdir(dp)) != NULL) {
cout << entry­>d_name << endl;
}
if(closedir(dp) == ­1)
error_exit();
Tuesday, February 4
CS 375 UNIX System Programming - Lecture 7
5
Directory Scanning




The telldir and seekdir routines allow you to
reset a directory scan to a prior position.
See the program on pages 124-125 of BLP for
an example of using the directory routines to
display a directory tree.
Note: Use chdir() and getcwd() to change and
get current working directories.
Refer to dirscan.cpp for another example.
Tuesday, February 4
CS 375 UNIX System Programming - Lecture 7
6
Formatted I/O


BLP discusses the C stdio routines: fopen,
fclose, fread, and fwrite. In addition, fscanf
and fprintf provide formatted output. These
routines are part of ANSI standard C.
The equivalent in C++ is the iostream types:
e.g., fstream, ifstream, ofstream, along with
operators << and >>, and manipulators for
formatting.
Tuesday, February 4
CS 375 UNIX System Programming - Lecture 7
7
Formatted I/O


Note that the read and write routines read and
write binary data (writenum1.cpp). They DO
NOT do format conversions (like cin /cout or
scanf / printf).
If you need formatted output, using a C++
stringstream or C sprintf to format binary data
is recommend (see writenum2.cpp for a
stringstream example).
Tuesday, February 4
CS 375 UNIX System Programming - Lecture 7
8
Error Handling




Most system routines return -1 on error. The
global errno variable (defined in errno.h) also is
set when an error occurs and indicates the type
of error.
CHECK ALL ROUTINES FOR ERRORS!!!!
The perror routine (defined in stdio.h) decodes
errno and displays a meaningful message on
stderr.
The strerror routine (defined in string.h) returns
the perror message as a char string. It can be
used to display custom error messages.
Tuesday, February 4
CS 375 UNIX System Programming - Lecture 7
9
Error Handling
int main(int argc, char *argv[])
{
if(argc != 2) {
cerr << "usage: showerrs filename" << endl; exit(1); }
int fd;
if((fd = open(argv[1], O_RDWR)) == ­1)
{ perror("showerrs"); exit(2); }
if(close(fd) == ­1)
{ perror("showerrs"); exit(3); }
return 0;
}
Tuesday, February 4
CS 375 UNIX System Programming - Lecture 7
10
Error Handling

Here is example output from running the
program on the previous slide:
$ ./showerrs /etc/passwd
showerrs: Permission denied
$ ./showerrs /etc
showerrs: Is a directory
$ ./showerrs xxxxxxxx
showerrs: No such file or directory
Tuesday, February 4
CS 375 UNIX System Programming - Lecture 7
11
The /proc Filesystem


Linux uses several pseudo filesystems. These
systems do not use up any space on the disk.
They typically provide a means for the kernel to
present information to applications or users.
We will discuss briefly only the /proc filesystem
which provides an interface to kernel data
structures. See man proc for more
information.
Tuesday, February 4
CS 375 UNIX System Programming - Lecture 7
12
The /proc Filesystem



There is a /proc/[number] directory for each
running process where number is the process
ID. There are cmdline, cwd, exe, environ,
etc. entries.
/proc/cpuinfo provides information about the
CPU and architecture.
/proc/cmdline contains the arguments that are
passed to the kernel at boot time.
Tuesday, February 4
CS 375 UNIX System Programming - Lecture 7
13
The /proc Filesystem



/proc/net contains several entries that provide
information about various network layers.
/proc/filesystems provides a list of all
filesystems compiled into the kernel.
/proc/ide and /proc/scsi provide information
about corresponding devices.
Tuesday, February 4
CS 375 UNIX System Programming - Lecture 7
14
The /proc Filesystem


/proc/sys contains several entries
corresponding to various kernel variables.
These can not only be read, but also set to tune
the kernel.
/proc/sys/fs/file-max defines the system wide
limit on the number of open files for all
processes. This can be changed easily (by
root):
$ echo 100000 > /proc/sys/fs/file­max
Tuesday, February 4
CS 375 UNIX System Programming - Lecture 7
15
The getopt( ) Routine



The getopt( ) routine parses command line
arguments. Successive calls to getopt( ) return
option characters and set global variables.
It can detect single character options of the
form "-abc" or "-a -b -c". It can parse lines with
options that require arguments "-fdata.txt" or
"-f data.txt".
Note: There is a getopt program (man 1
getopt) in addition to the getopt( ) routine
(man 3 getopt). The former can be used by
shell scripts.
Tuesday, February 4
CS 375 UNIX System Programming - Lecture 7
16
The getopt( ) Routine


getopt( ) permutes argv[ ] so that all nonoptions are at the end.
The getopt prototype is:
#include <unistd.h>
int getopt(int argc, char *const argv[], const char *optstring);

See getopt.cpp for an example.
Tuesday, February 4
CS 375 UNIX System Programming - Lecture 7
17
Download