Table of Contents

Mohsin Malik's spring2016 Opus

Stallman is my hero

Introduction

Hello. I am Mohsin Malik. I am a Computer Science major at CCC and am currently in my second year.

Richard Stallman is my hero.

Systems Programming Journal

January 21, 2016

Systems programming began two days ago and I am very excited to get more in depth with this course. We talked about what exactly we will be doing and stuff like that, the normal stuff.

We got into some actual programming on the first day as well. We wrote a semi working version of cat, which worked but didn't include any of the arguments normal cat would usually include.

It is now the second day and we have learned more about different system calls and different system libraries within a Unix/Linux system. We went over a number of mind-blowing facts and just in general read man pages and the “.h” files.

I am very excited for the different projects we will be doing in the future. From what I have heard we will eventually write chmod and stuff like that, so that will be fun.

February 2, 2016

Today we are continuing the creation of ls.

Last class, we started ls, and got the permissions to show, like the r's and the w's and the x's. That stuff. Today, we are gonna try and finish everything else.

We are starting off with the single number after the perms display. That number represents the number of hard links within the directory. This number can be found under the struct stat, which can be access by doing stat→d_type.

So we ran into a problem with a quick fix. Turns out, we weren't switching directory when we should have. So, this caused some issues.

February 4, 2016

Today we created ls in C. It was a very pleasant experience. It took a bit, but in the end, we got it. 10/10 would create again.

We ended up using the following data types.

  • DIR
  • struct dirent
  • struct state

We used the following functions.

  • scandir
  • stat
  • opendir
  • closedir

The whole process was fairly straightforward, although it required a bit of copying and pasting.

I'm starting to get used to the whole using the man pages for help when programming and using system calls. I think that the idea of having a whole man page section dedicated to system calls in Unix/Linux is an extremely cool idea, and would love to explore it even more.

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
//DIR *opendir(const char *name);
//struct dirent *readdir(DIR *dirp);

int main(int argc, char ** argv) {
        DIR * dirp;
        struct dirent ** aderp;
        struct stat * meh = (struct stat *) malloc(sizeof(struct stat));
        int n;

        dirp = opendir(argv[1]);
        if(dirp == NULL) {
                fprintf(stderr, "Error opening directory '%s'\n", argv[1]);
                exit(1);
        }

        n = scandir(argv[1], &aderp, NULL, alphasort);
        int a;
        for(a = 0; a < n; a++) {
                switch(aderp[a]->d_type) {
                        case DT_BLK:
                                printf("b");
                                break;
                        case DT_CHR:
                                printf("c");
                                break;
                        case DT_DIR:
                                printf("d");
                                break;
                        case DT_FIFO:
                                printf("p");
                                break;
                        case DT_LNK:
                                printf("l");
                                break;
                        case DT_REG:
                                printf("-");
                                break;
                        case DT_SOCK:
                                printf("s");
                                break;
                        case DT_UNKNOWN:
                                printf("!");
                                break;
                }
                stat(aderp[a]->d_name, meh);

                // User mode bits
                if((meh->st_mode & S_IRUSR) > 0) {
                        printf("r");
                } else {
                        printf("-");
                }
                if((meh->st_mode & S_IWUSR) > 0) {
                        printf("w");
                } else {
                        printf("-");
                }
                if((meh->st_mode & S_IXUSR) > 0) {
                        if((meh->st_mode & S_ISUID) > 0) {
                                printf("s");
                        } else {
                                printf("x");
                        }
                } else {
                        if((meh->st_mode & S_ISUID) > 0) {
                                printf("S");
                        } else {
                                printf("-");
                        }
                }

                // Group mode bits
                if((meh->st_mode & S_IRGRP) > 0) {
                        printf("r");
                } else {
                        printf("-");
                }
                if((meh->st_mode & S_IWGRP) > 0) {
                        printf("w");
                } else {
                        printf("-");
                }
                if((meh->st_mode & S_IXGRP) > 0) {
                        if((meh->st_mode & S_ISUID) > 0) {
                                printf("s");
                        } else {
                                printf("x");
                        }
                } else {
                        if((meh->st_mode & S_ISUID) > 0) {
                                printf("S");
                        } else {
                                printf("-");
                        }
                }

                // Other mode bits
                if((meh->st_mode & S_IROTH) > 0) {
                        printf("r");
                } else {
                        printf("-");
                }
                if((meh->st_mode & S_IWOTH) > 0) {
                        printf("w");
                } else {
                        printf("-");
                }
                if((meh->st_mode & S_IXOTH) > 0) {
                        if((meh->st_mode & S_ISUID) > 0) {
                                printf("s");
                        } else {
                                printf("x");
                        }
                } else {
                        if((meh->st_mode & S_ISUID) > 0) {
                                printf("S");
                        } else {
                                printf("-");
                        }
                }
                printf(" name: %s, inode: 0x%x, type: %hhu\n", aderp[a]->d_name, aderp[a]->d_ino, aderp[a]->d_type);
                free(aderp[a]);
        }

        free(aderp);
        closedir(dirp);

        exit(0);
}

February 22, 2016

I created chmod in C using different system calls. It also contains some different arguments using the argument systems which Matt showed up (using getopt).

The hardest part of this project was concerting the char * which contained the permissions into an octal number. This was difficult because I wasn't sure which function to use for base conversions. I ended up finding a function which worked perfectly, called strtol, which converts a char * into a long integer. It can take in one argument, which is the base of the number you want returned. The converted long integer is returned.

To actually change the permissions of the file, I used the chmod system call. It was very easy to use. The chmod function takes in two arguments, a char * which is the path of the file you want to change permissions of and the permissions which you want to apply.

If the -v argument was used, the program will output what the permissions are changed to. I accomplished this using the stat system call, which takes in a char * which is the path of the file you want to view information on and a struct stat *, which is where all of the information is put into.

March 7, 2016

Stuff.

March 15, 2016

Today we are continuing to learn about sockets in C. Sockets are used for cross machine communication over some network. So, for example, if we want computer A to send some sort of message to computer B, sockets would be a good way to go.

The first socket program we will be writing is a program which will communicate with a time server and get the current time.

We successfully wrote the server side program. It was cool.

March 17, 2015

Today we are writing a program called ipaddr.c. Essentially this program will look up the ip addresses of all network interfaces. This program would include the following headers.

  • stdio.h
  • stdlib.h
  • string.h
  • sys/types.h
  • sys/socket.h
  • ifaddrs.h
  • arpa/inet.h

One of the functions we used in the program is getifaddrs(struct ifaddrs **). This function will put into the double pointer information regarding the local machine network interfaces. The struct that this function actually returns is a linked list, where each struct ifaddrs will point to another struct ifaddrs, and so on until we reach NULL. More can be found on the man page.

A lot of this program will revolve around looping through the linked list and pulling information about each network interface. Certain information can then be displayed.

Something cool I learned today. Using calloc will clear the allocated memory with zeros, whereas with malloc, all it would do is return a pointer to some allocated memory without clearing it. So, if you use malloc and find a bunch of weird characters in that region of memory, try using calloc instead.

March 22, 2016

Today we began writing a program which will act as a client for the time server we created two classes ago.

Well that didn't take very long. It's already done. Here are the header files we used.

  • stdio.h
  • stdlib.h
  • strings.h
  • sys/types.h
  • sys/socket.h
  • netinet/in.h
  • netdb.h
  • unistd.h

That was the end of it.

April 5, 2016

We are about to learn about pipes. I have no idea what to expect so hopefully this will be a fun and exciting experience.

So we are currently looking at the pipe man page and the information in this page is a lot. Pipes are very cool.

We also talked about FIFO, or LILO. FIFO stands for “first in first out” whereas LILO stands for “last in last out”. These are also known as queues. We learned about queues in data structures. Essentially, a queue is a data structure in which the first pieces of data put into the queue are the first ones to come out.

We also talked about LIFO and FILO. LIFO stands for “last in first out” and FILO stands for “first in last out”. These terms are associated with a data structure known as a stack. In a stack, the first pieces of data put into the stack are the last ones to come out. This can be viewed as similar to a stack of paper, where only the top sheet of paper (or the last sheet of paper put into the stack) is visible first. To get to the first sheet of paper put into the stack, all the other sheets of paper must be taken out first. Therefore, it is last to come out of the stack.

We just made a program which uses pipes. It's really cool. The program takes in one input, which is a string. Basically, this program will take that string and pipe it from a child process to the main process or something like that.

April 12, 2016

Today we are learning about shared memory. Essentially, with shared memory, different processes can share certain regions of memory to make problem solving a lot easier between processes. Normally, you can't do things like this because memory is protected to prevent some process from corrupting the data of another running process.

Using this idea of shared memory, we have created several programs. The first program simply uses shared memory to access different chars and stuff like that. Everyone in the class had read and write access to a certain file and it was pretty crazy.

The next program we began to create was a (really messy) instant messaging program. Basically, using shared memory, we were able to share messages back and forth.

April 21, 2016

Today we played around with ncurses. Ncurses is a C library which allows the user to play around with the terminal and basically do really cool things with it, including make games. This is how games such as tetris-bsd or hangman are created. Using basic printf statements and stuff like that would take ages to create a game of that calibre, so this library does the hard stuff for us.

April 26, 2016

Today we are playing around with ncurses more. We are working on creating a program which will logically turn one screen into multiple screens, so that we can manipulate it in cool ways.

So far we have made it so that a bunch of dots fill up the screen in three different parts of the screen. A random color will appear in each row and bold dots will show up in the second part of the screen.

We made this neat program with ncurses which takes in user input to display different neat things to the screen. It's cool.

May 3, 2016

I missed class on Thursday unfortunately because yolo. I guess we started doing pacman like things so yea. We are continuing that today.

Portfolio

cprog

unix