Unix System Calls

Hello I need help on writing a program using system calls that changes the permissions on a file. The program can be written in C or C++.

The executable should be called mychmod and should be executed by:

mychmod -u rwx -g rwx -o rwx -U rwx -G rwx -O rwx file1 file2 ...

The lowercase options add permissions and the uppercase options will remove permissions. Each of the switches is optional and should be interpreted as the ones in the Unix command chmod(1).The permissions can be speci?ed as any permutation of the string rwx, and can be split over multiple switches such as:


mychmod -u r -u wx -g rx -G w -O wrx file1 file2 ...

If the command cannot be properly parsed, or if there is no file specifed, it should print a help message to show usage of the command and exit with a status value of 1. As well as send the help message to stderr.


A successful execution of command is indicated by a return status of 0.
That helps a lot i know i wil use that and the stat but the hard thing for me is getting started. Im not very good at files changes. Any more help will be greatly appreciated.
Anyone out there that can help me with this program ???
Is this homework?
Sort of kind of its a program i wanna know how to do because we will eventually get one assigned to us.
closed account (S6k9GNh0)
ajdin1, the GNU command "chmod" is literally synonomous to the function, just in a form that's usable from a more convenient position. It's not hard to utilize the function *at all*. The site that kbw provides gives you step by step instructions on things that can go wrong, how to use the function, and how to verify the result of the function.
I guess im asking i need help on starting the program i understand the chmod function but dont not know how to elaborate on it to do this program ?
closed account (S6k9GNh0)
What is there to not understand?

1
2
3
4
5
int main()
{
    //Call chmod here, check for errors, validate results
    return 0;
}
Is there an example i can find anywhere because this could is suppose to be around 190 lines including
comments. And i just dont see that.
**code
No one's going to do your homework for you. If you need some help with specific aspects we can do that.
No i dont want that i want to understand it.

This is the code i have been working on what i need is help to compile and do what it says in my original post. Im trying to write it in C++. As you can tell i think im having a hard time. This is what i need help with. I thought an example would just help me out to see if i am on the correct path to compiling and running this program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

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

int main(int argc, char *argv[])

// Reads

{
char char_read;
int fd;
if (argc < 2)
{
fprintf(stderr, "readfile: Filename must be specified\n");
exit(EXIT_FAILURE);
}
if ((fd = open(argv[1], O_RDONLY)) < 0) {
perror(argv[1]);
exit(EXIT_FAILURE);
}
while(read(fd, &char_read, 1) > 0)
printf("%c", char_read);
exit(EXIT_SUCCESS);
}

// fgetc()

char char_read;
FILE *fileptr;
if (argc < 2)
{
fprintf(stderr, "readfile: Filename must be specified\n");
exit(EXIT_FAILURE);
}
if ((fileptr = fopen(argv[1], "r")) == NULL)
{
perror(argv[1]);
exit(EXIT_FAILURE);
}
while((char_read = fgetc(fileptr)) != EOF)
printf("%c", char_read);
exit(EXIT_SUCCESS);
}

char mode[] = "";
char buf[] = "";
int i;
i = strtol(mode, 0, 8);
if (chmod (buf,i) < 0)
{
fprint(stderr, "%s: error in chmod(%s, %s) - %d (%s)\n",
argv[0], buf, mode, errno, strerror(errno));
exit(1);
}
return(0);
}
Well, you program is a little unusual in the command line args that it takes. So it's probably best to deal with yourself rather than trying to use getopt.

The arguments are [-[category] [read][write][execute]], where category is u,g,o,U,G,O. BTW, what's the difference between the upper and lower case versions?

So you need to loop thru all arguments expecting that sequence followed by one or more file specifications (like *.cpp).

So perhaps we could start there. Have you any idea how to do that?

As you're using C++, you can make your life a lot easier by using the standard string class for strings rather than trying to use char arrays.

I have no idea why you're trying to open files. Can you explain what you've done?
What i am suppose to do is Unix system calls and functions to change permissions on a file. The lowercase options will add permissions while the uppercase options will remove permissions. This needs to be interpreted as the ones in the Unix Command (chmod). These permissions can be specified as any permutation of the string rwx, and can be split ober multiple switches. If neither of these can be done 1. command cannot properly be phrased, 2. no file specified then i need to print a help msg and and exit. Sending the help msg to stderr. And your right i am not suppose to open files. I think the main goal is when the program compiles and executes the user should be able to write "mychmod the filename" and it should give what ls -l gives and the ability to change it.

Hopefully this makes sense. This is the reason for my confusion.
Ok. Have you any idea how to code the processing of the command line?
No i would need help. My main issue is starting it. Maybe after i would wrap my head around it. Thanks for replying quickly btw.
The command line arguments are available in a standard way to all C/C++ programs via arguments to main.

if you play around with this sample program you can get a feel for how they're passed in.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main(int argc, char* argv[])
{
    for (int i = 0; i < argc; ++i)
    {
        std::cout << i << ":\t" << argv[i] << std::endl;
    }

    return 0;
}


http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
http://wwwx.cs.unc.edu/~sparkst/howto/cpp_main.php
Topic archived. No new replies allowed.