0

I recently wrote a brute force program to turn the octal data from permissions in Unix into the human readable form (e.g. 755 -> rwxr-xr-x) However whenever I run my program called myperm I don't get a result (e.g. ./myperm 755 -> nothing) and I'm not quite sure why can anyone help with this

#include <stdio.h>
#include <string.h>
int main (int argc, char *argv[])
{
       if (argv[1] == "777")
       {
          printf("rwxrwxrwx");
       }

       else if (argv[1] == "755")
       {
            printf("rwxr-xr-x");
       }

       else if (argv[1] == "750")
       {
            printf("rwxr-x---");
       }
       else if (argv[1] == "700")
       {
            printf("rwxr-x---");
       }
       else if (argv[1] == "666")
       {
            printf("rw-rw-rw");
       }
       else if (argv[1] == "664")
       {
            printf("rw-rw-r--");
       }
       else if (argv[1] == "640")
       {
            printf("rw-r-----");
       }
       else if (argv[1] == "600")
       {
            printf("rw-------");
       }
       else if (argv[1] == "400")
       {
            printf("r--------");
       }
return (0);

}
LPs
  • 16,045
  • 8
  • 30
  • 61
  • 1
    `(argv[1] == "777")` -> `(strcmp(argv[1],"777") == 0)` – Jabberwocky Mar 29 '17 at 09:32
  • `"..."` evaluates to a character array, you need to compare characters, not addresses. – George Mar 29 '17 at 09:33
  • Sidenote: your code is terribly poor: consider to write a function that translates an octal digit to say "rwx" (for 7) otherwise your code will be painfully long and error prone to cover all cases. – Jabberwocky Mar 29 '17 at 09:35

2 Answers2

3

Comparing strings in C doesn't work like this, you have to use strcmp

Instead of doing if (argv[1] == "XXX"), you should do if (strcmp(argv[1], "XXX") == 0)

Read on here

Community
  • 1
  • 1
Dunno
  • 3,632
  • 3
  • 28
  • 43
  • 1
    Since you already found the proper canonical duplicate, just flag/close vote as duplicate rather then posting an answer. – Lundin Mar 29 '17 at 09:35
1
argv[1] == "755"

is not how you compare strings in C, you need to use:

strcmp (argv[1], "755") == 0

In any case, this "brute force" method is both totally unnecessary and unreliable (missing possibilities, only nine of the 512 possibilities are handled, even without taking into account setuid and setgid bits and so on).

You would be better off having a function to evaluate one octal digit, such as:

static const char * permXlat (char octal) {
    switch (octal) {
        case '0' : return "---";
        case '1' : return "--x";
        case '2' : return "-w-";
        case '3' : return "-wx";
        case '4' : return "r--";
        case '5' : return "r-x";
        case '6' : return "rw-";
    }
    return "rwx";
}

and then processing the argument one character at a time:

printf ("%s%s%s\n",
    permXlat (argv[1][0]),
    permXlat (argv[1][1]),
    permXlat (argv[1][2]));

adding whatever sanity checking you think is necessary (argument list size, character values and so on).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 3
    The smug version would be `ch -= '0'; for(uint8_t i = 0; i<3; i++) { result[i] = ch&1<<2-i?i["rwx"]:'-'; } result[3] = '\0';`. This version has... no advantage what-so-ever. But it looks cool. – Lundin Mar 29 '17 at 10:04