[an error occurred while processing this directive]

HP OpenVMS Systems Documentation

Content starts here

HP C
Run-Time Library Reference Manual for OpenVMS Systems


Previous Contents Index


getopt

A command-line parser that can be used by applications that follow UNIX command-line conventions.

Format

#include <unistd.h> (X/OPEN, POSIX-1)

#include <stdio.h> (X/OPEN, POSIX-2)

int getopt (int argc, char * const argv[], const char *optstring);

extern char *optarg;

extern int optind, opterr, optopt;


Arguments

argc

The argument count as passed to main .

argv

The argument array as passed to main .

optstring

A string of recognized option characters. If a character is followed by a colon, the option takes an argument.

Description

The variable optind is the index of the next element of the argv vector to be processed. It is initialized to 1 by the system, and it is updated by getopt when it finishes with each element of argv. When an element of argv contains multiple option characters, it is unspecified how getopt determines which options have already been processed.

The getopt function returns the next option character (if one is found) from argv that matches a character in optstring, if there is one that matches. If the option takes an argument, getopt sets the variable optarg to point to the option-argument as follows:

  • If the option was the last character in the string pointed to by an element of argv, then optarg contains the next element of argv, and optind is incremented by 2. If the resulting value of optind is not less than argc, getopt returns an error, indicating a missing option-argument.
  • Otherwise, optarg points to the string following the option character in that element of argv, and optind is incremented by 1.

If one of the following is true, getopt returns - 1 without changing optind :

argv[ optind ] is a NULL pointer
*argv[ optind ] is not the character --
argv[ optind ] points to the string "--"

If argv[ optind ] points to the string "-- --" getopt returns - 1 after incrementing optind .

If getopt encounters an option character not contained in optstring, the question-mark character (?) is returned.

If getopt detects a missing argument, the colon character (:) is returned if the first character of optstring is a colon; otherwise, a question-mark character is returned.

In either of the previous two cases, getopt sets the variable optopt to the option character that caused the error. If the application has not set the variable opterr to 0 and the first character of optstring is not a colon, getopt also prints a diagnostic message to stderr .


Return Values

x The next option character specified on the command line.

A colon is returned if getopt detects a missing argument and the first character of optstring is a colon.

A question mark is returned if getopt encounters an option character not in optstring or detects a missing argument and the first character of optstring is not a colon.

- 1 When all command-line options are parsed.

Example

The following example shows how you might process the arguments for a utility that can take the mutually exclusive options a and b and the options f and o, both of which require arguments:


#include <unistd.h>

int main (int argc, char *argv[ ])
{
         int c;
         int bflg, aflg, errflg;
         char *ifile;
         char *ofile;
         extern char *optarg;
         extern int optind, optopt;
         .
         .
         .
         while ((c = getopt(argc, argv, ":abf:o:)) != -1) {

                switch (c) {
                case 'a':
                        if (bflg)
                                errflg++;
                        else
                                aflg++;
                        break;
                case 'b':
                        if (aflg)
                               errflg++;
                        else {
                               bflg++;
                               bproc();
                        }

                        break;
                case 'f':
                        ifile = optarg;
                        break;
                case 'o':
                        ofile = optarg;
                        break;
                case ':':      /* -f or -o without operand */
                        fprintf (stderr,
                         "Option -%c requires an operand\n"' optopt);
                        errflg++;
                        break;
                case '?':
                        fprintf (stderr,
                                "Unrecognized option -%c\n"' optopt);
                        errflg++;
                }
         }
         if (errflg) {
                fprintf (stderr, "usage: ...");
                exit(2);
         }
         for ( ; optind < argc; optind++)  {
                if (access(argv[optind], R_OK)) {
         .
         .
         .
}

This sample code accepts any of the following as equivalent:


cmd -ao arg path path
cmd -a -o arg path path
cmd -o arg -a path path
cmd -a -o arg -- path path
cmd -a -oarg path path
cmd -aoarg path path

getpagesize

Gets the system page size.

Format

#include <unistd.h>

int getpagesize (void);


Description

The getpagesize function returns the number of bytes in a page. The system page size is useful for specifying arguments to memory management system calls.

The page size is a system page size and is not necessarily the same as the underlying hardware page size.


Return Value

x Always indicates success. Returns the number of bytes in a page.

getpgid (ALPHA, I64)

Gets the process group ID for a process.

Format

#include <unistd.h>

pid_t getpgid (pid_t pid);


Argument

pid

The process ID for which the group ID is being requested.

Description

The getpgid function returns the process group ID of the process specified by pid. If pid is 0, the getpgid function returns the process group ID of the calling process.

This function requires that long (32-bit) UID/GID support be enabled. See Section 1.5.8 for more information.


Return Values

x The process group ID of the session leader of the specified process.
(pid_t) - 1 Indicates an error. The function sets errno to one of the following values:
  • EPERM -- The process specified by pid is not in the same session as the calling process, and the implementation does not allow access to the process group ID of that process from the calling process.
  • ESRCH -- There is no process with a process ID of pid.
  • EINVAL -- The value of pid is invalid.

getpgrp (ALPHA, I64)

Gets the process group ID of the calling process.

Format

#include <unistd.h>

pid_t getpgrp (void);


Description

The getpgrp function returns the process group ID of the calling process.

The getpgrp function is always successful, and no return value is reserved to indicate an error.

This function requires that long (32-bit) UID/GID support be enabled. See Section 1.5.8 for more information.


Return Values

x The process group ID of the calling process.

getpid

Returns the process ID of the current process.

Format

#include <unistd.h>

pid_t getpid (void);


Return Value

x The process ID of the current process.

getppid

Returns the parent process ID of the calling process.

Format

#include <unistd.h>

pid_t getppid (void);


Return Values

x The parent process ID.
0 Indicates that the calling process does not have a parent process.

getpwent

Accesses user entry information in the user database, returning a pointer to a passwd structure.

Format

#include <pwd.h>

struct passwd *getpwent (void);

Function Variants The getpwent function has variants named __32_getpwent and __64_getpwent for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.10 for more information on using pointer-size-specific functions.

Description

The getpwent function returns a pointer to a structure containing fields whose values are derived from an entry in the user database. Entries in the database are accessed sequentially by getpwent . When first called, getpwent returns a pointer to a passwd structure containing the first entry in the user database. Thereafter, it returns a pointer to a passwd structure containing the next entry in the user database. Successive calls can be used to search the entire user database.

The passwd structure is defined in the <pwd.h> header file as follows:

pw_name The name of the user.
pw_uid The ID of the user.
pw_gid The group ID of the principle group of the user.
pw_dir The home directory of the user.
pw_shell The initial program for the user.

If an end-of-file or an error is encountered on reading, getpwent returns a NULL pointer.

Because getpwent accesses the user authorization file (SYSUAF) directly, the process must have appropriate privileges enabled or the function will fail.

Notes

All information generated by the getpwent function is stored in a per-thread static area and is overwritten on subsequent calls to the function.

Password file entries that are too long are ignored.

Return Values

x Pointer to a passwd structure, if successful.
NULL Indicates an end-of-file or error occurred. The function sets errno to one of the following values:
  • EIO -- Indicates that an I/O error occurred or the user does not have appropriate privileges enabled to access the user authorization file (SYSUAF).
  • EMFILE -- OPEN_MAX file descriptors are currently open in the calling process.
  • ENFILE -- The maximum allowable number of files is currently open in the system.

getpwnam, getpwnam_r

The getpwnam function returns information about a user database entry for the specified name.

The getpwnam_r function is a reentrant version of getpwnam .


Format

#include <pwd.h>

struct passwd *getpwnam (const char *name); (ISO POSIX-1)

struct passwd *getpwnam (const char *name, ...); (HP C EXTENSION)

int getpwnam_r (const char *name, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result); (ISO POSIX-1), (ALPHA, I64)

int getpwnam_r (const char *name, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result, ...); (HP C EXTENSION), (ALPHA, I64)

Function Variants The getpwnam and getpwnam_r functions have variants named __32_getpwnam , _getpwnam_r32 and __64_getpwnam , _getpwnam_r64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.10 for more information on using pointer-size-specific functions.

Arguments

name

The name of the user for which the attributes are to be read.

pwd

The address of a passwd structure into which the function writes its results.

buffer

A working buffer for the result argument that is able to hold the largest entry in the passwd structure. Storage referenced by the passwd structure is allocated from the memory provided with the buffer argument, which is bufsize characters in length.

bufsize

The length of the character array that buffer points to.

result

Upon successful return, is set to pwd. Upon unsuccessful return, the result is set to NULL.

...

An optional argument that can be either 1 or 0. If you specify 1, the directory specification is returned in OpenVMS format. If you specify 0, the directory specification (pathname) is returned in UNIX style format. If you omit this argument, the function returns the directory specification according to your current command-language interpreter. For more information about UNIX style directory specifications, see Section 1.4.3.

Description

The getpwnam function searches the user database for an entry with the specified name. The function returns the first user entry in the database with the pw_name member of the passwd structure that matches the name argument.

The passwd structure is defined in the <pwd.h> header file as follows:

pw_name The user's login name.
pw_uid The numerical user ID.
pw_gid The numerical group ID.
pw_dir The home directory of the user.
pw_shell The initial program for the user.

Note

All information generated by the getpwnam function is stored in a per-thread static area and is overwritten on subsequent calls to the function.

The getpwnam_r function is the reentrant version of getpwnam . The getpwnam_r function updates the passwd structure pointed to by pwd and stores a pointer to that structure at the location pointed to by result. The structure will contain an entry from the user database that matches the specified name. Storage referenced by the structure is allocated from the memory provided with the buffer argument, which is bufsize characters in length. The maximum size needed for this buffer can be determined with the _SC_GETPW_R_SIZE_MAX parameter of the sysconf function. On error or if the requested entry is not found, a NULL pointer is returned at the location pointed to by result.

Applications wishing to check for error situations should set errno to 0 before calling getpwnam . If getpwnam returns a NULL pointer and errno is nonzero, an error occurred.


Return Values

x getpwnam returns a pointer to a valid passwd structure, if a matching entry is found.
NULL getpwnam returns NULL if an error occurred or a the specified entry was not found. errno is set to indicate the error. The getpwnam function may fail if:
  • EIO -- An I/O error has occurred.
  • EINTR -- A signal was caught during getpwnam .
  • EMFILE -- OPEN_MAX file descriptors are currently open in the calling process.
  • ENFILE -- The maximum allowable number of files is currently open in the system.
0 When successful, getpwnam_r returns 0 and stores a pointer to the updated passwd structure at the location pointed to by result.
0 When unsuccessful (on error or if the requested entry is not found), getpwnam_r returns 0 and stores a NULL pointer at the location pointed to by result. The getpwnam_r function may fail if:
  • ERANGE -- Insufficient storage was supplied through buffer and bufsize to contain the data to be referenced by the resulting passwd structure.

getpwuid, getpwuid_r (ALPHA, I64)

The getpwuid function returns information about a user database entry for the specified uid.

The getpwuid_r function is a reentrant version of getpwuid .


Format

#include <pwd.h>

struct passwd *getpwuid (uid_t uid); (ISO POSIX-1)

struct passwd *getpwuid (uid_t uid, ...); (HP C EXTENSION)

int getpwuid_r (uid_t uid, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result); (ISO POSIX-1)

int getpwuid_r (uid_t uid, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result, ...); (HP C EXTENSION)

Function Variants The getpwuid and getpwuid_r functions have variants named __32_getpwuid , _getpwuid_r32 and __64_getpwuid , _getpwuid_r64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.10 for more information on using pointer-size-specific functions.

Arguments

uid

The user ID (UID) for which the attributes are to be read.

pwd

The location where the retrieved passwd structure is to be placed.

buffer

A working buffer for the result argument that is able to hold the entry in the passwd structure. Storage referenced by the passwd structure is allocated from the memory provided with the buffer argument, which is bufsize characters in size.

bufsize

The length of the character array that buffer points to.

result

Upon successful return, result is set to pwd. Upon unsuccessful return, result is set to NULL.

...

An optional argument that can be either 1 or 0. If you specify 1, the directory specification is returned in OpenVMS format. If you specify 0, the directory specification (pathname) is returned in UNIX style format. If you omit this argument, the function returns the directory specification according to your current command-language interpreter. For more information about UNIX style directory specifications, see Section 1.4.3.

Description

The getpwuid function searches the user database for an entry with the specified uid. The function returns the first user entry in the database with a pw_uid member of the passwd structure that matches the uid argument.

The passwd structure is defined in the <pwd.h> header file as follows:

pw_name The user's login name.
pw_uid The numerical user ID.
pw_gid The numerical group ID.
pw_dir The home directory of the user.
pw_shell The initial program for the user.

Note

All information generated by the getpwuid function is stored in a per-thread static area and is overwritten on subsequent calls to the function.

The getpwuid_r function is the reentrant version of getpwuid . The getpwuid_r function updates the passwd structure pointed to by pwd and stores a pointer to that structure at the location pointed to by result. The structure will contain an entry from the user database with a matching uid. Storage referenced by the structure is allocated from the memory provided with the buffer argument, which is bufsize characters in size. The maximum size needed for this buffer can be determined with the _SC_GETPW_R_SIZE_MAX parameter of the sysconf function. On error or if the requested entry is not found, a NULL pointer is returned at the location pointed to by result.

Applications wishing to check for error situations should set errno to 0 before calling getpwuid . If getpwuid returns a NULL pointer and errno is nonzero, an error occurred.


Return Values

x getpwuid returns a pointer to a valid passwd structure, if a matching entry is found.
NULL getpwuid returns NULL if an error occurred or a matching entry was not found. errno is set to indicate the error. The getpwuid function may fail if:
  • EIO -- An I/O error has occurred.
  • EINTR -- A signal was caught during getpwnam .
  • EMFILE -- OPEN_MAX file descriptors are currently open in the calling process.
  • ENFILE -- The maximum allowable number of files is currently open in the system.
0 When successful, getpwuid_r returns 0 and stores a pointer to the updated passwd structure at the location pointed to by result.
0 When unsuccessful (on error or if the requested entry is not found), getpwuid_r returns 0 and stores a NULL pointer at the location pointed to by result. The getpwuid_r function may fail if:
  • ERANGE -- Insufficient storage was supplied through buffer and bufsize to contain the data to be referenced by the resulting passwd structure.


Previous Next Contents Index