0

Please bear with me since I'm new to C programming, so here's my code:

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

int main(int argc, char* argv[])
{
    char name[0] = "/tmp/crt/client.conf";
    char name[1] = "/tmp/crt/haha";
    char name[2] = "/tmp/crt/Router.ovpn";
    char name[3] = "/tmp/crt/Router.txt";
    char name[4] = "/tmp/crt/sgdo11-tcpvpn.com-443.ovpn";
    char name[5] = "/tmp/crt/sgdo1-tcpvpn.com-443.ovpn";
    char name[6] = "/tmp/crt/sshdropbear.net-2016-10-22.ovpn";
    char name[7] = "/tmp/crt/sshdropbear.net.txt";
    char name[8] = "/tmp/crt/tcpvpn.txt";
    char name[9] = "/tmp/crt/test15.ovpn";
    char name[10] = "/tmp/crt/test15.txt";
    char name[11] = "/tmp/crt/UDP-1197.ovpn";
    char name[12] = "/tmp/crt/udp.group-1194-exp11nov.ovpn";
    char name[13] = "/tmp/crt/udp.group-1194.txt";

    int def_size[0] = 5874; // FILE: /tmp/crt/client.conf
    int def_size[1] = 1; // FILE: /tmp/crt/haha
    int def_size[2] = 2091; // FILE: /tmp/crt/Router.ovpn
    int def_size[3] = 15; // FILE: /tmp/crt/Router.txt
    int def_size[4] = 5694; // FILE: /tmp/crt/sgdo11-tcpvpn.com-443.ovpn
    int def_size[5] = 2092; // FILE: /tmp/crt/sgdo1-tcpvpn.com-443.ovpn
    int def_size[6] = 1779; // FILE: /tmp/crt/sshdropbear.net-2016-10-22.ovpn
    int def_size[7] = 36; // FILE: /tmp/crt/sshdropbear.net.txt
    int def_size[8] = 33; // FILE: /tmp/crt/tcpvpn.txt
    int def_size[9] = 5855; // FILE: /tmp/crt/test15.ovpn
    int def_size[10] = 26; // FILE: /tmp/crt/test15.txt
    int def_size[11] = 4983; // FILE: /tmp/crt/UDP-1197.ovpn
    int def_size[12] = 1930; // FILE: /tmp/crt/udp.group-1194-exp11nov.ovpn
    int def_size[13] = 31; // FILE: /tmp/crt/udp.group-1194.txt
    for(int i = 0; i < 14; i++) {
        struct stat st;
        stat(name[i], &st);
        int now = st.st_size;
        printf("VERIFYING: %s DEF_SIZE: %d NOW: %d\n", name[i], def_size[i], now);
        if (now != atoi(def_size[i])) {
            printf("(%s) has a different file size from the default one.\n", name[i]);
        }
    }


    return 0;
}

When I compile that, I get:

test.c: In function ‘main’:
test.c:26:17: warning: initializer-string for array of chars is too long
  char name[0] = "/tmp/crt/client.conf";
                 ^~~~~~~~~~~~~~~~~~~~~~
test.c:27:7: error: conflicting types for ‘name’
  char name[1] = "/tmp/crt/haha";
       ^~~~
test.c:26:7: note: previous definition of ‘name’ was here
  char name[0] = "/tmp/crt/client.conf";
       ^~~~
test.c:27:17: warning: initializer-string for array of chars is too long
  char name[1] = "/tmp/crt/haha";

What I am trying to achieve is to verify each file in that array, if it doesn't match the default size then show that printf how should I fix it ?

1
  • 3
    I suggest that you spend some time reading how variable definitions and initializations work in C. Commented Nov 10, 2016 at 10:26

2 Answers 2

7

This:

char name[0] = "/tmp/crt/client.conf";

means "make name be an array of 0 characters, initialized this string of 20 characters". And so on, which is why the compiler is warning.

You meant:

const char *names[] = { "/tmp/crt/client.conf", ... };

Which means "make names be an array of pointers to non-changing character data, initialized to point at these 0-terminated strings". This latter array can be iterated over like you wanted:

for(size_t i = 0; i < sizeof names / sizeof *names; ++i)
{
  printf("name %zu is '%s'\n", i, names[i]);
}

Even better, though, would be to store the names and the sizes together:

const struct {
  const char *name;
  size_t def_size;
} files[] = {
 { "/tmp/crt/client.conf", 5874 },
 { "/tmp/crt/haha", 1 },
 ... and so on ...
};

That would make the stat()-loop easier to write:

for(size_t i = 0; i < sizeof files / sizeof *files; ++i) {
    struct stat st;
    if(stat(files[i].name, &st) == 0) {
      printf("VERIFYING: %s DEF_SIZE: %zu NOW: %zu\n", files[i].name, files[i].def_size, (size_t) st.st_size);
      if (st.st_size != files[i].def_size)
        printf("(%s) has a different file size from the default one.\n", files[i].name);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

if I use your last code const struct { const char *name; then how do I compare the file size ? Sorry I'm new to this programming language, can you also write how to compare them as well ? Thank you
Thank you for clearing that up but I noticed you got some mistakes in files[i].size which should be files[i].def_size and another missing curly bracket in your if statement, anyway thank you :D
@hillz I fixed the typo, not sure what you mean about the bracket though. The innermost if has just a single statement as its body, so the brackets are dropped.
0

Change it to:

char* name[] =
{
    "/tmp/crt/client.conf",
    "/tmp/crt/haha",
    "/tmp/crt/Router.ovpn",
    "/tmp/crt/Router.txt",
    "/tmp/crt/sgdo11-tcpvpn.com-443.ovpn",
    "/tmp/crt/sgdo1-tcpvpn.com-443.ovpn",
    "/tmp/crt/sshdropbear.net-2016-10-22.ovpn",
    "/tmp/crt/sshdropbear.net.txt",
    "/tmp/crt/tcpvpn.txt",
    "/tmp/crt/test15.ovpn",
    "/tmp/crt/test15.txt",
    "/tmp/crt/UDP-1197.ovpn",
    "/tmp/crt/udp.group-1194-exp11nov.ovpn",
    "/tmp/crt/udp.group-1194.txt",
};

int def_size[] =
{
    5874, // FILE: /tmp/crt/client.conf
       1, // FILE: /tmp/crt/haha
    2091, // FILE: /tmp/crt/Router.ovpn
      15, // FILE: /tmp/crt/Router.txt
    5694, // FILE: /tmp/crt/sgdo11-tcpvpn.com-443.ovpn
    2092, // FILE: /tmp/crt/sgdo1-tcpvpn.com-443.ovpn
    1779, // FILE: /tmp/crt/sshdropbear.net-2016-10-22.ovpn
      36, // FILE: /tmp/crt/sshdropbear.net.txt
      33, // FILE: /tmp/crt/tcpvpn.txt
    5855, // FILE: /tmp/crt/test15.ovpn
      26, // FILE: /tmp/crt/test15.txt
    4983, // FILE: /tmp/crt/UDP-1197.ovpn
    1930, // FILE: /tmp/crt/udp.group-1194-exp11nov.ovpn
      31, // FILE: /tmp/crt/udp.group-1194.txt
};

1 Comment

Preferably it should be const char* name[].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.