how to use ls in a system call in C in linux
system
|
learn how to ask a question. what the hell are you talking about? "use ls"? do u mean, how does ls get its info? man getdents
|
programming
newguy21; not quite sure, but do you have reference to C language or is that what os has as a command within lists (ls). according to my resources ls or -l is directory command, ls gives pattern to match but not
those starting with c on the other (-l) will give long list contents. with proper options defined files will be shown. |
Huh?
|
Code:
xxx:~$ cat mine-ls.c |
Quote:
|
Quote:
Quote:
|
i know, i was assuming he wanted the system call and not the libc func tho. :) hard to give an answer when someone asks such an unclear question.
|
Hi gentlemen,
Infamous41md is right about the risk of buffer overflow in any program which deals with arguments, but the purpose of this simple program is to offer a functional example. Hko is also right; if we just want to read the contents of a directory, nothing is better than opendir(3), readdir(3) and closedir(3). Again Infamous41md is right about the difficulty to answer a question so generic. So, reading the answers already given, I decided to contribute with one more example, one more generic, which could help newguy21. |
not trying to pick on u, but there is not a risk in any program that deals with arguments, only a risk in programs that use functions like strcpy() with user input, or other insecure programming techniques. a simple strncpy() solves the problem in the above code.
|
Quote:
|
I may have misunderstood the question.. but ..how about..
char *cmd="ls -l /path/here/"; system(cmd); or system("ls -l /path/here/"); to read the result.. system("ls -l /path/here/ > file_name"); now the output is in the file.. Rohan |
Quote:
|
Gentlemen,
I'm back here, not to flame about, but to discuss some concepts. I want to discuss about the risk of buffer overflow again. I want to discuss about strcpy. After I read the first reply to my prior post oft today, I started to think if I am sooo wrong? I program C for about 15 years. strcpy is my breakfast, my lunch and my dinner. Is it a risky function? Yes! It is unsafe to cross the street. It is dangerous go out of home. It is dangerous to stay at home. To live is dangerous! I decided to see if someone uses this function. I greped kernel sources (2.4.26). I removed all the comments, defines and references on Makefile. Result: ~1473 calls to strcpy and ~558 to strncpy. The risk of buffer overflow is in the use of strcpy in an uncontrolled environment as in the example I did. I would check the size of the argument passed to the program before accept it. As I am programming, one of my attributions is to specify the proper size of the variable. The unsafe code would be replaced by: Code:
#include <stdio.h> Have a nice week end! |
Quote:
We're not here to bash others, for no reason. If you can't refrain, then move on and ignore, no one is forcing you to reply. If you do reply, I'm asking you now to refrain from posting such utter crap posts in the future. Sure his question was utter crap, but were not here to talk down to anyone, were here to help, not make matters worse. If you replied to every member who asks a question that others might not understand themselves in a rude fashion, well no one would ever come to this site, it would not be as big and helpful as it is now. Is it so hard to just be polite though? Think about it... if you can't and you just want to act like a bitter old perfect question asking member, then maybe you shouldn't respond to the members who can't ask proper questions and only search for the more intelligient question askers.. But this is not your forum and I'm following the rules set by the administrator. I can assure you, he'd rather have people who can't ask decent questions and then have decent people to encourage them to ask better questions in a polite and professional manner than those who respond like you did only most likely to run the person off or make them feel even dumber. So again, if you have a problem, click that button that says email under my name.. or you can email Jeremy the site administrator and owner if you don't like following our simple rules that let me remind you agreed to when registering. Regards. |
Quote:
Code:
ptr = calloc(1, (len = strlen(cmd) + strlen(argv[1]) + 1) ); Code:
char buf[BS] |
All times are GMT -5. The time now is 09:27 PM. |