OlveraeUNSW
Back to problems
COMP1521

COMP1521 2020T3 Q10 — Processes & syscalls

COMP1521 2020T3 Q10 · 9 marks · C
Topic: Processes & syscalls · Source: https://cgi.cse.unsw.edu.au/~cs1521/26T1/resources/practice_exams.html

Summary: Read arguments line by line and run the command repeatedly


Your task is to write a program named 20t3final_q10.c which executes a specified command with arguments it reads from
standard input.

Your program should read lines of input from stdin until end-of-file
and execute the command once for each line of input.

The command to execute will be specified as your program's first command line argument.

Each line will contain the arguments to execute the command with.
The arguments will be separated by one or more spaces.

You must execute the command with posix_spawnp(3) .

For example:

dcc 20t3final_q10.c -o 20t3final_q10
./20t3final_q10 cp
file1 file1.backup
file2 file2.backup
file3 file3.backup

In the above example, your program should call posix_spawnp(3) 3 times to execute these 3 commands:

cp file1 file1.backup
cp file2 file2.backup
cp file3 file3.backup

If your program is given more than one command-line argument,
any additional arguments should be passed as the first arguments to the command to be executed
followed by any arguments in each line read from standard input.

For example:

./20t3final_q10 cp file
file.backup1
file.backup2
file.backup3
file.backup4

In the above example, your program should call posix_spawnp(3) 4 times to execute these 4 commands:

cp file file.backup1
cp file file.backup2
cp file file.backup3
cp file file.backup4

You should ignore any spaces at the start and finish of lines.
There maybe more than one space between arguments.

For example:

./20t3final_q10 ls -l -a
20t3final_q1.s
20t3final_q2.c 20t3final_q3.c 20t3final_q4.s

-rw-r--r-- 1 z5555555 z5555555 42 Nov 28 13:08 20t3final_q1.s
-rw-r--r-- 1 z5555555 z5555555 42 Nov 28 13:21 20t3final_q2.c
-rw-r--r-- 1 z5555555 z5555555 42 Nov 28 13:39 20t3final_q3.c
-rw-r--r-- 1 z5555555 z5555555 42 Nov 28 13:58 20t3final_q4.s

In the above example, your program should call posix_spawnp(3) twice to execute these 2 commands:

ls -l -a 20t3final_q1.s
ls -l -a 20t3final_q2.c 20t3final_q3.c 20t3final_q4.s

If your program is given no arguments it should run the command echo(1) for each line of input.

In other words the command to run defaults to echo(1) .

For example:

./20t3final_q10
This is a difficult exam question!

This is a difficult exam question!

In the above example, your program should call posix_spawnp(3) once to execute this command:

echo This is a difficult exam question!

Note the output in the above example is from echo not 20t3final_q10.c .

There is no supplied code for this question.

Your solution must be writen in C only.

The only external program you may run is the specified command.

You must run this program with posix_spawnp(3)

You are not permitted to use system, popen, fork or exec.

No error checking is necessary.

Your program should produce no output.

The commands it runs may or may not produce output.

Your program should run the specified command once for every line of input.

You can assume that each line of input will not exceed 65536 bytes.


🤖 Use posix_spawnp(3). argv[1] is the command; any extra argv[2..] are prepended to every invocation. Read stdin lines until EOF; for each line, split on spaces and run the command once with (extra args + line args). For the autotest the command is echo.

Verified on the sandbox AND cross-checked against an independent oracle.

Samples

stdin
hello
world foo
expected
hello
world foo
stdin
a
b c
expected
PRE a
PRE b c
c-dcc
14