Subversion Repositories Kolibri OS

Compare Revisions

Regard whitespace Rev 8379 → Rev 8380

/programs/develop/ktcc/trunk/bin/lib/libck.a
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
/programs/develop/ktcc/trunk/libc/include/getopt.h
0,0 → 1,9
#ifndef GETOPT_H
#define GETOPT_H
 
extern int optind, opterr;
extern char *optarg;
 
int getopt(int argc, char *argv[], char *optstring);
 
#endif
/programs/develop/ktcc/trunk/libc/stdlib/getopt.c
0,0 → 1,63
#include <stdio.h>
#include <string.h>
#include <getopt.h>
 
char *optarg = NULL; // global argument pointer
int optind = 0; // global argv index
 
int getopt(int argc, char *argv[], char *optstring) {
static char *next = NULL;
char c;
char *cp = NULL;
if (optind == 0)
next = NULL;
 
optarg = NULL;
 
if (next == NULL || *next == '\0') {
if (optind == 0)
optind++;
 
if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0') {
optarg = NULL;
if (optind < argc)
optarg = argv[optind];
return EOF;
}
 
if (strncmp(argv[optind], "--", 2) == 0) {
optind++;
optarg = NULL;
if (optind < argc)
optarg = argv[optind];
return EOF;
}
 
next = argv[optind];
next++; // skip past -
optind++;
}
 
c = *next++;
cp = strchr(optstring, c);
 
if (cp == NULL || c == ':')
return '?';
 
cp++;
if (*cp == ':') {
if (*next != '\0') {
optarg = next;
next = NULL;
}
else if (optind < argc) {
optarg = argv[optind];
optind++;
}
else {
return '?';
}
}
return c;
}
/programs/develop/ktcc/trunk/samples/Makefile
14,6 → 14,7
../bin/kos32-tcc dir_example.c -lck -o dir_example.kex -I ../libc/include
../bin/kos32-tcc net/tcpsrv_demo.c -lck -o net/tcpsrv_demo.kex -I ../libc/include
../bin/kos32-tcc net/nslookup.c -lck -lnetwork -o net/nslookup.kex -I ../libc/include
../bin/kos32-tcc getopt_ex.c -lck -o getopt_ex -I ../libc/include
 
clean:
rm *.kex
/programs/develop/ktcc/trunk/samples/build_all.sh
14,4 → 14,5
../tcc dir_example.c -lck -o /tmp0/1/dir_example
../tcc net/tcpsrv_demo.c -lck -o /tmp0/1/tcpsrv_demo
../tcc net/nslookup.c -lck -lnetwork -o /tmp0/1/nslookup
../tcc getopt_ex.c -lck -o /tmp0/1/getopt_ex
exit
/programs/develop/ktcc/trunk/samples/getopt_ex.c
0,0 → 1,38
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
 
void main(int argc, char *argv[]) {
int c;
if(argc<2)
{
puts("Usage: getopt_ex [options]\n");
puts("-a Show 'Option a'");
puts("-B Show 'Option B'");
puts("-n [num] Show 'num'");
}
while ((c = getopt(argc, argv, "aBn:")) != EOF) {
switch (c) {
case 'a':
puts("Option 'a'");
break;
 
case 'B':
puts("Option 'B'");
break;
 
case 'n':
printf("Option n: value=%d\n", atoi(optarg));
break;
 
case '?':
printf("ERROR: illegal option %s\n", argv[optind-1]);
exit(0);
 
default:
printf("WARNING: no handler for option %c\n", c);
exit(0);
}
}
exit(0);
}