1

So this is a program that's supposed to categorize files into their file types by making new folders.

suppose you run

./category -f path/to/file1.jpg path/to/file2.c path/to/file3.zip

then the ./category program using -f will categorise and put all of the files that is right next to -f into their existing filetypes.

This has something to do with either Threading, Pipes, Sockets but I couldn't figure out which. Regardless, here's my attempt so far. There's a .zip and after unzipping then I try to attempt to categorise the content.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

void unzipfiles();

int main(int argc, char **argv) {

    pthread_t thread1, thread2;

    unzipfiles();


    for (int i=0; i < sizeof(argv); i++) {

        char *mkdr[3] = {"mkdir", argv[i], NULL};

        execv("/usr/bin/mkdir", mkdr);

        char *mv[3] = {"mv","-v", "*.$argv", "argv/", argv[i], NULL};

        execv("/usr/bin/mv", mv);

    }

    printf("This line will not be executed\n");

    return 0;

}

void unzipfiles() {

    char *unzp[3] = {"unzip", "soal3.zip", NULL};
    execv("/usr/bin/unzip", unzp);

}

There are no Threading, Pipes, or Sockets that I used above. I couldn't yet grasp the concept of using it

My question is, I know you're supposed to get the file types from argv , but how?

And what is -f ?

Thank you very much for your answers

ryyker
  • 22,849
  • 3
  • 43
  • 87
ghifari.akbar
  • 119
  • 1
  • 7
  • Please indent your code. `get the file types from argv, but how?` - Extract the part after the comma from the string. – KamilCuk Mar 31 '20 at 11:25
  • @KamilCuk how do you extract the part after the comma from the string? and what comma are you talking about? – ghifari.akbar Mar 31 '20 at 11:32
  • From your very terse description, the best I can guess is that `-f` is a command line tag that indicates the arguments that follow are files, and your task is to learn to parse the command line using `argc` and `argv[]`. Maybe [this will help](https://stackoverflow.com/questions/9642732/parsing-command-line-arguments-in-c). After you have the files from the command line, you are to determine filetypes of each by looking at its file descriptor. (i.e. its extension) – ryyker Mar 31 '20 at 12:00
  • ...and I think @KamilCuk may have meant `dot` (i.e. `.`) not `comma` (i.e. `,`) – ryyker Mar 31 '20 at 12:03
  • @ryyker It's just so much information and little resources from this subject that it's just ridiculous to solve. So now I'm looking for a way to get the file extensions by extracting the dot part of it. And then after that I'm supposed to use Threading/Pipe/Socket to solve it. But, not to forget about the `execv` commands. Haha, programming – ghifari.akbar Mar 31 '20 at 12:19
  • 1
    `how do you extract the part after the comma from the string?` - Iterate over the characters from the last characters till the first character in the string and extract the position of the dot. Then the part after the dot starts after the position with dot till the end of string. `and what comma are you talking about?` - Yes, I meant a dot, sorry. – KamilCuk Mar 31 '20 at 12:25

2 Answers2

1

From what you describe the program needs to do the following:

1) be executed on the command line with arguments: `-f` and some path\file.ext names
2) use `argc` and `argv`to capture command line arguments   
3) test argv[1] for being `-f` 
3) if `argv[1] == -f`, parse each command line starting from `argv[2]` to determine type.

The following is a very simple example that demonstrates how to do only the most bare-bones tasks listed above:

Note: I tested with the following command line:

prog.exe -f path/to/file1.jpg path/to/file2.c path/to/file3.zip

//usage category -f file1 file2 file3 ...

int main(int argc, char *argv[])
{
    char *tok = NULL;
    char type[80] = {0};

    if(argc == 1) return 0;

    if(strcmp(argv[1], "-f")==0)
    {
        for(int i = 2;i<argc;i++)//start with first arg 
        {
            tok = strtok(argv[i], ".");
            while(tok)
            {
                strcpy(type, tok);
                tok = strtok(NULL, ".");
            }
            printf("Type %d is: %s\n", i-1, type);
        }
    }

    return 0;
}

Results:

Type 1 is: jpg   
Type 2 is: c     
Type 3 is: zip
ryyker
  • 22,849
  • 3
  • 43
  • 87
  • 1
    There is a little known `strrchr`: `tok = strrchr(argv[i], '.')` – KamilCuk Mar 31 '20 at 12:24
  • 1
    Thank you very much for your detailed answer. You don't know how much means to me. It's five hours of non stop errors. – ghifari.akbar Mar 31 '20 at 12:27
  • @KamilCuk - `strrchr` is a very good suggestion. You are right, little known. Thanks! – ryyker Mar 31 '20 at 12:36
  • @ryyker `strtok` will treat a sequence of multiple delimiters as one. So you'll get the same tokens from "asdf.jpg" as you would from ".asdf...jpg", even though they are not the same file name. – Andrew Henle Mar 31 '20 at 12:48
  • @ryyker idk if you still want a crack at it, but I got a Segmentation fault (core dumped) using your code – ghifari.akbar Mar 31 '20 at 15:03
  • @usermax - As stated, the code as presented here is bare-bones. It does not check for all possible combinations of input, and it was tested only with the input you provided. If used as I have presented it, without _any_ changes, it should work. If you have changed the input in such a way that a string is too large for a buffer, then increase the buffer size.... – ryyker Mar 31 '20 at 15:32
  • ...If you use a filename without an extension, then step through the code to see how you might accommodate that scenario. It would also be possible that if you can collect the details of the exact _current_ problem you are seeing in such a way that it can be presented as a [mcve], then you can post that into a new question. – ryyker Mar 31 '20 at 15:32
0

Usage of -f acts as forceful in Linux hence now the command prog.exe -f path/to/file1.jpg path/to/file2.c path/to/file3.zip you are using must forcefully output each of the filetypes accordingly