Thread: Importing C libs that aren't in stdlib

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    9

    Importing C libs that aren't in stdlib

    Hello all. I have a function that I want to call that belongs to a C program, net-tools/rarp.c at master * ecki/net-tools * GitHub ... Since the net-tools package isn't standard on Ubuntu, it requires an apt-get install.

    Now if I want to import a function from rarp, how would I go about doing that? Should I copy and paste the code into my project's directory? I can see the executable in /sbin/rarp. But how can I use some of the functions from that in my own C code? Thanks for helping me learn

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,767
    Well for small one-off functions, the simplest is to just copy the code - providing the licence terms are something you can agree to.

    If you only have /sbin/rarp, then you're stuck with just running the program with whatever command line parameters give you access to that function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    9
    Thanks Salem that helps

  4. #4
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    241

    Thumbs up

    You could create an executable that imports the data and prints out a new file. using some sort of tag for when it gets injected.
    "without goto we would be wtf'd"

  5. #5
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    241
    You could create an executable that imports the data and prints out a new file.
    create.c:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define OPEN '~'
    #define CLOSE '~'
    #define MAX_DEPTH 10
    #define MAX_SIZE 128
    
    int lock[MAX_DEPTH];
    char results[MAX_DEPTH][MAX_SIZE];
    
    char *data = "~./import.c~\n~./a.c~\n"; 
    
    void readFile(char *filename) {
        FILE *fp = fopen(filename, "r");
        int readChar = 0, bp = 0;
        char buffer[30], ch;
        while ((ch = fgetc(fp)) != EOF) printf("%c",ch);
        fclose(fp);
    }
    
    int main(int args,char *argv[]) {
    
        int spot = -1, read = 0, nest = 0;
        int sLen = strlen(data);
    
        while (++spot < sLen) {
            if (data[spot] == OPEN) {
                nest++;
            } else if (data[spot] == CLOSE) {
                nest--;
            } else {
                results[nest][lock[nest]++] = data[spot];
            }
        }
    
        for (int i=0; i<MAX_DEPTH; i++) {
            if (results[i][0] == '.' || results[i][1] == '/' ) {
                readFile(results[i]);
            } else  {
                printf("%s",results[i]);
            }
        }
    
        return 0;
    }
    gcc create.c -o create.exe
    create > output.c
    Last edited by Structure; 02-01-2020 at 01:03 PM.
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stdlib.h in C
    By rm82co in forum C++ Programming
    Replies: 3
    Last Post: 06-07-2019, 09:39 PM
  2. Stdlib or Limits
    By ItzBlue in forum C Programming
    Replies: 3
    Last Post: 12-20-2014, 10:32 AM
  3. stdlib help!!
    By clique in forum C Programming
    Replies: 5
    Last Post: 10-08-2008, 10:57 AM
  4. Compiler won't take #<stdlib.h>
    By omnificient in forum C Programming
    Replies: 5
    Last Post: 12-11-2007, 02:36 PM
  5. Stdlib header help
    By MaGaIn in forum C++ Programming
    Replies: 8
    Last Post: 09-16-2007, 07:02 AM

Tags for this Thread