#include <stdlib.h>
int getsubopt(char **restrict optionp, char *const *restrict tokens,
char **restrict valuep);
Exigences de macros de test de fonctionnalités pour la glibc (consulter feature_test_macros(7)) :
getsubopt() :
_XOPEN_SOURCE >= 500
|| /* Depuis la glibc 2.12 : */ _POSIX_C_SOURCE >= 200809L
ro,name=xyz
L'argument tokens est un pointeur vers un tableau de pointeurs (terminé par NULL) vers les marqueurs que getsubopt() recherche dans optionp. Les marqueurs doivent être distincts, contenant des chaînes terminées par un caractère nul d'au moins un caractère, sans signe égal ou virgule.
Chaque appel à getsubopt() renvoie une information sur la prochaine sous-option contenue dans optionp qui n'a pas été traitée. Le premier signal égal (s'il existe) est interprété comme un séparateur entre un nom et une valeur de sous-option. La valeur se termine à la prochaine virgule ou (pour la dernière sous-option) à la fin de la chaîne. Si le nom d'une sous-option correspond à un nom de tokens et qu'une chaîne de valeur est trouvée, getsubopt() définit *valuep à l'adresse de cette chaîne. La première virgule in optionp est surpassée par un octet nul, ainsi *valuep est exactement « la chaîne de valeur » de cette sous-option.
Si la sous-option est reconnue, mais qu'aucune chaîne de valeur n'existe, *valuep est défini à NULL.
When getsubopt() returns, optionp points to the next suboption, or to the null byte ('\0') at the end of the string if the last suboption was just processed.
Puisque *optionp est modifié, la première sous-option avant l'appel à getsubopt() n'est pas nécessairement la même qu'après.
| Interface | Attribut | Valeur |
| getsubopt() | Sécurité des threads | MT-Safe |
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int
main(int argc, char *argv[])
{
enum {
RO_OPT = 0,
RW_OPT,
NAME_OPT
};
char *const token[] = {
[RO_OPT] = "ro",
[RW_OPT] = "rw",
[NAME_OPT] = "name",
NULL
};
char *subopts;
char *value;
int opt;
int readonly = 0;
int readwrite = 0;
char *name = NULL;
int errfnd = 0;
while ((opt = getopt(argc, argv, "o:")) != -1) {
switch (opt) {
case 'o':
subopts = optarg;
while (*subopts != '\0' && !errfnd) {
switch (getsubopt(&subopts, token, &value)) {
case RO_OPT:
readonly = 1;
break;
case RW_OPT:
readwrite = 1;
break;
case NAME_OPT:
if (value == NULL) {
fprintf(stderr,
"Missing value for suboption '%s'\n",
token[NAME_OPT]);
errfnd = 1;
continue;
}
name = value;
break;
default:
fprintf(stderr,
"No match found for token: /%s/\n", value);
errfnd = 1;
break;
}
}
if (readwrite && readonly) {
fprintf(stderr,
"Only one of '%s' and '%s' can be specified\n",
token[RO_OPT], token[RW_OPT]);
errfnd = 1;
}
break;
default:
errfnd = 1;
}
}
if (errfnd || argc == 1) {
fprintf(stderr, "\nUsage: %s -o <suboptstring>\n", argv[0]);
fprintf(stderr,
"suboptions are 'ro', 'rw', and 'name=<value>'\n");
exit(EXIT_FAILURE);
}
/* Remainder of program... */
exit(EXIT_SUCCESS);
}
Cette traduction est une documentation libre ; veuillez vous reporter à la GNU General Public License version 3 concernant les conditions de copie et de distribution. Il n'y a aucune RESPONSABILITÉ LÉGALE.
Si vous découvrez un bogue dans la traduction de cette page de manuel, veuillez envoyer un message à