Skip to content

Instantly share code, notes, and snippets.

@rushabhvg
Created March 20, 2024 12:32
Show Gist options
  • Save rushabhvg/e7927ee8340f385279068d407c8fcab7 to your computer and use it in GitHub Desktop.
Save rushabhvg/e7927ee8340f385279068d407c8fcab7 to your computer and use it in GitHub Desktop.
Rust file code for fgets() code
/****************************************************************************
* apps/examples/hello_rust/hello_rust_main.rs
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Attributes
****************************************************************************/
#![no_main]
#![no_std]
/****************************************************************************
* Uses
****************************************************************************/
use core::panic::PanicInfo;
/****************************************************************************
* Externs
****************************************************************************/
extern "C" {
pub fn printf(format: *const u8, ...) -> i32;
pub fn puts(s: *const core::ffi::c_char) -> core::ffi::c_int;
pub fn fgets(
buf: *mut core::ffi::c_char,
n: core::ffi::c_int,
stream: *mut core::ffi::c_void
) -> *mut core::ffi::c_char;
pub fn lib_get_stream(fd: core::ffi::c_int) -> *mut core::ffi::c_void;
}
/****************************************************************************
* Private functions
****************************************************************************/
/****************************************************************************
* Panic handler (needed for [no_std] compilation)
****************************************************************************/
#[panic_handler]
fn panic(_panic: &PanicInfo<_>) -> ! {
loop {}
}
/****************************************************************************
* Public functions
****************************************************************************/
/****************************************************************************
* hello_rust_main
****************************************************************************/
#[no_mangle]
pub extern "C" fn hello_rust_main(_argc: i32, _argv: *const *const u8) -> i32 {
/* "Hello, Rust!!" using printf() from libc */
unsafe {
printf(b"Hello, Rust!!\n\0" as *const u8);
printf(b"Please give your output:\n\0" as *const u8);
let stdin: *mut core::ffi::c_void = lib_get_stream(0);
let mut buf: [core::ffi::c_char; 256] = [0; 256];
if !fgets(
&mut buf[0],
buf.len() as i32 - 1,
stdin
).is_null() {
printf(b"You entered...\n\0" as *const u8);
puts(&buf[0]);
}
}
/* exit with status 0 */
0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment