Getting Started. Project 1

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

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

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

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

Lecture 05 Pointers ctd..

Pointers, Arrays, and Strings. CS449 Spring 2016

File IO and command line input CSE 2451

C mini reference. 5 Binary numbers 12

Input/Output and the Operating Systems

C Characters and Strings

System Software Experiment 1 Lecture 7

Course organization. Course introduction ( Week 1)

Arrays and Strings. CS449 Fall 2017

ECE551 Midterm Version 1

Procedural Programming

Stream Model of I/O. Basic I/O in C

CS201 Lecture 2 GDB, The C Library

Jacobs University Bremen February 7 th, 2017 Dr. Kinga Lipskoch. Practice Sheet. First Name:... Last Name:... Matriculation Number:...

Computer Programming: Skills & Concepts (CP) Strings

Characters, Character Strings, and string-manipulation functions in C

Programming in C. 4. Misc. Library Features, Gotchas, Hints and Tips. Dr. Neel Krishnaswami University of Cambridge

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

ECE551 Midterm Version 1

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Strings. Arrays of characters. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY

C for C++ Programmers

Chapter 5, Standard I/O. Not UNIX... C standard (library) Why? UNIX programmed in C stdio is very UNIX based

ECE551 Midterm Version 2

Lab # 4. Files & Queues in C

Memory Management. CSC215 Lecture

Programming. Pointers, Multi-dimensional Arrays and Memory Management

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

DAY 3. CS3600, Northeastern University. Alan Mislove

CSCI 171 Chapter Outlines

PRINCIPLES OF OPERATING SYSTEMS

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

Create a Program in C (Last Class)

C Programming Language Review and Dissection III

File I/O. Arash Rafiey. November 7, 2017

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

CS24 Week 2 Lecture 1

CSC 209H1 S 2012 Midterm Test Duration 50 minutes Aids allowed: none. Student Number: # 1: / 6

15213 Recitation Section C

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

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

ECE 551D Spring 2018 Midterm Exam

ECE551 Midterm. There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly.

Tutorial 1 C Tutorial: Pointers, Strings, Exec

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Characters and Strings

Dynamic Memory. Dynamic Memory Allocation Strings. September 18, 2017 Hassan Khosravi / Geoffrey Tien 1

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Introduction to Programming Systems

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

2009 S2 COMP File Operations

Computer Science & Engineering 150A Problem Solving Using Computers

Carnegie Mellon. C Boot Camp. September 30, 2018

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

Fundamentals of Programming & Procedural Programming

cc -o build/test_translate build/test_translate.o b... $

Lecture 8 Dynamic Memory Allocation

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

ECE 551D Spring 2018 Midterm Exam

Carnegie Mellon. C Boot Camp. Oct 6th, Jack Biggs Raghav Goyal Nikhil Jog

Library Functions. General Questions

Standard C Library Functions

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

25.2 Opening and Closing a File

Contents. A Review of C language. Visual C Visual C++ 6.0

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

CSC209 Review. Yeah! We made it!

Student Number: Computer Science 211b Final Examination. 28 April hours

In Java we have the keyword null, which is the value of an uninitialized reference type

POINTER AND ARRAY SUNU WIBIRAMA

ECE 15B COMPUTER ORGANIZATION

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

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

File Access. FILE * fopen(const char *name, const char * mode);

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

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

20 Dynamic allocation of memory: malloc and calloc

CSC 209H1 S 2012 Midterm Test Duration 50 minutes Aids allowed: none. Student Number: # 1: / 7

EL2310 Scientific Programming

Arrays, Strings, & Pointers

Midterm Review. What makes a file executable? Dr. Jack Lange 2/10/16. In UNIX: Everything is a file

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

Princeton University Computer Science 217: Introduction to Programming Systems. Modularity design principles

EM108 Software Development for Engineers

Exercise Session 3 Systems Programming and Computer Architecture

8. Characters, Strings and Files

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

CS201 - Lecture 1 The C Programming Language

System Programming. Standard Input/Output Library (Cont d)

Programming in C Quick Start! Biostatistics 615 Lecture 4

LSN 3 C Concepts for OS Programming

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

Transcription:

Getting Started Project 1

Project 1 Implement a shell interface that behaves similarly to a stripped down bash shell Due in 3 weeks September 21, 2015, 11:59:59pm Specification, grading sheet, and test examples are located on the website

Shell? Interpreter for a simple programming language Can interface with directly Or run a file called a shell script Usually used to quickly interface with an operating system

Shell Examples sh The first shell, came with Unix csh The C-shell ksh The Korn shell tchsh The Tenex C-shell, used on linprog bash The Bourne Again Shell (default on most Linux distributions) DOS/cmd The Windows Shell

Shell Preparation These next few lectures will walk you through how to build a shell Feel free to follow the steps and use the code templates I provide The code won't be complete as some things will be left for you to figure out on your own The first thing to cover is C

C Standard Library Provides a standard way to: Open / close files Read / write data Manipulate and compare c-strings Convert c-strings to other types (and vice-versa) Allocate/free memory Sort/search input.

Opening and Closing Files #include <stdio.h> FILE *fopen(const char *file_name, const char *mode) void fclose(file *file) FILE pointers provide access to the contents of a file Modes: r Read. Error if file does not exist w Write. Replaces existing file or creates a new file a Append. Add data to existing file or creates a new file w+ Equivalent to both 'r' and 'w' a+ Equivalent to both 'r' and 'a'

C-strings C functions using strings, require the string to be null-terminated That is the final character in the string needs to be '\0' Otherwise, the function will extend beyond the bounds of the string If there are any other '\0' characters within the string, then you can not use these functions An example would be a 'string' containing raw data

Writing Output #include <stdio.h> int printf(const char *format, ) int sprintf(const char *buffer, const char *format, ) int fprintf(file *stream, const char *format, ) int fputs(char *str, FILE *stream) Takes in a c-string and format specifiers to format the output sprintf writes to a buffer fprintf and fputs write to a file The return value is the number of characters written Null character implicitly added in sprintf is not counted

Format Specifiers Most I/O functions in the C Standard Library use format specifiers and flags Common specifiers: %d - signed integer value %u - unsigned integer value %f - float value %x - hexadecimal value %c - character value %s - string value Typing man printf in a shell will give a more complete list Example printf( %s %d\n, Project due: Sept., 21); Project due: Sept. 21

Reading Input #include <stdio.h> int scanf(const char *format, ) int sscanf(const char *buffer, const *format, ) int fscanf(file *stream, const char *format, ) int fgets(char *buffer, int num, FILE *stream) Scanf functions return number of items read Fgets reads num characters from a file into buffer returns the number of characters read

C-string Comparison #include <string.h> int strncmp (const char *str1, const char *str2, size_t num) int strcmp (const char *str1, const char *str2) Returns <0 if str2 contains the large value at the first non-matching character 0 if value of str1 == value of str2 >0 if str1 contains the large value at the first non-matching character Do not do if(str1 == str2) This is a pointer comparison, not a value comparison

C-String Copying #include <string.h> char *strncpy(char *dest, const char *src, size_t num) char *strcpy(char *dest, const char *src) Copies source string into destination string Returns the pointer to dest string Make sure to allocate enough room for dest string Again, do not do dest = src

C-string Searching #include <string.h> char *strstr(const char *pattern, const char *string) char *strchr(const char character, const char *string) Search for the first occurrence of a pattern/character in a string Returns the starting address of the target item Null if not found

Memory Allocation #include <stdlib.h> void *malloc(const size_t num_bytes) void *calloc(const size_t num_objs, const size_t obj_size) void free(void *obj) Need to use when you don't know the size ahead of time Need to cast malloc, calloc to desired type e.g. char *str = (char *)malloc(sizeof(char) * num_chars); calloc returns a 0-initialized pointer Recommended over malloc free deallocates dynamically allocated memory.

Potential Problems with Free void memory_leak(int size) { } /* Never freed! Can not access! */ int *leak = (int *)calloc(size, sizeof(int)); return;

Potential Problems with Free void dangaling_reference(int size) { } int *reference = (int *)malloc(size * sizeof(int); free(ref); /* Already freed! Should not access! */ printf( %d\n, ref[0]); return;

Tools

Man Pages Documentation that comes with most Unix-like systems Contains information for C functions, packages, bash commands, system calls, etc Examples man bash man strncpy man bsearch When there are multiple definitions, it will refer to the lower section Use man 3 printf to see C version Otherwise it will show bash version (man 1 printf) Section information can be found at man man

tar Tape ARchiver To archive tar cvf tarfile.tar files to tar To extract tar xvf tarfile.tar For gzipping Use 'z' flag and.gz extension tar xvfz tarfile.tar.gz

Make Automated software build system You'll use it provide a simple way to the executable for your project Name it: Makefile It works by specifying a target, what it depends on, and how to transform the dependencies In general, it looks like: target : dependency1, dependency2, command1 command2 command3

Make Example CC=gcc CFLAGS=-I. -ansi -pedantic -Wall.PHONY : compile clean run compile : main.x main.x : main.o util.o <tab>$(cc) $(CFLAGS) -o hello.x hello.o main.o : main.c <tab>$(cc) $(CFLAGS) -o main.o -c main.c util.o : util.c <tab>$(cc) $(CFLAGS) -o util.o -c util.c Clean : <tab>rm -f *.o *.x run : compile <tab>./main.x