loop #2

Merged
cromer merged 11 commits from loop into master 2021-06-19 23:03:40 -04:00
5 changed files with 36 additions and 32 deletions
Showing only changes of commit a7cf487631 - Show all commits

View File

@ -2,7 +2,7 @@ CC=clang
CFLAGS=-Wall -Isrc/include -DDEBUG -g -std=c17 CFLAGS=-Wall -Isrc/include -DDEBUG -g -std=c17
LDFLAGS= LDFLAGS=
FILENAME=myshellin FILENAME=myshellin
SRC=src/myshellin.c src/loop.c src/user.c SRC=src/myshellin.c src/loop.c src/console_line.c
OBJ=$(SRC:.c=.o) OBJ=$(SRC:.c=.o)
all: myshellin all: myshellin

View File

@ -13,8 +13,12 @@
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#define _GNU_SOURCE
#include <limits.h>
#include <pwd.h> #include <pwd.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h> #include <unistd.h>
char *get_user() { char *get_user() {
@ -22,3 +26,23 @@ char *get_user() {
pass = getpwuid(getuid()); pass = getpwuid(getuid());
return pass->pw_name; return pass->pw_name;
} }
char *get_working_directory() {
char *cwd = NULL;
cwd = getcwd(NULL, PATH_MAX);
if (cwd != NULL) {
return cwd;
}
else {
perror("getcwd() error: ");
exit(EXIT_FAILURE);
}
}
void print_input_line() {
char *name = get_user();
char *cwd = get_working_directory();
printf("%s:%s $ ", name, cwd);
free(name);
free(cwd);
}

View File

@ -13,7 +13,9 @@
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef _MYSHELLIN_USER #ifndef _MYSHELLIN_CONSOLE_LINE
#define _MYSHELLIN_USER #define _MYSHELLIN_CONSOLE_LINE
char *get_user(); char *get_user();
char *get_working_directory();
void print_input_line();
#endif #endif

View File

@ -16,7 +16,5 @@
#ifndef _MYSHELLIN_LOOP #ifndef _MYSHELLIN_LOOP
#define _MYSHELLIN_LOOP #define _MYSHELLIN_LOOP
void remove_new_line(char *line); void remove_new_line(char *line);
char *get_working_directory();
void print_input_line();
void loop(); void loop();
#endif #endif

View File

@ -14,39 +14,15 @@
*/ */
#define _GNU_SOURCE #define _GNU_SOURCE
#include <errno.h>
#include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/types.h> #include "console_line.h"
#include <unistd.h>
#include "user.h"
void remove_new_line(char* line) { void remove_new_line(char* line) {
line[strcspn(line, "\n")] = 0; line[strcspn(line, "\n")] = 0;
} }
char *get_working_directory() {
char *cwd = NULL;
cwd = getcwd(NULL, PATH_MAX);
if (cwd != NULL) {
return cwd;
}
else {
perror("getcwd() error: ");
exit(EXIT_FAILURE);
}
}
void print_input_line() {
char *name = get_user();
char *cwd = get_working_directory();
printf("%s:%s $ ", name, cwd);
free(name);
free(cwd);
}
void loop() { void loop() {
size_t buffer_size = 0; size_t buffer_size = 0;
char *line = NULL; char *line = NULL;
@ -57,7 +33,9 @@ void loop() {
if (feof(stdin)) { if (feof(stdin)) {
// the stdin was closed, this usually happens for CTRL-D // the stdin was closed, this usually happens for CTRL-D
printf("\n"); printf("\n");
if (line != NULL) {
free(line); free(line);
}
break; break;
} }
else { else {
@ -72,7 +50,9 @@ void loop() {
remove_new_line(line); remove_new_line(line);
if (strcmp(line, "quit") == 0) { if (strcmp(line, "quit") == 0) {
if (line != NULL) {
free(line); free(line);
}
break; break;
} }
} }