This is an old revision of the document!
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.
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.
We used the following functions.
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); }