**********************************************************************/
*                                                                    */
*  File:          soundinfo.c                                        */
*  Author:        Andrew W. Scherpbier                               */
*  Version:       1.00                                               */
*  Created:       18 Nov 1992                     		      */
*                                                                    */
*  Copyright (c) 1991, 1992 Andrew Scherpbier                        */
*                All Rights Reserved.                                */
*                                                                    */
*                                                                    */
*--------------------------------------------------------------------*/
*  Description:  Display or modify the info field of an audio        */
*		  file header                                         */
*                                                                    */
**********************************************************************/

include <stdio.h>
include <fcntl.h>
include <multimedia/libaudio.h>
include <multimedia/audio_hdr.h>


define	PUBLIC
define	PRIVATE		static

define	TRUE		(1)
define	FALSE		(0)

define	OK		(0)
define	NOTOK		(-1)

define	when		break;case
define	orwhen		case
define	otherwise	break;default


* Private routines
 * ================
 */
RIVATE void set_info(char *soundfile, char *info);
RIVATE void sound_info(char *soundfile);


* Private variables
 * =================
 */


* Public routines
 * ===============
 */


* Public variables
 * ================
 */


**************************************************************************
 * PUBLIC main(int ac, char **av)
 */
UBLIC main(int ac, char **av)

	int		c;
	int		do_set_info = 0;
	extern int	optind;
	extern char	*optarg;
	char		infobuf[2048];

	while ((c = getopt(ac, av, "i:")) != EOF)
	{
		switch (c)
		{
			when 'i':
				do_set_info++;
				strcpy(infobuf, optarg);
			otherwise:
				fprintf(stderr, "usage: soundinfo [-i info] soundfile ...\n");
				exit(1);
		}
	}

	if (do_set_info && optind < ac)
	{
		set_info(av[optind], infobuf);
		exit(0);
	}

	for (; optind < ac; optind++)
	{
		printf("%24s -- ", av[optind]);
		sound_info(av[optind]);
	}



**************************************************************************
 * PRIVATE void sound_info(char *soundfile)
 * PURPOSE:
 *   Display the info field from the header of this soundfile
 */
RIVATE void sound_info(char *soundfile)

	int		fd;
	char		infop[2048];
	Audio_hdr	hp;

	if (!audio_isaudiofile(soundfile))
	{
		printf("Not an audio file\n");
		return;
	}
	fd = open(soundfile, O_RDONLY);
	if (fd < 0)
	{
		printf("no such file\n");
		return;
	}

	if (audio_read_filehdr(fd, &hp, infop, 2048) == AUDIO_SUCCESS)
	{
		printf("%s\n", infop);
	}
	else
	{
		printf("Invalid sound file header\n");
	}
	close(fd);




**************************************************************************
 * PRIVATE void set_info(char *soundfile, char *info)
 * PURPOSE:
 *   Set the info field of an audio file
 */
RIVATE void set_info(char *soundfile, char *info)

	char		*buf;
	int		fd;
	char		infop[2048];
	Audio_hdr	hp;

	if (!audio_isaudiofile(soundfile))
	{
		fprintf(stderr, "%s is not an audio file\n", soundfile);
		exit(1);
	}
	fd = open(soundfile, O_RDONLY);
	if (fd < 0)
	{
		perror(soundfile);
		exit(1);
	}

	if (audio_read_filehdr(fd, &hp, infop, 2048) != AUDIO_SUCCESS)
	{
		fprintf(stderr, "Invalid sound file header in %s\n", soundfile);
		exit(1);
	}

	/*
	 * Read the whole file into memory
	 */
	buf = (char *)malloc(hp.data_size);
	read(fd, buf, hp.data_size);
	close(fd);

	fd = open(soundfile, O_WRONLY | O_TRUNC);
	if (fd < 0)
	{
		perror(soundfile);
		exit(1);
	}

	if (audio_write_filehdr(fd, &hp, info, strlen(info) + 1) != AUDIO_SUCCESS)
	{
		fprintf(stderr, "Unable to write back the audio file\n");
		exit(1);
	}
	write(fd, buf, hp.data_size);
	close(fd);




