Question:
reading parameters for c files?
2006-03-29 19:15:58 UTC
I'm attempting to write a C program that will tell me the permissions of a file. I need to know how to read the parameters you enter when you run the file (for example, if my file was permis and i typed 'permis network.c')

...or...

is there a program that will show me the permissions of a fiel?
Four answers:
morgan
2006-03-29 20:12:54 UTC
I'm not a Windows person, so to be conservative I decided

to provide two variations on this program. The first only

uses features provided by ANSI C. This does NOT give you

the ability to extract a numeric set of permissions, but

the function"access()" does let you check for R/W/X and

file existence.





/*

* Check for specific permissions on a file

*/



#include

#include



int

main(int argc, char* argv[])

{

 for (int i=1; argv[i]; i++) {

  if (access(argv[i], R_OK) == 0)

   printf("File %s exists and has read access\n", argv[i]);

  else

   printf("File %s does NOT have read access\n", argv[i]);

 }

 return 0;

}



Sample execution:



 $ a.out /etc/passwd /etc/shadow

 File /etc/passwd exists and has read access

 File /etc/shadow does NOT have read access



This program could be expanded to use the other permission

tests and print out read/write/execute details. That will

be left as an exercise for the reader.



 R_OK Test for read permission.

 W_OK Test for write permission.

 X_OK Test for execute or search permission.

 F_OK Check existence of file



If your system is Posix compliant (and I think Windows is,

but I'm not sure), then you can use the "stat()" function.

This version of the program extracts the file permissions

and prints them in octal format.





#include

#include

#include

#include

#include



int

main(int argc, char* argv[])

{

 struct stat sbuff;



 if (!argv[1])

  fprintf(stderr, "usage: %s file-to-check\n", argv[0]), exit(-1);



 if (stat(argv[1], &sbuff)< 0)

  fprintf(stderr, "stat(%s) failed\n", argv[1]), exit(-1);



 printf("%s \t has mode %12o\n", argv[1], sbuff.st_mode);



 return 0;

}





Sample execution:



 $ cc permis2.c

 $ a.out /etc/passwd

 /etc/passwd has mode 100644

 $ a.out /etc/shadow

 /etc/shadow has mode 100400





To interpret these permissions, look at the lower three

digits. /etc/passwd has mode 644, which gives user,

group, and other permissions of RW-, R--, and R--.

/etc/shadow has mode 400, which gives the corresponding

permissions of R--, ---, and ---.
rj_in_pg
2006-03-29 19:25:11 UTC
You need to use the parameters argc and argv, passed to the main() function. I'd give you a more specific answer, but it's been way too many years since I've written in c (I've been out of programming as a profession for 5 years). But look up main(), argc, and argv.



In the MS world (DOS, Windows command line), the command "attrib" will let you see and set file permissions/attributes. If you want something more than "read-only, read/write, archive, hidden, system", or if you're in *nix, then I'm not sure where to go.
?
2016-10-15 13:42:03 UTC
the first parameter is the call of the textfile that you're reading from. try an entire route if it won't be able to locate the document, you are able to ought to apply double backslash like C:document.txt.
nastymerlin
2006-03-29 19:21:59 UTC
why make it hard for yourself? Just right click on the file and click properties.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...