Author : LUPG
Page : << Previous 7 Next >>
continue;
}
/* check this directory */
findfile(pattern);
/* finally, restore the original working directory. */
if (chdir("..") == -1) {
fprintf(stderr, "Cannot chdir back to '%s': ", cwd);
perror("");
exit(1);
}
}
}
}
/*
* function: main. find files with a given pattern in their names, in
* a given directory tree.
* input: path to directory to search, and pattern to match.
* output: names of all matching files on the screen.
*/
void
main(int argc, char* argv[])
{
char* dir_path; /* path to the directory. */
char* pattern; /* pattern to match. */
struct stat dir_stat; /* used by stat(). */
/* read command line arguments */
if (argc != 3 || !argv[1] || !argv[2]) {
fprintf(stderr, "Usage: %s <directory path> <file name pattern>\n",
argv[0]);
exit(1);
}
dir_path = argv[1];
pattern = argv[2];
/* make sure the given path refers to a directory. */
if (stat(dir_path, &dir_stat) == -1) {
perror("stat:");
exit(1);
}
if (!S_ISDIR(dir_stat.st_mode)) {
fprintf(stderr, "'%s' is not a directory\n", dir_path);
exit(1);
}
/* change into the given directory. */
if (chdir(dir_path) == -1) {
fprintf(stderr, "Cannot change to directory '%s': ", dir_path);
perror("");
exit(1);
}
/* recursively scan the directory for the given file name pattern. */
findfile(pattern);
}
/*
* rename-log.c - check the size of a given log file in the '/tmp/var/log'
* directory. If it is larger then 1024KB, rename it to
* a have a '.old' suffix and create a new, empty log file.
*
* IMPORTANT NOTE: we intentionally use a fictitious log directory. Do NOT
* use this program against your system's real log directory.
*/
#include <stdio.h> /* standard input/output routines. */
#include <unistd.h> /* access(), etc. */
#include <sys/stat.h> /* stat(), etc. */
#include <fcntl.h> /* open(), close(), etc. */
#include <malloc.h> /* malloc(), etc. */
#include <string.h> /* strcpy(), strcat(). */
#define LOG_DIR "/tmp/var/log" /* full path to the logs directory. */
#define FILE_SIZE 1024*1024 /* size of file to rename - 1024KB. */
#define FILE_SUFFIX ".old" /* suffix for old log file. */
/*
* function: main.
* input: name of a log file.
* output: log file is being renamed if it is too big.
*/
void
main(int argc, char* argv[])
{
char* file_name; /* name of the log file. */
char* new_file_name; /* new name for the log file. */
struct stat file_status; /* status info of the log file. */
int fd; /* descriptor for new file. */
umode_t file_mode; /* permissions of old log file. */
/* read command line arguments */
if (argc != 2 || !argv[1]) {
fprintf(stderr, "Usage: %s <log file name>\n", argv[0]);
exit(1);
}
file_name = argv[1];
new_file_name = (char*)malloc(strlen(file_name)+strlen(FILE_SUFFIX)+1);
if (!new_file_name) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
strcpy(new_file_name, file_name);
strcat(new_file_name, FILE_SUFFIX);
/* check we have read/write permissions to the /tmp/var/log directory. */
if (access(LOG_DIR, F_OK) == -1) {
fprintf(stderr, "Directory '%s' does not exist.\n", LOG_DIR);
exit(1);
}
if (access(LOG_DIR, R_OK | W_OK) == -1) {
fprintf(stderr, "Directory '%s': access denied.\n", LOG_DIR);
exit(1);
}
/* switch current directory to the logs directory. */
if (chdir(LOG_DIR) == -1) {
fprintf(stderr, "Cannot change directory to '%s':", LOG_DIR);
perror("");
exit(1);
}
/* check the size of the file. */
if (stat(file_name, &file_status) == -1) {
fprintf(stderr, "Cannot stat file '%s':", file_name);
perror("");
exit(1);
}
if (file_status.st_size > FILE_SIZE) {
if (rename(file_name, new_file_name) == -1) {
fprintf(stderr, "Cannot rename '%s' to '%s':",
file_name, new_file_name);
perror("");
exit(1);
}
/* create a new, empty log file, with the same access permissions */
/* as the original log file had. */
file_mode = file_status.st_mode & ~S_IFMT;
umask(0);
fd = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, file_mode);
if (fd < 0) {
fprintf(stderr, "Failed creating empty log file:");
perror("");
exit(1);
}
close(fd);
}
}
/*
* read-access-check.c - check if the user has read permission to a given
* file. If not, check where the problem lies.
*
* Background: In order to read from a file, we must have execute permission
* in all directories leading to this file, as well as read access
* to the file itself. For example, to read from file
* "/etc/config/blabla.conf", we must have X permission on '/etc'
* and on '/etc/config', as well as R permission on
* "/etc/config/blabla.conf" itself.
*
* Algorithm: split the file path to its directories, make sure the directory
* exists and that the user has execute permission to all
* directories. Finally check that the file exists, and that the
* user has read permission to the file itself.
*/
#include <stdio.h> /* standard input/output routines. */
#include <unistd.h> /* access(), R_OK etc. */
#include <string.h> /* strchr(), etc. */
#include <malloc.h> /* malloc(), etc.
Page : << Previous 7 Next >>