Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

argv: Sanitizing wildcards

Tags:

c

linux

shell

gcc

glob

I was working on an example in the K&R C book where it asks you to essentially build an RPN calculator that takes input through command line arguments. My solution essentially iterates through the given arguments and spits out the answer, but I noticed something:

If I were to give the multiplication character (an asterisk) '*' without single quotes, gcc assumes that to be a wildcard input, so my input of

$./rpn 5 10 *

gives me an output of

read 5
read 10
read rpn
read rpn.c
= 0

Wrapping the asterisk with single quotes remedies the issue

$./rpn 5 10 '*'
read 5
read 10
read *
= 50

My question is would there be a way to sanitize input so that my program does not require the asterisk to be wrapped in single quotes, or is this behavior caused by something more fundamental (e.g. Linux/POSIX/UNIX binary execution and argument handling)?

like image 775
MSalmo Avatar asked Nov 24 '14 05:11

MSalmo


1 Answers

The shell is expanding the glob before executing the program. You quote the glob not because of GCC, but because of the shell. If you don't want this behavior then use a shell that does not honor globs.

like image 186
Ignacio Vazquez-Abrams Avatar answered Oct 20 '22 07:10

Ignacio Vazquez-Abrams