/*
* Copyright (c) 1999
* Steven G. Kargl.  All rights reserved.
*
* 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.
*
* This software is provided by Steven G. Kargl 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 Steven G. Kargl 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 <stdlib.h>
#include <strings.h>
/*
*  Allocate pointers to pointers to chars, i.e., an array of strings.
*/
char **alloc_strings(int n) {
	char **s;
	s = (char **) malloc((size_t)(n + 1) * sizeof(char *));
	if (!s) {
		fputs("f77: alloc_names: memory allocation\n", stderr);
		exit(1);
	}
	return s;
}
/*
*
*/
char *char_malloc(int n) {
	char *s;
	s = (char *) malloc((size_t) (n + 1) * sizeof(char));
	if (!s) {
		fputs("f77: char_malloc: memory allocation\n", stderr);
		exit(1);
	}
	return s;
}
/*
*  Allocate memory, copy s into memory, and return pointer to memory.
*/
char *mkstring(char *s1) {
	char *s2;
	s2 = char_malloc(strlen(s1));
	strcpy(s2, s1);
	return s2;
}
/*
*  Given s = "path/name.ext", find and return name.
*/
char *extract_name(char *s) {
	char *t, *u, *v;
	t = s;
	u = v = NULL;
	/* Scan for last occurences of '.' and '/' */
	while (*t) {
		if (*t == '/') u = t + 1;
		if (*t == '.') v = t;
		t++;
	}
	
	if (u == NULL && v == NULL) { /* s = "name" */
		t = char_malloc(strlen(s));
		strcpy(t, s);
	} else if (u != NULL && v == NULL) { /* s = "path/name" */
		t = char_malloc(strlen(u));
		strcpy(t, u);
	} else if (u == NULL && v != NULL) { /* s = "name.ext" */
		int i;
		i = (int)(v - s);
		t = char_malloc(i);
		strncpy(t, s, i);
		t[i] = '\0';
	} else { /* s = "path/name.ext" */
		int i;
		i = (int)(v - u);
		t = char_malloc(i);
		strncpy(t, u, i);
		t[i] = '\0';
	}
	return t;
}
/*
*  Combine the name and extension.
*/
char *combine(char *name, char *ext) {
	char *s;
	s = char_malloc(strlen(name) + strlen(ext) + 1);
	strcpy(s, name);
	strcat(s, ".");
	strcat(s, ext);
	return s;
}


syntax highlighted by Code2HTML, v. 0.9.1