Using stat() outside of main() - getting incorrect info

For a project, I am use to write a programs that the user provides an inode number and the program returns all information on that file. (OS: Back Track 5 using VMplayer, C code)

I am using the sprintf and popen() function to find the file name based on the inode number. Then, I call a function called inodeData(char* file) and inside that function, I use stat() to gather the information on that file.

When I run the code, the file name is correct (based on the inode) but all the other information is not correct. Almost all examples I have found using stat() take in user input from the main, but I am not doing that in this program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<time.h>

#define COMMAND_LEN 1024
#define DATA_SIZE 1024

int inodeData(char* file)
{
	struct stat sb;
	printf("FILE:\t\t\t%s", file);
	printf("FILE TYPE:\t\t");

	switch (sb.st_mode & S_IFMT)
	{
		case S_IFBLK: printf("BLANK DEVICE\n");		break;
		case S_IFCHR: printf("CHARACTER DEVICE\n");	break;
		case S_IFDIR: printf("DIRECTORY\n");		break;
		case S_IFIFO: printf("SYMLINK\n");		break;
		case S_IFLNK: printf("REGULAR FILE\n");		break;
		case S_IFSOCK: printf("SOCKET\n");		break;
		default:      printf("UNKNOWN\n");		break;
	}

	printf("I-NODE NUMBER:		%ld\n", (long) sb.st_ino);
	printf("MODE:			%lo (octal)\n",
		(unsigned long) sb.st_mode);
	printf("LINK COUNT:		%ld\n", (long) sb.st_nlink);
	printf("OWNERSHIP:		UID=%ld    GID=%ld\n",
		(long) sb.st_uid, (long) sb.st_gid);
	printf("I/O BLOCK SIZE: 	%ld bytes\n",
		(long) sb.st_blksize);
	printf("FILE SIZE:		%lld bytes\n",
		(long long) sb.st_size);
	printf("BLOCKS ALLOCATED:	%lld\n", (long long) sb.st_blocks);
	printf("FILE PERMISSIONS:\t");
	printf((S_ISDIR(sb.st_mode)) ? "d" : "-");
	printf((sb.st_mode & S_IRUSR) ? "r" : "-");
	printf((sb.st_mode & S_IWUSR) ? "w" : "-");
	printf((sb.st_mode & S_IXUSR) ? "x" : "-");
	printf((sb.st_mode & S_IRGRP) ? "r" : "-");
	printf((sb.st_mode & S_IWGRP) ? "w" : "-");
	printf((sb.st_mode & S_IXGRP) ? "x" : "-");
	printf((sb.st_mode & S_IROTH) ? "r" : "-");
	printf((sb.st_mode & S_IWOTH) ? "w" : "-");
	printf((sb.st_mode & S_IXOTH) ? "x" : "-");
	printf("\nLAST STATUS CHANGE:	%s", ctime(&sb.st_ctime));
	printf("LAST FILE ACCESS:	%s", ctime(&sb.st_atime));
	printf("LAST FILE MODIFICATION:	%s\n", ctime(&sb.st_mtime));

	return 0;
}

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

	printf("Enter inode number to be found: ");
	scanf("%s", input);
	iNode = (int)atol(input);

	/*Uses the sprintf function to make a command line command to
	search for the file name based on the provided inode number*/
	sprintf(command, "find . -inum %i -type f", iNode);
	pf = popen(command, "r");

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

	/*Grabs data from process execution*/
	fgets(data, DATA_SIZE, pf);

	/*function call to display file information*/
	inodeData(data);

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

	return 0;
}
1
2
3
4
5
        struct stat sb;
	printf("FILE:\t\t\t%s", file);
	printf("FILE TYPE:\t\t");

	switch (sb.st_mode

You're never actually calling stat()

Another, more subtle, error, is that your "file" contains an extra endline character (that's what fgets does), so even if you call stat(file, &sb);, it will fail.
Last edited on
Topic archived. No new replies allowed.