Warningi z gcc format ‘%ld’ expects argument of type ‘long int’, but argument...

0

Cześć,
mam taki kod:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>

void show_stat_info(char *fname, struct stat *buf);
void show_nice_stat_info(char *fname, struct stat *buf);
void mode_translator(mode_t mode, char* str);

int main(int argc, char *argv[]) {
    struct stat info;
    if (argc > 1) {
        if (stat(argv[1], &info) != -1) {
            show_nice_stat_info(argv[1], &info);
            return 0;
        } else {
            printf("Nie znaleziono pliku %s\n", argv[1]);
        }
    } else {
        printf("Uzycie: %s nazwa_pliku\n", argv[0]);
    }

    return 1;
}

void show_stat_info(char *fname, struct stat *buf) {
    printf("   mode: %o\n", buf->st_mode); /* type + mode */
    printf("  links: %d\n", buf->st_nlink); /* # links     */
    printf("   user: %d\n", buf->st_uid); /* user id     */
    printf("  group: %d\n", buf->st_gid); /* group id    */
    printf("   size: %ld\n", buf->st_size); /* file size   */
    printf("accesst: %ld\n", buf->st_atim); /* actualized    */
    printf("modtime: %ld\n", buf->st_mtim); /* modified    */
    printf("changed: %ld\n", buf->st_ctim); /* changed    */
}

void show_nice_stat_info(char *fname, struct stat *buf) {
    printf("File name:  %s\n", fname);

    if (S_ISDIR(buf->st_mode)) {
        printf("File type:  directory\n");
    } else if (S_ISLNK(buf->st_mode)) {
        printf("File type:  link\n");
    } else if (S_ISREG(buf->st_mode)) {
        printf("File type:  normal\n");
    } else {
        printf("File type:  unknow\n");
    }

    char mode[9];
    mode_translator(buf->st_mode, mode);
    printf("Mode:       %s (%o)\n", mode, buf->st_mode);
    printf("Links:      %d\n", buf->st_nlink); /* # links     */
    
    struct passwd *user_info = getpwuid(buf->st_uid); 
    printf("UID:        %s (%d)\n", user_info->pw_name, buf->st_uid); /* user id     */
    
    struct group *group_info = getgrgid(buf->st_gid);
    printf("GID:        %s (%d)\n", group_info->gr_name, buf->st_gid); /* group id    */

    printf("Size:       %ld\n", buf->st_size); /* file size   */
    
    printf("Accesed:    %ld\n", buf->st_atim); /* actualized    */
    printf("Modified:   %ld\n", buf->st_mtim); /* modified    */
    printf("Changed:    %ld\n", buf->st_ctim); /* changed    */
    
}

void mode_translator(mode_t mode, char* str) {
    strcpy(str, "--------");

    if (mode & S_IRUSR) str[0] = 'r';
    if (mode & S_IWUSR) str[1] = 'w';
    if (mode & S_IXUSR) str[2] = 'x';

    if (mode & S_IRGRP) str[3] = 'r';
    if (mode & S_IWGRP) str[4] = 'w';
    if (mode & S_IXGRP) str[5] = 'x';

    if (mode & S_IROTH) str[6] = 'r';
    if (mode & S_IWOTH) str[7] = 'w';
    if (mode & S_IXOTH) str[8] = 'x';
}

I gdy kompiluję to z -Wallem to dostaję:

@ubuntu:~/Lab3/Zad2$ gcc -Wall Zad2.c -o Zad2
Zad2.c: In function ‘show_stat_info’:
Zad2.c:34:5: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 2 has type ‘struct timespec’ [-Wformat=]
     printf("accesst: %ld\n", buf->st_atim); /* actualized    */
     ^
Zad2.c:35:5: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 2 has type ‘struct timespec’ [-Wformat=]
     printf("modtime: %ld\n", buf->st_mtim); /* modified    */
     ^
Zad2.c:36:5: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 2 has type ‘struct timespec’ [-Wformat=]
     printf("changed: %ld\n", buf->st_ctim); /* changed    */
     ^
Zad2.c: In function ‘show_nice_stat_info’:
Zad2.c:65:5: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 2 has type ‘struct timespec’ [-Wformat=]
     printf("Accesed:    %ld\n", buf->st_atim); /* actualized    */
     ^
Zad2.c:66:5: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 2 has type ‘struct timespec’ [-Wformat=]
     printf("Modified:   %ld\n", buf->st_mtim); /* modified    */
     ^
Zad2.c:67:5: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 2 has type ‘struct timespec’ [-Wformat=]
     printf("Changed:    %ld\n", buf->st_ctim); /* changed    */
     ^

Pomoże ktoś usunąć te warningi? :(

1

No chyba jasne co tu się dzieje, nie?

0

Hehe co się dzieje to ja wiem tylko jak to zmienić bez przepisywania całego kodu? :)

0

Rzutować to jakoś??

0

W jaki sposób chcesz wczytać strukturę w C? I jeszcze jako long int?
Nie wiem czy rzutować, pokaż jak wyglądają te struktury.

0

Poniżej cały plik stat.h z którego korzystam:

#ifndef __ASM_GENERIC_STAT_H
#define __ASM_GENERIC_STAT_H

/*
 * Everybody gets this wrong and has to stick with it for all
 * eternity. Hopefully, this version gets used by new architectures
 * so they don't fall into the same traps.
 *
 * stat64 is copied from powerpc64, with explicit padding added.
 * stat is the same structure layout on 64-bit, without the 'long long'
 * types.
 *
 * By convention, 64 bit architectures use the stat interface, while
 * 32 bit architectures use the stat64 interface. Note that we don't
 * provide an __old_kernel_stat here, which new architecture should
 * not have to start with.
 */

#include <asm/bitsperlong.h>

#define STAT_HAVE_NSEC 1

struct stat {
	unsigned long	st_dev;		/* Device.  */
	unsigned long	st_ino;		/* File serial number.  */
	unsigned int	st_mode;	/* File mode.  */
	unsigned int	st_nlink;	/* Link count.  */
	unsigned int	st_uid;		/* User ID of the file's owner.  */
	unsigned int	st_gid;		/* Group ID of the file's group. */
	unsigned long	st_rdev;	/* Device number, if device.  */
	unsigned long	__pad1;
	long		st_size;	/* Size of file, in bytes.  */
	int		st_blksize;	/* Optimal block size for I/O.  */
	int		__pad2;
	long		st_blocks;	/* Number 512-byte blocks allocated. */
	long		st_atime;	/* Time of last access.  */
	unsigned long	st_atime_nsec;
	long		st_mtime;	/* Time of last modification.  */
	unsigned long	st_mtime_nsec;
	long		st_ctime;	/* Time of last status change.  */
	unsigned long	st_ctime_nsec;
	unsigned int	__unused4;
	unsigned int	__unused5;
};

/* This matches struct stat64 in glibc2.1. Only used for 32 bit. */
#if __BITS_PER_LONG != 64 || defined(__ARCH_WANT_STAT64)
struct stat64 {
	unsigned long long st_dev;	/* Device.  */
	unsigned long long st_ino;	/* File serial number.  */
	unsigned int	st_mode;	/* File mode.  */
	unsigned int	st_nlink;	/* Link count.  */
	unsigned int	st_uid;		/* User ID of the file's owner.  */
	unsigned int	st_gid;		/* Group ID of the file's group. */
	unsigned long long st_rdev;	/* Device number, if device.  */
	unsigned long long __pad1;
	long long	st_size;	/* Size of file, in bytes.  */
	int		st_blksize;	/* Optimal block size for I/O.  */
	int		__pad2;
	long long	st_blocks;	/* Number 512-byte blocks allocated. */
	int		st_atime;	/* Time of last access.  */
	unsigned int	st_atime_nsec;
	int		st_mtime;	/* Time of last modification.  */
	unsigned int	st_mtime_nsec;
	int		st_ctime;	/* Time of last status change.  */
	unsigned int	st_ctime_nsec;
	unsigned int	__unused4;
	unsigned int	__unused5;
};
#endif

#endif /* __ASM_GENERIC_STAT_H */
0

Problem rozwiązany!
Po prostu źle odnosiłem się do elementów tej struktury - zła nazwa :-) Zamiat "time" napisałem w kodzie "tim" i to był cały problem!

Ale mam podobny problem z drugim kodem i nie mogę dojść jak to poprawić?

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <pwd.h>
#include <stdlib.h>

void show_stat_info(char *fname, struct stat *, char* mode);
void mode_to_letters(int mode, char *str);

int main(int argc, char *argv[]) {
    struct stat info;
    int opcja;

    if (argc != 3) {
        fprintf(stderr, "Sposob uzycia %s -argument <nazwa_pliku>\n", argv[0]);
        return EXIT_FAILURE;
    }


    opcja = getopt(argc, argv, ":r:w:x:");

    if (opcja < 0) {
        fprintf(stderr, "Nie podano argumentu\n");
        return EXIT_FAILURE;
    }

    stat(argv[2], &info);


    switch (opcja) {
        case 'r':
            if (info.st_mode & S_IRUSR) {
                printf("Wlasciciel +\n");
            } else {
                printf("Wlasciciel -\n");
            }
            if (info.st_mode & S_IRGRP) {
                printf("Grupa      +\n");
            } else {
                printf("Grupa      -\n");
            }
            if (info.st_mode & S_IROTH) {
                printf("Inni       +\n");
            } else {
                printf("Inni       -\n");
            }
            break;
        case 'w':
            if (info.st_mode & S_IWUSR) {
                printf("Wlasciciel +\n");
            } else {
                printf("Wlasciciel -\n");
            }
            if (info.st_mode & S_IWGRP) {
                printf("Grupa      +\n");
            } else {
                printf("Grupa      -\n");
            }
            if (info.st_mode & S_IWOTH) {
                printf("Inni       +\n");
            } else {
                printf("Inni       -\n");
            }
            break;
        case 'x':
            if (info.st_mode & S_IXUSR) {
                printf("Wlasciciel +\n");
            } else {
                printf("Wlasciciel -\n");
            }
            if (info.st_mode & S_IXGRP) {
                printf("Grupa      +\n");
            } else {
                printf("Grupa      -\n");
            }
            if (info.st_mode & S_IXOTH) {
                printf("Inni       +\n");
            } else {
                printf("Inni       -\n");
            }
            break;
    }
    return EXIT_SUCCESS;
}
@ubuntu:~/Lab3/Zad3$ gcc -Wall Zad3.c -o Zad3
Zad3.c: In function ‘main’:
Zad3.c:21:5: warning: implicit declaration of function ‘getopt’ [-Wimplicit-function-declaration]
     opcja = getopt(argc, argv, ":r:w:x:");
     ^
0

#include <getopt.h>

0

Wszystko pięknie działa tylko mam jeszcze jeden problem z kodem z pierwszego postu w tym temacie...
Mianowicie data ostatniego dostępu, modyfikacji i zmiany pliku wyświetlana jest dziesiętnie... a chciałbym żeby było to w takim "normalnym" formacie... jak to zmienić?
Coś z time.h ?

1 użytkowników online, w tym zalogowanych: 0, gości: 1