User Tools

Site Tools


blog:spring2016:mmalik1:journal

This is an old revision of the document!


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.

blog/spring2016/mmalik1/journal.1458057461.txt.gz · Last modified: 2016/03/15 15:57 by mmalik1