Platform independent method to access command line in C? [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
On windows, the programmer could do something like: system("ls > outputFile.txt")
Is there a platform independent way to access the command line, or a least a way to determine which platform the program is being executed on (because calls for the same functionality vary quite a bit)?

The system(3) function is standard ANSI C, it's already platform-independent. Any conforming C implementation will allow you to call it to run the system default command line processor/shell application. Of course, the actual programs you can run will vary from system to system (e.g. dir only works on Windows, while ls usually works on Unix-like platforms).

system() itself is a standard C function defined in stdlib.h. The way it interprets its argument, though, is not standard (e.g. ls in UNIX, dir in Windows/DOS, etc.). If you're really asking whether there's a platform-independent way to list the files in a directory, the answer is (unfortunately) no. Some libraries do provide portable (to some degree) implementations, most notably Boost: How can I get the list of files in a directory using C or C++?

Related

How to find the available POSIX system calls api list for my Linux? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm working on Ubuntu 16.04 and I need to use those functions
int spawnv( mode, path, argv );
int spawnve( mode, path, argv, envp );
int spawnvp( mode, file, argv );
int spawnvpe( mode, file, argv, envp );
I know that they are compiler dependent, so how to find system calls supported by my compiler/system? or how to find my multi-tasking api for processes system calls?
I tried using man spawn + clicking on tab but nothing appears.
The Wikipedia page on Spawn (computing) indicates that the spawn*() functions you reference are from DOS/Windows. They don't have direct analogues in Unix — although they were originally derived from Unix (fork() and exec*()) and adapted to DOS/Windows.
There are no direct analogues to those functions in POSIX. Arguably the nearest approach is posix_spawn() and its multitude of support functions (see the 'SEE ALSO' section on that page for links to the other functions).
I didn't find any similar functions in Linux, even when looking at:
https://man7.org/linux/man-pages/man3
https://man7.org/linux/man-pages/man2
https://linux.die.net/man/2
https://linux.die.net/man/3
Similar functionality can probably be written using fork() (sometimes), exec*() and waitpid() or one of its relatives (sometimes), but it might not be as easy as all that. It depends in part on how exact and complete the emulation functionality has to be.

why we use unix system interface in c programming? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
there is a part about UNIX system functions in R&K C programming guide.
there are system calls like read(), write(), open() or etc ,while we can use scanf(), printf(), fopen() or other c library functions to do the same work.
so what do we need C library functions instead of them?
why do we need to know and use UNIX system functions in C programming while we have that functions in C library?
The functions from the standard library provide your program an operating system independent interface to have the system perform tasks for you. So they are not UNIX specific, although they first appeared with many UNIX implementations because that was the de-facto operating system in those days (not counting IBM360, Honeywell GCOS, and other mainframe/mini OS-es).
Unix has system calls like write() that are used by library function like printf() for formatted and more comfortable usage. printf() is using write() in a certain fashion, and sometimes we would like to use it in another way, that's why it would be helpful to also be familiar with those read(), write() andopen(). That said, most of the time, printf() will be the more suitable choice for writing ,instead of write().
Also, read this
Functions read, write, ... are more primitive and provide direct reading/writing to OS. fread, fwrite, ... implement a more convenient buffered interface to those primitive functions. However, the convenience costs something. For example asynchronous input/output is not available for those functions. Also there is no equivalent to select. If you need those functionalities, you don't have the choice.

How to search for command line arguments in C? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have compiled and run a C code (a lot of files) but I need to understand the physical meaning of the command line arguments.I run code like this
./testmt signal1 3 5 1 1
where
signal1 is the input file
How to search multiple .c files in order to find command line arguments(hopefully with commented lines)?
It is operating system specific. I guess you are on Linux or some other Posix system. Read first Advanced Linux Programming. Read also about globbing. Be aware that your Unix shell is expanding the arguments (and after expansion calling the execve(2) system call....). If you shell is bash (and actually that behavior is mandated by POSIX) read about Shell Operation then about Shell expansions. Read also the Program Arguments chapter of libc documentation. See also my answer on "good habits for designing command line arguments?".
The main function (of signature int main(int argc, char**argv);, and such a [possible] signature is defined by the C standards) - of the program started by execve - gets the positive argument count and a NULL terminated array of strings. So in your case (./testmt signal1 3 5 1 1) you get argc=6 and argv[0] is "./testmt", argv[1] is "signal", argv[2] is "3", etc... argv[5] is the last "1" and argv[6] is NULL.
So dive into your code, and look for main.
PS. AFAIK, on Windows the behavior is probably different. The program (not the shell) is expanding the arguments (actually, probably done in startup files, before they call main). But I know nothing about Windows. See also Operating Systems: Three easy pieces.

C, using F commands in functions such as open() [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Kind of a stupid question but seeing if its possible.
With the f commands you have for example, "w" which would be write to file, create new file if it doesn't exist.
With the O commands its a bit more complex, my research shows its: O_WRONLY|O_TRUNC|O_CREAT
the o commands seems a bit too much to remember oppose to the f command. Is it "good coding" and possible to use W as the flag in an open function
The fopen function is defined in the ISO C standards (C99 7.19.5.3 for example). It is implemented in the C runtime library. As such all compliant C implementations, across all platforms, have to implement it.
The open function is defined in POSIX. It is specific to unix-like platforms.
When you call fopen on a unix-like, POSIX compliant platform, the runtime library inspects the arguments you are supplying and translates them into the corresponding arguments to the POSIX open function.
If you called fopen on another system, such as Windows, the runtime library would be calling a windows specific function to open the file - perhaps OpenFile.
You can't just supply the fopen arguments directly to POSIX open, or to Windows OpenFile for that matter. Those functions don't understand them.
In terms of "good programming" and which of these layers to use: in general, you should avoid directly calling OS system calls (such as the POSIX ones or the windows ones) unless you specifically need to for some reason. The reason is that your program will be more portable if you don't. It will be possible to compile it for any platform which has a compliant C compiler.
On the other hand, if you need some capability or option which is not available in the C Runtime libraries, then you should use the OS system calls.

hard to understand this macro [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
#define __HAVE_ARCH_STRCPY
What's the meaning of __HAVE_ARCH ? I'm not a native speaker and I fail to find the meaning of it by google...(maybe this question is quite silly)
By defining the __HAVE_ARCH_XXXX pre-processor tokens, it allows other locations in the OS kernel to test if the current hardware platform supports the strcpy, memset, etc. functionality. You'll notice that on some platforms, this token is defined, and then a basic implementation of these functions are defined as inline functions along with the token, since on those platforms, the functionality is not provided by some other kernel library or kernel code module. On other platforms, the functions are defined in some other code module, and may be simply declared as extern just after the pre-processor token.
Keep in mind that the kernel itself in Linux does not have access to the standard libc library, so these functions have to be defined separately from what you would typically use in a user-land application that is linked against libc. Thus it's important to define what standard functions are present, and which ones are not, as it may vary from platform-to-platform.
"This architecture has strcpy()".

Resources