Linux only C function help


Results 1 to 9 of 9

Thread: Linux only C function help

  1. #1
    Join Date
    Sep 2003
    Posts
    18

    Question Linux only C function help

    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.

  2. #2
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    813
    isn't there a system() sub that will just send a string to the shell? you could then just use "whoami" etc.
    Please use your spare cpu cycles to help a good cause (its free!!1)... http://folding.stanford.edu

    BSD, Java, and Functional Programming fan.

  3. #3
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    813
    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.
    Please use your spare cpu cycles to help a good cause (its free!!1)... http://folding.stanford.edu

    BSD, Java, and Functional Programming fan.

  4. #4
    Join Date
    Sep 2002
    Posts
    974
    a bit of a hack!? man that **** is ugly

  5. #5
    Join Date
    Jul 2003
    Location
    Scott, Louisiana
    Posts
    206

    Re: Linux only C function help

    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.
    Registered Linux User #328016
    I cna ytpe 300 wrods pre mniute

    Slackware 10.0
    Shuttle AN35N nForce2 mobo
    AMD Athlon XP 2600
    512 MB DDR RAM
    XFX GeForce FX 5200 256 MB
    40 GB Western Digital HDD

  6. #6
    Join Date
    Oct 2003
    Posts
    31
    ^^^ 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.

  7. #7
    Join Date
    Feb 2003
    Location
    London
    Posts
    1,022
    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
    Cheers
    Chris

  8. #8
    Join Date
    Nov 2003
    Posts
    90
    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

  9. #9
    Join Date
    Apr 2001
    Location
    SF Bay Area, CA
    Posts
    14,936
    If you want to read the output of a command that you run:

    http://www.justlinux.com/forum/showt...819#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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •