diff --git a/formats/ecmp/ecmp.c b/formats/ecmp/ecmp.c index 49ecc5b..7f42e7b 100644 --- a/formats/ecmp/ecmp.c +++ b/formats/ecmp/ecmp.c @@ -2,14 +2,11 @@ #include "stdlib.h" #include "string.h" #include "unistd.h" -#include "malloc.h" + #include "../../include/libspm.h" #include "../../include/cutils.h" -#include "../../include/hashtable.h" -#include - #define uint unsigned int diff --git a/include/cutils.h b/include/cutils.h index 5e34475..fad47aa 100644 --- a/include/cutils.h +++ b/include/cutils.h @@ -3,7 +3,7 @@ The CUtils Library is a collection of C functions. It can be used for any C project, but it was originally made for the Libspm/CCCP project. It is licensed under the GNU General Public License v3.0. - * Copyright (C) 2019-2020 PKD + * Copyright (C) 2019-2024 PKD */ #include "string.h" @@ -41,7 +41,6 @@ long rdfile(const char* filePath,char** buffer); // write entire file safely int wrnfile(const char* filePath,char* buffer,long size); #define wrfile(filePath,buffer) wrnfile(filePath,buffer,strlen(buffer)) - /* Check if a dir exists : * 0 - doesn't exist @@ -49,10 +48,15 @@ int wrnfile(const char* filePath,char* buffer,long size); * 2 - Not a directory */ int isdir (const char *d); + // create dir recursivelty (similar to mkdir -p) int pmkdir (const char *dir); // move a file and create the dir if it doesn't exist -int mvsp(char* old_path,char* new_path,char* root); +int mvsp(char* old_path,char* new_path); +// move a symlink +int mvlink(char* old_path,char* new_path); +// get the relative path between two paths +char* relpath(char* start,char* end); // LIST file in a dir char** ls(char* path); // exec a shell command and return the output @@ -63,9 +67,9 @@ String utils Functions : * popchar - Remove a character from a string * popcharn - Remove a character from a string (with a size limit) - * s_size should be a size_t (=long unsigned int) * splita - Split a string into an array of strings * countc - Count the number of occurences of a char in a string + * strinarr - Check if a string is in an array of strings */ #define popcharn(str,pos,s_size) if (pos < s_size) { memmove(&str[pos], &str[pos + 1], s_size - pos - 1); str[s_size-1] = '\0'; } @@ -77,6 +81,9 @@ unsigned int splita (char* string,char delim,char*** dest); // to count the number of occurences of a char in a string unsigned int countc(const char* string,char c); +// check if a string is in an array of strings +int strinarr( char* val, char** arr,long arrsize); + /* Logging and debug utils Functions : @@ -102,6 +109,73 @@ int f_dbg__(int level,int line,const char* function,const char* file,char* messa #define dbg(level,message,...) f_dbg__(level,__LINE__,__func__,__FILE__,message,##__VA_ARGS__) +/* Hashtable (Dict) implementation */ + +// A pair of values : +// * key : the key of the pair (in bytes) +// * value : the value of the pair (pointer to anything) +typedef struct pair { + char* key; + void* value; +} pair; + +// An item in the hashtable : +// * data : the pairs of the item +// * size : the number of pairs in the item +// * capacity : the capacity of the item +typedef struct { + pair* data; + int size; + int capacity; +} item; + +// The hashtable : +// * items : the items in the hashtable +// * capacity : the capacity of the hashtable +typedef struct { + item *items; + int capacity; +} hashtable; + +// create a new hashtable +// * capacity : the capacity of the hashtable you want +hashtable *hm_create(int capacity); +// destroy a hashtable (free the memory) +// * hm : the hashtable you want to destroy +void hm_destroy(hashtable *hm); +// add a pair to the hashtable +// * hm : the hashtable you want to add the pair to +// * key : the key of the pair +// * value : the value of the pair +// Equivalent to hm[key] = value +int hm_add(hashtable *hm, char *key, void *value); +// get a value from the hashtable +// * hm : the hashtable you want to get the value from +// * key : the key of the pair +// Equivalent to hm[key] +void* hm_get(hashtable *hm, char *key); +// remove a pair from the hashtable +// * hm : the hashtable you want to remove the pair from +// * key : the key of the pair +// Equivalent to del hm[key] +int hm_rm(hashtable *hm, char *key); +// visualize the hashtable +// basic pretty print of the hashtable +// * hm : the hashtable you want to visualize +int hm_visualize(hashtable *hm); +// initialize a hashtable with a list of key-value pairs +// * kvlist : the list of key-value pairs +// * size : the size of the list +hashtable* hm_init(void* kvlist[][2],int size); +// get the hash of a key +// * hm : the hashtable you want to get the hash from +// * key : the key you want to get the hash of +// WARNING : Used only internally +unsigned int hm_hash(hashtable *hm, char *key);; + + + + // memory safety and debugging void* dbg_malloc(size_t size,char* file,int line); void* dbg_calloc(size_t nmemb,size_t size,char* file,int line); diff --git a/include/hashtable.h b/include/hashtable.h deleted file mode 100755 index 2175d0d..0000000 --- a/include/hashtable.h +++ /dev/null @@ -1,26 +0,0 @@ -typedef struct pair { - char* key; - void* value; -} pair; - -typedef struct { - pair* data; - int size; - int capacity; -} item; - -typedef struct { - item *items; - int capacity; -} hashtable; - - -hashtable *hm_create(int capacity); -void hm_destroy(hashtable *hm); -int hm_add(hashtable *hm, char *key, void *value); -void* hm_get(hashtable *hm, char *key); -int hm_rm(hashtable *hm, char *key); -int hm_visualize(hashtable *hm); -hashtable* hm_init(void* kvlist[][2],int size); - -unsigned int hm_hash(hashtable *hm, char *key);; \ No newline at end of file diff --git a/include/libspm.h b/include/libspm.h index 76a79dc..ef90739 100755 --- a/include/libspm.h +++ b/include/libspm.h @@ -2,6 +2,7 @@ #include "cutils.h" #include "globals.h" +#include diff --git a/lib/cutils b/lib/cutils index fa38903..2fac5f8 160000 --- a/lib/cutils +++ b/lib/cutils @@ -1 +1 @@ -Subproject commit fa389032d3b0de4dad3403bfa12d153c767de10d +Subproject commit 2fac5f87d0447f3ac31a72f729f657f638f2747d diff --git a/makefile b/makefile index 7926c37..7e403ba 100755 --- a/makefile +++ b/makefile @@ -85,6 +85,7 @@ check: test check-all libs: for i in $(LOCAL_LIBS); do make -C $$(dirname $$i) all; done + for i in $(LOCAL_LIBS); do cp $$(dirname $$i)/*.h include ; done direct: $(CC) $(CFLAGS) $(SRCS) $(LIBS) -g -shared -fPIC -o $(LIBOUT) @@ -96,7 +97,7 @@ formats: for i in $(FMT_DIR)/*; do \ echo "Building $$i"; \ if [ -d $$i ]; then \ - $(CC) $(CFLAGS) -shared -fPIC $$i/*.c -o $(BINDIR)/plugins/$$(basename $$i).so; \ + $(CC) $(CFLAGS) -shared -fPIC $$i/*.c $(LOCAL_LIBS) -o $(BINDIR)/plugins/$$(basename $$i).so; \ fi; \ done diff --git a/src/hashtable.c b/src/hashtable.c deleted file mode 100644 index 001f0df..0000000 --- a/src/hashtable.c +++ /dev/null @@ -1,137 +0,0 @@ -#include "stdio.h" -#include -#include - -#include "hashtable.h" -#include "cutils.h" - -hashtable* hm_create(int capacity) -{ - if (capacity == 0) capacity = 1024; - - hashtable *hm = calloc(1,sizeof(hashtable)); - hm->items = calloc(capacity, sizeof(item)); - if (hm->items == NULL) { - fprintf(stderr, "Out of memory"); - exit(1); - } - hm->capacity = capacity; - return hm; -} - -void hm_destroy(hashtable *hm) -{ - for (int i = 0; i < hm->capacity; i++) { - if (hm->items[i].data != NULL) { - free(hm->items[i].data); - } - } - free(hm->items); - free(hm); -} - -int hm_add(hashtable *hm, char *key, void *value) -{ - - int index = hm_hash(hm,key); - item x = hm->items[index]; - if (x.data == NULL) { - x.data = calloc(8, sizeof(pair)); - x.size = 0; - x.capacity = 8; - } - else if (x.size == x.capacity) { - x.capacity += 8; - //printf("reallocating %d\n", x.capacity); - x.data = realloc(x.data, sizeof(pair)*(x.capacity)); - } - - pair apair = {key, value}; - x.data[x.size++] = apair; - hm->items[index] = x; - - return 0; -} - -void* hm_get(hashtable *hm, char *key) -{ - int index = hm_hash(hm,key); - item x = hm->items[index]; - if (x.data == NULL) return NULL; - for (int i = 0; i < x.size; i++) { - if (strcmp(x.data[i].key,key) == 0) { - return x.data[i].value; - } - } - return NULL; -} - -// here we are converting a pair list into a hash table -hashtable* hm_init(void* kvlist[][2],int size) { - hashtable* hm = hm_create(size*2); - for (int i = 0; i < size; i++) { - hm_add(hm, kvlist[i][0], kvlist[i][1]); - } - return hm; -} - -int hm_rm(hashtable *hm, char *key) -{ - int index = hm_hash(hm,key); - item *x = &hm->items[index]; - if (x->data == NULL) return 1; - for (int i = 0; i < x->size; i++) { - if (strcmp(x->data[i].key,key) == 0) { - x->data[i] = x->data[x->size-1]; - //printf("replacing %s with %s\n", x->data[i].key, x->data[x->size-1].key); - x->size--; - if (x->size == 0) { - //printf("freeing %s\n", x->data[i].key); - free(x->data); - x->data = NULL; - } - return 0; - } - } - return 1; -} - -int hm_visualize(hashtable *hm) -{ - for (int i = 0; i < hm->capacity; i++) { - item x = hm->items[i]; - if (x.data == NULL) continue; - printf("===========================\n"); - printf("%d: ", i); - for (int j = 0; j < x.size; j++) { - printf("%s ", x.data[j].key); - } - printf("\n"); - printf("===========================\n"); - } - return 0; -} - -unsigned int hm_hash(hashtable *hm, char *key) -{ - if (key == NULL) return 0; - - unsigned int len = strlen(key); - - unsigned char *p = (unsigned char*) key; - unsigned int h = 0; - - while(len--) { - h += *p++; - h += (h << 9); - h ^= (h >> 19); - } - - h += (h << 3); - h ^= (h >> 17); - h += (h << 11); - - //printf("hash: %d\n", h % hm->capacity); - - return h % hm->capacity; -} \ No newline at end of file diff --git a/src/move.c b/src/move.c index e4eb176..199c56f 100755 --- a/src/move.c +++ b/src/move.c @@ -55,7 +55,7 @@ void move_binaries(char** locations, long loc_size) { } // Move the files from the build directory to the destination location - switch (mvsp(build_loc, dest_loc, getenv("SOVIET_BUILD_DIR"))) + switch (mvsp(build_loc, dest_loc)) { case -1: msg(FATAL, "Moving %s to %s failed, could not create dir", build_loc, dest_loc); diff --git a/src/update.c b/src/update.c index 02bca13..db79ceb 100644 --- a/src/update.c +++ b/src/update.c @@ -105,14 +105,12 @@ int update() free(files_array); - if(new_version_found != 0) - { - msg(WARNING, "new version found for one or more packages, use --upgrade to upgrade"); + if(new_version_found != 0) { + msg(WARNING, "New version found for one or more packages, use --upgrade to upgrade"); + } + else { + msg(WARNING, "all packages are up to date"); } - else - { - msg(WARNING, "all packages are up to date"); - } return 0; } diff --git a/test/assets/split.txt b/test/assets/split.txt index 6a73ebd..be3430b 100644 --- a/test/assets/split.txt +++ b/test/assets/split.txt @@ -1 +1 @@ -Ldg5En7J5,wK4haKNq21CLxQAKJpe9lgbe2tjHBg7DmL9CAt7j8LqXJ8zykW8gMdCU0TpFryahJksx9oZUheaaTByxEOznQPwnb23uNecK8rakjLtRhxjgcl4pTgda5s4DPqNvITQJfcIB1sja9GsCfYUf7K9CmT,m,l1nXLYkTv2pMEwPOlHY6QmhFUrfsivAU11GnQiEL73X9IibIpY7MMC8djqqZ2NzSufmaSWP7gU1LUjv1,bgmbpXnM8FSdCG61ioC7zvbOzd5DPRKCryKMlxezsvMCTnzYhXkSlKzy5y77RLr6mrA4haWUyr8ZbrayoRjCjEZCvyMgzOcD6ETRthgkqmGGUy6oqYyNgJBMLpEZsAmhIcKa45KWMRGShvGNKuK4upc43BEW,sjwCUo4ykb2h6v,NETEaX9V2g0JgXI45qwiruNAGeTsPf5fSXGskdLFof1OkBnabL53HLL5CWteosxyVTQFmoBeFqpbOzC69kSslJO45eKpBse6 \ No newline at end of file +oVil0JdC1BlYeL07Fv8VIwy2LBSxQxAHuXRBYfsNx2UbiBbBrli4ydwhwvd08CTnHxsdmmqG2WSqSnE33d3IsalyOrljQWXh8zUP8TgNP,EzszJSkt1CVWJ899MCNkQCCePjIfrLQNVaUuZEkWAi0X2UrzV3Pvz4fPVebdgENIF6ld,8Fccg9PT,h2MCth6JvahLEKwtLnsg98zz1DJhjmQb29EbAotjUNgHjLIORR6BuHCwbmWoj3whfVuqTPP,1MDxpKQ,6Z5GfHX3GQBhLBna,a24DvMFd4mwlD7LEEQvzhRBjD4AQFsQvvgl735AAe,W4IxFjfNmHPjFIx1iO5M0D0vLCQgZBnJTGuiWbZUa5UFLDrT,RSuvj20HLTqXXrBQow4WlqU,JyWgjBArm,Y0w7o6lUdBCHzIWR4BfgA3KH1DBTqXwnpBqEMZPDHqcmU9bIP8PEtt7xbkQDVS4HbUd6aQegldT0a2YL0xQPiX7sJylECkcLFk0GGkKvNv \ No newline at end of file diff --git a/test/spm.c b/test/spm.c index 359e087..dbf7508 100644 --- a/test/spm.c +++ b/test/spm.c @@ -1,5 +1,6 @@ #include "test.h" -#include +#include "../include/libspm.h" +#include "../include/cutils.h" char WORKING_DIR[2048]; diff --git a/test/test.c b/test/test.c index c6dd66b..5886055 100644 --- a/test/test.c +++ b/test/test.c @@ -23,7 +23,9 @@ void test_pm(char* spm_path) { init() ; assert(install_package_source(spm_path,0) == 0); + assert(check("vim") == 0); assert(uninstall("vim") == 0); + assert(check("vim") == 1); unsetenv("SOVIET_ROOT"); unsetenv("SOVIET_ROOT");