Click to See Complete Forum and Search --> : Linux only C function help
Leo TS
09-07-2004, 10:11 PM
Hello,
I just started writing code for Linux. I have been writing code for DOS, and Windows for years. I am looking for something that will assist in writing C/C++ code for Linux, especially Linux only commands (i.e. function to get domain, and logged on user). Also, what app is best to find text strings inside of files in linux. In Windows there is the find function, but I can't find anything that will do the same in Linux.
Thanks for the assistance.
CaptainPinko
09-07-2004, 10:53 PM
isn't there a system() sub that will just send a string to the shell? you could then just use "whoami" etc.
CaptainPinko
09-07-2004, 11:00 PM
just looked it up it's the SYSTEM(3) call. It's from stdlib.h. Just do a man system and look'er up.
I would system (&"whoami > ./iam.txt"); and then just open the file iam.txt read from it and delete the file. It is a bit of a hack but it should work.
Fryguy8
09-08-2004, 12:02 AM
a bit of a hack!? man that **** is ugly :)
duncanbojangles
09-08-2004, 12:16 AM
Originally posted by Leo TS
Also, what app is best to find text strings inside of files in linux. In Windows there is the find function, but I can't find anything that will do the same in Linux.
Thanks for the assistance.
grep. Grep is your friend: Just type in "man grep" at your shell.
mwinterberg
09-08-2004, 12:38 AM
^^^ dang it, that's what I get for trying to find getpwuid
grep(1) will be the best to find text strings inside of files.
To get logged on user would be a two-step process:
first:
getuid(2)
Then:
getpwuid(3)
(I think... it's available on Solaris and FreeBSD, so here's hopin').
Using apropos(1) will be one of your best resources for finding out how to get most of the items. Man pages in sections 2 and 3 will be most helpful.
Just to cover everything, man page sections are shown in parens after the name of the command. So, for example getuid(2) is in section 2, system(3) (from CaptainPinko) is in section 3, and apropos(1) is in section 1.
chrism01
09-08-2004, 05:41 AM
Note that the cmd option to specify a section of the man pages is only required if a reference exists in more than one section and would look like :
man 2 getuid
otherwise
man getuid
is suffcient.
HTH
jim mcnamara
09-08-2004, 03:32 PM
There are several very good books on Unix programming (There is no such thing as a Linux-only api like there is in Windoze for NT and XP)
In Unix there are system calls - this book does a good job
Marc Rochkind 'Advanced UNIX Programming'
Also see:
http://www.advancedlinuxprogramming.com/
Lets you download a free pdf-format book that covers a lot of material.
For general topics like using Unix commands
www.tldp.org
bwkaz
09-08-2004, 06:35 PM
If you want to read the output of a command that you run:
http://www.justlinux.com/forum/showthread.php?s=&postid=301819#post301819
illustrates a VERY good way of doing it. In the default case on the switch, the parent just reads from fd[0] to get the text that the child is printing out.
To figure out what's going on, most of the system calls here are in section 2 of the manpages. There is also read(2) and write(2), if you need them.