-3

So I am writing a C program that is more of a shell and I have come to the point where I need to create a command to open a file in nano. I actually have this working pretty nicely and it is all working. Except that I am getting a Sh 1. Permission Denied when I open a file.

char filename[4080];
printf("Enter the file you wish to edit: ");
scanf("%s", filename);
char thething[4080];
sprintf(thething, "/usr/local/script/send_it.pl %s",
filename);
system(filename);`

When I run it this is what happens

pi@raspberrypi ~/JacoShell $ sudo ./shell

JacoShell: /home/pi/JacoShell $nano
Enter the file you wish to edit: /home/pi/JacoShell/test.txt
sh: 1: /home/pi/JacoShell/test.txt: Permission denied

JacoShell: /home/pi/JacoShell $

./shell is chmoded appropriately so I don't know what the problem is

  • 4
    You're trying to execute `/home/pi/JacoShell/test.txt`, not `nano`. – lxg Sep 12 '14 at 22:32
  • @lxg, no I'm not. Have you ever heard of JacoShell? That's because I am writing it. It prompts you for the file and passes that file name to nano is shows in the code – Jacob Misirian Sep 12 '14 at 22:43
  • So `system` is a function you've written? – Oliver Charlesworth Sep 12 '14 at 22:49
  • 1
    ...but `system()` is defined to invoke `sh`, not JacoShell (or bash, or ksh, or anything else that isn't `sh`). – Charles Duffy Sep 12 '14 at 22:50
  • 1
    @JacobMisirian I think you need to read more about the [`system`](http://man7.org/linux/man-pages/man3/system.3.html) function. – Some programmer dude Sep 12 '14 at 22:50
  • system() is a part of the C standard library used ro execute linux executables. I actually have it executing nano by itself and working just fin. This is just executing it with arguments. – Jacob Misirian Sep 12 '14 at 23:27
  • 1
    @JacobMisirian, incorrect -- `system()` doesn't invoke executables directly; it does so through a shell. If you want to invoke executables _directly_, read the manual for the execve() family of calls -- this is what _real_ shells do. – Charles Duffy Sep 12 '14 at 23:30

1 Answers1

3

You probably meant to pass thething to system(), not filename. As written, your code does not use the results of that sprintf().

Before you do that, though, you may want to consider what happens if your filename contains a space or other special characters…

  • A space? That's not scary. If your file is `/tmp/$(rm -rf /)/hello`, that's more interesting. :) – Charles Duffy Sep 12 '14 at 23:02
  • ...that said, encouraging someone who's writing a shell to use `system()` at all is a bit unfortunate -- given as `system()` itself invokes a shell. – Charles Duffy Sep 12 '14 at 23:33
  • sh: 1: /usr/local/script/send_it.pl: not found – Jacob Misirian Sep 12 '14 at 23:39
  • 2
    Oh, I see, you're copying code from [ancient forum threads](http://cboard.cprogramming.com/c-programming/52075-need-pass-parameters-system.html). That script was part of the original question. It doesn't exist on your system. You'll need to use an appropriate command. –  Sep 12 '14 at 23:57