LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-14-2008, 12:04 AM   #1
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Rep: Reputation: 70
A question about running functions straight off a terminal


Can you run functions in C straight off a terminal?

For example, I typed in
getuid()
and then I got a ">" prompt
what would I type in after that to return the user's id?

Also a question about this code

// Writing data
if(write(fd, &userid, 4) == -1) // Write user ID before note data
fatal("in main() while writing userid to file");
write(fd, "\n", 1); // Terminate line

I just need to understand the if(write(fd, &userid, 4) == -1) line.

is the 'if' statement checking to see that the fd, hex address of userid or a dereference?? and the int 4 are all not equal to -1? Is this understanding correct? and the last line is just to add a "\n" to the end of the file right?

Also, where does the write function actually take place if there's an 'if' in front of it?

Also, can anyone make a good irc chat room suggestion that I could go to for questions such as these? I'm gonna have a bunch of questions on C.

Last edited by trist007; 12-14-2008 at 12:11 AM.
 
Old 12-14-2008, 12:23 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
the 'if' keyword tests the value of an expression. In this case, the expression is the value of a test for equality, which evaluates to either 0 or non-zero. The values involved in the equality expression are the '-1', and the return value from the write() function. When write() encounters an eror condition, it returns '-1', and sets the value of the global variable 'errno'.

The expression is evaluated by first evaluating the parameters passed to write(), then calling write(), then comparing the return value against '-1'. if(....), simply branches based on the value of the expression, and does not affect the evaluation of the expression.


--- rod.
 
Old 12-14-2008, 12:56 AM   #3
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
that cleared it up, thanks
 
Old 12-14-2008, 01:06 AM   #4
arunmathew1984
Member
 
Registered: Nov 2008
Posts: 31

Rep: Reputation: 15
Try reading the C programming book by Kernighan and Richie - "The C Programming Language"

http://www.amazon.com/Programming-La.../dp/0876925964


That should answer your query and provide you will all the input you are looking for.


Good Luck!


Linux Archive

Last edited by arunmathew1984; 12-20-2008 at 11:22 AM.
 
Old 12-14-2008, 02:42 AM   #5
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
What you are wanting to do is impossible. C code must be compiled into a binary executable. There is however, something that may get you close. There is a tiny compiler called tcc which will let you run such code as shown withotu compiling first. It does a just-in-time compile of the code. Most simply-written C code can then be run as a script by the compiler.

I found something interesting the other day which does much the same ecept it is using gcc. You could do it from the terminal but it would be tedious typing anything useful in that way. But a small bash script can also be used to produce a JIT program:

Code:
#!/bin/bash
echo 'Si, Senor!' | sed 's/^/¡/;s/i/Ã*/;s/n/ñ/' | \
  `f=\`mktemp /tmp/SeñorXXXXXX\`; echo -e '#include<stdio.h>'\\\\n\
  'int main(int argc,char *argv[]){unsigned char *o=
  "\\\\340\\\\075\\\\212\\\\111\\\\102\\\\066\\\\027\\\\321\\\\124\\\\137\\\\010"
  ,c;while(*o&&(c=getchar())!=EOF)putchar(*(o++)^c);putchar(0x0A);
  return(0);}' >${f}.c;rm $f;make $f 2>&1 | grep ' -o ' | sed 's/.*-o //'`
 
Old 12-14-2008, 02:53 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by trist007 View Post
For example, I typed in
getuid()
and then I got a ">" prompt
what would I type in after that to return the user's id?
The shell expected the definition of the function. It is a valid syntax in bash and it's similar to C. The shell has simply interpreted your statement as a new function definition and offered the secondary prompt waiting for completion.
 
Old 12-14-2008, 03:34 AM   #7
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
So then if I wanted I could just search for the getuid() function in the C /usr/includes and just type it in when the ">" prompt appears, hit enter and it should return the userID, gonna try it out.
 
Old 12-14-2008, 04:23 AM   #8
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Shell functions only superficially resemble C, they are not C. You won't be able to find the definition of getuid and type it in as a shell function.

You seem confused about a number of elementary programming concepts. I don't want to be discouraging, as C is still valuable to understand, but perhaps you should start off with another language and come back to C with a better foundation? There are some excellent free textbooks out there for other languages. I would recommend either the Structure and Interpretation of Computer Programs (if you are seriously interested in understanding software), or one of the excellent introductions to programming in Python (Dive into Python, for example).
 
Old 12-14-2008, 04:33 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by trist007 View Post
So then if I wanted I could just search for the getuid() function in the C /usr/includes and just type it in when the ">" prompt appears, hit enter and it should return the userID, gonna try it out.
Nope. I assumed you've understood what gnashley replied. You cannot run C functions in the shell!

The shell is an interpreted language and it has its own syntax, built-in functions and rules. C is a compiled programming language: you write the code in a text file, compile it and then run the resulting executable. That's it.

In the shell you can define your own functions: they resemble the C syntax just for the opening declaration, for example
Code:
my_fun(){
  echo This is my custom shell function.
  return
}
This is the reason why the shell expected further input when you typed getuid(). But it is far from running a C function in the shell environment.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about running a program from terminal window ganoo Linux - Newbie 7 11-01-2010 12:44 PM
Debian 4.0: How to replace GNOME with IceWM? How to boot straight into terminal? linkmaster03 Linux - General 4 06-22-2008 03:55 PM
How to launch an app in terminal, & keep it running after I close terminal? kornerr Linux - General 7 06-24-2006 05:54 PM
concurrent running of functions in c prog k_wjss Programming 5 10-24-2004 09:31 PM
Running functions in the background vexer Programming 6 05-02-2003 01:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration