Utmp.h The utmp file records information about who is currently using the system The file is a sequence of utmp entries, as defined in struct utmp in the.

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

C Language.
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
File and I/O system calls int open(const char* path, int flags, mode_t modes) int creat(const char *path, mode_t mode) ssize_t read(int fd, void *buf,
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
LV2IDL, a software package for automatic data transfer between LabVIEW and IDL Gelu M. Nita New Jersey Institute of Technology.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
System Files and Process Environment Password file Group file System identification Time Process environment.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
CS 241 Section Week #2 2/4/10. 2 Topics This Section MP1 overview Part1: Pointer manipulation Part2: Basic dictionary structure implementation Review.
Data Management and File Organization
Chapter 8 Files and User Information Utilities. Logical Partitions referred to as file systems like a drive in windows world $ df (display filesystems)
Internet Applications: Telnet, Ping and Traceroute.
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
Chapter 4 Getting Help. Using CDE Help Help Manager – primary help tool SunSolve Online – Web-based online help from Sun Solaris Support – Web-based Sun.
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
Unix Process Environment. main Function A C program starts execution with a function called main. The prototype for the main function is: int main (int.
Guide To UNIX Using Linux Third Edition
Basic Elements of C++ Chapter 2.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
1 The UNIX date command myclock.cpp example The C/C++ time() and ctime() functions myclock2.cpp example Inline function definitions Inline class member.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
S -1 Project Management. S -2 Dependencies include.h proto.h globals.h xserver.c iserver.c
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Adv. UNIX: info/191 Advanced UNIX v Objectives –examine a few system data files (and their C interfaces) which record user and system information.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Lesson 1-Logging On to the System. Overview Importance of UNIX/Linux. Logging on to the system.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
FTP Server API Implementing the FTP Server Registering FTP Command Callbacks Data and Control Port Close Callbacks Other Server Calls.
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
Operating Systems Process Creation
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Computer Programming for Engineers
24 4/11/98 CSE 143 Stream I/O [Appendix C]. 25 4/11/98 Input/Output Concepts  Concepts should be review!  New syntax, but same fundamental concepts.
Files A collection of related data treated as a unit. Two types Text
Mandatory Assignment INF3190. Part 1: Client-server communication via TCP Develop a client-server application in C which allows a client to send UNIX.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Advanced Programming in the UNIX Environment Hop Lee.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
REEM ALAMER REVISION FOR C LANGUAGE COURSE. OUTPUTS int main (void) { int C1, C2; int *p1, *p2; C1 = 8; p1 = &C1; C2 = *p1 / 2 + 5; p2 = &C2; printf ("C1.
CHAPTER 6. SYSTEM DATA FILES AND INFORMATION System Programming 本份投影片大量參考熊博安教授的系統程式投影片 羅習五 國立中正大學資訊工程學系
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Lecture 8 String 1. Concept of strings String and pointers
User Access and User ID Commands in UNIX
Basic Elements of C++.
UNIX System Overview.
Basic Elements of C++ Chapter 2.
Popping Items Off a Stack Lesson xx
C programming Language
Character Arrays char string1[] = “first”;
Files Chapter 8.
Presentation transcript:

Utmp.h The utmp file records information about who is currently using the system The file is a sequence of utmp entries, as defined in struct utmp in the utmp.h file The utmp gives the name of the special file associated with the user’s terminal, user’s login name, and the time of the login time /usr/include/utmp.h /var/adm/utmp $more /usr/include/utmp.h

#define UTMP_FILE “var/adm/utmp” #define WTMP_FILE “var/adm/wtmp” Wtmp file records all logins and logouts. #define ut_name ut_user Struct utmp{ char ut_user[32]; // login name char ut_id[14]; char ut_line[32]; // device name char ut_type; // type of entry pid_t ut_pid; // process id struct exit_status{ short e_termination; // process termination status short e_exit; // process exit status } ut_exit; time_t ut_time; // time entry was made char ut_host[64]; // host name };

Writing who We have three system calls we need to connect to a file, extract chunks of data from a file, and close a file. #include #define SHOWHOST // include remote machine on output

int main() { struct utmp current_record; int utmpfd; int reclen = sizeof(current_record); if( (utmpfd = open(UTMP_FILE,O_RDONLY)) == -1){ perror(UTMP_FILE); exit(1); } while( read(utmpfd, &current_record, reclen) == reclen) show_info(&current_record); close(utmpfd); return 0; }

Show_info() – displays contents of the utmp struct show_info( struct utmp *utbufp) { printf(“%-8.8s”, utbufp->ut_name); printf(“ “); printf(“%-8.8s”, utbufp->ut_line); printf(“ “); printf(“%10ld”, utbufp->ut_time); printf(“ “); #ifdef SHOWHOST printf(“(%s)”, utbufp->ut_host); #endif printf(“ “); }

Blank record Who command lists username of users logged into the system. The above program lists what it finds in the utmp file. The utmp file seems to have records for all terminals, even unused ones Modify the program to skip the entries with blank user names In utmp.h, there are following definition for ut_type #define EMPTY 0 #define RUN_LVL 1 #define BOOT_TIME 2 #define OLD_TIME 3 #define NEW_TIME 4 #define INIT_PROCESS 5 #define LOGIN_PROCESS 6 #define USER_PROCESS 7 #define DEAD_PROCESS 8

Display login time in Human- Readable form Time_t data type – unix stores times as the number of seconds since midnight, Jan. 1,1970. Time_t data type is an integer Ut_time in utmp records represents the login time as the number of seconds since the beginning of the Epoch

Making a time_t readable: ctime ctime() is function that converts a number of seconds since the start of Unix time into a sensible format. char *ctime( const time_t *timep); It converts the calendar time into a string in the form “Wed Jun 30 21:49: ” We have time_t value in the utmp records. We want a string looks like Jun 30 21:49 Wed Jun 30 21:49: Pick 12 characters from position 4 Printf(“%12.12s”, cp+4);

Rewrite show_info void show_info(struct utmp *utbufp) { char * cp; if( utbufo->ut_type != USER_PROCESS) return; printf(“%-8.8s”, utbufp->tu_name); printf(“ “); printf(“%-8.8s”, utbufp->ut_line); cp = ctime(&utbuf->ut_time); printf(“12.12s”, cp+4); #ifdef SHOWHOST if( utbufp->ut_host[0] != ‘\0’) printf(“ (%s)”, utbufp->ut_host); #endif printf(“\n”); }