What does the "e" flag mean in fopen - c

I saw a code snippet using fopen(file_name, "r+e"). What does the e flag mean in fopen? I couldn't find any information from the linux man page.

On a GNU webpage I found that the e means that the "file descriptor will be closed if you use any of the exec… functions".

It's documented in the man page on my system (release 3.54 of the Linux man-pages project).
e (since glibc 2.7)
Open the file with the O_CLOEXEC flag. See open(2) for more information. This flag is ignored for fdopen().
Scroll down; it's under "Glibc notes". This is a non-standard extension.
An online copy of the man page is here.

Related

Print man contents as help prompt of a C program

I plan to use pandoc to generate the man files from markdown for a very small C program.
I do not want to repeat myself and type the help prompt (command [ -h | --help ]) into the source code of the program.
Question
Q: Could I permanently include man content from a file at compile time of a C program?
OR
Q: Is there a way in C to print the contents of the installed man page to stdout?
[WIP] Solution
Based on #KamilCuk's answer here's what i am doing:
In terminal:
file command.1
# OUTPUT --> command.1: troff or preprocessor input, ASCII text
xxd -i command.1 > command.hex
Then in main.c
#include "command.hex"
printf("%s", command_1);
This is almost exactly what I am looking for except
I need the hexed troff to be parsed before printing. Or parsed before hexing. I am not sure which to do / how to do that.
No, I do not want to call man and pipe the output.
I would prefer using a library + a macro to generate the hex but I am not sure how it's done.
Clarifications
What I mean by help prompt:
The output of git --help which prints to stdout
NOT the output of git --help branch which opens the man for git-branch
However, please do share the latter too. It isn't what I want: my manual is very short; but I would be glad to learn an alternative if not the exact one I seek.
Considerations I have noted:
man pages are not always installed to the same location
man pages may not be installed in some systems; or there may be no man program
Could I permanently include man content from a file at compile time?
You can include any content of any file at compile time. The simplest is use xxd -i and compile the output.
Is there a way in C to print the contents of the installed man page?
You can troff output from man -k
You can get output from man by replacing pager, along MANPAGER=cat man.
In C: setenv("MANPAGER", "cat"); execp("man", (char*[]){"man", "something", 0}); or just system("MANPAGER=cat man some_command").
man pages are not always installed to the same location
And system administractors can use /etc/man_db.conf to configure the behavior of man command.
man pages may not be installed in some systems; or there may be no man program
So it seems users of such systems do not care about man pages, so there's no point in supporting such cases.
I believe your intention is to render a man page from your program, where you have a static troff input saved and want to use man to display it. You could save the input to a file, save it and pass the filename to man - man will parse troff and render it. Or you could the usual pipe()+fork()+exec() and from one process pass troff data to stdin to man - - the - will make man read troff input from stdin.
Is there a way in C to print the contents of the installed man page to stdout?
You could invoke the mancommand by using a pipe stream . Function popen is what you are looking for. More info here.
This is an fgets() solution, though you might want to be careful, in case you want to read more characters.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp = NULL;
char buffer[1000];
fp = popen("man man","r");
if (fp == NULL)
{
/* error running the command */
exit(EXIT_FAILURE);
}
while (fgets(buffer,sizeof(buffer),fp))
{
puts(buffer);
}
fclose(fp);
return 0;
}
getline() solution, in case you don't know how many bytes you are going to read.
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp = NULL;
char *buffer = NULL;
size_t len = 0U;
ssize_t nread = 0;
fp = popen("man man","r");
if (fp == NULL)
{
/* error running the command */
exit(EXIT_FAILURE);
}
/* getline will set errno uppon failure, you might want to check that too */
while ((nread = getline(&buffer,&len,fp)) != -1)
{
buffer[nread] = 0; /* NULL terminate your buffer */
puts(buffer);
}
free(buffer);
fclose(fp);
return 0;
}
More on getline() here.
The man program is written in C and is open source. It is more complex than you believe, since it deals with terminal (and their width). See also termios(3) and tty(4). Be aware of ANSI escape codes and the ncurses library. Take into account that in 2021 UTF-8 is used everywhere, and that some man pages could contain § or ° characters. Be also aware of internationalization and localization issues, see locale(7). In France, a lot of Linux systems are installed with man pages in French.
So you could get the source code of the man-db package, study it, and incorporate it inside your program.
My opinion is that it would be silly to do so.
You could instead read Advanced Linux Programming, study various syscalls(2), and use fork(2), execve(2), waitpid(2) to run the existing and installed /usr/bin/man program (whose existence could be tested with test(1) or access(2) or stat(2) in C code; but if it does not exist your execve(2) will fail...)
Regarding display of --help, you could use (if so allowed) GNU libc program argument parsing facilities, and take inspiration from the source code of existing free software programs, such as GNU coreutils.
Of course, your build infrastructure (e.g. your Makefile if you build your software with GNU make) could run programs to share data and textual descriptions. You could (if so allowed) use open source tools to generate some C code (e.g. #included stuff) at build time, such as GNU m4, GNU gawk, GPP, etc..
Take also inspiration from the source code of git. It is open source (and useful for you): you can download its source code and study it.

O_PATH equivalent in MacOS?

Linux has the O_PATH flag to open() which allows one to get a fd to be used in fstat, fcntl and others without actually opening the file for reading (or having permissions to do so). However the O_PATH flag is Linux specific.
Is there an equivalent to the O_PATH flag to open() in MacOS? For example, how can I use fstat() on a file I don't have read permissions for?
macOS doesn't have an equivalent to O_PATH, so it's impossible to have a reference to a file without opening it. Regarding the one bit of functionality that you mentioned, you can call stat with a given file path as long as you have "execution" rights to its parent directory, regardless of whether you have any rights to that file.

C programming - execute another program with sudo privileges

i have a C program that opens an mp3 and extract the jpg artwork in the same folder. If i execute this program with no root privileges i get a crash. If i execute it with sudo it works normally.
Now, i need another C programs who launch the previous program when it needs a jpg artwork for the selected mp3.
I tried to call popen("./firstProgram test.mp3" , "r") function or system("/(absolute path)/firstProgram test.mp3") function by calling them even with sudo in the command or not and either with relative or absolute paths. But no version seems to work.
How can i launch the first program from the second one with success?
Thanks!
fork and then use execl
char sudo[]="/usr/bin/sudo";
char pbin[]="/usr/local/bin/puppet";
NOTICE("running puppet: %s %s",sudo,pbin);
errno=0;
execl(sudo,sudo,pbin,(char *)NULL);
/* we should never get as far as this */
obviously I recommend reading man execl for further info
Unix (Linux) systems have contained a C Programming Manual in them since possibly forever. Look in Section 2, "System Calls".
This Wikipedia Page explains the Unix Manual "sections"
It is section 2 of the manual you can read about "System Calls"
Try the command: man 2 setuid
This will give you the manual for the setuid() system call which I think is what you want.
That manual page will also list references to other related system calls that may be what you want.
Remember when compiling C programs and using system calls that do low-level hardware access, to use the -O2, or -O3 option to gcc. There is a mention of it in the manual.
Ultimately the setuid() system call makes a running process started by one user change the UID of that running process to be running as some other user. (For example, you may see the Apache running as "apache", even though it was started by root).
setuid(0) lets you be root.

Different between read(2) and read(1)? [duplicate]

For example: man(1), find(3), updatedb(2)?
What do the numbers in parentheses (Brit. "brackets") mean?
It's the section that the man page for the command is assigned to.
These are split as
General commands
System calls
C library functions
Special files (usually devices, those found in /dev) and drivers
File formats and conventions
Games and screensavers
Miscellanea
System administration commands and daemons
Original descriptions of each section can be seen in the Unix Programmer's Manual (page ii).
In order to access a man page given as "foo(5)", run:
man 5 foo
The section the command is documented in the manual. The list of sections is documented on man's manual. For example:
man 1 man
man 3 find
This is useful for when similar or exactly equal commands exist on different sections
The reason why the section numbers are significant is that many years ago when disk space was more of an issue than it is now the sections could be installed individually.
Many systems only had 1 and 8 installed for instance. These days people tend to look the commands up on google instead.
As #Ian G says, they are the man page sections. Let's take this one step further though:
1. See the man page for the man command with man man, and it shows the 9 sections as follows:
DESCRIPTION
man is the system's manual pager. Each page argument given
to man is normally the name of a program, utility or func‐
tion. The manual page associated with each of these argu‐
ments is then found and displayed. A section, if provided,
will direct man to look only in that section of the manual.
The default action is to search in all of the available sec‐
tions following a pre-defined order ("1 n l 8 3 2 3posix 3pm
3perl 5 4 9 6 7" by default, unless overridden by the SEC‐
TION directive in /etc/manpath.config), and to show only the
first page found, even if page exists in several sections.
The table below shows the section numbers of the manual fol‐
lowed by the types of pages they contain.
1 Executable programs or shell commands
2 System calls (functions provided by the kernel)
3 Library calls (functions within program libraries)
4 Special files (usually found in /dev)
5 File formats and conventions eg /etc/passwd
6 Games
7 Miscellaneous (including macro packages and conven‐
tions), e.g. man(7), groff(7)
8 System administration commands (usually only for root)
9 Kernel routines [Non standard]
A manual page consists of several sections.
2. man <section_num> <cmd>
Let's imagine you are Googling around for Linux commands. You find the OPEN(2) pg online: open(2) — Linux manual page.
To see this in the man pages on your pc, simply type in man 2 open.
For FOPEN(3) use man 3 fopen, etc.
3. man <section_num> intro
To read the intro pages to a section, type in man <section_num> intro, such as man 1 intro, man 2 intro, man 7 intro, etc.
To view all man page intros in succession, one-after-the-other, do man -a intro. The intro page for Section 1 will open. Press q to quit, then press Enter to view the intro for Section 8. Press q to quit, then press Enter to view the intro for Section 3. Continue this process until done. Each time after hitting q, it'll take you back to the main terminal screen but you'll still be in an interactive prompt, and you'll see this line:
--Man-- next: intro(8) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
Note that the Section order that man -a intro will take you through is:
Section 1
Section 8
Section 3
Section 2
Section 5
Section 4
Section 6
Section 7
This search order is intentional, as the man man page explains:
The default action is to search in all of the available sections follow‐
ing a pre-defined order ("1 n l 8 3 2 3posix 3pm 3perl 5 4 9 6 7" by default, unless overrid‐
den by the SECTION directive in /etc/manpath.config)
Why did they choose this order? I don't know (please answer in the comments if you know), but just realize this order is correct and intentional.
Related:
Google search for "linux what does the number mean in parenthesis after a function?"
SuperUser: What do the parentheses and number after a Unix command or C function mean?
Unix & Linux: What do the numbers in a man page mean?
Note also that on other unixes, the method of specifying the section differs. On solaris, for example, it is:
man -s 1 man
It indicates the section of the man pages the command is found in. The -s switch on the man command can be used to limit a search to certain sections.
When you view a man page, the top left gives the name of the section, e.g.:
User Commands printf(1)
Standard C Library Functions printf(3C)
So if you are trying to look up C functions and don't want to accidentally see a page for a user command that shares the same name, you would do 'man -s 3C ...'
Wikipedia details about Manual Sections:
General commands
System calls
Library functions, covering in particular the C standard library
Special files (usually devices, those found in /dev) and drivers
File formats and conventions
Games and screensavers
Miscellanea
System administration commands and daemons

Error:unresolved external symbol _popen

Why does using popen result in Error: Failed to close command stream under windows?
You actually have to use: _popen and _pclose (yes WITH the silly underscore) under Windows.
See MSDN Entry on it; it has a nice example
Now as for _popening "ps" you know whether or not you have a ps on your system.

Resources