Lecture 6 Software Development under Unix - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Lecture 6 Software Development under Unix

Description:

Note on gcc E catGetchar.c. On Solaris, Linux has getchar ... Notes on gcc S catGetchar.c. On Linux (so Intel assembly instead of Sparc) main: pushl ëp ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 34
Provided by: mantonm5
Category:

less

Transcript and Presenter's Notes

Title: Lecture 6 Software Development under Unix


1
Lecture 6Software Development under Unix

CSCE 510 Systems Programming
  • Topics
  • Tsearch revisited
  • Gcc
  • Ar implementation
  • make
  • Time functions

September 6, 2005
2
  • Last Time
  • File times
  • Ar archive formats
  • tsearch
  • Ar implementation
  • Today
  • Overview of the Compilation Process
  • Tsearch revisited
  • Ar implementation headers
  • make
  • Time functions

3
Unix Software Development Tools GCC
  • NAME
  • gcc - GNU project C and C compiler
  • SYNOPSIS
  • gcc -c-S-E -stdstandard
  • -g -pg -Olevel
  • -Wwarn... -pedantic
  • -Idir... -Ldir...
  • -Dmacrodefn... -Umacro
  • -foption... -mmachine-option...
  • -o outfile infile...
  • Only the most useful options are listed
    here g accepts mostly the same options as gcc.
  • DESCRIPTION
  • When you invoke GCC, it normally does
    preprocessing, compilation,
  • assembly and linking.

4
Unix Tools GCC options
  • -c-S-E ? stop process before linking
    assembly compiling
  • -stdstandard ? C language options
  • -g ? preserve symbol table - debugging
    information
  • -Olevel ? optimizer
  • -Wwarn... -pedantic
  • -Idir...
  • -Ldir...
  • -Dmacrodefn... -Umacro
  • -foption... -mmachine-option...
  • -o outfile infile...

5
Printf example
  • thetisgt gcc -o printf printf.c
  • /tmp/ccsvOtyD.o(.text0x41) In function main'
  • undefined reference to atan'
  • collect2 ld returned 1 exit status
  • This strange looking message means the function
    atan is undefined
  • thetisgt gcc -o printf printf.c -lm
  • thetisgt

6
Example catGetchar.c
  • include ltstdio.hgt
  • main() / copy input to output /
  • char bufBUFSIZ
  • int c
  • while ((c getchar()) ! EOF)
  • putchar(c)
  • return 0
  • Then try E (on Solaris), -S and examine the
    intermediate files

7
Note on gcc E catGetchar.c
  • On Solaris, Linux has getchar function
  • / lots of whitespace and /
  • while ((c (--( (__iob0) )-gt_cnt lt 0 ?
    __filbuf( (__iob0) ) (int)
  • ( (__iob0) )-gt_ptr) ) ! (-1) )
  • (--( (__iob1) )-gt_cnt lt 0 ?
    __flsbuf(( ( c ) ), ( (__iob1) ))
  • (int)(( (__iob1) )-gt_ptr (unsigned
    char) ( ( c ) )))
  • return 0

8
Notes on gcc S catGetchar.c
  • On Linux (so Intel assembly instead of Sparc)
  • main
  • pushl ebp
  • movl esp, ebp
  • subl 8216, esp
  • andl -16, esp
  • movl 0, eax
  • subl eax, esp
  • .L2
  • call getchar / note getchar
    function /
  • movl eax, -8204(ebp)
  • cmpl -1, -8204(ebp)
  • jne .L4
  • jmp .L3
  • .L4
  • subl 12, esp
  • pushl -8204(ebp)
  • call putchar

9
Optimizer
.L2 cmpl 99, -4(ebp) jle .L5
jmp .L3 .L5 movl -4(ebp), edx
movl edx, eax sall 3, eax
addl edx, eax addl eax,
eax addl edx, eax leal
-8(ebp), edx addl eax, (edx)
leal -4(ebp), eax incl (eax)
jmp .L2
  • Several levels
  • include ltstdio.hgt
  • main()
  • int i, sum
  • for(i0,sum0 i lt 100 i)
  • sum sum 19i
  • printf("Sum is d\n", sum)

10
AR - archive format revisited
  • Ar Header
  • struct ar_hdr
  • char ar_name16
  • char ar_date12
  • char ar_uid6
  • char ar_gid6
  • char ar_mode8
  • char ar_size10
  • char ar_fmag2

Archive format !ltarchgt Header1 file1 Header2 file2
Note files of odd length are padded with a
blank (\b)
11
TSEARCH(3) Revisited
  • NAME
  • tsearch, tfind, tdelete, twalk - manage a
    binary tree
  • void tsearch(const void key, void
    rootp,
  • int(compar)(const void ,
    const void ))
  • Notes
  • pointer to root pointer, so the function can
    change the root
  • Compare function passed as argument

12
Example tsearch.ex.c
  • struct node
  • char word
  • int length
  • int count
  • struct node root NULL
  • When we pass it as an argument we pass root

13
Comparisons
  • We need to supply a compare function to compare
    nodes for sorting
  • Sorting (tree insertion) choices arument to
    compare
  • define ASCEND 1
  • define DESCEND 2
  • define STRLEN 3
  • int mode ASCEND / global variable /

14
  • printf("Enter words a single word to a line.
    terminated withControlD\n")
  • while(gets(buffer) ! NULL)
  • printf("Read s",buffer)
  • if ((n (struct node
    )tfind(tmpnode,root,mycompare))
    NULL)
  • printf("\tInserting s",buffer)
  • new newnode()
  • new -gt word strdup(buffer)
  • new -gt length strlen(buffer)
  • new -gt count 1
  • info (struct node )
    (tsearch((void )new, (void )root,
    mycompare))
  • printf("After insert s length
    d count d",
  • info-gtword, info-gtlength,
    info-gtcount)
  • else / just update node found /

15
mycompare
  • int
  • mycompare(struct node new, struct node old)
  • int val
  • val strcmp(new-gtword, old-gtword)
  • if(mode ASCEND) return(val)
  • else if(mode DESCEND) return(-1val)
  • else if(mode STRLEN)
  • if (new-gtlength lt old-gtlength )
    return(-1)
  • else if (new-gtlength gt
    old-gtlength) return(1)
  • else return(0)
  • / N.B. strings of same length
    are considered equal/
  • else fatal("Mycompare undefined mode
    error")

16
Getopt References/Examples
  • http//www.gnu.org/software/libc/manual/html_node/
    Getopt.html
  • http//www.opengroup.org/onlinepubs/009695399/func
    tions/getopt.html

17
Getopt variables
  • int opterrIf the value of this variable is
    nonzero, then getopt prints an error message to
    the standard error stream if it encounters an
    unknown option character or an option with a
    missing required argument. This is the default
    behavior. If you set this variable to zero,
    getopt does not print any messages, but it still
    returns the character ? to indicate an error.
  • int optoptWhen getopt encounters an unknown
    option character or an option with a missing
    required argument, it stores that option
    character in this variable. You can use this for
    providing your own diagnostic messages.
  • int optindThis variable is set by getopt to the
    index of the next element of the argv array to be
    processed. Once getopt has found all of the
    option arguments, you can use this variable to
    determine where the remaining non-option
    arguments begin. The initial value of this
    variable is 1.
  • char optargThis variable is set by getopt to
    point at the value of the option argument, for
    those options that accept arguments.
  • int getopt (int argc, char argv, const char
    options)The getopt function gets the next
    option argument from the argument list specified
    by the argv and argc arguments.

18
  • while ((c getopt (argc, argv, "abc")) ! -1)
  • switch (c)
  • case 'a' aflag 1 break
  • case 'b' bflag 1 break
  • case 'c' cvalue optarg break
  • default abort ()
  • printf ("aflag d, bflag d, cvalue s\n",
    aflag, bflag, cvalue)

19
(No Transcript)
20
Make
  • Make - a system for maintaining programs that
    consist of multiple source files
  • make specification files
  • specification captures dependencies
  • builds a tree
  • performs minimal recompilation

21
Make Advantages
  • Make saves time - both typing and recompilation
  • The Makefile documents the dependencies.
  • For distribution of software one can make' and
    make install' a package with knowing anything
    about it.

22
Make Specification Files
  • Makefiles - Make Specification Files
  • Definitions of the form namevalue
  • Target Groups of the form
  • target_1 dependencylist_1
  • cmdlist_1
  • target_2 dependencylist_2
  • cmdlist_2
  • ...

Nota Bene First character of command line must
be a TAB
23
A Simple Makefile
  • Makefile Example
  • prog main.o routines.o
  • cc -o prog main.o routines.o
  • Each command line starts with a \tab
  • main.o main.c defs.h
  • cc -c main.c
  • routines.o routines.c defs.h
  • cc -c routines.c

24
Make Tree (forest)
25
Macros - Builtin Rules
  • make p
  • macros

26
Another Makefile Example
  • FILES Makefile defs.h main.c routines.c
  • OBJS main.o routines.o
  • LIBES -lm
  • CFLAGS -g
  • LP /fac/matthews/bin/p2c
  • INSTALL_DIR /fac/matthews/bin
  • prog main.o routines.o
  • (CC) (CFLAGS) (OBJS) (LIBES) -o prog
  • (OBJS) defs.h
  • cleanup -rm .o -du
  • install prog mv prog (INSTALL_DIR)
  • print (FILES) pr ? gt /tmp/manton (LP)
    /tmp/manton -rm /tmp/manton touch print

27
Make Implementation Algorithm
  • Procedure newest(target)
  • If target is not in the target tree then
  • If file exists return(modification_time)
  • Else return(FAIL)
  • Else min modification_time of target
  • Foreach child in the dependency list Do
  • child_time newest(child)
  • If child_time lt min Then min child_time
  • End
  • If min lt modification_time of target Then
  • build(target)
  • min now
  • EndIf
  • End
  • End
  • Begin Main
  • Parse Specification File
  • Build Dependency Tree
  • newest(target)

28
(No Transcript)
29
File Time Data Structures
  • timeval, timezone and tm structures
  • struct timeval
  • long tv_sec / seconds since Jan. 1, 1970 /
  • long tv_usec / and microseconds /
  • struct timezone
  • int tz_minuteswest / of Greenwich /
  • int tz_dsttime / type of dst correction to
    apply /

30
struct tm
  • struct tm
  • int tm_sec / seconds (0 - 59) /
  • int tm_min / minutes (0 - 59) /
  • int tm_hour / hours (0 - 23) /
  • int tm_mday / day of month (1 - 31) /
  • int tm_mon / month of year (0 - 11) /
  • int tm_year / year - 1900 /
  • int tm_wday / day of week (Sunday 0) /
  • int tm_yday / day of year (0 - 365) /
  • int tm_isdst / flag daylight savings time in
    effect /
  • long tm_gmtoff / offset from GMT in seconds /
  • char tm_zone / abbreviation of timezone name
    /

31
File Time system calls and functions
  • time_t time(time_t tloc) - return the current
    time
  • char ctime(time_t clock) - convert time to
    character string for printing time_t now now
    time(NULL) printf("the current time is s\n",
    ctime(now))
  • int gettimeofday(struct timeval tp, struct
    timezone tzp)
  • char asctime(struct tm tm) convert struct tm
    pointer to character for printing
  • struct tm localtime(time_t clock) - convert
    time_t to "broken down time"
  • struct tm gmtime(time_t clock)- convert time_t
    to "broken down time"
  • double difftime(time_t time1, time_t time0)
  • time_t mktime(struct tm timeptr)
  • int strftime (char s, size_t maxsize, char
    format, struct tm tm) - print time out using
    special conversion specifiers e.g. I -the hour
    on 12 hour clock, m the month, ...

32
Figure 6.8 Time Function Relationships
string
gmtime
mktime
time
kernel
struct tm
time_t
mktime
formatted string
33
Time.c
  • include lttime.hgt
  • include ltstdio.hgt
  • main()
  • time_t now
  • struct tm bdtime, bdt_ptr
  • char buf1024
  • now time(NULL)
  • printf("the number of seconds since the Epoch
    is ld\n", now)
  • printf("the current time is s\n",
    ctime(now))
  • bdt_ptr localtime(now)
  • strftime(buf, 1024, "a b d H m j y u
    Z", bdt_ptr)
  • printf("s\n", buf)
Write a Comment
User Comments (0)
About PowerShow.com