Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aggregate libc functions into one file. #1050

Open
ahaoboy opened this issue Dec 3, 2023 · 1 comment
Open

Aggregate libc functions into one file. #1050

ahaoboy opened this issue Dec 3, 2023 · 1 comment

Comments

@ahaoboy
Copy link

ahaoboy commented Dec 3, 2023

Currently, libc functions are individually imported in each file. If there is a need to replace a particular function, modifications are required in all files using it. Consolidating all libc functions into one file would require modifications only in that file for any changes.

// a.c
#include <stdio.h>

int funA(int n){
  puts("funA");
  printf("funA");
  return n;
}

// b.c
#include <stdio.h>

int funB(int n){
  printf("funB");
  return n;
}
// b.c
use ::libc;
extern "C" {
    fn printf(_: *const libc::c_char, _: ...) -> libc::c_int;
}
#[no_mangle]
pub unsafe extern "C" fn funB(mut n: libc::c_int) -> libc::c_int {
    printf(b"funB\0" as *const u8 as *const libc::c_char);
    return n;
}

// a.rs
use ::libc;
extern "C" {
    fn printf(_: *const libc::c_char, _: ...) -> libc::c_int;
    fn puts(__s: *const libc::c_char) -> libc::c_int;
}
#[no_mangle]
pub unsafe extern "C" fn funA(mut n: libc::c_int) -> libc::c_int {
    puts(b"funA\0" as *const u8 as *const libc::c_char);
    printf(b"funA\0" as *const u8 as *const libc::c_char);
    return n;
}
@pitaj
Copy link

pitaj commented Mar 4, 2024

Ideally these would just import from libc directly:

// b.c
use ::libc::printf;

#[no_mangle]
pub unsafe extern "C" fn funB(mut n: libc::c_int) -> libc::c_int {
    printf(b"funB\0" as *const u8 as *const libc::c_char);
    return n;
}

// a.rs
use ::libc::{printf, puts};

#[no_mangle]
pub unsafe extern "C" fn funA(mut n: libc::c_int) -> libc::c_int {
    puts(b"funA\0" as *const u8 as *const libc::c_char);
    printf(b"funA\0" as *const u8 as *const libc::c_char);
    return n;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants