OSDN Git Service

When locale is enabled, sprintf("%.123s", str) is counting characters, not bytes...
[android-x86/external-toybox.git] / toys.h
1 /* Toybox infrastructure.
2  *
3  * Copyright 2006 Rob Landley <rob@landley.net>
4  */
5
6 // Stuff that needs to go before the standard headers
7
8 #include "generated/config.h"
9 #include "lib/portability.h"
10
11 // General posix-2008 headers
12 #include <ctype.h>
13 #include <dirent.h>
14 #include <errno.h>
15 #include <fcntl.h>
16 #include <fnmatch.h>
17 #include <grp.h>
18 #include <inttypes.h>
19 #include <limits.h>
20 #include <libgen.h>
21 #include <math.h>
22 #include <pwd.h>
23 #include <regex.h>
24 #include <sched.h>
25 #include <setjmp.h>
26 #include <signal.h>
27 #include <stdarg.h>
28 #include <stddef.h>
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <strings.h>
34 #include <sys/mman.h>
35 #include <sys/resource.h>
36 #include <sys/stat.h>
37 #include <sys/statvfs.h>
38 #include <sys/time.h>
39 #include <sys/times.h>
40 #include <sys/types.h>
41 #include <sys/utsname.h>
42 #include <sys/wait.h>
43 #include <syslog.h>
44 #include <time.h>
45 #include <unistd.h>
46 #include <utime.h>
47 #include <utmpx.h>
48
49 // Posix networking
50
51 #include <arpa/inet.h>
52 #include <netdb.h>
53 #include <net/if.h>
54 #include <netinet/in.h>
55 #include <netinet/tcp.h>
56 #include <poll.h>
57 #include <sys/socket.h>
58 #include <sys/un.h>
59
60 // Internationalization support (also in POSIX and LSB)
61
62 #include <locale.h>
63 #include <wchar.h>
64 #include <wctype.h>
65
66 // LSB 4.1 headers
67 #include <pty.h>
68 #include <sys/ioctl.h>
69 #include <sys/statfs.h>
70 #include <sys/sysinfo.h>
71
72 #include "lib/lib.h"
73 #include "toys/e2fs.h"
74
75 // Get list of function prototypes for all enabled command_main() functions.
76
77 #define NEWTOY(name, opts, flags) void name##_main(void);
78 #define OLDTOY(name, oldname, opts, flags)
79 #include "generated/newtoys.h"
80 #include "generated/oldtoys.h"
81 #include "generated/flags.h"
82 #include "generated/globals.h"
83
84 // These live in main.c
85
86 struct toy_list *toy_find(char *name);
87 void toy_init(struct toy_list *which, char *argv[]);
88 void toy_exec(char *argv[]);
89
90 // Flags describing command behavior.
91
92 #define TOYFLAG_USR      (1<<0)
93 #define TOYFLAG_BIN      (1<<1)
94 #define TOYFLAG_SBIN     (1<<2)
95 #define TOYMASK_LOCATION ((1<<4)-1)
96
97 // This is a shell built-in function, running in the same process context.
98 #define TOYFLAG_NOFORK   (1<<4)
99
100 // Start command with a umask of 0 (saves old umask in this.old_umask)
101 #define TOYFLAG_UMASK    (1<<5)
102
103 // This command runs as root.
104 #define TOYFLAG_STAYROOT (1<<6)
105 #define TOYFLAG_NEEDROOT (1<<7)
106 #define TOYFLAG_ROOTONLY (TOYFLAG_STAYROOT|TOYFLAG_NEEDROOT)
107
108 // Call setlocale to listen to environment variables.
109 // This invalidates sprintf("%.*s", size, string) as a valid length constraint.
110 #define TOYFLAG_LOCALE   (1<<8)
111
112 // Array of available commands
113
114 extern struct toy_list {
115   char *name;
116   void (*toy_main)(void);
117   char *options;
118   int flags;
119 } toy_list[];
120
121 // Global context shared by all commands.
122
123 extern struct toy_context {
124   struct toy_list *which;  // Which entry in toy_list is this one?
125   char **argv;             // Original command line arguments
126   char **optargs;          // Arguments left over from get_optflags()
127   unsigned optflags;       // Command line option flags from get_optflags()
128   int exitval;             // Value error_exit feeds to exit()
129   int optc;                // Count of optargs
130   int exithelp;            // Should error_exit print a usage message first?
131   int old_umask;           // Old umask preserved by TOYFLAG_UMASK
132   int toycount;            // Total number of commands in this build
133   int signal;              // generic_signal() records what signal it saw here
134   int signalfd;            // and writes signal to this fd, if set
135
136   // This is at the end so toy_init() doesn't zero it.
137   jmp_buf *rebound;        // longjmp here instead of exit when do_rebound set
138 } toys;
139
140 // Two big temporary buffers: one for use by commands, one for library functions
141
142 extern char toybuf[4096], libbuf[4096];
143
144 extern char **environ;
145
146 #define GLOBALS(...)
147
148 #define ARRAY_LEN(array) (sizeof(array)/sizeof(*array))