A port of src/bin/uuidgen from FreeBSD to OpenBSD. The FreeBSD version
relies on a call to uuidgen(2) in libc, which isn't in OpenBSD. I
re-implemented this in the application itself (as void genuuid) vice in
libc, since I'm not particularly keen to poke around in there. Source is
attached below, and should also be available via cvs:

[email protected]:/cvs
cvs co -P uuidgen

=========== src/bin/uuidgen/uuidgen.c ===========
/* $Id: uuidgen.c,v 1.6 2014/10/18 03:21:06 grobe0ba Exp $ */

/*
 * Copyright (c) 1980, 1990, 1993, 1994
 *      The Regents of the University of California.  All rights reserved.
 * (c) UNIX System Laboratories, Inc.
 * All or some portions of this file are derived from material licensed
 * to the University of California by American Telephone and Telegraph
 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
 * the permission of UNIX System Laboratories, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <stdio.h>
#include <uuid.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>

void check(char *section, uint32_t status)
{

  switch(status)
    {
    case uuid_s_ok:
      break;
    case uuid_s_bad_version:
      printf("%s: uuid_s_bad_version\n", section);
      exit -1;
      break;
    case uuid_s_invalid_string_uuid:
      printf("%s: uuid_s_invalid_string_uuid\n", section);
      exit -1;
      break;
    case uuid_s_no_memory:
      printf("%s: uuid_s_no_memory\n", section);
      exit -1;
      break;
    }
}

void genuuid(FILE *fd)
{
  char *out;
  uuid_t *uuid;
  uint32_t status;

  uuid = malloc(sizeof(uuid_t));
  if(uuid == NULL)
    {
      (void)fprintf(stderr, "uuidgen: Could not malloc\n");
      exit(-1);
    }

  uuid_create(uuid, &status);
  check("uuid_create", status);

  uuid_to_string((const uuid_t*)uuid, &out, &status);
  check("uuid_to_string", status);

  fprintf(fd, "%s\n", out);

  free(out);
  free(uuid);
}

void usage()
{
  fprintf(stderr, "usage: uuidgen [-1] [-n count] [-o filename]\n");
  exit(-1);
}

int main(int argc, char **argv)
{
  int batch_one_at_time,to_file;
  int num_batch=1;
  int ch;
  FILE *fd;

  fd=stdout;

  while((ch = getopt(argc, argv, "1n:o:")) != -1)
    {
      switch(ch)
        {
        case '1':
          batch_one_at_time=true;
          break;
        case 'n':
          num_batch=atoi(optarg);
          break;
        case 'o':
          to_file=true;
          if((fd = fopen(optarg, "w")) == NULL)
            {
              (void)fprintf(stderr, "uuidgen: %s: file error\n", optarg);
            }
          break;
        case '?':
        default:
          usage();
        }
    }
  argc -= optind;
  argv += optind;

  for(int i=0; i<num_batch; i++)
    {
      genuuid(fd);
    }

  if(to_file)
    fclose(fd);

  return 0;
}
=========== end of src/bin/uuidgen/uuidgen.c ===========

=========== Makefile ===========
#       $Id: Makefile,v 1.4 2014/10/18 03:21:06 grobe0ba Exp $

PROG=   uuidgen

.include <bsd.prog.mk>

=========== end of Makefile ===========

-- 
FT2(SS) Byron Grobe, USN
USS Providence (SSN-719)

Reply via email to