Topic : Files and Folders
Author : LUPG
Page : << Previous 8  Next >>
Go to page :


             */

/*
* function: main.
* input:    full path to a file.
* output:   'OK' if permission is granted, or 'ACCESS DENIED' and a
*           detailed explanation if some problem was found.
*/
void
main(int argc, char* argv[])
{
    char* file_path;         /* full path to file.           */
    char* dir_path;         /* full path to a directory.    */
    char* p_slash;         /* location of next '/' in path. */

    /* read command line arguments */
    if (argc != 2 || !argv[1]) {
 fprintf(stderr, "Usage: %s <full file path>\n", argv[0]);
 exit(1);
    }
    file_path = argv[1];
    dir_path = (char*)malloc(strlen(file_path)+1);
    if (!dir_path) {
 fprintf(stderr, "out of memory\n");
 exit(1);
    }

    /* scan all directories in the path and check each of them. */
    p_slash = file_path;
    while ( (p_slash = strchr(p_slash, '/')) != NULL) {
 /* copy directory path (including trailing slash) into dir_path. */
 strncpy(dir_path, file_path, p_slash-file_path+1);
        dir_path[p_slash-file_path+1] = '\0';

 /* check existance and execute permission for this directory. */
 if (access(dir_path, F_OK) == -1) {
     printf("%s: Directory '%s' in the path does not exist.\n",
                   "ACCESS DENIED", dir_path);
     exit(2);
 }
 if (access(dir_path, X_OK) == -1) {
     printf("%s: Directory '%s' in the path - no 'X' permission.\n",
     "ACCESS DENIED", dir_path);
     exit(2);
 }
 /* skip the current slash, so the next strchr() call will find */
 /* the next slash in the file path.                            */
 p_slash++;
    }

    /* all directories in the path exist and are executable.   */
    /* now check existance and read access to the file itself. */
    if (access(file_path, F_OK) == -1) {
        printf("%s: File does not exist.\n", "ACCESS DENIED");
        exit(2);
    }
    if (access(file_path, R_OK) == -1) {
        printf("%s: no read access to file.\n", "ACCESS DENIED");
        exit(2);
    }

    /* all tests passed. */
    printf("OK\n");
}


stdc-small-db.c



/*
* stdc-small-db.c - handle a small database file, with fixed-length records.
* uses standard C library I/O functions, including fopen, fseek, fgetpos,
* fsetpos etc.
*/

#include <stdio.h>              /* standard input/output routines.    */

#define RECORD_LEN 30  /* size of a record.                  */

/*
* function: get_record. Reads a record from the given file.
* input:    pointer to file stream, number of record in file (0 is
*           the first record), and a pointer to a buffer.
* output:   '1' on success, '0' on failure. In case of success, the record
*           is copied to the supplied buffer.
*/
int
get_record(FILE* file, long record_num, char* buf)
{
    long file_pos;    /* stores the original read/write pointer position. */
    int success = 0;  /* success/failure indicator. assume failure here.  */

    /* sanity checks */
    if (!file || record_num < 0 || !buf) {
        return 0;
    }

    /* note the current read/write pointer position.     */
    file_pos = ftell(file);
    if (file_pos < 0) {
        perror("get_record: ftell");
    }
    else {
        /* move to the record's location.                */
        if (fseek(file, record_num * RECORD_LEN, SEEK_SET) < 0) {
            perror("get_record: fseek");
        }
        else {
            /* read the record from the file stream.     */
            if (fread(buf, RECORD_LEN, 1, file) == 1) {
                success = 1;  /* mark that we succeeded in reading the record */
            }
     else {
                perror("get_record: fread");
     }

         /* restore the original read/write pointer position. */
         if (fseek(file, file_pos, SEEK_SET) < 0) {
                perror("get_record: fseek");
         }
        }
    }

    return success;
}

/*
* function: put_record. Writes a record to the given file.
* input:    pointer to file stream, number of the record in file (0 is
*           the first record)  and a pointer to a buffer containing the
*           data to be written.
* output:   '1' on success, '0' on failure.
*/
int
put_record(FILE* file, long record_num, char* buf)
{
    long file_pos;    /* stores the original read/write pointer position. */
    int success = 0;  /* success/failure indicator. assume failure here.  */

    /* sanity checks */
    if (!file || record_num < 0 || !buf) {
        return 0;
    }

    /* note the current read/write pointer position.     */
    file_pos = ftell(file);
    if (file_pos < 0) {
        perror("put_record: ftell");
    }
    else {
        /* move to the record's location.                */
        if (fseek(file, record_num * RECORD_LEN, SEEK_SET) < 0) {
            perror("put_record: fseek");
        }
        else {
            /* write the record to the file stream.      */
            if (fwrite(buf, RECORD_LEN, 1, file) == 1) {
                success = 1;  /* mark that we succeeded in writing the record */
            }
     else {
                perror("put_record: fwrite");
     }

         /* restore the original read/write pointer position. */
         if (fseek(file, file_pos, SEEK_SET) < 0) {
                perror("put_record: ftell");
         }
        }
    }

    return success;
}

/*
* function: main. write and read some records to/from a file.
* input:    none.
* output:   none.
*/
void
main()
{
    char* file_name = "small_db.data";
    char buf[RECORD_LEN];
    FILE*


Page : << Previous 8  Next >>