Linux system() API [closed] - c

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Can anybody tell me the output of the below code, whether "bye" will be printed or not?
#include <stdio.h>
int main()
{
system("ls -l");
printf("bye");
return 0;
}

man system says:
int system(const char *command);
system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed.
And after system() returned, your printf will be executed.

Yes, it will be printed.
Why don't you compile it and test it for yourself? Cut and paste what you've written to a file, e.g. foo.c, and then do the following
gcc -o foo foo.c
./foo
As there is no newline character after your printf("bye") it will end up at the start of your current line; putting printf("bye\n") instead will be a little more clear.

Why wouldn't it? There's no conditional statement, so every part of the code will be executed.
It will print the output of the command provided by system and then print "bye"

The program is going to execute ls -l just fine.
Also, "bye" should indeed be printed. However, since you have not included a '\n' character you will only be seeing it with your prompt appended to it.
Also, if you are not seeing it, then for some reason your output is not flushing when the program exits. Adding a '\n' character may very well fix that issue if that is what you are seeing.

It all depends upon the free memory.
You will need to check return value of the system command.it returns 0 on success.
Your printf statement will be called but not sure that system command will always success.
Thanks,
Neel

Related

scanf function not working on GCC ubuntu [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm starting to write a C code after a gap of almost 10 year. I was teaching my sister and unfortunately got stuck at a very basic scanf() function for a simple interest program on Ubuntu.
My program is not giving any compilation error but it's not showing anything on screen
Here is the code:
#include <stdio.h>
int main()
{
int p,n;
float r,si;
printf("enter values of p,n,r");
scanf("%d %d %f",&p,&n,&r);
si = p*n*r/100;
printf("%f" , si);
return 0;
}
This is a common "issue" when trying to printf() and scanf() after one another. The operating system buffers stdin and stdout in order to increase performance and only flushes them when necessary. You can explicitly flush stdout by calling fflush(stdout); right after printf().
See C/C++ printf() before scanf() issue.
Your code would end up looking like
#include <stdio.h>
int main()
{
int p,n;
float r,si;
printf("enter values of p,n,r\n");
fflush(stdout); // Force stdout to be flushed
scanf("%d %d %f",&p,&n,&r);
si = p*n*r/100;
printf("%f" , si);
return 0;
}
I think you are just not looking at your console properly. The output "si" is getting printed on the same line as your first printf since you do not have a newline inserted at the end of it.
When you say
not showing anything on screen
(emphasis added), I take you to mean that when you run it, it's not displaying the data-entry prompt you're printing. This would be because the standard output is line-buffered by default when connected to a terminal -- it will buffer the output in memory before printing it, until either the buffer is filled, or (because line buffered) a newline is output, or the stream is closed (including when the program terminates normally).
One way to cause the prompt to be displayed, therefore, is to append a newline to it:
printf("enter values of p,n,r\n");
Another is to use an output function that automatically appends a newline:
puts("enter values of p,n,r");
If you want to ensure that the output appears even though no newline has been sent then you can flush the stream (standard output in this case) instead:
printf("enter values of p,n,r: ");
fflush(stdout);
Your code worked perfectly for me in my Ubuntu system.This may be because of the mistake of the IDE you are using.
Try running code in Ubuntu this way:-
save file as program.c on desktop.
open terminal and type cd Desktop (navigate to desktop).
compile code by typing gcc -o program program.c .
run program by typing ./program.

After I use malloc, program suddenly asks for input [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I'm a newbie programmer
//allocating space for final output
//text_count is given by another function, let's just say i have a value for it
//out is my typedef struct
out *fin[text_count];
for(i=0; i<text_count; i++){
fin[i] = malloc(sizeof(out));
}
//this is a test
fin[1]->appearances = 1;
printf("%d",fin[1]->appearances);
// ..other code
I noticed that after this allocation, program suddenly outputs this:
1_
The underscore is blinking, indicating it needs an input.
What can be my problem here? Is it the allocation? Or the codes down below?
okay sorry, let me clarify this, im using codeblocks . so when i get a blinking cursor it means it needs an input. and after i got this output, an infinite loop of inputs seems to be happening.
You're simply seeing the text terminal's cursor. It may be blinking or it may not, that doesn't mean anything in itself.
You might want to add a linefeed:
printf("%d\n", fi[1]->appearances);
to get the output on a line of its own.
A cursor after your printf does not mean, that it is waiting for input.
You can confirm this, by using strace, if it is waiting at read syscall.
Usage:
strace ./myApplication arg1 arg2...

How to accept input with ints and strings with spaces? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am trying to accept input from a file. This file is in the format of an integer, followed by a tab, followed by a string (which may or may not have spaces).
for example:
1\tls -l
2\tls
I tried using:
int cmd_num;
char command[80];
while (fscanf(ifp, "%d\t%s", &cmd_num, command) != EOF) {
...
}
However, this failed when it saw a space. Any help?
You probably need to use a scan-set to read the string:
if (fscanf(fp, "%d\t%79[^\n]\n", &cmd_num, command) != 2)
...error handling...
else
...use cmd_num and command...
Note the size constraint in the format string to prevent buffer overflow.
Note, too, that you will not know whether the newline is matched. You might be better off using fgets() to read the whole line (or getline()), and then using sscanf() instead of fscanf(); at least you'll know whether the newline was collected (and can gobble to the newline if necessary).
Try this in your fscanf function:
fscanf(fp,"%d\t%[^\n]s",&cmd_num,command);
This will surely work...

Execute code inside an if(0) block [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
A challenge for all the C experts out there:
On a linux machine, you have an already compiled C program, and you are given its source code:
...
char buffer[20];
int code;
gets(buffer);
if(code==1234) ...
...
...
if(0) func();
You don't posses root privileges and the program is read-only. Find a way to execute func. I am certain this is possible, so please don't post any "It isn't possible" answers.
Good luck!
The answer lies in the unchecked buffer overflow that is waiting to happen with gets(buffer); and an understanding of what the stack looks like.
You could try setting the return address to the func() call by overflowing buffer.
http://en.wikipedia.org/wiki/Stack_buffer_overflow#Exploiting_stack_buffer_overflows
It may or may not be possible. If there are no other references to func(), the compiler may well have decided not to generate code for it in the first place -- dead code can be optimized away entirely.
This question is pretty underspecified, anyway. What do you mean by "the program is read-only"? The source code, or the executable? Are we attacking it from inside the process at runtime, inside the process by changing the source code, outside the process by trying to invoke it in funny ways, ...?
If the compiler generated code for the function (i.e. it didn't get chopped by the dead code optimizer) and you have a debugger and debug symbols, just attach a debugger and tell it to find and invoke func().
If you want to exploit the code at runtime, you can cause a buffer overflow in gets(), and take control of the process from inside, but you still have to find func() so you can jump to it -- having the source code won't help you here, and nothing will help you here if the compiler didn't generate code for it.
Since we have the source code, I would make the following edit and recompile:
-if(0) func();
+func();
Serious answer, there are thousands of ways of doing it (hacked environments, buffer overflows, etc), but the common pitfall is that a good compiler should optimize if (0) {} away. If that is the case there would be no way of executing func(). If not, then I would just start up my trusty debugger and jumping to the right spot.
Obviously, execute the famous set 0=1 command before running your executable.
But seriously, this is way off-topic...

why there are some errors in the mex.h? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I opened a c file in Turbo C, the beginning of this c file looks like this:
#include <math.h>
#include <stdio.h>
#include "mex.h"
#include "matrix.h"
It uses some of the matlab structures and do something in c
I checked the directory and am sure that they are right. but when I tried to compile it, some errors here (I just copy some and paste, they look similar):
Error C:\MYFILE~1\TC2.0\MEX.H 1: Illegal character '#' (0x23)
Error C:\MYFILE~1\TC2.0\MEX.H 1: Unexpected end of file in conditional...
Error C:\MYFILE~1\TC2.0\MATRIX.H 1: Expression syntax
What's wrong?? Seems to be errors in these files, but I just copy this files into the correct folders... Thank you!!
Turbo C? Seriously? Its like 20 years old.
(It was a classic, and I loved it... I'm not bad mouthing it. Its just no longer applicable.)
mex.h surely uses aspects now that were not valid in the days of Turbo C.
Either, you need to do some porting work to fix up mex.h and matrix.h, or you need to get a modern compiler (several good ones are free).
Check that the date of the file is something like 1990, not 2010!
Check that the file is plain ASCII (no BOM at the beginning).
Check that the last line ends with a line break.
If you opened and saved the file with some editor, it may have been changed to a format incompatible with Turbo C.
There seems to be a stray '#' on line 1 of MEX.H.
Without seeing the file, it is hard to say what the problem is. It might be that there are blanks before the '#', or a comment - but the line is an otherwise valid #ifdef line. Once upon a long time ago (but possibly when Turbo C was created), C preprocessor directives needed the '#' in column 1.

Resources