Unix Stream Unix Advanced V2.0

download Unix Stream Unix Advanced V2.0

of 98

Transcript of Unix Stream Unix Advanced V2.0

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    1/98

    17 September 2010 TCS Internal

    Advanced Unix

    TCS Internal

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    2/98

    17 September 2010TCS Internal 2

    Course Outline

    After going through this part of the course, you would be familiarized with

    Communication tools

    Shell scripting

    System calls

    Inter-process communication

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    3/98

    17 September 2010TCS Internal 3

    UNIX Communication Tools

    write

    mesg

    talk

    mail

    wall

    finger

    ftp

    telnet

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    4/98

    17 September 2010TCS Internal 4

    The write Command

    It lets you have a two-way communication with any person who is currently logged in.One user writes his message and then waits for the reply from the other. Theconversation continues until one or both the users decide to terminate it:$ write ntpuser1Have you completed exercise 1?^d # Ctrl-d is the end of file from terminal

    Here ntpuser1 is the login-id of the recipient as argument and the text of the messagecomes from the standard input.

    It is equally possible to store this message in a file mymsgand apply input redirection:$ write ntpuser1 < mymsg

    The message appears on ntpuser1s terminal provided he is logged in, else sender getsan error message response. If the recipient is logged in more than one terminal, themessage is sent to the lowest numbered terminal unless you have provided the terminalnumbers as more arguments:

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    5/98

    17 September 2010TCS Internal 5

    The write mesg Commands

    Once into the write program, you can execute any UNIX command by preceding it with theoperator !. The command output is not transmitted.

    Since write ties you up in this interactive (and slow) conversation, UNIX offers the mailcommand to permit deferred communication so that your own work does not get affected.

    Normally, you are not permitted to receive messages from other users for securityconsiderations. Such permissions are granted by the mesgcommand to be discussed next.

    The mesgcommand can be used to prevent other people from writing to your terminal asfollows:

    $ mesg n # default mode is y

    You can know the status of your terminal by typing:

    $ mesg # y or n will be the response

    The message of today is stored in a file /etc/motd.

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    6/98

    17 September 2010TCS Internal 6

    Administrator can broadcast messages to all users (users can also do so on somesystems):

    wall Machine will be shut down in 10 minutes. So log out, please!

    ^d # Ctrl-d is the end of file from terminal

    wall

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    7/98

    17 September 2010TCS Internal 7

    The talkCommand

    The write tool was nice for communication one at a time. Simultaneous typing isenabled by the talktool. Entering mesg n similarly just turns talk off.

    talkis an interactive communication tool that enables you to have an actualconversation in real-time mode with a user who has to be logged in.

    You get an appropriate message if someone does not want to talk to you, else yourterminal shows a divided screen. You can type your message on the top half of thescreen and it appears verbatim on the other terminal at the bottom half of the screen.

    Both users can type messages simultaneously. The connection can be closed byentering .

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    8/98

    17 September 2010TCS Internal 8

    The mailCommand

    Electronic mail (E-mail) has taken the world by storm. It is stored in a file called systemmail in a mailbox that has the same name as that of the user. For example, the userashoks mailbox under Linux is /var/spool/mail/ashok.

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    9/98

    17 September 2010TCS Internal 9

    The mailCommands

    Pine, a trademark of the University of Washington, stands forProgram forInternetNews and E-mail. Pine is also capable of sending documents and graphics.

    Like write, mailuses standard input but is capable of input redirection from a file.

    The mail program when used for the purpose of viewing the mail, features a prompt(&). There are a whole lot of internal commands that you can enter at this prompt tosave, delete, ignore, reply the mail that you have received. On-line help can be soughtby typing ? at the prompt.

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    10/98

    17 September 2010TCS Internal 10

    The mail -Internal Commands

    All the available options in the Mail can be listed by typing a ? after the &. The listedoptions are:

    t - type messages

    n - go to and type next messagee - edit messages

    f - give headlines of messages

    d - delete messages

    s file - append messages to file

    u - undelete messages

    R - reply to message senders

    r - reply to message senders and all recipients

    pre -make messages go back to /usr/spool/mail

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    11/98

    17 September 2010TCS Internal 11

    The mail -Internal Commands

    m - mail to specific usersq - quit, saving unresolved messages in mboxx - quit, do not remove system mailboxh - print out active message headers

    ! - shell escapecd [directory] - chdir to directory or home if none given

    A consists of integers, ranges of same, or user names separated byspaces. If omitted, Mail uses the last message typed.

    A consists of user names or aliases separated by spaces.

    File .mailrc in your home directory is invoked when the mail command is executed.Aliases are defined in this file.

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    12/98

    17 September 2010TCS Internal 12

    The finger Command

    This product from Berkeley, is more versatile compared to who, and produces anextremely readable output. finger, when used without arguments - $ fingersimply produces a list of all users who are currently logged in (default output of 6 fieldswith headings).

    fingercan also be followed by a list of login names. For each name, it will display a

    detailed list, whether logged in or not.$ fingerntpuser1 Login: ntpuser1 Name: (null)Directory: /home/ntpuser1 Shell: /bin/bashNever logged in.No Plan.

    fingercan be used with either the first or last name provided the entry has beenproperly made in /etc/passwd file.

    It is often necessary to leave behind your schedule and other important info for other tosee. Since it is not possible to send mail to all users, fingeralso shows the contents of

    two files .plan and .project (only the first time), both in the home directory.

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    13/98

    17 September 2010TCS Internal 13

    The ftp Command

    File Transfer Protocol (FTP) is a method of transferring files from one computer toanother. FTP provides the capability of transferring files to and from a remote networksite as well as means for sharing public files. The ftp service is the user interface to theFTP.

    Anonymous FTP enables users to access remote sites without having an authorizeduser-id and password. Generally the login-id is anonymous and password is guest.Most current systems require your e-mail address as the password.

    The client host with which ftp is to communicate may be specified on the command line:

    $ ftp [option] hostname ftp immediately attempts to establish a connection to an FTP server on that host. Ahostname can be either a domain address or an ip address. After this the remote FTPserver awaits your commands.

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    14/98

    17 September 2010TCS Internal 14

    The ftp - Internal Commands

    ? orhelp [command] - displays a list of ftp commands; if no argument is given,displays a list of the known commands.

    bell - Sounds a bell after each file transfer.

    ascii - Sets the file transfer type to network ASCII (default).

    binaryorbin - Sets the file transfer type to binary mode.

    quitorbye - Terminates the FTP session with the remote computer andthen exits.

    cd - Changes the working directory on the remote machine.

    cdup - Goes to the parent of the current remote machinesworking directory.

    dirorls [remote-directory] - List the contents of the remote-directory.

    Note that the ls command in ftp behaves like the ls alin Linux.

    mdir[remote-files] - Like dir, except multiple remote files may be specified.

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    15/98

    17 September 2010TCS Internal 15

    The ftp - Internal Commands ...

    pwd - Displays cur-work-directory name on remote m/c.

    delete remote-file - Deletes remote file on remote m/c.

    mdelete [remote-files] - Deletes the remote files on remote machine.

    rmdirdirectory-name - Deletes a directory on the remote machine.

    lcd[directory] - Changes working directory on local machine.If no directory is specified, users home directory is used.

    open host[port] - Establishes a connection to specified host FTP server on theoptional port number.

    close ordisconnect - Terminates the FTP session with the remote server.

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    16/98

    17 September 2010TCS Internal 16

    The ftp - Internal Commands

    get remote-file [local-file]Retrieves the remote-file and stores it on local m/c with the same or different name.

    mgetremote-filesExpands the remote files on the remote m/c and does a getfiles based on regular expressions. For example, f*.tarwill get all the files startingwith f.

    put local-file [remote-file] - Stores a local file on the remote m/c.

    mputlocal-files - Stores the local files (specified as a regular expression) on the remote m/c with the same names.

    However, it is a good practice to set the directories on the local machine and the FTP server before initiating the file transfer.

    The I option turns off interactive prompting during multiple file transfers.

    Usually is used to abort a sending file transfer. Receiving transfers are halted by sending an ftp ABOR command to the remoteserver.

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    17/98

    17 September 2010TCS Internal 17

    The telnetCommand

    It is used to communicate with another host using the TELNET protocol. Iftelnetis invoked without the host argument, itenters command mode. Normally you would use-

    telnethostname [port]

    where hostname is the host you want to connect to and port indicates a port number (an applications address), else the

    default telnet port is used.

    telnetcan log you in either one of two modes: char-by-char or line-by-line. In char-by-char mode, most text typed isimmediately sent to the remote host for processing. In line-by-line mode, all text is echoed locally, and only completedlines are sent to the remote host.

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    18/98

    17 September 2010TCS Internal 18

    The telnetCommand ...

    The following commands are available under the command mode:

    open host[ [-l] user] [-port]

    Opens a connection to the named host; uses the default port if port number not specified. The [-l] option may be

    used to specify the user name.close Closes a telnet session and returns to cmd-mode

    quit Closes any open telnet session and exits telnet. An end of file (in cmd-mode) will also close a session and exit.

    ? Displays a list of telnet commands.

    ? [command]

    Displays a short summary of the specified command.

    set Specifies the character for switching into telnet command mode.

    status displays the current status of telnet, including the name of the remote computer the user is connected to.

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    19/98

    17 September 2010TCS Internal 19

    Shell Programming and Shell Scripts

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    20/98

    17 September 2010TCS Internal 20

    Shell Programming and Shell Scripts

    UNIX shell can be considered as a master utility program that enables a user to gain access to all other utilities andresources of the computer.

    Frequently used UNIX commands can be stored in a file. Shell can read the file and execute the commands in it. Such afile is called a scriptfile.

    The script file can be created by:

    $ vi filename or

    $ cat > filename

    Executing a Shell Script:$ sh filename or $ filename

    But, remember to modify the FAP for the file containing the shell script for execute permission: $ chmod u+x filename

    Pressing u at any point, right up to before pressing , lets you clear everything on the command line.

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    21/98

    17 September 2010TCS Internal 21

    Shell Programming - Variables

    User-defined shell variables can be created at any point of time by a simple assignment of a value:$today=20 ; var=beta$name1=Bharat Bhushanor Bharat Bhushan

    NOTE: The assignment operator = must not have surrounding spaces. Variables can be referenced as:

    $ name2=$name1 or name2=${name1}

    But see what happens when braces are not used:X=$todayth # would not produce desired resultX=${today}th # would produce 20thSo, braces are optional if no concatenation is involved.

    The echo command is used for displaying messages on screen (possibly terminated by \c, \n, \007, etc).

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    22/98

    17 September 2010TCS Internal 22

    The Evaluation of Shell Variables

    $var value ofvar; nothing ifvarundefined

    ${var} same; useful if alphanumerics follow

    variable name

    ${var-str} value ofvarif defined; else

    str; $varunchanged.

    ${var=str} value ofvarif defined; else

    str; if undefined, $varset to str.${var?str} if defined, $var; else, print

    strand exit shell; ifstrempty, print:

    var:parameter not set

    ${var+str} strif$vardefined, else null.

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    23/98

    17 September 2010TCS Internal 23

    The Global and Local Variables

    When a variable is referenced, it is known only to the shell which created it. A new shell created by typing sh, is born unaware of the parent shellsvariables.

    The same variable name (as in the parent shell) can be given a different value in the new shell. Such a variable is called a localvariable that isknown only to the child shell.

    In many instances, it may be necessary for the child shell to know about the parent shells variables. They are known as globalvariables.

    $ city=Bombay ; echo $city # Create variable; Response: Bombay

    $ sh ; echo $city # create a child shell; No response

    $ city=Delhi; echo $city # Create new variable; Response: Delhi

    # Return to parent

    echo $city # Parent continues to have value Bombay

    The global variables must be exported so that the child shell is aware of them as follows:

    $exportcity # only the copy passed to the child

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    24/98

    17 September 2010TCS Internal 24

    Backward quotes and set & shiftcommands

    Recall the usage of pipes in joining commands. Other way is through command substitution:$ echo The date is `date` $ count=`cat fileA | wc -l Single backward quotes enclose command(s) to be executed.

    The setcommand sets the values of $1 to $n to the words given explicitly/implicitly as its arguments:$ date Fri Sep 24 11:27:42 IST 2004$ set `date` $ echo $1 $2 $3 Fri Sep 24

    The shiftcommand transfers the contents of a positional parameter to a lower numbered one as shown below:$ shift 2 # Output: 24 11:27:42 ISTNote: shift and shift 1 have the same effect.

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    25/98

    17 September 2010TCS Internal 25

    Arithmetic Evaluation with expr

    Shell does not support numerical variables. All variables are treated as character strings. Simulation of numericvariables is possible by exprcommand which is used to evaluate arithmetic expressions without decimals.Computing square of n:n=3echo Square of$n is `expr ${n} \* ${n}`n=`expr ${n} + 1` ; echo $n

    NOTE The arithmetic operators (+, -, *, /)must be surrounded by at least one space. The * should be preceded by a

    backslash (\) else shell will interpret it as a wildcard character.

    Unix understands only the whole numbers, so, 5.2 and 5.6 both are interpreted as 5.

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    26/98

    17 September 2010TCS Internal 26

    String Handling with expr

    The expris quite handy for finding the length of a string and extracting a sub-string:$str=abcdefghijk ; n=`expr "$str" : .* ; echo $n11 # expr gave how many times any character (.*) occurs.

    # This feature is very useful in validating data entry.

    Extracting a sub-string:$ expr $str : \(..\)gh

    Note that there are 6 dots preceding the sequence \(..\). This advanced regular expression signifies that the first sixcharacters of the string are to be ignored and extraction should start from the 7th character. Two dots inside \(..\)suggests that this extraction is limited to two characters only (backslashes override the usual interpretation of ()).

    Location of the first occurrence of a character inside string:$ expr "$str" : '[ d]*d4

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    27/98

    17 September 2010TCS Internal 27

    Shells Built-in Variables and evalcommand

    Shells Built-in Variables$# The number of arguments$* all arguments to shell$@ similar to $*$- options supplied to the shell$? Return value of the last command executed$$ process-id of the shell$! process-id of the last command started with &

    The evalcommand is used to execute commands generated by multiple levels of indirection:$ a=c; b=m; c=d; cmd=date$eval$`echo $a$b$c`Tue Nov 23 16:45:43 EDT 2000

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    28/98

    17 September 2010TCS Internal 28

    Shell Programming input/output

    The readcommand can be used to get run-time input from the user (standard input). It reads :$ read var1 var2

    A file can also be read sequentially through read:$ read var1 var2 var3 < myfile

    One line is read at a time; the next word in the line is assigned to each successive variable specified as an argumentofread; extra words are assigned to the final argument ofread.

    Screen display from echo can be redirected (>) or appended (>>) to a file. The double-quoted string formed byconcatenating the fields together separated by the desired delimiter forms echos argument for standard or file

    output:$ echo $name | $code | $price | $qty >> Salesfile

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    29/98

    17 September 2010TCS Internal 29

    Coding Branching/Looping Conditions

    The testcommand can be used to write conditions that can be used in if, case, and while commands. String comparison operators: = != < >= Example: To check if variables name and myname have the same value, and variable uids value is 10:

    test ${name} = ${myname} -a ${uid} = 10Same as [ ${name} = ${myname} -a ${uid} = 10]

    Arithmetic comparison operators:

    -eq -ne -gt -ge -lt -leNOTE:1. All comparison operators must be surrounded by at least one space.2. Multiple conditions in one test command can be formed usinga / -o for Anding / Oring two truth values.

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    30/98

    17 September 2010TCS Internal 30

    Options for Testing File-types and Strings

    File test options:Condition Return value whentest-ffname returns true if file exists and is ordinarytest-dfname returns true if file exists and is a directorytest-rfname returns true if file exists and is readabletest-wfname returns true if file exists and is writeabletest-xfname returns true if file exists and is executabletest-s fname returns true if file exists and is not empty

    String test options:test str Returns true if str is not nulltestn str Returns true if length of str is greater than zerotestzstr Returns true if length of str is equal to zero

    The &&, ||, :, ; meta-characters of shellC1 && C2 Cmd C1 Executed; if success, then C2 Executed.C1 || C2 Cmd C1 Executed; if failure, then C2 Executed.: - Do-nothing command - used mostly in shell scripts.; - Command separator used to type multiple cmds in a line.

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    31/98

    17 September 2010TCS Internal 31

    Decision-making Constructs

    1. ifthen [else ] # Optional else partfi

    2. ifthen elif # Many elif-then allowedthen

    else fi

    Example:

    echo Want to quit (type Y or y):

    read ans

    if test $ans = y o $ans = Y

    then exit

    fi

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    32/98

    17 September 2010TCS Internal 32

    Decision-making Constructs ...

    3. case-esaccommand in shell scripts is a multiple-choice construct:case ${num} in

    dozen) echo 12;;score) echo 20;;*) echo num is neither a dozen nor a score

    esacNOTE: the last command in any choice must be delimited by a pair of semi-colons to delimit it from the commands

    of the next choice.

    4. exitcommand is used to exit from the shell script and return to the parent shell. It also takes an argument whosevalue is 0 when not specified. It is taken as the return value which is assigned to the parameter $?.

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    33/98

    17 September 2010TCS Internal 33

    Constructs for Supporting Iteration

    1. while do # executed as long as the condition is true

    done

    2. until #complement ofwhiledo # executed as long as the condition is false

    done

    n=0while [ $n -lt 10]do n=`expr $n + 1`

    echo $n `expr $n \* $n`done

    Example

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    34/98

    17 September 2010TCS Internal 34

    Constructs for Supporting Iteration

    3(a) forn in 1 7 8 50do

    echo Value of n is $ndone

    3(b) forfilename in `ls tempdir`do

    echo Displaying contents of ${filename}cat ${filename}

    done

    3(c) foritemlist; do commands; done# executes once for each item in itemlist that contains all positional# parameters passed in to shell program on the command line.

    For each iteration in both loops, variable n and filename take on one value from their lists.

    Command breakcauses the termination of a loop and continue resumes execution at its top.

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    35/98

    17 September 2010TCS Internal 35

    Shell Scripts with Command-line Parameters

    Parameters are essentially used to create generalized shell scripts. The command name (first word on command line)is put into a variable called $0, the first argument of command (second word) is put into $1, etc.

    UNIX shell creates a maximum of ten variables. The variables $1 through $9 are also called thepositional parametersof the command line.

    A Shell Script test with Parameters:echo Program name is $0echo First argument is $1echo Number of arguments = $#echo The arguments are $*

    A Sample Run:$ sh test arg1 arg2 arg3 Program name is testFirst argument is arg1Number of arguments = 3The arguments are arg1 arg2 arg3

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    36/98

    17 September 2010TCS Internal 36

    Process

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    37/98

    17 September 2010TCS Internal 37

    Background Processing

    UNIX provides a method of running time-consuming commands in the background while the user continues working

    in the foreground by typing ampersand (&) at the end of line:$ wc myfile &

    $ vi newfile

    Here, user gives the wc command and then immediately begins editing newfile without waiting for the completion ofthe previous wc process.

    Well, the user must be careful that the background and foreground processes do not interfere with each other. Anyscreen output from the background process would disrupt the screen in the middle of users work with the foreground

    process. This should be avoided by output redirection:$ wc myfile 1 > outfile 2 > errfile &

    $ vi newfile

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    38/98

    17 September 2010TCS Internal 38

    Background Processing

    Sometimes it is needed to check the current status of a background process. Thejobs command gives thejob-numberof

    each active process. Theps command generates a one-line entry for each of the active processes showing its process-id,users terminal number, time for which the process has been active, and the name of command. If the backgroundprocess is not listed in the output, it may be completed already. The killcommand can be used to stop backgroundprocess if it is not functioning as desired:$ kill process-id or, $ killjob-number

    If a command issued in foreground is taking too long to finish, and you want to execute it in background so that you coulduse the system again, do the following:

    $ z bgPressing z suspends a running process and bgrestarts it in background. c kills a job running in foreground.

    Similarly, z fgpidorz fg%jobnumberforces a suspended process to continue to execute in the foreground.

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    39/98

    17 September 2010TCS Internal 39

    UNIX SYSTEM CALLS

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    40/98

    17 September 2010TCS Internal 40

    UNIX SYSTEM CALLS

    system call is the mechanism used by an applicationprogram to request service from the operating system, or

    more specifically, the operating system kernel.

    Different privilege levels are provided so that operating

    systems can restrict the operations that programs runningunder them can perform, for reasons of security andstability.

    Such operations include accessing hardware devices,enabling and disabling interrupts, and accessing memorymanagement units. The operating system kernel would runin supervisor mode, and user applications in user mode.

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    41/98

    17 September 2010TCS Internal 41

    i~==~==~

    _=~=`=J =

    a=======~====rkfu=p

    i~=====~====~===~J=

    p=~=~=~===rkfu=h

    p=`~=~==~=rkfu==

    i~=======~=~=~===

    ~===

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    42/98

    17 September 2010TCS Internal 42

    File Related Operations

    1. opening files

    2. reading files

    3. writing in files

    4. creating files

    5. changing the permission of files

    6. changing the owner and group of files

    7. seeking to file-location

    8. closing files

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    43/98

    17 September 2010TCS Internal 43

    1. opening files open()

    2. reading files read()

    3. writing in files

    write()4. creating files creat()

    5. changing the permission of files chmod()

    6. changing the owner and group of files chown()

    7. seeking to file-location lseek()

    8. closing files close()

    Related System calls

    open()

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    44/98

    17 September 2010TCS Internal 44

    open()

    The open system call can be used to open an

    existing file or to create a new file if it does not exist already.# include

    # include

    # include

    int open(const char *path, int flags);int open(const char *path, int flags, int modes);

    The first form is normally used to open an existing file,

    The second form to open a file and to create a fileif it does not exist already.

    Both forms returns an integer called the file descriptor.

    The file descriptor will be used for reading from and writingto the file.

    If the file cannot be opened or created, it returns -1.

    Continued... open()

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    45/98

    17 September 2010TCS Internal 45

    The first parameterpath in both forms specifies the file name to beopened or created.

    The second parameter (flags) specifies how the file may be used.

    The following list some commonly used flag values.

    Flag Description

    O_RDONLY open for reading onlyO_WRONLY open for writing only

    O_RDWR open for reading and writing

    O_APPEND append on each write

    O_CREAT create file if it does not exist

    p ()

    Continued... open()

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    46/98

    17 September 2010TCS Internal 46

    Constant NameOctalValue

    Description

    S_IRWXU 0000700 rwx mask for owner

    S_IRUSR 0000400 r for owner

    S_IWUSR 0000200 w for owner

    S_IXUSR 0000100 x for owner

    S_IRWXO 0000007 rwx mask for other S_IROTH 0000004 r for other

    S_IWOTH 0000002 w for other

    S_IXOTH 0000001 x for other

    R: read, W: write, and X:executable

    Commonly used modes (or access permissions) include

    p ()

    Continued... open()

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    47/98

    17 September 2010TCS Internal 47

    For example, to open file "tmp.txt" in the current working directory

    for reading and writing:

    fd = open("tmp.txt", O_RDWR);

    To open tmp.txt" in the current working directory

    for appending or create it, if it does not exist, with read,

    write and execute permissions for owner only:

    fd = open("tmp.txt", O_WRONLY|O_APPEND|O_CREAT, S_IRWXU);

    A file may be opened or created outside the current working

    directory.

    In this case, an absolute path and relative path may prefix the filename.

    For example, to create a file in /tmp directory:

    open("/tmp/tmp.txt", O_RDWR);

    p ()

    Continued open()

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    48/98

    17 September 2010TCS Internal 48

    Example :

    Opening the file test in read mode and checking forthe error condition

    int fd;

    fd = open(/usr/trg/test, O_RDONLY );if ( fd == -1 )

    {

    printf(error in opening file test);exit(1);

    }

    Continued... open()

    creat()

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    49/98

    17 September 2010TCS Internal 49

    creat()

    intcreat(char * pathname, int fileMode);

    The named file is created as an empty file and opened for writing, and apositive integer, the open file descriptor is returned.

    The file location is set to 0. The

    fileMode (in decimal) defines file access rights.

    If an error occurs in trying to create the file, a negative integer is returned.

    The open file descriptor returned by open and creat are local to theprocess making the calls. Thus, an open file descriptor in one process isnot related to an open file identifier in another process.

    # include

    # include

    # include

    Continued creat()

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    50/98

    17 September 2010TCS Internal 50

    create system call creates and opens a file for writing.

    if the file does not exists, creates a file with modepermissions.

    If file does exists the contents are discardedand the mode value is ignored.The perminssions of the existing file is retained

    Continued... creat()

    close()

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    51/98

    17 September 2010TCS Internal 51

    The close system call closes a file. Its syntax is

    # include

    int close(int fd);

    It returns the value 0 if successful;otherwise the value -1 is returned

    close()

    read()

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    52/98

    17 September 2010TCS Internal 52

    The system call for reading from a file is read.# include

    int read(int fd, char *buf, size_t nbytes);

    The first parameterfdis the file descriptor of the file you wantto read from, it is normally returned from open.

    The second parameterbufis a pointer pointing the memorylocation where the input data should be stored.

    The last parameternbytes specifies the maximum number of

    bytes you want to read.

    The system call returns the number of bytes it actually read,and normally this number is either smaller or equal to nbytes.

    read()

    Continued read()

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    53/98

    17 September 2010TCS Internal 53

    Each file has a pointer, normally called read/write offset,indicating where next readwill start from.

    This pointer is incremented by the number of bytes actually read by the

    readcall.

    For the above example, if the offset was zero before the readand itactually read 1024 bytes, the offset will be 1024 when the readreturns.This offset may be changed by the system call lseek, which will be

    covered shortly.

    int actual_count = 0;int fd = open("tmp.txt", O_RDONLY);char *buf = (char*) malloc(1024);actual_count = read(fd, buf, 1024);

    The following segment of code reads up to 1024 bytes from filetmp.txt:

    Continued... read()

    Example :

    Continued... read()

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    54/98

    17 September 2010TCS Internal 54

    Example :

    Reading the file test 100 characters at a time

    while ( ( n = read(fd,buff,100)) > 0 )

    {

    printf(file test has contents %s ,buff);

    }

    if ( n == 0 )

    printf ( End of file );

    if ( n == -1 )

    printf (Error in reading file test);

    When each read is finished the pointer advances by 100 bytes so thatthe next read picks from there

    If the number of characters left are less than nbytes ( in this example -100 ) then read() will pick up what is left over

    ()

    write()

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    55/98

    17 September 2010TCS Internal 55

    The system call write is to write data to a file.# include

    ssize_t write(int fd, const void *buf, size_t nbytes);

    It writes nbytes of data to the file referenced by file descriptorfdfrom the buffer pointed by buf.

    The write starts at the position pointed by the offset of the file.Upon returning from write, the offset is advanced by thenumber of bytes which were successfully written.

    The function returns the number of bytes that were actuallywritten, or it returns the value -1 if failed.

    write()

    Continued... write()

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    56/98

    17 September 2010TCS Internal 56

    b~=W

    =E=E==Z=~=EIINMMFF=[=M=F

    N=Z==ENIINMMFX

    LG===~~==GL

    LG===WJ[=N=== GL=E=N=ZZ=JN=F

    Eb====?FX

    =E==ZZ=JN=F

    Eb==~===?FX

    Continued... write()

    lseek()

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    57/98

    17 September 2010TCS Internal 57

    The lseeksystem call allows random accessto a file by reposition the offset for next read or write.# include

    # include

    long lseek(int fd, long offset, int reference);

    It repositions the offset of the file descriptorfdto theargument offsetaccording to the directive reference.

    The reference indicate whetheroffsetshould be consideredfrom the beginning of the file (with reference 0),from the current position of the read/write offset (withreference 1),or from the end of the file (with reference 2).

    The call returns the byte offset where the next read/write

    b~ W

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    58/98

    17 September 2010TCS Internal 58

    b~=W

    =Z=EINMMMIMFX

    =E==ZZ=JN=F

    =

    E ====NMMM=====FX

    EINMMMIMF=======NMMM=======~ ~==

    ~===NMMN=

    unlink()

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    59/98

    17 September 2010TCS Internal 59

    The unlinkmay be used to delete a file (A file may have multiplenames (also called links), here we assume that a file in this

    context has only one name or link.).# include

    int unlink(const char *path);

    path is the file name to be deleted.

    The unlinksystem call returns the value 0 if successful, otherwiseit returns the value -1.

    chmod()

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    60/98

    17 September 2010TCS Internal 60

    File access permissions may be set using the chmodsystem call(note that there is a command with the same name for settingaccess permissions.). It has two forms:# include

    # include

    int chmod(const char *path, int mode); orint fchmod(int fd, int mode);

    Both forms set the access permission of a file to mode.

    In the first form the file is identified by its name, and in the second

    it is identified by a file descriptor returned from the open systemcall.

    The system call returns the value 0 if successful, otherwise it

    returns the value -1.

    Continued... chmod()

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    61/98

    17 September 2010TCS Internal 61

    b~=W

    =Z=EKIMSMMFX

    =E==ZZ=JN=F

    E =~===FX

    `~=====K

    KKI==~=~=====

    chown()

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    62/98

    17 September 2010TCS Internal 62

    To change the owner and group of a file# include

    # include

    int chown(const char * filename, int owner, int group);

    Where the the filename names the file and owner and group are the

    new owner and group id for the file.

    If the owner or group is specified as 1, then that id is not changed.

    Only the super-user can change the owner of the file.

    The owner of the file may change the group of the file to any group

    of which that owner is a member.

    c o ()

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    63/98

    17 September 2010TCS Internal 63

    Process Related System calls

    fork ()

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    64/98

    17 September 2010TCS Internal 64

    G===EF=~=~=======~==

    G===`====~=~======~=

    G===m~==~=======

    `==~==M

    @= YLK[

    @YK[

    EF

    Continued... fork()

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    65/98

    17 September 2010TCS Internal 65

    if (!fork()){

    printf("I'm the child!\n");

    exit(0);}else{

    printf("I'm the parent!\n");wait(NULL);

    }

    getpid () getppid ()

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    66/98

    17 September 2010TCS Internal 66

    G==EF==========~==============

    G==EF===~=====~=

    Continued...

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    67/98

    17 September 2010TCS Internal 67

    @=YK[

    ~EF

    =X

    I= X

    X

    =Z=EFX

    Emmfa=J B=mfaJ B==J B=yI=EF=I=

    EF=I=FX

    Emdom=J B=y=I=EF=FX

    it()

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    68/98

    17 September 2010TCS Internal 68

    wait() causes a parent to stop running and await thetermination of a child process

    wait()

    #include sys/types.h>

    #include

    int wait(int * status)

    wait() returns the pid of the process that ended.

    status stores the value returned by the child process

    exec

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    69/98

    17 September 2010TCS Internal 69

    The exec system calls reinitialize a process from a

    designated program; the program changes while the processremains

    On the other hand, the fork system call creates a new

    process that is a clone of an existing one, by just copyingover the instruction, user-data and system-data segments;

    the new process is not initialized from a program, so old andnew processes executethe same instructions.

    Continued... exec

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    70/98

    17 September 2010TCS Internal 70

    Actually, there is no system call named exec. The so-calledexec system calls are a set of six, with names of the form

    execAB, whereA is eitherl orv, depending on whether thearguments are directly in the call (list) or in an array (vector),and B is either absent, a p to indicate that the PATH

    environment variable should be used to search for the

    program, or an e to indicate that a particular environment isto be used. (Oddly, you cant get both the p and e features

    in the same call.) Thus, the six names are

    execl, execv, execlp, execvp, execle, and execve.

    Continued... exec

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    71/98

    17 September 2010TCS Internal 71

    #include

    int execl(

    const char *path, /* program pathname */

    const char *arg0, /* first arg (file name) */

    const char *arg1, /* second arg (if needed) */..., /* remaining args (if needed) */

    NULL /* arg list terminator */

    );

    Continued... exec

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    72/98

    17 September 2010TCS Internal 72

    The argument path must name a program file that is

    executable by the effective user-ID (mode 755, say)and has the correct contents for executable programs.

    The processs instruction segment is overwritten by theinstructions from the program, and the processs user-data segment is overwritten by the data from theprogram, with a re-initialized stack. Then the process

    executes the new program from the top (that is, itsmain function is called).

    Continued... exec

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    73/98

    17 September 2010TCS Internal 73

    There can be no return from a successful execl

    because the return location is gone. An unsuccessfulexecl does return, with a value of 1,

    The arguments to execl that follow path are collected

    into an array of character pointers; the last argument,which must be NULL, stops the collection and terminates

    the array.

    The first argument, by convention, is the name of theprogram file (not the entire path).

    The environment pointed to by environ is passed on,

    too, and it is accessible through the new programsenviron pointer or with getenv

    b~===EF

    Continued... exec

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    74/98

    17 September 2010TCS Internal 74

    @=YK[

    ~EF

    X E=m~==yFX======================

    =E=E==Z=EF=F=ZZ=M=F

    Ep~ ==yFX===

    ELL~I~IMFX

    E m~==~~=yFX==

    Continued...

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    75/98

    17 September 2010TCS Internal 75

    execvexecute file with argument vector

    #include

    int execv(

    const char *path, /* program pathname */

    char *const argv[] /* argument vector */

    );

    /* Returns -1 on error (sets errno) */

    Continued... exec

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    76/98

    17 September 2010TCS Internal 76

    execlpexecute file with argument list and PATH search#include

    int execlp(

    const char *file, /* program file name */

    const char *arg0, /* first arg (file name)

    */

    const char *arg1, /* second arg (if needed)

    */

    ..., /* remaining args (if needed) */

    NULL /* arg list terminator */

    );

    * - *

    Continued... exec

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    77/98

    17 September 2010TCS Internal 77

    execvpexecute file with argument vector and PATHsearch

    #include

    int execvp(

    const char *file, /* program file name */

    char *const argv[] /* argument vector */

    );

    /* Returns -1 on error (sets errno) */

    execleexecute file with argument list and environment

    Continued... exec

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    78/98

    17 September 2010TCS Internal 78

    g

    #include

    int execle(

    const char *path, /* program pathname */

    const char *arg0, /* first arg (file name)

    */

    const char *arg1, /* second arg (if

    needed) */

    ..., /* remaining args (if needed) */

    NULL, /* arg list terminator */

    char *const envv[] /* environment vector

    */

    Continued... exec

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    79/98

    17 September 2010TCS Internal 79

    execveexecute file with argument vector andenvironment

    #include

    int execve(

    const char *path, /* program pathname */

    char *const argv[], /* argument vector */

    char *const envv[] /* environment vector

    */

    );

    /* Returns -1 on error (sets errno) */

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    80/98

    17 September 2010TCS Internal 80

    Inter Process Communication

    Signals

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    81/98

    17 September 2010

    TCS Internal81

    Signals are software generated interrupts that are sent to a process when aevent happens.

    Signals can be synchronously generated by an error in an application, such asSIGFPE and SIGSEGV, but most signals are asynchronous.

    Signals can be posted to a process when the system detects a software event,such as a user entering an interrupt or stop or a kill request from another

    process.

    Signals can also be come directly from the OS kernel when a hardware eventsuch as a bus error or an illegal instruction is encountered.

    Continued... Signals

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    82/98

    17 September 2010

    TCS Internal82

    Some signals stop the receiving process and other signals can beignored. Each signal has a default action which is one of thefollowing:

    The signal is discarded after being receivedThe process is terminated after the signal is receivedA core file is written, then the process is terminatedStop the process after the signal is received

    Each signal defined by the system falls into one of five classes:Hardware conditionsSoftware conditions

    Input/output notificationProcess controlResource control

    Continued... Signals

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    83/98

    17 September 2010

    TCS Internal83

    Macros are defined in header file for commonsignals.

    These include:

    SIGHUP 1 /* hangup */SIGINT 2 /* interrupt */SIGQUIT 3 /* quit */SIGILL 4 /* illegal instruction */

    SIGABRT 6 /* used by abort */SIGKILL 9 /* hard kill */

    SIGALRM 14 /* alarm clock */SIGCONT 19 /* continue a stopped process */

    SIGCHLD 20 /* to parent on child stop or exit */

    Signals can be numbered from 0 to 31.

    #include

    Continued... Signals

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    84/98

    17 September 2010

    TCS Internal84

    #include

    #include

    #include

    int main(void)

    {

    void sigint_handler(int sig); /* prototype */

    char s[200];

    /* set up the handler */

    if (signal(SIGINT, sigint_handler) == SIG_ERR) {

    perror("signal");

    exit(1);

    }

    printf("Enter a string:\n");

    Continued... Signals

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    85/98

    17 September 2010

    TCS Internal85

    if (gets(s) == NULL)

    perror("gets");

    else

    printf("You entered: \"%s\"\n", s);

    return 0;

    }

    /* this is the handler */

    void sigint_handler(int sig)

    {

    printf("Not this time!\n");

    }

    Named Pipes

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    86/98

    17 September 2010

    TCS Internal86

    A FIFO ("First In, First Out", pronounced "Fy-Foh") is sometimes known asa named pipe. That is, it's like a pipe, except that it has a name! In thiscase, the name is that of a file that multiple processes can open() and readand write to.

    Named pipes exist as a device special file in the file system.

    Processes of different ancestry can share data through a named pipe.

    When all I/O is done by sharing processes, the named pipe remains in thefile system for later use.

    #include

    #i l d dlib h

    Continued... Pipes

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    87/98

    17 September 2010

    TCS Internal87

    #include #include #include #include #define FIFO_FILE "MYFIFO"int main(void){

    FILE *fp;char readbuf[80]; /* Create the FIFO if it does not exist */

    umask(0);mknod(FIFO_FILE, S_IFIFO|0666, 0);while(1){

    fp = fopen(FIFO_FILE, "r"); fgets(readbuf, 80, fp);printf("Received string: %s\n", readbuf);fclose(fp);

    }return(0);

    }

    #include

    #i l d tdlib h

    Continued... Pipes

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    88/98

    17 September 2010

    TCS Internal88

    #include #define FIFO_FILE "MYFIFO"int main(int argc, char *argv[]){

    FILE *fp;

    if ( argc != 2 ){

    printf("USAGE: fifoclient [string]\n");exit(1);

    }if((fp = fopen(FIFO_FILE, "w")) == NULL){

    perror("fopen");exit(1);

    }fputs(argv[1], fp);fclose(fp);return(0);

    }

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    89/98

    17 September 2010

    TCS Internal89

    make

    make

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    90/98

    17 September 2010

    TCS Internal90

    The make utility automatically determines which pieces of alarge program need to be recompiled, and issues commandsto recompile them

    To prepare to use make, you must write a file called themakefile that describes the relationships among files in your

    program and provides commands for updating each file.

    In a program, typically, the executable file is updated fromobject files, which are in turn made by compiling source files.

    O it bl k fil i t h ti h

    make

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    91/98

    17 September 2010

    TCS Internal91

    Once a suitable makefile exists, each time you changesome source files, this simple shell command:

    makesuffices to perform all necessary recompilations. Themake program uses the makefile data base and the

    last-modification times of the files to decide which ofthe files need to be updated.

    For each of those files, it issues the commandsrecorded in the data base.

    make

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    92/98

    17 September 2010

    TCS Internal92

    Here is a makefile that describes the way an executable filecalled edit depends on eight object files which, in turn,depend on eight C source and three header files.

    In this example, all the C files include defs.h, but only those

    defining editing commands include command.h, and only lowlevel files that change the editor buffer include buffer.h.

    edit : main.o kbd.o command.o display.o \insert.o search.o files.o utils.o

    cc -o edit main o kbd o command o display o \

    make

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    93/98

    17 September 2010

    TCS Internal93

    cc -o edit main.o kbd.o command.o display.o \insert.o search.o files.o utils.o

    main.o : main.c defs.hcc -c main.c

    kbd.o : kbd.c defs.h command.h

    cc -c kbd.ccommand.o : command.c defs.h command.h

    cc -c command.cdisplay.o : display.c defs.h buffer.h

    cc -c display.c

    insert.o : insert.c defs.h buffer.hcc -c insert.c

    search.o : search.c defs.h buffer.hcc -c search.c

    files.o : files.c defs.h buffer.h command.h

    cc -c files.cutils.o : utils.c defs.h

    cc -c utils.cclean :

    rm edit main.o kbd.o command.o display.o insert.o search.o files.o utils.o

    To use this makefile to create the executable file called

    make

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    94/98

    17 September 2010

    TCS Internal94

    To use this makefile to create the executable file callededit, type:

    makeTo use this makefile to delete the executable file and all

    the object files from the directory, type:make clean

    In the example makefile, the targets include the

    executable file `edit', and the object files `main.o' and`kbd.o'. The prerequisites are files such as `main.c' and`defs.h'. In fact, each `.o' file is both a target and a

    prerequisite. Commands include `cc -c main.c' and `cc -c kbd.c'

    The target `clean' is not a file, but merely the name of anaction Since you normally do not want to carry out the

    make

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    95/98

    17 September 2010

    TCS Internal95

    action. Since you normally do not want to carry out theactions in this rule, `clean' is not a prerequisite of any otherrule.

    when you give the command: makemake reads the makefile in the current directory and begins

    by processing the first rule. In the example, this rule is for

    relinking edit; but before make can fully process this rule, itmust process the rules for the files that edit depends on,which in this case are the object files. Each of these files isprocessed according to its own rule. These rules say to

    update each `.o' file by compiling its source file. Therecompilation must be done if the source file, or any of theheader files named as prerequisites, is more recent than theobject file, or if the object file does not exist

    make

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    96/98

    17 September 2010

    TCS Internal96

    The other rules are processed because their targets appear as

    prerequisites of the goal. If some other rule is not depended on

    by the goal (or anything it depends on, etc.), that rule is notprocessed, unless you tell make to do so (with a command such

    as make clean).

    Before recompiling an object file, make considers updatingits prerequisites, the source file and header files. This makefile

    does not specify anything to be done for themthe `.c' and `.h' filesare not the targets of any rulesso make does nothing for these files.

    makedefine a variable objects with a line like this in the makefile:

    objects = main.o kbd.o command.o display.o insert.o search.o files.o utils.oThen, each place we want to put a list of the object file names,we can substitute the variable's

    value by writing `$(objects)'

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    97/98

    17 September 2010

    TCS Internal97

    objects = main.o kbd.o command.o display.o \

    insert.o search.o files.o utils.oedit : $(objects)

    cc -o edit $(objects)

    main.o : main.c defs.hcc -c main.ckbd.o : kbd.c defs.h command.h

    cc -c kbd.ccommand.o : command.c defs.h command.h

    cc -c command.c

    display.o : display.c defs.h buffer.hcc -c display.c

    insert.o : insert.c defs.h buffer.hcc -c insert.c

    search.o : search.c defs.h buffer.h

    cc -c search.cfiles.o : files.c defs.h buffer.h command.hcc -c files.c

    utils.o : utils.c defs.hcc -c utils.c

    clean :

    rm edit $(objects)

    y g $( j )

    makeIt is not necessary to spell out the commands for compiling the individual Csource files, because make can figure them out: it has an implicit rule for

    updating a `.o' file from a correspondingly named `.c' file using a `cc -c'd Wh ` ' fil i d t ti ll i thi it i l

  • 7/31/2019 Unix Stream Unix Advanced V2.0

    98/98

    17 September 2010

    TCS Internal

    98

    objects = main.o kbd.o command.o display.o \insert.o search.o files.o utils.o

    edit : $(objects)cc -o edit $(objects)

    main.o : defs.hkbd.o : defs.h command.hcommand.o : defs.h command.hdisplay.o : defs.h buffer.hinsert.o : defs.h buffer.hsearch.o : defs.h buffer.hfiles.o : defs.h buffer.h command.hutils.o : defs.hclean : rm edit $(objects)

    updating a .o file from a correspondingly named .c file using a cc ccommand. When a `.c' file is used automatically in this way, it is alsoautomatically added to the list of prerequisites. We can therefore omitthe `.c' files from the prerequisites, provided we omit the commands.