terom@4: #include "util.h" terom@4: terom@4: #include terom@17: #include terom@4: terom@4: int chfext (char *buf, size_t len, const char *newext) terom@4: { terom@4: char *ext; terom@4: terom@4: // find .ext terom@17: if ((ext = strrchr(buf, '.')) == NULL) { terom@17: errno = EINVAL; terom@4: return -1; terom@17: } terom@4: terom@4: // check length terom@17: if (ext + strlen(newext) >= buf + len) { terom@17: errno = ENAMETOOLONG; terom@4: return -1; terom@17: } terom@4: terom@4: // change to .foo terom@4: strcpy(ext, newext); terom@4: terom@4: // ok terom@4: return 0; terom@4: } terom@4: terom@4: int path_with_fext (const char *path, char *buf, size_t len, const char *newext) terom@4: { terom@4: // verify length terom@17: if (strlen(path) > len) { terom@17: errno = ENAMETOOLONG; terom@4: return -1; terom@17: } terom@4: terom@4: // copy filename terom@4: strcpy(buf, path); terom@4: terom@4: // change fext terom@4: return chfext(buf, len, newext); terom@4: }