Question: Write a C program which: reads a line of input from standard input (file descriptor 0) if the line starts with q, u, i, t

Write a C program which: reads a line of input from standard input (file descriptor 0) if the line starts with q, u, i, t then exit otherwise print the line to standard output (file descriptor 1) and loop You will need to implement the following in part-1.c: system call wrappers for read, write, and exit logic to read a line of input using read. (I suggest reading 1 byte at a time and sticking it in a buffer; if its then add a zero on the end and youre done) a function to print a string using write. (again I suggest doing it a byte at a time) a simple loop, with a check after reading a line to see if the user entered quit. You can assume that input lines are never more than 200 bytes long. Remember that you cant return from the main function you have to call exit. Youll probably want to factor out readline and print. Remember that you cant use any functions other than the ones that you write yourself. That means no printf use GDB for your debugging (and no strcmp for string comparisons, ormalloc for allocating memory). Compiling You can compile part 1 with the command make part-1. You can clean up (i.e. delete the compiled executables and intermediate files) with the command make clean. Testing You wont need to do a lot of testing for this one - it either works or it doesnt. Before you submit your work I would suggest you run it under valgrind, which will tell you if youre reading or writing memory that you didnt intend to: user@linux:proj1$ valgrind ./part-1 ==12338== Memcheck, a memory error detector ==12338== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==12338== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info ==12338== Command: ./part-1 ==12338== Hello, type lines of input, or 'quit': > this is a test you typed: this is a test > quit ==12338==

==12338== HEAP SUMMARY: ==12338== in use at exit: 0 bytes in 0 blocks ==12338== total heap usage: 0 allocs, 0 frees, 0 bytes allocated ==12338== ==12338== All heap blocks were freed -- no leaks are possible ==12338== ==12338== For lists of detected and suppressed errors, rerun with: -s ==12338== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) user@linux:proj1$

/* * file: part-1.c */

/* THE ONLY INCLUDE FILE */ #include "sysdefs.h"

/* write these functions */

int read(int fd, void *ptr, int len); int write(int fd, void *ptr, int len); void exit(int err);

/* ---------- */

/* Factor, factor! Don't put all your code in main()! */

/* read one line from stdin (file descriptor 0) into a buffer: */

/* print a string to stdout (file descriptor 1) */

/* ---------- */

void main(void) { /* your code here */ }

/* * file: sysdefs.h * description: system definitions for "bare metal" homework */ #ifndef __SYSDEFS_H__ #define __SYSDEFS_H__

/* found in syscall.S */ extern long int syscall(int, ...);

/* system call numbers */ #define __NR_read 0 #define __NR_write 1 #define __NR_exit 60 #define __NR_open 2 #define __NR_close 3 #define __NR_lseek 8 #define __NR_munmap 11 #define __NR_mmap 9

/* flags for mmap, lseek */ #define O_RDONLY 0 #define PROT_READ 1 #define PROT_WRITE 2 #define PROT_EXEC 4 #define MAP_PRIVATE 2 #define MAP_ANONYMOUS 0x20 #define MAP_FAILED (void*)-1 #define SEEK_SET 0 #define NULL 0

/* round 'a' up to the next multiple of 'b' */ #define ROUND_UP(a, b) (((a + b - 1) / b) * b) #define ROUND_DOWN(a, b) ((a / b) * b)

#endif

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!