0

Running the following lines of code and running into issues that I am not sure about. The main idea is for a user to enter in an inode to be searched. Once found, the file name associated with that inode is printed, then the command "stat" is ran on that file name to produce info on that file.

#define COMMAND_LEN 1024
#define DATA_SIZE 1024

int main(int argc, char **argv) {
    FILE *pf;
    char command[COMMAND_LEN];
    char data[DATA_SIZE];
    char inode_data[DATA_SIZE];
    int iNode;

    printf("Enter inode: ");
    scanf(argv[0], iNode);

    sprintf(command, "find -inum %i -type f", iNode);
    pf = popen(command, "r");

    if(!pf){
        fprintf(stderr, "Could not open pipe for outlet.\n");
        return;
    }

    fgets(data, DATA_SIZE, pf);
    fprintf(stdout, "%s\n", data);

    sprintf(command, "stat %s", data);
    pf = popen(command, "r");
    fgets(inode_data, DATA_SIZE, pf);
    fprintf(stdout, "%s\n", inode_data);

    if (pclose(pf) != 0)
        fprintf(stderr, "Error: Failed to close command stream!\n");

    Return 0;
}

Once I compile, run, and enter an inode number, I get the following:

Enter inode: 148869

stat: missing operand
Try 'stat --help' for more information.

Error: Failed to close command stream!

Received useful help from perivous question post Save information from sprintf to a variable

Community
  • 1
  • 1
seiryuu10
  • 843
  • 1
  • 10
  • 14
  • 1
    The purpose of the `stat` utility is to give you access to the `stat` system call from the shell. Since you are writing code in C, it makes a lot more sense to just call `stat` instead of spawning a shell to call it for you. `man 2 stat` – William Pursell Feb 09 '13 at 06:43

2 Answers2

2

You should not use scanf with the argv[0] parameter unless you passed a format string in the command line arguments. Also, scanf should pass an int* as the second parameter, so it sould really be scanf("%d", &iNode);

Forhad Ahmed
  • 1,761
  • 13
  • 18
0

If you read the find manual page you see that it needs a path from where to search. You do not have that in your find command line.

You also don't read the input from the user correct. argv[0] is the name of your program. You should pass a proper formatting string, e.g.

scanf("%s", iNode);

If you used a debugger, or even printed out the input after scanf, you should have seen this very quickly.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I originally used sprintf(command, "find / -inum %i -type f", iNode); before, but the search game me the file and anything else in the directory that came close to the inode. – seiryuu10 Feb 09 '13 at 06:36
  • @seiryuu10 Do you have multiple disks mounted? Remember that an inode number is only unique for a single filesystem. Multiple filesystems may have files with the same inode number. – Some programmer dude Feb 09 '13 at 06:38
  • I am running in a VM for this project. So, it is only accessing the directory of what is within the VM. – seiryuu10 Feb 09 '13 at 06:40
  • @seiryuu10 Then it's probably because you don't read the input from the user correctly. – Some programmer dude Feb 09 '13 at 06:42