-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dirname: implementation of the dirname command
- Loading branch information
1 parent
d5cbe7f
commit 26bd6e1
Showing
3 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
PLATFORM=x86_64-lux | ||
CCFLAGS=-Wall -c -O3 | ||
LDFLAGS=-llux | ||
CC=x86_64-lux-gcc | ||
LD=x86_64-lux-gcc | ||
SRC:=$(shell find . -type f -name "*.c") | ||
OBJ:=$(SRC:.c=.o) | ||
|
||
all: dirname | ||
|
||
%.o: %.c | ||
@echo "\x1B[0;1;32m cc \x1B[0m $<" | ||
@$(CC) $(CCFLAGS) -o $@ $< | ||
|
||
dirname: $(OBJ) | ||
@echo "\x1B[0;1;93m ld \x1B[0m dirname" | ||
@$(LD) $(OBJ) -o dirname $(LDFLAGS) | ||
|
||
install: dirname | ||
@cp dirname ../out/ | ||
|
||
clean: | ||
@rm -f dirname $(OBJ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* luxOS - a unix-like operating system | ||
* Omar Elghoul, 2024-25 | ||
* | ||
* dirname: Implementation of the dirname command | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <limits.h> | ||
|
||
int main(int argc, char **argv) { | ||
if(argc < 2) { | ||
fprintf(stderr, "usage: %s string...\n", argv[0]); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
char new[PATH_MAX]; | ||
const char *old; | ||
|
||
for(int i = 1; i < argc; i++) { | ||
old = argv[i]; | ||
memset(new, 0, PATH_MAX); | ||
|
||
if((strlen(old) == 1) && (old[0] == '/')) { | ||
new[0] = '/'; | ||
goto print; | ||
} | ||
|
||
if((strlen(old) == 2) && (old[0] == '/') && (old[1] == '/')) { | ||
new[0] = '/'; | ||
goto print; | ||
} | ||
|
||
strcpy(new, old); | ||
|
||
for(int j = strlen(new)-1; j >= 0; j--) { | ||
if(new[j] == '/') new[j] = 0; | ||
else break; | ||
} | ||
|
||
if(!strchr(new, '/')) { | ||
sprintf(new, "."); | ||
goto print; | ||
} | ||
|
||
for(int j = strlen(new)-1; j >= 0; j--) { | ||
if(new[j] != '/') new[j] = 0; | ||
else { | ||
new[j] = 0; | ||
break; | ||
} | ||
} | ||
|
||
if(!strlen(new)) sprintf(new, "/"); | ||
|
||
print: | ||
printf("%s\n", new); | ||
} | ||
|
||
return EXIT_SUCCESS; | ||
} |