how to call unix function in c programming?

This is a discussion on how to call unix function in c programming? within the C Programming forums, part of the General Programming Boards category; Hi all, currently, i am writing c program in unix platform. now i need to use some unix functions in ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    8

    Smile how to call unix function in c programming?

    Hi all,
    currently, i am writing c program in unix platform. now i need to use some unix functions in my c program. but i do not know if it is possible. can anyone advise me on this. i need to call an unix function and capture the function's return data.

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Read the manpage of the function, look at the header required and the prototype, call it.

    Example, man 2 fork:
    Code:
    NAME
           fork - create a child process
    
    SYNOPSIS
           #include <sys/types.h>
           #include <unistd.h>
    
           pid_t fork(void);
    Therefore, in your code:
    Code:
    #include <sys/types.h>
    #include <unistd.h>
    
    int main(void)
    {
        pid_t foo;
        foo = fork();
        /* deal with return value */
    }

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Sounds like popen() to me
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Oh, he said function, not process, is he confused?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > i need to call an unix function and capture the function's return data.
    Unless the use of the word 'unix' is completely redundant, in which case getting data back from a function is exactly the same as it is for any other platform.

    The OP should clarify whether they mean functions like fgets()
    Or command line programs like 'ls'
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by cwr
    Oh, he said function, not process, is he confused?
    Ha...we think you know everything, Salem. Surely you know whether or not the OP is confused.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    system()

    this will call whichever command line application you want to specify, with whatever arguments you want to pass it.

    Code:
    system ( ls *.jpg );
    Quote Originally Posted by Jeff Henager
    If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.

  8. #8
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    Quote Originally Posted by Jaqui
    system()

    this will call whichever command line application you want to specify, with whatever arguments you want to pass it.

    Code:
    system ( ls *.jpg );
    You're missing quotes, system accepts a const char * as its parameter.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Not to mention that it's impossible to capture the output of the program run by system(). You'd have to redirect the output to a temp file and then read in the temp file. If the OP is talking about running a program like that then popen() is definitely better than system() for capturing output.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    Thanks all for the response. i think i need to clarify my intention. currently, i am writing programs in unix platform. this workstation is " HP workstation c3750 ". so there is some built-in functions that i can use. for example, BINAND. when i do this: BINAND(10,5), i will get an answer '0'. this function convert the 2 numbers to binary form, AND them together and return the decimal result. so, what i wish to do is, using c programing to call these unix functions and get the result. pls advise me, thanks.

  11. #11
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    So in other words, BINAND(10,5) is like 10 & 5, because & is the C bitwise and operator. You are confused, "BINAND" is not a "unix function", since nobody would implement something that's done using an operator on all sensible languages.

    That said, from google, I see: http://docs.hp.com/cgi-bin/doc3k/B3271590001.10189/212

    Notice that the language is "HP Business BASIC", not C?

  12. #12
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    Quote Originally Posted by cwr
    So in other words, BINAND(10,5) is like 10 & 5, because & is the C bitwise and operator. You are confused, "BINAND" is not a "unix function", since nobody would implement something that's done using an operator on all sensible languages.

    That said, from google, I see: http://docs.hp.com/cgi-bin/doc3k/B3271590001.10189/212

    Notice that the language is "HP Business BASIC", not C?
    thanks for the information. yes, i am trying to call this "HP Business Basic" and used it in my c code. is it possible?

  13. #13
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Highly unlikely. From a look at that manual, it seems you can create C code that can be called from "HP Business BASIC", but not the other way around. Read the manual and/or contact your vendor? This really doesn't have much to do with C, but the specific obscure environment you're working in.

  14. #14
    Registered User
    Join Date
    Jul 2005
    Posts
    8
    thanks for the information. if i want to call the actual unix functions and not the hp business basic, then it will be what u have taught me as below right?

    Quote Originally Posted by cwr
    Read the manpage of the function, look at the header required and the prototype, call it.

    Example, man 2 fork:
    Code:
    NAME
           fork - create a child process
    
    SYNOPSIS
           #include <sys/types.h>
           #include <unistd.h>
    
           pid_t fork(void);
    Therefore, in your code:
    Code:
    #include <sys/types.h>
    #include <unistd.h>
    
    int main(void)
    {
        pid_t foo;
        foo = fork();
        /* deal with return value */
    }

  15. #15
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Yes, calling a "unix function" is like calling any other C function, include the required headers so you have a prototype, then call the function.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading files with a function call
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 04-10-2007, 07:19 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. Unknown C ESP function call error message
    By wbeasl in forum C Programming
    Replies: 1
    Last Post: 10-08-2003, 01:33 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21