Skip to content

Commit e0feef8

Browse files
committed
Add a help arg
1 parent 416d884 commit e0feef8

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

spwd.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66

77
char* currentPath(short* len);
88
short terminalWidth();
9+
bool isHelp(char* arg);
910
void printShort(short* plen, char* path, short deltaLen);
1011

1112
int main(int argc, char *argv[]) {
1213
short alen = 0, plen = 0; //Init len vars
1314
char* cpath = currentPath(&plen); //Get current path and its lenght
1415

15-
if (argc > 1) alen = atoi(argv[1]); //Get aviable len (if exists)
16-
else alen = terminalWidth();
16+
if (argc > 1) {
17+
if (!isHelp(argv[1])) alen = atoi(argv[1]); //Get aviable len (if exists)
18+
else return 0;
19+
} else alen = terminalWidth(); //Set the terminal widht if it is not a help exec
1720

1821
/* check for the ability to write the path in the available len */
1922
if (plen <= alen) printf(cpath); //Print default path
@@ -34,6 +37,18 @@ short terminalWidth() {
3437
return w.ws_col;
3538
}
3639

40+
bool strcomp(char* str1, char* str2) {
41+
for (short i = 0; str1[i] != '\0' || str2[i] != '\0'; i++)
42+
if (str1[i] != str2[i]) return false;
43+
return true;
44+
}
45+
46+
bool isHelp(char* arg) {
47+
if (!strcomp(arg, "-h") && !strcomp(arg, "--help")) return false;
48+
printf("Usage:\n spwd <max width>\nExample:\n spwd 123");
49+
return true;
50+
}
51+
3752
/* Get all titles' lenght */
3853
short* titleLenghts(char* path, short* size) {
3954
short* buffer = (short*)malloc(512 * sizeof(short));

0 commit comments

Comments
 (0)