Using system() function in c - c

I want to use the system function to get the number of accounts on windows and on linux.
I have no idea where to look. Please just point me in the right direction.

The system() function runs a program. If you know a command line that does what you need, you can use system() to run that command line.
I'm not sure what command-line program would give the number of accounts on Windows. You could get an approximation by looking at the number of home directories. On Windows the home directories are in \Users and on Linux home directories are in `/home'.
The system() function doesn't capture the output of the program. You would then likely need to run a command line that redirects the program output to a file, then open this file and parse the output.
You would probably have an easier time solving this problem using a language like Python. Python programs are very portable and there are some wrappers for system stuff.
Good luck.

I don't know in Linux, but on windows:
NetUserEnum() or NetQueryDisplayInformation() and ofcourse from the Registry here: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList
PS: system(const char *command) function call executes the command in the command window/terminal.

I used Google to find the following:
Windows:
http://social.msdn.microsoft.com/Forums/en/vcgeneral/thread/8c72b948-d32c-4785-930e-0d6fdf032ecc
Linux:
http://www.linuxforums.org/forum/miscellaneous/29508-list-all-user-accounts-system.html
(search strings: "win32 get user account information", "linux get user account information")
The Linux page gives a command line, so you can put that in your system() call. In Windows though, you don't use system(), you use Win32 API.

For Linux systems you might like to count the number of lines in the file /etc/passwd. This file contains an entry for each user to the system.
To count lines of a text file under Linux the wc command can be used.
Anyway, if you need this info in a C program I propose you take a different approach:
You could open a text file using fopen() and read each line using fgets() until fgets() tells you there are no more lines. Doing so you'll be getting the number of users.

Related

If the paths of .sh file and linux c file are different, then how to use the system command? Seen one ans. on stackoverflow but not clear

I have a .sh file, say B.sh, that calls goal.c (a Linux C file), and goal.c calls A.sh. The paths of goal.c and A.sh are different.
Upon being called by goal.c, A.sh produces some output which will be seen in the same directory as that of A.sh. But I do not see anything as a problem occurs in goal.c calling A.sh using the system command.
I used as the system ("Pathname of A.sh"), but a warning comes as, system() ignored and I feel the command is not executed.
How do I use the system command in this case?

How do I copy everything from my terminal to a file including the stdout and the prompt using C?

I know how to get the stdout into a file using dup/dup2 system calls, but how do I get the entire output that would be normally shown on my terminal(including the prompt that says my username along with the $ symbol and the current working directory) to a file?
Yes you can, but this may be difficult in many details (depending on your expert level). For the shell to behave normally (I would mean exactly as in a terminal), then it needs to interact with a terminal (special system object). So you need to create a program that behave like a terminal, this what pseudo-terminals devices (/dev) are intended for. Read documentation about this to implement it but roughly, your application should behave like the user so should be connected to the slave side of the pseudo-terminal, and the shell to the master side of the pseudo-terminal. Then you can easily log real inputs made by the user and catch outputs made by the shell.
Can't comment cause of low reputation.
I would say there is no way to do that inside a code in C. Instead, you could use bash for example to redirect everything to a file, and leave the code in C as it is.
In this way you have all the info you want to save: prompt, current directory, call to the program (including flags), and of course the output of the program.
Well, you can do:
-For bash prompt PS1: Echo expanded PS1 (in case you want it expanded, if not there is a simple way to do it just echong PS1)
- For executed command: https://unix.stackexchange.com/questions/169259/how-to-capture-command-line-input-into-logfile-and-execute-it-at-the-same-time
- Standard output and error output: Redirect stderr and stdout in a Bash script
And that's all you want to capture, I think.
Look up the script command in Unix systems. If you want to capture all keyboard and std in/out for a command, use the script executable. If you want to see how it's done, look up the source.

Where to physically write commands for input redirection in C?

This might sound like a retarded question but I am just learning C and all the websites I looked at this showed you the command to do it (project < somefile.txt) but not where to do it. Does it go in my project somewhere or command prompt? And if it is command prompt how do I get to where I need to enter it?
It goes on your command prompt. You first need to cd into location where your compiled binaries are. That command basically says "run project and feed contents of somefile.txt into it"
If you're on Windows hit Win+R and run cmd. On UNIX you should find terminal app somewhere in your menu.
Write your C code to use the built in FILE pointers defined in stdio.h. Read from STDIN and write to STDOUT. When you run your command like this: project < somefile.txt, the shell will open somefile.txt as STDIN before your program runs.

Make C Program for Interactive Mode

Here I have one command which is like interactive mode:
obex_test -b $BD_ADDR $CH_NUM
This command is from a script but I want to run this command through a system call in a C program.
obex_test is nothing but obex file transfer library.
Here I want to receive a file from remote device to local device through bluetooth.
This is the manual page of obex_test
Please can anybody tell me how can I put my C program in interactive mode like this command, and I want to use this command also.
I used popen(command,"r") but its not useful; it does not take input from the user.
If I used "w" mode then I don't know what happens; I directly get a message like >Unknown Command. It's the error this command gives when we give different options. So it's taken something as a write mode.
You could have two pairs of pipes (created with the pipe(2) system call); one for data from your program to obex_test's stdin and one from obex_test's stdout to your program. Then you would fork and execve... Beware of deadlocks (your program blocked on writing to obex_test stdin when its output pipe is full and blocking it), you might need to call poll(2) or select(2)...
However, as it man pages explain, "obex_test is a test application for the libopenobex library". So why don't call directly functions inside this libopenobex library, which you would link to your program?
You can use the system command. Check the manual page for more details.
For e.g. system( "obex_test -b 172.16.7.1 1234" );

How to read output and give input to a program from c program?

This is with reference to the below question:
Execute program from within a C program
How do I do the same on Windows with Tiny C Compiler?
I need to execute a .exe fro c program and give input to it from within the same C program by using a file or string as source and read the output from it into a string or file.I have been using system() frunction. Any suggesstions or examples are welcome..Thanks in advance
The simplest way if you don't have popen() etc, or you want to avoid the complexity, is to simplly write a data file eg. infile with fwrite() execute the external program with system() and then read outfile.
system("prog.exe <infile >outfile")
Your prog.exe only has to read stdin and write stdout.
This way you can easily test it with the contents of in/out file.
You would normally do this in your tmp directory and then delete them when you are finished.
The only thing to be careful of is the path to the .exe
Google for "windows popen" and you find this link:
lists.trolltech.com/qt-interest/1999-09/thread00282-0.html
The C runtime library also has _popen(), but I would recommend against it.

Resources