[go: up one dir, main page]

Menu

[014e0a]: / src / ftok.c  Maximize  Restore  History

Download this file

236 lines (199 with data), 6.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* shmcat: Dump Shared Memory Segments and more
* (C) 2012, 2014 by Stefan Gast
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ftok.c: ftok main program
*/
#include "config.h"
#include "keythelper.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <locale.h>
#include <unistd.h>
#include <errno.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/ipc.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include "translate.h"
enum
{
EXITCODE_OK = 0,
EXITCODE_ERROR = 1,
EXITCODE_USAGE = 10
};
typedef enum
{
OUTPUT_HEXADECIMAL = 0,
OUTPUT_DECIMAL = 1,
OUTPUT_OCTAL = 2
} OutputFormat;
static void usage(const char *progname)
{
printf(_("Usage: %s [OPTION] FILENAME ID\n"), progname);
puts(_("Calculate a System V interprocess communication key using the\n"
"ftok(3) library function and print the result to\n"
"standard output.\n"));
puts(_("Following OPTIONs are supported:\n"));
#ifdef HAVE_GETOPT_LONG
puts(_("-d, --decimal decimal output (instead of hexadecimal).\n"
"-o, --octal octal output (instead of hexadecimal).\n"));
puts(_("-h, --help display this help and exit.\n"
"-? same as -h.\n"
"-V, --version output version information and exit.\n"));
#else
puts(_("-d decimal output (instead of hexadecimal).\n"
"-o octal output (instead of hexadecimal).\n"));
puts(_("-h display this help and exit.\n"
"-? same as -h.\n"
"-V output version information and exit.\n"));
#endif
}
static void show_version(const char *progname)
{
printf(_("%s, part of %s\n"), progname, PACKAGE_STRING);
puts(_("Copyright (C) 2012, 2014 by Stefan Gast"));
puts(_("This is free software. You may redistribute copies of it under the terms of\n"
"the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n"
"There is NO WARRANTY, to the extent permitted by law.\n"));
/* TRANSLATORS: The placeholder indicates the bug-reporting address
for this package. Please add _another line_ saying
"Report translation bugs to <...>\n" with the address for translation
bugs (typically your translation team's web or email address). */
printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
}
static void wrong_usage(const char *progname)
{
fprintf(stderr, _("%s: Wrong usage, try \"%s -h\" for more information!\n"),
progname, progname);
}
int main(int argc, char **argv)
{
const char *optstr = "dhoV";
#if defined HAVE_GETOPT_LONG && defined HAVE_GETOPT_H
static const struct option long_options[] =
{
{ "decimal", no_argument, NULL, 'd' },
{ "help", no_argument, NULL, 'h' },
{ "octal", no_argument, NULL, 'o' },
{ "version", no_argument, NULL, 'V' },
{ NULL, 0, NULL, 0 }
};
#endif
int opt;
long id;
char *endptr;
OutputFormat format = OUTPUT_HEXADECIMAL;
key_t key;
int printfResult;
/* Initialize i18n support */
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
/* Process options */
opterr = 0;
optind = 1;
#if defined HAVE_GETOPT_LONG && defined HAVE_GETOPT_H
while((opt = getopt_long(argc, argv, optstr, long_options, NULL)) != -1)
#else
while((opt = getopt(argc, argv, optstr)) != -1)
#endif
{
switch(opt)
{
case 'd':
format = OUTPUT_DECIMAL;
break;
case 'h':
usage(argv[0]);
return EXITCODE_OK;
case 'o':
format = OUTPUT_OCTAL;
break;
case 'V':
show_version(argv[0]);
return EXITCODE_OK;
case '?':
if(optopt == '?')
{
usage(argv[0]);
return EXITCODE_OK;
}
else
{
wrong_usage(argv[0]);
return EXITCODE_USAGE;
}
default:
fprintf(stderr, _("%s: BUG: Unhandled command line option: %c\n"), argv[0], opt);
return EXITCODE_ERROR;
}
}
/* The help and version options cause the program to quit in the
* getopt loop, so now we can do the real work, but we need
* the two arguments FILENAME and ID now. */
if(optind + 2 != argc)
{
wrong_usage(argv[0]);
return EXITCODE_USAGE;
}
/* Try to get the ID. Note that we also support 0 as an ID, even though
* the man page of ftok says the behavior is unspecified in this case.
* But on Linux this works as well, so we just assume the user knows
* what he is doing in this case. */
errno = 0;
id = strtol(argv[optind+1], &endptr, 0);
if(*endptr != '\0' || id < 0L || id > 255L || errno != 0)
{
fprintf(stderr, _("%s: The ID has to be a number between 0 and 255!\n"), argv[0]);
return EXITCODE_USAGE;
}
/* Now try to get the key from the parameters. */
key = ftok(argv[optind], (int)id);
if(key == (key_t)-1)
{
fprintf(stderr, _("%s: Cannot calculate a key for the given parameters: %s\n"),
argv[0], strerror(errno));
return EXITCODE_ERROR;
}
/* Finally, print the result */
switch(format)
{
case OUTPUT_HEXADECIMAL:
/* Convert to matching unsigned type first for hexadecimal output */
printfResult = printf("0x%jx\n", (uintmax_t)(KEY_T_UNSIGNED)key);
break;
case OUTPUT_DECIMAL:
/* Decimal can be signed */
printfResult = printf("%ji\n", (intmax_t)key);
break;
case OUTPUT_OCTAL:
/* Convert to matching unsigned type first for octal output */
printfResult = printf("0%jo\n", (uintmax_t)(KEY_T_UNSIGNED)key);
break;
}
if(printfResult <= 0)
{
fprintf(stderr, _("%s: Cannot print to standard output: %s\n"),
argv[0], strerror(errno));
return EXITCODE_ERROR;
}
return EXITCODE_OK;
}