libc is everywhere - Lemmy<br>#include #include int main(int argc,<br>char *argv[]) { int flags, opt; int nsecs, tfnd; nsecs = 0; tfnd = 0; flags = 0;<br>while ((opt = getopt(argc, argv, "nt:")) != -1) { switch (opt) { case 'n': flags<br>= 1; break; case 't': // converting string to integer nsecs = atoi(optarg); tfnd<br>= 1; break; default: /* '?' */ exit(EXIT_FAILURE); } } printf("flags=%d;<br>tfnd=%d; nsecs=%d; optind=%d\n", flags, tfnd, nsecs, optind); if (optind >=<br>argc) { fprintf(stderr, "Expected argument after options\n");<br>exit(EXIT_FAILURE); } printf("name argument = %s\n", argv[optind]);<br>exit(EXIT_SUCCESS); } ::: and the same program in no_std rust ::: spoiler Rust<br>rust #![no_std] #![no_main] //use libc; // 0.2.186 #[cfg(not(test))]<br>#[panic_handler] fn panic(_: &core::panic::PanicInfo) -> ! { unsafe {<br>libc::abort() } } unsafe extern "C" { static mut optarg: *mut libc::c_char;<br>static mut optind: i32; } #[unsafe(no_mangle)] extern "C" fn main(argc: i32,<br>argv: *const *mut libc::c_char) { let (mut nsecs, mut tfnd, mut flags) = (0, 0,<br>0); loop { let opt = unsafe { libc::getopt(argc, argv, c"nt:".as_ptr()) }; if<br>opt == -1 { break; } match opt as u8 { b'n' => { flags = 1; } b't' => { nsecs =<br>unsafe { libc::atoi(optarg) }; tfnd = 1; } _ => unsafe {<br>libc::exit(libc::EXIT_FAILURE) }, } } unsafe { libc::printf( c"flags=%d;<br>tfnd=%d; nsecs=%d; optind=%d\n".as_ptr(), flags, tfnd, nsecs, optind, ); }<br>unsafe { if optind >= argc { libc::printf(c"Expected argument after<br>options\n".as_ptr()); libc::exit(libc::EXIT_FAILURE) } libc::printf(c"name<br>argument = %s\n".as_ptr(), *argv.add(optind as usize)); } unsafe {<br>libc::exit(libc::EXIT_SUCCESS) } } ::: Important notice: it is not some dismiss<br>of rust, it is just a factual state. MacOS for example pushes you to use libc,<br>since their syscalls ABI is not stable. So there is no way to avoid it"> #include #include int main(int argc,<br>char *argv[]) { int flags, opt; int nsecs, tfnd; nsecs = 0; tfnd = 0; flags = 0;<br>while ((opt = getopt(argc, argv, "nt:")) != -1) { switch (opt) { case 'n': flags<br>= 1; break; case 't': // converting string to integer nsecs = atoi(optarg); tfnd<br>= 1; break; default: /* '?' */ exit(EXIT_FAILURE); } } printf("flags=%d;<br>tfnd=%d; nsecs=%d; optind=%d\n", flags, tfnd, nsecs, optind); if (optind >=<br>argc) { fprintf(stderr, "Expected argument after options\n");<br>exit(EXIT_FAILURE); } printf("name argument = %s\n", argv[optind]);<br>exit(EXIT_SUCCESS); } ::: and the same program in no_std rust ::: spoiler Rust<br>rust #![no_std] #![no_main] //use libc; // 0.2.186 #[cfg(not(test))]<br>#[panic_handler] fn panic(_: &core::panic::PanicInfo) -> ! { unsafe {<br>libc::abort() } } unsafe extern "C" { static mut optarg: *mut libc::c_char;<br>static mut optind: i32; } #[unsafe(no_mangle)] extern "C" fn main(argc: i32,<br>argv: *const *mut libc::c_char) { let (mut nsecs, mut tfnd, mut flags) = (0, 0,<br>0); loop { let opt = unsafe { libc::getopt(argc, argv, c"nt:".as_ptr()) }; if<br>opt == -1 { break; } match opt as u8 { b'n' => { flags = 1; } b't' => { nsecs =<br>unsafe { libc::atoi(optarg) }; tfnd = 1; } _ => unsafe {<br>libc::exit(libc::EXIT_FAILURE) }, } } unsafe { libc::printf( c"flags=%d;<br>tfnd=%d; nsecs=%d; optind=%d\n".as_ptr(), flags, tfnd, nsecs, optind, ); }<br>unsafe { if optind >= argc { libc::printf(c"Expected argument after<br>options\n".as_ptr()); libc::exit(libc::EXIT_FAILURE) } libc::printf(c"name<br>argument = %s\n".as_ptr(), *argv.add(optind as usize)); } unsafe {<br>libc::exit(libc::EXIT_SUCCESS) } } ::: Important notice: it is not some dismiss<br>of rust, it is just a factual state. MacOS for example pushes you to use libc,<br>since their syscalls ABI is not stable. So there is no way to avoid it"> #include #include int main(int argc,<br>char *argv[]) { int flags, opt; int nsecs, tfnd; nsecs = 0; tfnd = 0; flags = 0;<br>while ((opt = getopt(argc, argv, "nt:")) != -1) { switch (opt) { case 'n': flags<br>= 1; break; case 't': // converting string to integer nsecs = atoi(optarg); tfnd<br>= 1; break; default: /* '?' */ exit(EXIT_FAILURE); } } printf("flags=%d;<br>tfnd=%d; nsecs=%d; optind=%d\n", flags, tfnd, nsecs, optind); if (optind >=<br>argc) { fprintf(stderr, "Expected argument after options\n");<br>exit(EXIT_FAILURE); } printf("name argument = %s\n", argv[optind]);<br>exit(EXIT_SUCCESS); } ::: and the same program in no_std rust ::: spoiler Rust<br>rust #![no_std] #![no_main] //use libc; // 0.2.186 #[cfg(not(test))]<br>#[panic_handler] fn panic(_: &core::panic::PanicInfo) -> ! { unsafe {<br>libc::abort() } } unsafe extern "C" { static mut optarg: *mut libc::c_char;<br>static mut optind: i32; } #[unsafe(no_mangle)] extern "C" fn main(argc: i32,<br>argv: *const *mut libc::c_char) { let (mut nsecs, mut tfnd, mut flags) = (0, 0,<br>0); loop { let opt = unsafe { libc::getopt(argc, argv, c"nt:".as_ptr()) }; if<br>opt == -1 { break; } match opt as u8 { b'n' => { flags = 1; } b't' => { nsecs =<br>unsafe { libc::atoi(optarg) }; tfnd = 1; } _ => unsafe {<br>libc::exit(libc::EXIT_FAILURE) }, } } unsafe { libc::printf( c"flags=%d;<br>tfnd=%d; nsecs=%d; optind=%d\n".as_ptr(),...