Fun with C

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

Fun with C

#1 Post by 01micko »

Anyone post your C code and snippets here.

I haven't written much in any real programming language but since I studied java in my course it has given me a better understanding of objects, classes and methods, not that C uses all of those but the underlying concepts are similar, not to mention syntax, since C is mother of most modern languages.

Here is something I threw together just to get a better handle on things (I can hear them now.. should he be fixing woof? :lol: ).

This is about the first fully functional app I've written from the ground up. It is just a simple program to get kernel major, minor, sub, sub-sub versions and extra version info that some kernel builders tack onto the end. It supports 2 and 3 series kernels. There are probably bugs and bloat in it, though hopefully I've removed most of that. It can be compiled normally with gcc using shared libs or statically with musl (and probably uclibc, dietlibc etc).

Oh and before anyone says.. "oh but I can do that in one line of shell!".. er, so can I.. (yes it's over 200 lines :shock: )

Attached is the source with configure script too, no it doesn't use automake or anything! It's just easy to add options, (like --enable-static if you have musl. makes 26k bin, upx'd >> 17k bin).

Thanks to Karl Godt for testing and advice.

Tested on slacko, dpups wheezy and sqzd (2.6 kernel) and slackware 32 and 64.

###########################

v0.5 added sys/utsname.h dependency, surprisingly makes static musl build smaller! ..gcc build about the same. system() calls add bloat it seems.
v0.6 removed kernel_min function, kernel_sub fucntion along with dotnum handles it all. Changed dotnum from int to void, changed kernel_sub to accept param based on which option calls it. reworked algorithms to be more robust.
Attachments
ksub-0.6.tar.gz
source code
(2.45 KiB) Downloaded 349 times
Last edited by 01micko on Thu 12 Dec 2013, 12:49, edited 4 times in total.
Puppy Linux Blog - contact me for access

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#2 Post by musher0 »

Fun... with C? Wow, man, you're weird! :lol:
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#3 Post by sunburnt »

I`ve been looking at bits of C code for creating an X window. Lots of code to do so little.
When I get more time I`ll see if I can make it work and then draw on it using the XCB library.
I`ve built a number of controls, most of them I built in Visual Basic a decade and a half ago.

User avatar
mavrothal
Posts: 3096
Joined: Mon 24 Aug 2009, 18:23

Re: Fun with C

#4 Post by mavrothal »

01micko wrote: I'll attach source with configure script too
Dropping the -march=$ARCH flag from configure allowed compilation in 64bit PPC :wink:
Worked too.
== [url=http://www.catb.org/esr/faqs/smart-questions.html]Here is how to solve your[/url] [url=https://www.chiark.greenend.org.uk/~sgtatham/bugs.html]Linux problems fast[/url] ==

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#5 Post by 01micko »

musher0 wrote:Fun... with C? Wow, man, you're weird! :lol:
Thank you ! :D

mavrothal wrote:Dropping the -march=$ARCH flag from configure allowed compilation in 64bit PPC :wink:
Worked too.
Amazing.. had no idea macs use uname.. and that the kernel is delimited by dots, first field has only 1 digit and there are a max of 4 fields (for the dots) and possibly a fifth if delimited by a hyphen.

Next version is more of a test as I am actually using utsname header now, not system call.
Last edited by 01micko on Thu 12 Dec 2013, 12:50, edited 1 time in total.
Puppy Linux Blog - contact me for access

User avatar
mavrothal
Posts: 3096
Joined: Mon 24 Aug 2009, 18:23

#6 Post by mavrothal »

01micko wrote: Amazing.. had no idea macs use uname.. and that the kernel is delimited by dots, first field has only 1 digit and there are a max of 4 fields (for the dots) and possibly a fifth if delimited by a hyphen.
OS X is FreeBSD in the core.

Now I want to see it geting the name of the XOpup/OLPC kernels (2.6.35.13_xo1-20110713.2128.olpc.da7074b_Puppy :shock: ) :lol:
== [url=http://www.catb.org/esr/faqs/smart-questions.html]Here is how to solve your[/url] [url=https://www.chiark.greenend.org.uk/~sgtatham/bugs.html]Linux problems fast[/url] ==

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#7 Post by 01micko »

mavrothal wrote:
01micko wrote: Amazing.. had no idea macs use uname.. and that the kernel is delimited by dots, first field has only 1 digit and there are a max of 4 fields (for the dots) and possibly a fifth if delimited by a hyphen.
OS X is FreeBSD in the core.
Yep, knew that.
mavrothal wrote:Now I want to see it geting the name of the XOpup/OLPC kernels (2.6.35.13_xo1-20110713.2128.olpc.da7074b_Puppy :shock: ) :lol:
it's close but I removed support for "_" delimiter (Karl Godt on the nabble forum :P ) plus there are extra dots and all sorts of crap. I just tried the musl static version actually.. http://01micko.com/packages/ksub-0.5-i6 ... tic.tar.gz .. confirmed runs (with bugs) on olpc.
Puppy Linux Blog - contact me for access

User avatar
Karl Godt
Posts: 4199
Joined: Sun 20 Jun 2010, 13:52
Location: Kiel,Germany

#8 Post by Karl Godt »

cat /proc/sys/kernel/osrelease
2.6.37.4-KRG-i586TSC-64GB-1

CONFIG_LOCALVERSION: │
│ │
│ Append an extra string to the end of your kernel version. │
│ This will show up when you type uname, for example. │
│ The string you set here will be appended after the contents of │
│ any files with a filename matching localversion* in your │
│ object and source tree, in that order. Your total string can │
│ be a maximum of 64 characters.

Difficult to say , I think I need to compile a -rcY kernel to see how that gets appended .
VERSION = 3
PATCHLEVEL = 8
SUBLEVEL = 0
EXTRAVERSION = -rc1
NAME = Terrified Chipmunk

3.8.0.-rc1 OR 3.8.0-rc1 ??

What will happen then : ?
Will it show 2 6 39 41 ??
Or 2 6 39 41stJanuary2014 ?
Or 2 6 39 412014 ?
Attachments
local_version.gif
(16.47 KiB) Downloaded 2169 times
«Give me GUI or Death» -- I give you [[Xx]term[inal]] [[Cc]on[s][ole]] .
Macpup user since 2010 on full installations.
People who want problems with Puppy boot frugal :P

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#9 Post by 01micko »

Code: Select all

[root@xo-4a-b2-3b olpc]# ksub -h
ksub-0.6
Usage :
	-m - shows version level
	-p - shows patchlevel
	-s - shows sublevel
	-n - shows extra name
	-ss - shows sub-sublevel (2 series kernels)

	Only one argument is accepted
	If no argument the kernel version is printed
	If extra name or sub-sublevel are undefined program exits with 0 status
[root@xo-4a-b2-3b olpc]# ksub
2.6.31_xo1.5-20100823.1646.1.olpc.12d64069981699a
[root@xo-4a-b2-3b olpc]# ksub -n
xo1.5-20100823.1646.1.olpc.12d64069981699a
[root@xo-4a-b2-3b olpc]# ksub -m
2
[root@xo-4a-b2-3b olpc]# ksub -p
6
[root@xo-4a-b2-3b olpc]# ksub -s
31
[root@xo-4a-b2-3b olpc]# ksub -ss
[root@xo-4a-b2-3b olpc]# 
binary package << http://01micko.com/packages/ksub-0.6-i6 ... tic.tar.gz 12k (upx'd)

Source in top post.
Puppy Linux Blog - contact me for access

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#10 Post by 01micko »

I seem to make a habit of this:

In the middle of slacko development I usually get a bit bogged down, so I take a break for a few days.. well a change is as good as a holiday they say, so I got into some more fun with C!

I have developed a simple splash message program based on Xlib and Cairo (yay! no GTK!) called funnily enough "cairo-msg" .

I've been wanting to do this ever since yaf-splash's demise due to lack of UTF-8 support. (jamesbond has patched it though I believe, haven't tried). Anyway, this is simpler but just as powerful. Cairo is easy to learn and could be the basis for a lot more small but useful and pretty gui programs.

Rather than explaining here go and check it out on github:

https://github.com/01micko/cairo-msg
Attachments
cairo-msg.jpg
(14.46 KiB) Downloaded 1133 times
Puppy Linux Blog - contact me for access

mcewanw
Posts: 3169
Joined: Thu 16 Aug 2007, 10:48
Contact:

#11 Post by mcewanw »

And here's me been pondering giving up programming in the not too distant future because it takes up so much time! Not that I probably will give up entirely though - every so often I can't help but experiment with some programming idea or other... and have a few current project addons/extension ideas I still want to try out and maybe 'complete' one day... Haven't done any C programming in a long time, though used to be my favourite at system level anyway. I think I'm becoming lazier as I get older, and C programming needs constant practice to remain fluent(ish) at it whereas shell scripting is always easy to pick up again after a long sleep.

William
github mcewanw

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#12 Post by sunburnt »

Hi William and other folks; C is very gritty, but also the most capable.

Consider BaCon ( BAsic CONverter ).
Easy to remember Basic language ( It was easy to go from Visual Basic to it ).
And it produces C binary executable files.
Has GTK based HUG ( Highlevel Universal GUI ) for window apps.

Micko is looking to convert his popup notice to BaCon on it`s forum.

Web site: http://www.basic-converter.org/

Forum: http://basic-converter.proboards.com/
.

User avatar
darkcity
Posts: 2534
Joined: Sun 23 May 2010, 19:16
Location: near here
Contact:

#13 Post by darkcity »

I've worked through a little bit of
learncodethehardway C

For anyone wanting to learn this language I'd recommend it.
http://c.learncodethehardway.org/book/

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#14 Post by technosaurus »

I was just about to start a thread on optimizing your C and best practices. The problem with C is most of the people who use it think they are smart so most of the tutorials are a couple levels past being understandable by commoners. When I've spent a lot of time figuring something out I have to write stuff down in the simplest way possible so _I_ can read it later and not have to re-read it and re-look stuff up.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#15 Post by technosaurus »

01micko wrote:I have developed a simple splash message program based on Xlib and Cairo (yay! no GTK!) called funnily enough "cairo-msg"
https://github.com/01micko/cairo-msg
What, no xcb + cairo-xcb port?
j/k I know how difficult it is to find good xcb examples - though I did find enough to write a very basic xcbmessage program a while back (no fancy cairo based images though)
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#16 Post by 01micko »

technosaurus wrote:What, no xcb + cairo-xcb port?
Rome wasn't built in a day :P
technosaurus wrote:j/k I know how difficult it is to find good xcb examples
This one looks useful as a start.
technosaurus wrote:- though I did find enough to write a very basic xcbmessage program a while back (no fancy cairo based images though)
I remember that! I had a bit of a look at xcb but went xlib because it fell into place ( = more docs and examples, which I referenced in the github readme). The program does what I want and looks ok, contrary to vovchik's "so goddamn ugly" comment. Of course the glam is all cairo.

Q> in your opinion, briefly, what are the advantages of xcb over xlib?
Puppy Linux Blog - contact me for access

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#17 Post by technosaurus »

01micko wrote:Q> in your opinion, briefly, what are the advantages of xcb over xlib?
xlib is ... xlib. xcb is smaller, faster and asynchronous You could have one thread sending requests, one handling responses, another handling errors and another (or a few) handling their callbacks while other threads handle program stuff. With xlib you send a request, wait for a response, handle, repeat. With glibc threads are actually pretty heavy (8MB+ or so each), but musl-libc has really lightweight threads (in the 10s of kb range). Now that quad-core processors with multiple execution units are a virtual standard even in mobile space, xcb _could_ really make a difference especially with openMP and other technologies that make it easier for us to take advantage. A few of us have been using musl pretty extensively and the rough edges are getting much smoother but we haven't really tested the threads much.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#18 Post by 01micko »

Interesting.

For my little venture, a very simple splash, xlib seems to suffice.

But..
technosaurus wrote:xcb is smaller, faster and asynchronous You could have one thread sending requests, one handling responses, another handling errors and another (or a few) handling their callbacks while other threads handle program stuff.
Remember when you had ideas to make icons for a JWM only desktop? What about xcb and cairo for that? Sure, a bit of a project. I don't think musl would support the cairo part though, not without extensive (probably way too much work) patching.
Puppy Linux Blog - contact me for access

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#19 Post by technosaurus »

see goingnuts' patches to idesk, they turned out pretty well.
It would be a shame to use an external program though, jwm already has everything in place to create them... I have even simulated icons using a solid color background and matching no-border trays for each icon (vertically oriented, fixed position with a icon+tooltip on top and optionally text on bottom) We only need a way to set individual tray background colors (probably as a property) and have the color be none (copy from parent)...not opacity as it is, that affects everything on the tray as well. If that happens I'll update my jwm tools to a 1.0 version.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
matiasbatero
Posts: 60
Joined: Fri 12 Oct 2012, 01:27
Location: Mar del Plata, Argentina

#20 Post by matiasbatero »

I'm new in programming. I'm started with C a week ago.

The code, compiles and runs OK.
But it is only a functional structure.

structs, pointers, unions etc.. in order to provide a:
"Readable post-code".
Attachments
str.c.tar
(5 KiB) Downloaded 243 times

Post Reply