1327Sjkh/* 2228990Suqs * FreeBSD install - a package for the installation and maintenance 3327Sjkh * of non-core utilities. 4327Sjkh * 5327Sjkh * Redistribution and use in source and binary forms, with or without 6327Sjkh * modification, are permitted provided that the following conditions 7327Sjkh * are met: 8327Sjkh * 1. Redistributions of source code must retain the above copyright 9327Sjkh * notice, this list of conditions and the following disclaimer. 10327Sjkh * 2. Redistributions in binary form must reproduce the above copyright 11327Sjkh * notice, this list of conditions and the following disclaimer in the 12327Sjkh * documentation and/or other materials provided with the distribution. 13327Sjkh * 14327Sjkh * Jordan K. Hubbard 15327Sjkh * 18 July 1993 16327Sjkh * 17327Sjkh * Miscellaneous string utilities. 18327Sjkh * 19327Sjkh */ 20327Sjkh 2193520Sobrien#include <sys/cdefs.h> 2293520Sobrien__FBSDID("$FreeBSD$"); 2393520Sobrien 24327Sjkh#include "lib.h" 25327Sjkh 26327Sjkhchar * 2784745Ssobomaxstrconcat(const char *s1, const char *s2) 2811780Sjkh{ 2911780Sjkh static char tmp[FILENAME_MAX]; 3011780Sjkh 3111780Sjkh tmp[0] = '\0'; 3281571Sobrien strncpy(tmp, s1 ? s1 : s2, FILENAME_MAX); /* XXX: what if both are NULL? */ 3311780Sjkh if (s1 && s2) 3411780Sjkh strncat(tmp, s2, FILENAME_MAX - strlen(tmp)); 3511780Sjkh return tmp; 3611780Sjkh} 3711780Sjkh 384546Sjkh/* Get a string parameter as a file spec or as a "contents follow -" spec */ 394546Sjkhchar * 40327Sjkhget_dash_string(char **str) 41327Sjkh{ 42327Sjkh char *s = *str; 43327Sjkh 44327Sjkh if (*s == '-') 45115325Slioux *str = copy_string_adds_newline(s + 1); 46327Sjkh else 478087Sjkh *str = fileGetContents(s); 48327Sjkh return *str; 49327Sjkh} 50327Sjkh 514546Sjkh/* Rather Obvious */ 52327Sjkhchar * 5384745Ssobomaxcopy_string(const char *str) 54327Sjkh{ 5581571Sobrien return (str ? strdup(str) : NULL); 56327Sjkh} 57327Sjkh 58115325Slioux/* Rather Obvious but adds a trailing \n newline */ 59115325Sliouxchar * 60115325Sliouxcopy_string_adds_newline(const char *str) 61115325Slioux{ 62115325Slioux if (str == NULL) { 63115325Slioux return (NULL); 64115325Slioux } else { 65115325Slioux char *copy; 66115325Slioux size_t line_length; 67115325Slioux 68115325Slioux line_length = strlen(str) + 2; 69115325Slioux if ((copy = malloc(line_length)) == NULL) 70115325Slioux return (NULL); 71115325Slioux memcpy(copy, str, line_length - 2); 72115325Slioux copy[line_length - 2] = '\n'; /* Adds trailing \n */ 73115325Slioux copy[line_length - 1] = '\0'; 74115325Slioux 75115325Slioux return (copy); 76115325Slioux } 77115325Slioux} 78115325Slioux 79327Sjkh/* Return TRUE if 'str' ends in suffix 'suff' */ 80327SjkhBoolean 8184745Ssobomaxsuffix(const char *str, const char *suff) 82327Sjkh{ 83327Sjkh char *idx; 84327Sjkh Boolean ret = FALSE; 85327Sjkh 8667429Sjkh idx = strrchr(str, '.'); 87327Sjkh if (idx && !strcmp(idx + 1, suff)) 88327Sjkh ret = TRUE; 89327Sjkh return ret; 90327Sjkh} 91327Sjkh 92327Sjkh/* Assuming str has a suffix, brutally murder it! */ 93327Sjkhvoid 94327Sjkhnuke_suffix(char *str) 95327Sjkh{ 96327Sjkh char *idx; 97327Sjkh 9867429Sjkh idx = strrchr(str, '.'); 99327Sjkh if (idx) 100327Sjkh *idx = '\0'; /* Yow! Don't try this on a const! */ 101327Sjkh} 102327Sjkh 103327Sjkh/* Lowercase a whole string */ 104327Sjkhvoid 105327Sjkhstr_lowercase(char *str) 106327Sjkh{ 107327Sjkh while (*str) { 108327Sjkh *str = tolower(*str); 109327Sjkh ++str; 110327Sjkh } 111327Sjkh} 11274699Ssobomax 11374699Ssobomaxchar * 11474699Ssobomaxget_string(char *str, int max, FILE *fp) 11574699Ssobomax{ 11674699Ssobomax int len; 11774699Ssobomax 11874699Ssobomax if (!str) 11974699Ssobomax return NULL; 12074699Ssobomax str[0] = '\0'; 12174699Ssobomax while (fgets(str, max, fp)) { 12274699Ssobomax len = strlen(str); 12374699Ssobomax while (len && isspace(str[len - 1])) 12474699Ssobomax str[--len] = '\0'; 12574699Ssobomax if (len) 12674699Ssobomax return str; 12774699Ssobomax } 12874699Ssobomax return NULL; 12974699Ssobomax} 130