working -R

This commit is contained in:
elvis
2022-04-29 21:04:45 +02:00
parent 6a7b018f25
commit 59ac9663c9
5 changed files with 69 additions and 38 deletions

View File

@ -747,24 +747,13 @@ int cmd_R(char *numStr, char *dir, int print) {
if(!numStr) // skips the step of converting n from string to int
goto skipGetNumber;
// we copy numStr because we are nice
char *tofree = malloc(strnlen(numStr, MAXARGLENGTH)+1);
if(!tofree) {
perror("malloc");
return -1;
}
strncpy(tofree, numStr, strnlen(numStr, MAXARGLENGTH));
if(strlen(numStr) < 2)
goto skipGetNumber;
char *secondArg = tofree;
strsep_gnu(&secondArg, ",");
if (!secondArg) {
n = -1;
} else if (secondArg[0] == 'n' && secondArg[1] == '=') {
char *number = &secondArg[2];
if (numStr[0] == 'n' && numStr[1] == '=') {
char *number = &numStr[2];
for (int i = 0; i < strlen(number); ++i) {
if (!isdigit(number[i])) {
free(tofree);
errno = EINVAL;
return -1;
}
@ -772,13 +761,10 @@ int cmd_R(char *numStr, char *dir, int print) {
n = (int) strtol(number, NULL, 10);
n = (n==0)?-1:n;
} else {
free(tofree);
errno = EINVAL;
return -1;
}
free(tofree);
skipGetNumber:
if (readNFiles(n, dir) == -1) {
return -1;