Error compiling C program in ubuntu

1
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#define N_BITS 3
// argc : Cantidad de argumentos (Contando ejectuable);
// argv : Array de char* y con los argumentos introducidos;
void main(int argc, char* argv[])
{
    unsigned int i, mask = 0700;
    struct stat buff;
    struct passwd* pwd;
    struct group* grp;
    struct tm* time;
    static char* perm[] = {"---","--x","-w-","-wx","r--","r-x","rw-","rwx"};
    printf("_______________________________________________________________________\n");
    if (argc > 1)
    {
        if ((stat(argv[1], &buff)) != -1)
        {
            printf("Permisos de %s: ", argv[1]);
            for (i=3; i; --i)
            {
                printf("%3s",perm[(buff.st_mode & mask) >> (i-1)*N_BITS]);
                mask >>= N_BITS;
            }
            putchar('\n');
            //Obtiene datos del struct en base al UID obtenido de /etc/passwd
            pwd = getpwuid(buff.st_uid);
            //Obtiene datos del struct en base al GID obtenido de /etc/group
            grp = getgrgid(buff.st_gid);
            printf("Usuario: %s Grupo: %s\n", pwd->pw_name, grp->gr_name);
            time = localtime(&buff.st_atime); / /Obtención de fecha de creación de fichero
            printf("Fecha de creación: %s", asctime(time));
            time = localtime(&buff.st_mtime); //Reinicialización de estructura y obtención de fecha de último acceso
            printf("Fecha de último acceso: %s", asctime(time));
        }
        else
        {
            printf("\nERROR: No se encuentra el fichero/directorio\n");
            exit(1);
        }
        printf("_______________________________________________________________________\n");
    }
    else
    {
        fprintf(stderr, "Usage: %s file_name\n", argv[0]);
    }
}

Well, that's it, I'm doing a simple program but it gives me that error and I'm a little skating.

    
asked by Ald 14.10.2016 в 00:01
source

2 answers

2

Compiling with gcc 4.8.2 gives me the following errors:

10:6: warning: return type of 'main' is not 'int' [-Wmain]
 void main(int argc, char* argv[])
  ^
 In function 'main':
47: error: expected expression before '/' token
         time = localtime(&buff.st_atime); / /Obtención de fecha de creación de fichero
                                           ^
35:47: error: stray '3' in program
35:47: error: stray '3' in program
35:47: error: stray '3' in program
35:47: error: stray '3' in program

Basically you have a bad comment on line 47 and the accented o ( ó ) of creación is not liking the compiler (something already commented as possible problem here ).

Correcting all these problems, the program works normally and shows:

_______________________________________________________________________
Permisos de test.txt: rw-------
Usuario: test Grupo: test
Fecha de creacion: Fri Oct 14 19:21:13 2016
Fecha de último acceso: Fri Oct 14 19:21:12 2016
_______________________________________________________________________

The errors are VERY self-explanatory, you should not have trouble understanding them by taking a little time to analyze them.

    
answered by 14.10.2016 в 12:29
0

Accented characters are not a problem within a comment, the problem is that the comment is not considered as such. That is punctually your mistake. Additionally if main is type int then you must return an integer to the shell. The latter is only a warning but it is not minor since in a certain part you return 1:

#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#define N_BITS 3
// argc : Cantidad de argumentos (Contando ejectuable);
// argv : Array de char* y con los argumentos introducidos;
int main(int argc, char* argv[])
{
    unsigned int i, mask = 0700;
    struct stat buff;
    struct passwd* pwd;
    struct group* grp;
    struct tm* time;
    static char* perm[] = {"---","--x","-w-","-wx","r--","r-x","rw-","rwx"};
    printf("_______________________________________________________________________\n");
    if (argc > 1)
    {
        if ((stat(argv[1], &buff)) != -1)
        {
            printf("Permisos de %s: ", argv[1]);
            for (i=3; i; --i)
            {
                printf("%3s",perm[(buff.st_mode & mask) >> (i-1)*N_BITS]);
                mask >>= N_BITS;
            }
            putchar('\n');
            //Obtiene datos del struct en base al UID obtenido de /etc/passwd
            pwd = getpwuid(buff.st_uid);
            //Obtiene datos del struct en base al GID obtenido de /etc/group
            grp = getgrgid(buff.st_gid);
            printf("Usuario: %s Grupo: %s\n", pwd->pw_name, grp->gr_name);
            time = localtime(&buff.st_atime); // Obtención de fecha de creación de fichero
            printf("Fecha de creación: %s", asctime(time));
            time = localtime(&buff.st_mtime); //Reinicialización de estructura y obtención de fecha de último acceso
            printf("Fecha de último acceso: %s", asctime(time));
        }
        else
        {
            printf("\nERROR: No se encuentra el fichero/directorio\n");
            exit(1);
        }
        printf("_______________________________________________________________________\n");
    }
    else
    {
        fprintf(stderr, "Usage: %s file_name\n", argv[0]);
    }

    exit(0);
}
    
answered by 19.10.2016 в 17:29