Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- int main(int argc, char *argv[])
- {
- int ch;
- char *filename;
- FILE *pf;
- if (argc > 1)
- {
- filename = argv[1];
- }
- else
- {
- printf("Usage: %s <file-name-to-read>\n", argv[0]);
- exit(EXIT_SUCCESS);
- }
- if (chmod(filename, S_IRUSR | S_IWUSR) < 0)
- {
- perror("chmod()");
- exit(EXIT_FAILURE);
- }
- if (access(filename, F_OK) < 0)
- {
- perror("File does not exist");
- exit(EXIT_FAILURE);
- }
- if ((pf = fopen(filename, "r")) == NULL)
- {
- perror("fopen()");
- exit(EXIT_FAILURE);
- }
- fprintf(stderr, "File %s is opened.\n", filename);
- if (chmod(filename, S_IWUSR) < 0)
- {
- perror("chmod()");
- exit(EXIT_FAILURE);
- }
- if (access(filename, R_OK) < 0)
- {
- fprintf(stderr, "File can't be read: %s.\n", strerror(errno));
- errno = 0;
- }
- else
- {
- fprintf(stderr, "File is still readable!\n");
- exit(EXIT_FAILURE);
- }
- while (! feof(pf))
- {
- if ((ch = fgetc(pf)) >= 0)
- {
- putchar(ch);
- }
- }
- fclose(pf);
- fprintf(stderr, "File is successfuly readed!\n");
- if (chmod(filename, S_IRUSR | S_IWUSR) < 0)
- {
- perror("chmod()");
- exit(EXIT_FAILURE);
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement