CSCI-E28 LECTURE 2 SAMPLES PAGE 1

Similar documents
CSCI-E28 Lecture 3 Outline. Directories, File Attributes, Bits, File Operations. Write our own versions of Unix programs

CSCI-E28 Lecture 4 Outline. Internal Structure of Files, Directories, File Systems. Work from user view tosystem view, write pwd

csci-e28 lecture 4samples page 1

PRINCIPLES OF OPERATING SYSTEMS

CSE 333 SECTION 3. POSIX I/O Functions

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

OS COMPONENTS OVERVIEW OF UNIX FILE I/O. CS124 Operating Systems Fall , Lecture 2

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

CSI 402 Lecture 2 Working with Files (Text and Binary)

Student Number: Instructor: Reid Section: L0101 (10:10-11:00am)

SYSTEM AND LIBRARY CALLS. UNIX Programming 2015 Fall by Euiseong Seo

CS240: Programming in C

de facto standard C library Contains a bunch of header files and APIs to do various tasks

Why files? 1. Storing a large amount of data 2. Long-term data retention 3. Access to the various processes in parallel

File IO and command line input CSE 2451

Goals of this Lecture

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

Important Dates. October 27 th Homework 2 Due. October 29 th Midterm

CSC209H Lecture 10. Dan Zingaro. March 18, 2015

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

Student Number: Instructor: Reid Section: L5101 (6:10-7:00pm)

What Is Operating System? Operating Systems, System Calls, and Buffered I/O. Academic Computers in 1983 and Operating System

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

CSCI-E26 Lecture 3 Outline. text, text, text (arrays, functions, strings) Build CGI programs, then build the tools they use

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

CSE 333 SECTION 3. POSIX I/O Functions

Overview of Time Related Data Structures and Functions in Unix/C. It s About Time

structs as arguments

CSE 12 Spring 2016 Week One, Lecture Two

Unix Basics Compiling and Using. CMPT 300 Operating Systems I Summer Segment 2: Unix Basics. Melissa O Neill

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

Creating a Shell or Command Interperter Program CSCI411 Lab

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Preview. Review. System Data Files (Password File) System Data Files (Password File) System Data Files (Password File)

Standard File Pointers

CS240: Programming in C

Chapter 6. System Data Files and Information

UNIX System Programming

CSE 410: Systems Programming

CMPT 300. Operating Systems. Brief Intro to UNIX and C

csci-e26 lecture 3samples page 1

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

Outline. OS Interface to Devices. System Input/Output. CSCI 4061 Introduction to Operating Systems. System I/O and Files. Instructor: Abhishek Chandra

Processes. Processes (cont d)

Naked C Lecture 6. File Operations and System Calls

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Lecture 3. Introduction to Unix Systems Programming: Unix File I/O System Calls

Introduction. Files. 3. UNIX provides a simple and consistent interface to operating system services and to devices. Directories

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

CSE 333 Midterm Exam 2/12/16. Name UW ID#

CSC 271 Software I: Utilities and Internals

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING SOFTWARE ENGINEERING DEPARTMENT

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

CS 3113 Introduction to Operating Systems Midterm October 11, 2018

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

Computational Methods of Scientific Programming Fall 2007

SYSTEMS PROGRAMMING AND COMPUTER ARCHITECTURE Assignment 4: Programming in C [1]

Final Exam. Fall Semester 2016 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

CSE 333 Midterm Exam July 24, 2017 Sample Solution

'HYHORSPHQWDQGXVDJHRI&-DYDDQG7FOEDVHGVFDQQHUV LQ7FODSSOLFDWLRQV Dr. Detlef Groth and Alexander Straub, MPIMG Berlin, Germany, dgroth(at)molgen.mpg.

Maria Hybinette, UGA. ! One easy way to communicate is to use files. ! File descriptors. 3 Maria Hybinette, UGA. ! Simple example: who sort

CSE 12 Spring 2018 Week One, Lecture Two

Question 1. Part (a) Part (b) Part (c) Part (d) Part (e) Part (f) APRIL 2012 Final Examination Solutions CSC 209H1S

CSC UNIX System, Spring 2015

The course that gives CMU its Zip! I/O Nov 15, 2001

10. I/O System Library

COP4342 UNIX Tools Assignment #3: A Simple Unix Shell. Instructor: Dr. Robert Van Engelen Teaching Assistant: Imran Chowdhury Spring 2018

CS240: Programming in C. Lecture 14: Errors

C for C++ Programmers

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Overview of today s lecture. Quick recap of previous C lectures. Introduction to C programming, lecture 2. Abstract data type - Stack example

CSE 333 Midterm Exam July 24, Name UW ID#

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

Student Number: Instructor: Reid Section: L0201 (12:10-1:00pm)

Interacting with Unix

Exercise Session 2 Systems Programming and Computer Architecture

CS242: Object-Oriented Design and Programming

PROGRAMMAZIONE I A.A. 2017/2018

Question 1: Complex numbers

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Figure 1 Ring Structures

Lab 2: More Advanced C

CS , Fall 2003 Exam 2

Operating System Labs. Yuanbin Wu

File I/O. Preprocessor Macros

Lecture 23: System-Level I/O

Main differences with Java

3COM0271 Computer Network Protocols & Architectures A

Lecture 9 Assertions and Error Handling CS240

M.CS201 Programming language

COSC Operating Systems Design, Fall Lecture Note: Unnamed Pipe and Shared Memory. Unnamed Pipes

15213 Recitation Section C

15-213/18-243, Spring 2011 Exam 2

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

CSE 374 Midterm Exam Sample Solution 2/6/12

Transcription:

CSCI-E28 LECTURE 2 SAMPLES PAGE 1 :::::::::::::: who0.c :::::::::::::: * who version 0 * main outline but no substance main() int fd; for file des of utmp struct utmp current_record; hold info from file int reclen = sizeof(struct utmp); fd = open( UTMP_FILE, O_RDONLY ); if ( fd == -1 ) perror( "who0" ); while ( read ( fd, &current_record, reclen ) == reclen ) show_info( &current_record ); close ( fd ); :::::::::::::: who1.c :::::::::::::: * who version 1 - read /etc/utmp and list info therein define INFOFILE UTMP_FILE void show_info( struct utmp *utbufp ); int main(int ac, char **av) struct utmp utbuf; read info into here int utmpfd; read from this descriptor if ( (utmpfd = open( INFOFILE, O_RDONLY )) == -1 ) fprintf(stderr,"%s: cannot open %s\n", *av, INFOFILE); while ( read( utmpfd, &utbuf, sizeof(utbuf) ) == sizeof(utbuf) ) show_info( &utbuf ); close( utmpfd ); return 0; went ok * show info() * displays the contents of the utmp struct * in human readable form * *note* these sizes should not be hardwired void show_info( struct utmp *utbufp ) // printf("%-8.8s", utbufp->ut_name); the logname printf("%s", utbufp->ut_name); the logname printf(" "); a space printf("%-8.8s", utbufp->ut_line); the tty printf(" "); a space printf("%10ld", (long) utbufp->ut_time); login time printf(" "); a space printf("(%s)", utbufp->ut_host); the host printf("\n"); newline

CSCI-E28 LECTURE 2SAMPLES PAGE 2 :::::::::::::: who2.c :::::::::::::: include <time.h> * who version 2 - read /etc/utmp and list info therein * - surpresses empty records * - formats time nicely UTMP_FILE is a symbol defined in utmp.h note: compile with -DTEXTDATE for dates format: Feb 5, 1978 otherwise, program defaults to NUMDATE (1978-02-05) define TEXTDATE ifndef DATE_FMT ifdef TEXTDATE define DATE_FMT "%b %e %H:%M" text format else define DATE_FMT "%Y-%m-%d %H:%M" the default int main(int ac, char *av[]) struct utmp utbuf; read info into here int utmpfd; read from this descriptor void show_info(struct utmp *); if ( (utmpfd = open( UTMP_FILE, O_RDONLY )) == -1 ) fprintf(stderr,"%s: cannot open %s\n", *av, UTMP_FILE); while ( read( utmpfd, &utbuf, sizeof(utbuf) ) == sizeof(utbuf) ) show_info( &utbuf ); close( utmpfd ); return 0; * show info() * displays the contents of the utmp struct * in human readable form * * displays nothing if record has no user name void show_info( struct utmp *utbufp ) void showtime(time_t, char *); if ( utbufp->ut_type!= USER_PROCESS ) return; printf("%-8s", utbufp->ut_name); the logname printf(" "); a space printf("%-12.12s", utbufp->ut_line); the tty printf(" "); a space showtime( utbufp->ut_time, DATE_FMT); display time if ( utbufp->ut_host[0]!= \0 ) printf(" (%s)", utbufp->ut_host); the host printf("\n"); newline define MAXDATELEN 100 void showtime( time_t timeval, char *fmt ) * displays time in a format fit for human consumption. * Uses localtime to convert the timeval into a struct of elements * (see localtime(3)) and uses strftime to format the data char result[maxdatelen]; struct tm *tp = localtime(&timeval); convert time strftime(result, MAXDATELEN, fmt, tp); format it fputs(result, stdout);

CSCI-E28 LECTURE 2SAMPLES PAGE 3 :::::::::::::: Makefile :::::::::::::: makefile for lecture 2 CC = cc CFLAGS = -Wall -Wextra -g who1: who1.c $(CC) $(CFLAGS) -o who1 who1.c who2: who2.c $(CC) $(CFLAGS) -o who2 who2.c who3: who3.o utmplib.o $(CC) $(CFLAGS) -o who3 who3.o utmplib.o llcopy: llcopy.c $(CC) $(CFLAGS) llcopy.c -o llcopy utmplib.o: utmplib.c $(CC) $(CFLAGS) -c utmplib.c who3.o: who3.c $(CC) $(CFLAGS) -c who3.c clean: rm -f who1 who2 who3 llcopy utmplib.o tests if llcopy works like cp copytest: llcopy last -20 > data llcopy data data.copy if diff data data.copy ; then echo success ; else echo error ; fi

CSCI-E28 LECTURE 2SAMPLES PAGE 4 :::::::::::::: llcopy.c :::::::::::::: * ** low level copy ** uses read and write with tunable buffer size ** ** usage: llcopy src dest * define BUFFERSIZE 4096 define COPYMODE 0644 void fatal( char *, char *); int main( int ac, char *av[] ) int in_fd, out_fd, n_chars; char buf[buffersize]; * check args if ( ac!= 3 ) fprintf( stderr, "usage: %s source destination\n", *av); * open files if ( ( in_fd=open(av[1], O_RDONLY) ) == -1 ) fatal("cannot open ", av[1]); if ( ( out_fd=creat( av[2], COPYMODE ) ) == -1 ) fatal( "Cannot creat", av[2]); * copy files while ( (n_chars = read( in_fd, buf, BUFFERSIZE )) > 0 ) if ( write( out_fd, buf, n_chars )!= n_chars ) fatal("write error to ", av[2]); * close them up if ( n_chars == -1 ) perror(av[1]); fatal("error reading input",""); if ( close(in_fd) == -1 ) fatal("error closing input file:", av[1]); if ( close(out_fd) == -1 ) fatal("error closing output file:", av[2]); return 0; void fatal(char *s1, char *s2) fprintf(stderr,"error: %s%s\n", s1, s2);

CSCI-E28 LECTURE 2SAMPLES PAGE 5 :::::::::::::: who3.c :::::::::::::: include <time.h> include "utmplib.h" include interface def for utmplib.c * who version 3.0 - read /etc/utmp and list info therein * - surpresses empty records * - formats time nicely * - buffers input (using utmplib) define TEXTDATE ifndef DATE_FMT ifdef TEXTDATE define DATE_FMT "%b %e %H:%M" text format else define DATE_FMT "%Y-%m-%d %H:%M" the default int main(int ac, char **av) struct utmp *utbufp, holds pointer to next rec *utmp_next(); returns pointer to next void show_info( struct utmp * ); if ( utmp_open( UTMP_FILE ) == -1 ) fprintf(stderr,"%s: cannot open %s\n", *av, UTMP_FILE); while ( ( utbufp = utmp_next() )!= NULL ) show_info( utbufp ); utmp_close( ); return 0; * show info() * displays the contents of the utmp struct * in human readable form * * displays nothing if record has no user name void show_info( struct utmp *utbufp ) void showtime( time_t, char *); if ( utbufp->ut_type!= USER_PROCESS ) return; printf("%-8s", utbufp->ut_name); the logname printf(" "); a space printf("%-12.12s", utbufp->ut_line); the tty printf(" "); a space showtime( utbufp->ut_time, DATE_FMT ); display time if ( utbufp->ut_host[0]!= \0 ) printf(" (%s)", utbufp->ut_host); the host printf("\n"); newline define MAXDATELEN 100 void showtime( time_t timeval, char *fmt ) * displays time in a format fit for human consumption. * Uses localtime to convert the timeval into a struct of elements * (see localtime(3)) and uses strftime to format the data char result[maxdatelen]; struct tm *tp = localtime(&timeval); convert time strftime(result, MAXDATELEN, fmt, tp); format it fputs(result, stdout);

CSCI-E28 LECTURE 2SAMPLES PAGE 6 :::::::::::::: utmplib.h :::::::::::::: ifndef UTMPLIB_H define UTMPLIB_H utmplib.h - header file with decls of functions in utmplib.c int utmp_open(char *); struct utmp *utmp_next(); int utmp_close(); :::::::::::::: utmplib.c :::::::::::::: utmplib.c - functions to buffer reads from utmp file * * functions are * int utmp_open( filename ) - open file * returns -1 on error * struct utmp *utmp_next( ) - return pointer to next struct * returns NULL on eof or read error * int utmp_close() - close file * * reads NRECS per read and then doles them out from the buffer * hist: 2012-02-14 slightly modified error handling (thanks mk) * note: this version is extended from the handout version: this tracks cache hits define NRECS 32 define UTSIZE (sizeof(struct utmp)) static struct utmp utmpbuf[nrecs]; storage static int num_recs; num stored static int cur_rec; next to go static int fd_utmp = -1; read from static int utmp_reload(); * utmp_open -- connect to specified file * args: name of a utmp file * rets: -1 if error, fd for file otherwise int utmp_open( char *filename ) fd_utmp = open( filename, O_RDONLY ); open it cur_rec = num_recs = 0; no recs yet return fd_utmp; report * utmp_next -- return address of next record in file * args: none * rets: address of record in buffer * note: this location will be reused on next buffer reload struct utmp *utmp_next() struct utmp *recp; if ( fd_utmp == -1 ) error? return NULL; if ( cur_rec==num_recs && utmp_reload() <= 0 ) any more? return NULL; recp = &(utmpbuf[cur_rec]); get address of next record cur_rec++; return recp;

CSCI-E28 LECTURE 2SAMPLES PAGE 7 static int utmp_reload() * read next bunch of records into buffer * rets: 0=>EOF, -1=>error, else number of records read int amt_read; amt_read = read(fd_utmp, utmpbuf, NRECS*UTSIZE); read data if ( amt_read < 0 ) mark errors as EOF return -1; num_recs = amt_read/utsize; how many did we get? cur_rec = 0; reset pointer return num_recs; report results * utmp_close -- disconnenect * args: none * rets: ret value from close(2) -- see man 2 close int utmp_close() int rv = 0; if ( fd_utmp!= -1 ) don t close if not rv = close( fd_utmp ); open fd_utmp = -1; record as closed return rv;