<commit_before>/**
 * Copyright (c) 2017 <NAME>
 *   All Rights Reserved, Licensed under terms of Lesser GNU Public License version 2.1
 *
 * owner.c - Example of an FSHM_OWNER - 
 *   Creates an fshm mapping and writes data to it.
 *
 *   "guest" is the corrosponding FSHM_GUEST
 *
 *
 * Takes up to two arguments:
 *  
 *  First, if provided, is the stream name. Defaults to DEFAULT_STREAM_NAME below.
 *
 *  Second is number of seconds to sleep before destroying stream. During this sleep time
 *    guests will be able to access the written data.
 *
 */

#include "shmfile.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#include "owner_guest_private.h"

#define DEFAULT_SLEEP_TIME 10


int main(int argc, char* argv[])
{
    FILE *fObj;

    char *streamName;
    int sleepTime;
    OwnerGuestData data = { 0 };

    if ( argc == 1 )
    {
        sleepTime = DEFAULT_SLEEP_TIME;
        streamName = DEFAULT_STREAM_NAME;
    }
    else if ( argc == 2 )
    {
        sleepTime = DEFAULT_SLEEP_TIME;
        streamName = argv[1];
    }
    else if ( argc == 3 )
    {
        sleepTime = atoi(argv[2]);
        streamName = argv[1];
    }
    else
    {
        fprintf(stderr, "Wrong number of arguments.\n\nUsage: owner (optional: [stream name] [sleep time])\n");
        return EINVAL;
    }

    printf("Trying to create fshm stream at:  \"%s\"\n\n", streamName);

    errno = 0;
    fObj = fshm_open(streamName, 0775, FSHM_OWNER);
    if (!fObj)
    {
        if ( errno == EEXIST )
        {
            fprintf(stderr, "-- Existing stream, forcing close.\n\n");
            fshm_force_destroy(streamName);
            fObj = fshm_open(streamName, 0775, FSHM_OWNER);
        }
        else
        {
            goto handle_fobj_null;
        }
    }

    if (!fObj) {
handle_fobj_null:
        fprintf(stderr, "Error! %d: %s\n", errno, strerror(errno));
        return 1;
    }

    strcpy(data.msg, "Hello World!\n");
    data.numAccess = 0;
    data.lastAccessPid = getpid();

    write( fileno(fObj), &data, sizeof(OwnerGuestData) );

    sleep(sleepTime);

    fclose(fObj);


    return 0;
}

/* vim: set ts=4 sw=4 st=4 expandtab : */<commit_msg>Make owner more verbose about what it's doing<commit_after>/**
 * Copyright (c) 2017 <NAME>
 *   All Rights Reserved, Licensed under terms of Lesser GNU Public License version 2.1
 *
 * owner.c - Example of an FSHM_OWNER - 
 *   Creates an fshm mapping and writes data to it.
 *
 *   "guest" is the corrosponding FSHM_GUEST
 *
 *
 * Takes up to two arguments:
 *  
 *  First, if provided, is the stream name. Defaults to DEFAULT_STREAM_NAME below.
 *
 *  Second is number of seconds to sleep before destroying stream. During this sleep time
 *    guests will be able to access the written data.
 *
 */

#include "shmfile.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#include "owner_guest_private.h"

#define DEFAULT_SLEEP_TIME 10


int main(int argc, char* argv[])
{
    FILE *fObj;

    char *streamName;
    int sleepTime;
    OwnerGuestData data = { 0 };

    if ( argc == 1 )
    {
        sleepTime = DEFAULT_SLEEP_TIME;
        streamName = DEFAULT_STREAM_NAME;
    }
    else if ( argc == 2 )
    {
        sleepTime = DEFAULT_SLEEP_TIME;
        streamName = argv[1];
    }
    else if ( argc == 3 )
    {
        sleepTime = atoi(argv[2]);
        streamName = argv[1];
    }
    else
    {
        fprintf(stderr, "Wrong number of arguments.\n\nUsage: owner (optional: [stream name] [sleep time])\n");
        return EINVAL;
    }

    printf("Trying to create fshm stream at:  \"%s\"\n\n", streamName);

    errno = 0;
    fObj = fshm_open(streamName, 0775, FSHM_OWNER);
    if (!fObj)
    {
        if ( errno == EEXIST )
        {
            fprintf(stderr, "-- Existing stream, forcing close.\n\n");
            fshm_force_destroy(streamName);
            fObj = fshm_open(streamName, 0775, FSHM_OWNER);
        }
        else
        {
            goto handle_fobj_null;
        }
    }

    if (!fObj) {
handle_fobj_null:
        fprintf(stderr, "Error! %d: %s\n", errno, strerror(errno));
        return 1;
    }

    printf("Writing data...\n\n");

    strcpy(data.msg, "Hello World!\n");
    data.numAccess = 0;
    data.lastAccessPid = getpid();

    write( fileno(fObj), &data, sizeof(OwnerGuestData) );

    printf("Waiting %d seconds before closing stream.\n\n", sleepTime);

    sleep(sleepTime);

    printf("Removing stream..\n");
    fclose(fObj);


    return 0;
}

/* vim: set ts=4 sw=4 st=4 expandtab : */
{'fraction_non_alphanumeric': 0.12342733188720173, 'fraction_numerical': 0.013015184381778741, 'mean_word_length': 2.370614035087719, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4051878', 'n_tokens_mistral': 1659, 'n_tokens_neox': 1573, 'n_words': 434}