Skip to content
This repository has been archived by the owner on Nov 27, 2018. It is now read-only.

Vance Leon - Unicorns #184

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ type `make` in the `examples/` directory.) It should print `Testing: PASS`.

Name at least three things that an operating system is responsible for handling?

1. Keeps track of all the running processes on the machine
2. Determines what and when processes are executed through scheduling.
3. establish a User interface for the user to communicate with the machine


## Challenge 2

Write a program in C, `lsls.c`, that prints out a directory listing for the
Expand Down Expand Up @@ -44,6 +49,8 @@ Downloads
src
```



**Hint**: Start by just printing out the contents of the current directory `.`,
and then add the command line parsing later after you have it working.

Expand Down Expand Up @@ -73,10 +80,14 @@ the declarations for `DIR`, `struct dirent`, `opendir()`, `readdir()`, and
_You should check for errors. If there is one, print an error message and exit
(using the `exit()` function)._



* `struct dirent *readdir(DIR *d)`: Reads the next directory entry from the
`DIR*` returned by `opendir()`. Returns the result as a pointer to a `struct
dirent` (see below). Returns `NULL` if there are no more directory entires.



* `closedir(DIR *d)`: Close a directory (opened previously with `opendir()`)
when you're done with it.

Expand Down
Binary file added examples/testdir
Binary file not shown.
Binary file added lsls/lsls
Binary file not shown.
100 changes: 100 additions & 0 deletions lsls/lsls-old.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@

// while (printf("> "), fgets(commandLine, sizeof commandLine, stdin) != NULL) {


// if((dir = opendir(".")) == NULL) {
// perror("Cannot open");
// exit(1);
// }
// while ((dp = readdir(dir)) != NULL) {
// if( errno != 0) {
// perror("Error reading directory");
// }else{
// (void)
// }
// }


// // int pid = fork();
// // if (pid < 0 ) {
// // fprintf(stderr, "Fork Failed \n");
// // exit(1);
// // } else if (pid == 0) {
// // execvp(exec_commands[0], exec_commands);
// // perror("exec");
// // exit(1);
// // } else {
// // wait(NULL);
// // // printf("Parent Process");

// // }





// }


// return 0;
// }



// int main(int argc, char **argv)
// {
// // char path = ;
// int d = opendir("/mnt/c/Users/VLeon/Documents/Code");
// printf("d: %s", d);
// while(d != "\0" || NULL ){
// printf("while loop d: %s", d);
// readdir(d);

// };

// // if ((dir = opendir(".")) == NULL) {
// // perror("Cannot open .");
// // exit(1);
// // }

// // while ((dp = readdir(dir)) != NULL) {

// // }


// return 0;
// }




// int main(int argc, char **argv)
// {
// char commandLine[9892];


// if (pipe(p) < 0) {
// fprintf(stderr, "fork failed\n");
// exit(1);


// int pid = fork();
// }else if (pid == 0) {
// // open

// // read and print



// // printf("Child Process");
// }else {
// wait(NULL);
// // printf("Parent Process");

// }

// return 0;
// }



58 changes: 53 additions & 5 deletions lsls/lsls.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,66 @@
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>

// #define MAX_CMD_LEN 9824
// #define MAX_CMD_ARGS 128

/**
* Main
*/
int main(int argc, char **argv)
{
// steps
// Parse command line

// Open directory

// Repeatly read and print entries

// Close directory



int main(int argc, char **argv)
// int main(void)
{
char *dirname;
// char commandLine[MAX_CMD_LEN];

if (argc == 1){
dirname = ".";
}else if (argc == 2) {
dirname = argv[1];
}else{
fprintf(stderr, "error: lsls [dirname]\n");
return 1;
}


// while (printf("> ", fgets(commandLine, sizeof commandLine, stdin)) != NULL){

DIR *d = opendir(dirname);

if (d == NULL ) {
perror("lsls");
return 1;
}

struct dirent *de;
char fullpath[9812];


while((de = readdir(d)) != NULL ){
sprintf(fullpath, "%s/%s", dirname, de->d_name);
printf("%s\n", de->d_name);
}

int closedir(DIR *de);

// }
return 0;
}


}