From ba067958e13d2bdb3fa95644771ee40ac13c4db2 Mon Sep 17 00:00:00 2001 From: sijanec Date: Sat, 14 Nov 2020 00:00:59 +0100 Subject: bio vaja vaja 1 kikiriki --- bio/vaje/1/programi/a.out | Bin 0 -> 13256 bytes bio/vaje/1/programi/explode.c | 56 +++++++++++++++++++++++++ bio/vaje/1/programi/fopenmkdir.c | 34 +++++++++++++++ bio/vaje/1/programi/koliko.php | 16 +++++++ bio/vaje/1/programi/mkdirp.c | 43 +++++++++++++++++++ bio/vaje/1/programi/povpreci.c | 87 +++++++++++++++++++++++++++++++++++++++ bio/vaje/1/programi/strlcat.c | 39 ++++++++++++++++++ bio/vaje/1/programi/strlcpy.c | 43 +++++++++++++++++++ 8 files changed, 318 insertions(+) create mode 100755 bio/vaje/1/programi/a.out create mode 100644 bio/vaje/1/programi/explode.c create mode 100644 bio/vaje/1/programi/fopenmkdir.c create mode 100755 bio/vaje/1/programi/koliko.php create mode 100644 bio/vaje/1/programi/mkdirp.c create mode 100644 bio/vaje/1/programi/povpreci.c create mode 100644 bio/vaje/1/programi/strlcat.c create mode 100644 bio/vaje/1/programi/strlcpy.c (limited to 'bio/vaje/1/programi') diff --git a/bio/vaje/1/programi/a.out b/bio/vaje/1/programi/a.out new file mode 100755 index 0000000..e987781 Binary files /dev/null and b/bio/vaje/1/programi/a.out differ diff --git a/bio/vaje/1/programi/explode.c b/bio/vaje/1/programi/explode.c new file mode 100644 index 0000000..094dd41 --- /dev/null +++ b/bio/vaje/1/programi/explode.c @@ -0,0 +1,56 @@ +#pragma once +#include +char *strdup(const char *src) +{ + char *tmp = malloc(strlen(src) + 1); + if(tmp) + strcpy(tmp, src); + return tmp; +} + +void explode(const char *src, const char *tokens, char ***list, size_t *len) +{ + if(src == NULL || list == NULL || len == NULL) + return; + + char *str, *copy, **_list = NULL, **tmp; + *list = NULL; + *len = 0; + + copy = strdup(src); + if(copy == NULL) + return; + + str = strtok(copy, tokens); + if(str == NULL) + goto free_and_exit; + + _list = realloc(NULL, sizeof *_list); + if(_list == NULL) + goto free_and_exit; + + _list[*len] = strdup(str); + if(_list[*len] == NULL) + goto free_and_exit; + (*len)++; + + + while((str = strtok(NULL, tokens))) + { + tmp = realloc(_list, (sizeof *_list) * (*len + 1)); + if(tmp == NULL) + goto free_and_exit; + + _list = tmp; + + _list[*len] = strdup(str); + if(_list[*len] == NULL) + goto free_and_exit; + (*len)++; + } + + +free_and_exit: + *list = _list; + free(copy); +} diff --git a/bio/vaje/1/programi/fopenmkdir.c b/bio/vaje/1/programi/fopenmkdir.c new file mode 100644 index 0000000..afca4f1 --- /dev/null +++ b/bio/vaje/1/programi/fopenmkdir.c @@ -0,0 +1,34 @@ +#pragma once +#include +#include +#include +#include +#include +FILE * fopen_mkdir(char* filename, char* mode) { + FILE * file_to_return; + file_to_return = fopen(filename, mode); + if(file_to_return == NULL) { + char folder_of_file[PATH_MAX]; + strcpy(folder_of_file, filename); + char * p; + p = strrchr(folder_of_file, '/'); + if (!p) { + printf("[fopenmkdir.c] filename contains no slash.\n"); + return NULL; + } else { + // printf("folder is %s\n", folder_of_file); + // printf("filename is %s\n", filename); + *p = '\0'; + } + if(mkdir_p(folder_of_file) != 0) { + return NULL; + } + file_to_return = fopen(filename, mode); + if(file_to_return == NULL) { + return NULL; + } + return file_to_return; + } else { + return file_to_return; + } +} diff --git a/bio/vaje/1/programi/koliko.php b/bio/vaje/1/programi/koliko.php new file mode 100755 index 0000000..6ec7933 --- /dev/null +++ b/bio/vaje/1/programi/koliko.php @@ -0,0 +1,16 @@ +#!/usr/bin/env php + $s) { + $p .= "$k,$s\n"; + } + file_put_contents($argv[2], $p); + var_dump($v); +?> diff --git a/bio/vaje/1/programi/mkdirp.c b/bio/vaje/1/programi/mkdirp.c new file mode 100644 index 0000000..a6c58e6 --- /dev/null +++ b/bio/vaje/1/programi/mkdirp.c @@ -0,0 +1,43 @@ +// borrowed from https://gist.github.com/JonathonReinhart/8c0d90191c38af2dcadb102c4e202950 +#pragma once +#include +#include /* PATH_MAX */ +#include /* mkdir(2) */ +#include +int mkdir_p(const char *path) { + /* Adapted from http://stackoverflow.com/a/2336245/119527 */ + const size_t len = strlen(path); + char _path[PATH_MAX]; + char *p; + + errno = 0; + + /* Copy string so its mutable */ + if (len > sizeof(_path)-1) { + errno = ENAMETOOLONG; + return -1; + } + strcpy(_path, path); + + /* Iterate the string */ + for (p = _path + 1; *p; p++) { + if (*p == '/') { + /* Temporarily truncate */ + *p = '\0'; + + if (mkdir(_path, S_IRWXU) != 0) { + if (errno != EEXIST) + return -1; + } + + *p = '/'; + } + } + + if (mkdir(_path, S_IRWXU) != 0) { + if (errno != EEXIST) + return -1; + } + + return 0; +} diff --git a/bio/vaje/1/programi/povpreci.c b/bio/vaje/1/programi/povpreci.c new file mode 100644 index 0000000..6f3e7c1 --- /dev/null +++ b/bio/vaje/1/programi/povpreci.c @@ -0,0 +1,87 @@ +#include +#include +#include +#define delim argv[2] +#define valuesfield argv[1] +#define vfdelim argv[3] +int main(int argc, char *argv[]) { + if(argc != 1+3) { + fprintf(stderr, "usage: %s valuesfield delim vfdelim\n", argv[0]); + return 1; + } + char **list; + size_t i, len; + size_t * counts; + char line[1337]; // yeah + int iter = 0, count; + double value; + double * values; + char * delimpointer; + char * nextdp; + fgets(line, 1337, stdin); + line[strcspn(line, "\n")] = 0; + while(1) { + explode(line, delim, &list, &len); + if (iter == 0) { + values = malloc(sizeof(double)*(len+1)); + counts = malloc(sizeof(size_t)*(len+1)); + for (i = 0; i <= len; i++) { + values[i] = 0; + counts[i] = 0; + } + } + if (iter != 0) // prvi je header + for (i = 0; i < len; i++) { + fprintf(stdout, "%s", list[i]); + if (i + 1 == len && i != atoi(valuesfield)) + fprintf(stdout, "\n"); + else + fprintf(stdout, ","); + if (i == atoi(valuesfield)) { + delimpointer = strtok(list[i], vfdelim); + value = 0; + count = 0; + while (delimpointer != NULL) { + value = value + atof(delimpointer); + count++; + delimpointer = strtok(NULL, vfdelim); + } + fprintf(stdout, "%lf", value/count); + values[i] = 0; + counts[i] = 1; + values[i+1] = values[i+1]/++(counts[i+1]); + if (i + 1 == len) + fprintf(stdout, "\n"); + else + fprintf(stdout, ","); + } else { + if (i > atoi(valuesfield)) { + values[i+1] = values[i+1]/++(counts[i+1]); + } else { + values[i] = values[i]/counts[i]; + } + } + } + /* free list */ + for(i = 0; i < len; ++i) { + free(list[i]); + } + free(list); + fgets(line, 1337, stdin); + if(feof(stdin)) { + fprintf(stderr, "averages:\n"); + for (i = 0; i <= len; i++) { + fprintf(stderr, "%lf", values[i]/counts[i]); + if (i + 1 == len) + fprintf(stderr, ","); + else + fprintf(stderr, "\n"); + } + break; + } + // fprintf(stderr, "d: %s", line); + line[strcspn(line, "\n")] = 0; + iter++; + } + return 0; +} diff --git a/bio/vaje/1/programi/strlcat.c b/bio/vaje/1/programi/strlcat.c new file mode 100644 index 0000000..5d4e99e --- /dev/null +++ b/bio/vaje/1/programi/strlcat.c @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2011 Apple, Inc. All rights reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_LICENSE_HEADER_END@ + */ + +#include + +size_t +strlcat(char * restrict dst, const char * restrict src, size_t maxlen) { + const size_t srclen = strlen(src); + const size_t dstlen = strnlen(dst, maxlen); + if (dstlen == maxlen) return maxlen+srclen; + if (srclen < maxlen-dstlen) { + memcpy(dst+dstlen, src, srclen+1); + } else { + memcpy(dst+dstlen, src, maxlen-1); + dst[dstlen+maxlen-1] = '\0'; + } + return dstlen + srclen; +} + diff --git a/bio/vaje/1/programi/strlcpy.c b/bio/vaje/1/programi/strlcpy.c new file mode 100644 index 0000000..1dc6147 --- /dev/null +++ b/bio/vaje/1/programi/strlcpy.c @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2011 Apple, Inc. All rights reserved. + * + * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. The rights granted to you under the License + * may not be used to create, or enable the creation or redistribution of, + * unlawful or unlicensed copies of an Apple operating system, or to + * circumvent, violate, or enable the circumvention or violation of, any + * terms of an Apple operating system software license agreement. + * + * Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ + */ + +#include "string.h" + +#undef strlcpy +size_t +strlcpy(char * dst, const char * src, size_t maxlen) { + const size_t srclen = strlen(src); + if (srclen + 1 < maxlen) { + memcpy(dst, src, srclen + 1); + } else if (maxlen != 0) { + memcpy(dst, src, maxlen - 1); + dst[maxlen-1] = '\0'; + } + return srclen; +} + -- cgit v1.2.3