Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Friday, November 7, 2014

Programming in C - Before We Get Started

Requirements | Framework | POSIX

Recently I have been spending quite a bit of time learning how to program in C. It has been quite a few years since I have had anything to do with C, spending most of my time in a very different OSI layer entirely. Even when I did come across it some time ago, I was never anything but barely competent - this ignorance on my part has always disturbed me, and so I have endeavored to do something about it.

Currently I am taking a few computer science classes at Harvard University; it is my plan to summarize a portion of what I have taken from those classes concerning the C programming language here on my website for readers who are interested.

For readers with an advanced knowledge of C, these readings could be viewed as refresher courses. The content, although filtered through my humble brain, will be entirely the result of knowledge acquired through Harvard, so my hope is that even for the experienced C hacker there may be something of interest.

For the layman, the content should be easily digestible. We will include plenty of links and such for further reading. And as always, readers are encouraged to leave comments or contact me directly should you have any questions about the material covered here.

In order to get started, you will require a software development environment that allows you to compile and execute programs written in C. If your computer runs Linux - congratulations! You already have this. If you have a Windows computer, you should probably download MinGW, which I explain in greater detail below. Mac users should install xcode, which includes the clang compiler and a variety of other development tools. LLVM is another option for users of a variety of operating systems.

You will also need a compiler. With Windows, installing MinGW takes take of this for you by including the gcc compiler compilation with the installation package. For Linux users, I recommend installing either clang or gcc. The commands to do this depend on your distribution. BSD users should already have clang as part of their OS. I've included a couple of command examples below to install clang, or you can check out more comprehensive documentation through the LLVM project.

#sudo yum install clang

#sudo apt-get install clang

Don't I need a special computer to use C?

No! While I use the Red Hat flavor of Linux as my operating system-of-choice for development, you can just as easily use any old computer that you wish. Windows users, for example, have a variety of options available to them to prepare an environment for C. The two options that I prefer, depending on the project, are Cygwin and MinGW.

What is Cygwin?

The developers of Cygwin describe their project as having two primary components. First, Cygwin contains a large collection of GNU and Open Source tools which provide functionality similar to a Linux distribution on Windows. Second, Cygwin has a DLL (cygwin1.dll) which provides substantial POSIX API functionality. Cygwin provides a huge and diverse amount of functionality through its library of third party downloads (way too much to list here).

What is MinGW?

MinGW stands for "Minimalist GNU for Windows". As the name suggests, MinGW provides a very minimal software development interface for native-Windows applications. If you wish to learn how to program C using nothing other than a Windows computer MinGW is absolutely the tool of choice. This is because MinGW requires no third party libraries or other add-ons to get going; all that MinGW needs is the native C runtime library included with Windows, MSVCRT.DLL, and a few other native Windows DLLs.

MinGW ships with a Windows port of the gcc compiler, which lets you compile not just applications written in C, but also C++, ADA and FORTRAN if you are so inclined (C++ is great, but if you are writing FORTRAN applications in 2014, seek professional help immediately). It gives you a linker so you can write your own libraries, an assembler, and an archive manager. 

In summary - for learning how to program C on your Windows computer (especially if you want to create programs that run natively in Windows), MinGW is the tool to use.

What is POSIX? 

POSIX stands for "Portable Operating System Interface". POSIX is a set of standards for applications to ensure interoperability between operating systems.

In theory, an application that is completely POSIX compliant can be run on any OS that is also POSIX compliant. In practice, very few Operating Systems are entirely POSIX compliant. Those that are fully compliant are not widely used outside of big businesses. Some examples of fully POSIX compliant Operating Systems that you may have used or heard of are: Solaris (originally created by AT&T and Sun Microsystems, now owned by Oracle) and HP-UX (Hewlett Packard's proprietary version of Unix). Other examples that are less widely known are AIX, IRIX, RTEMS and A/UX. As you can see, these operating systems tend to share certain characteristics. They are mostly closed source, owned by massive corporations, and designed to work with proprietary hardware. 

Most commonly used Operating Systems are partially POSIX compliant. Most versions of Linux and BSD fall within this group. 

Windows is partially POSIX compliant through the use of a compatibility feature set or a third party application. Cygwin is the application of choice for Windows POSIX compatibility, but it is not the only option. Other applications that provide similar functionality are UWIN (from AT&T Research Labs) and MKS Toolkit (by MKS Inc).  

Windows users may also opt to install an additional subsystem, rather than an application or API, in order to enable a POSIX-compliant environment. Until Windows 2000, Windows NT users could install the frankly-named "Microsoft POSIX subsystem" to enable partial POSIX compatibility. A now-defunct company, Softway Systems, built on top of the Microsoft POSIX subsystem to develop a fully POSIX compliant environment for Windows called Interix. With the relase of Windows 2000, Microsoft provided the Windows Services for UNIX, or SFU (a sly insult to users, perhaps?), also referred to as the Subsystem for UNIX-based Applications or SUA. 

Services for UNIX changed the game quite a bit for developers using Windows, providing a much more functional and less buggy application interface for developers. This is also the current Microsoft-provided POSIX solution as of Windows Server 2012 / Windows 8 (as of this writing Windows 10 is still pre-alpha). As with Cygwin, Services for UNIX provides a whole heap of functionality beyond a simple POSIX-compatible development environment, almost all of which is outside the scope of our discussion. We may return to SFU in the future, as it deserves its own series of posts and explanations. Users are welcome to install this - just follow the instructions provided by Microsoft.

From here on out we will take for granted that our readers have a POSIX-compliant development environment, including the standard C libraries (stdio.h, for example) and a compiler or . 

Saturday, November 1, 2014

Scratch from MIT & Back to School

As time goes on, having a knowing how to write in a programming language is becoming less of an odd and obscurantist lifestyle choice and more of a necessity for gainful employment. Already, anyone wanting to pursue a career in the hard scientists will be finding themselves either developing or working with custom applications. But even entry-level and intern positions frequently have a "please help us with our website / CMS / database" component to them.

The trouble is, people are terrified of code; even very smart people. It looks like ancient greek. For students of ancient greek it looks like Farsi. For Persian students of the Asiatic classics it looks like, err, English, probably. My point is that going from using the internet for Facebook and using the internet for push requests on Github has a very steep learning curve. So steep that most people fall right the hell off the curve.

Enter Scratch. Scratch is an object oriented programming language developed by the Smarty Pantses at MIT. What makes Scratch unique is that every component of the language is graphical; each variable and operand is a colorful building block that you can stick together in your Browser's Flash player, like puzzle pieces. Each puzzle piece only fits together when the instructions that those pieces represent can follow one another logically.

There is a lot here for the layman to enjoy - plenty of plain English Help documentation, sets of components that are based entirely around easy to understand concepts (like motion, for example), and a surprisingly active community of people who are posting some pretty amazing projects with a very limited array of tools. Some clever folks have even made very limited OS emulators.

I came across Scratch myself because I have started taking a few classes at Harvard. Despite a career working as a systems administrator, when I first attended university almost 15 years ago I was fairly certain that I would end up a humanities teacher. Most of my first go round at school was spent with the classics and modern analytic philosophy

The philosophy bit, while never ceasing to get a laugh out of co-workers, has ended up being directly applicable to my career. Having an in depth Ludwig Wittgenstein's logic tables has been very useful when dealing with arrays, for example. The writers that held the most interest for me were logicians - Boole, Whitehead, Russel; people whose work formed the original building blocks of what is now Computer Science.

Anyway, I'm getting ahead of myself. So - I'm taking some classes at Harvard. Because I was a humanities concentrator, I find myself back at the basics. Intro to Computer Science I, or CS50; which has now overtaken Intro to Economics as the largest class in the entire school. Everyone wants their own f*cking `start-up`, I'm sure.

So the first week of this course had me and about 800 eager young minds getting briefed on MIT's Scratch and writing teeny little scratch applications.

Many, many years ago, when I was a very young lad, I had a very special relationship with computers. Things were much different then. Simply knowing how to operate the thing for any task other than word processing made you a bit special (not always in a good way). I had an acoustic coupler and a toshiba *laptop* with dual floppy drives. I could log onto BBS from pay-phones with my coupler. But before I could do even that, there was "Gorillas" and there was QBASIC. Each line of instructions with its only little number in front so the computer didn't get confused. I pored over each line of Gorillas to see how it worked. I broke it; I fixed it; I changed the color of the Gorillas. I made the bananas go faster.

What I'm getting at here is that early Microsoft crap gets me a bit misty eyed and nostalgic. So for my first week project, I made a clone of Windows 95-era Microsoft paint that I named "simple_paint". The pencil, brush, eraser, paint can and circle tool work; the default is the pencil - just click the icon of the tool you want to use from the toolbar on the left, just a you would in the original MS Paint. All of the colors at the bottom work, and the current color icon works, too. Be gentle, its my very first go at using Scratch, and quite a middling example of what can be accomplished. For example, here is a much better example of how to use Scratch to create a wee graphics application.

As time permits, why not give it a go and see what you can make? You can also "remix" my editor if you think you can do better. Start off by using my code base and go from there (a remix is what they call a branch in the super-cool Scratch community).

This is all highly recommended for teachers and educators, btw. If I had come across this as a kid ... it would have changed things. Scratch is completely unintimidating and the ease with which users can begin assimilating and building onto otherwise complex topics is very cool and very unique.

UPDATE: Getting into Harvard can be tough. That's why I will be posting all of my course notes and projects for my classes online. If you can't make a class, or got course materials online but can't attend lectures, or just missed a lecture, you are welcome to get your notes here!

NSA Leak Bust Points to State Surveillance Deal with Printing Firms

Earlier this week a young government contractor named Reality Winner was accused by police of leaking an internal NSA document to news outle...