0% found this document useful (0 votes)
56 views6 pages

Message

Uploaded by

Manish Meena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views6 pages

Message

Uploaded by

Manish Meena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <unistd.h>
int main(int argc, char **argv)
{
void *handle;
char buf[512], *error;
long int (*convert)(char *);
/* check for argument */
if (argc < 2) {
fprintf(stderr, "usage: %s <integer>\n", argv[0]);
exit(1);
}
/* load shared library function */
if (!getcwd(buf, sizeof(buf) - 1)) {
fprintf(stderr, "failed to obtain current directory");
exit(1);
}
strncat(buf, "/libconvert.so", sizeof(buf) - (strlen(buf)
+ 1));
fprintf(stdout, "using: %s\n", buf);
handle = dlopen (buf, RTLD_NOW);
if (!handle) {
fprintf(stderr, "falling back to default lib ...\n");
if (!(handle = dlopen("/usr/local/lib/libconvert.so",
RTLD_NOW))) {

This study source was downloaded by 100000819592957 from CourseHero.com on 02-21-2023 07:10:43 GMT -06:00

https://www.coursehero.com/file/132855292/messagetxtdocx/
fputs(dlerror(), stderr);
exit(1);
}
}
convert = dlsym(handle, "atoi");
if ((error = dlerror()) != NULL) {
fputs(error, stderr);
exit(1);
}
/* convert first argument to integer and multiply by two */
fprintf(stdout, "%ld\n", 2 * (*convert)(argv[1]));
/* cleanup */
dlclose(handle);
exit(0);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int len(int argc, char **argv)
{
/* set up command buffer */
char cmdbuf[128] = "export IFS=' \t\n'; /usr/bin/file ";
char *input = cmdbuf + strlen(cmdbuf);
int len = sizeof(cmdbuf) - (strlen(cmdbuf) + 1);
gid_t egid = getegid();
setregid(egid, egid);

This study source was downloaded by 100000819592957 from CourseHero.com on 02-21-2023 07:10:43 GMT -06:00

https://www.coursehero.com/file/132855292/messagetxtdocx/
/* read input -- use safe function to prevent buffer
overrun */
fprintf(stdout, "Please enter a filename: ");
fgets(input, len, stdin);
/* sanitize input -- replace unsafe shell characters */
for (; *input != '\0'; ++input) {
switch (*input) {
case '|': case '&':
case '<': case '>':
case '!': case '$':
case ';':
*input = ' ';
break;
}
}
/* execute command */
system(cmdbuf);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
char cmdbuf[128] = "echo interrupt signal caught,
terminating ";
char *progname;
/*

This study source was downloaded by 100000819592957 from CourseHero.com on 02-21-2023 07:10:43 GMT -06:00

https://www.coursehero.com/file/132855292/messagetxtdocx/
* Handle a ^C keyboard interrupt in case the program is
running
* too long and the user terminates.
*/
void handle_signal(int sig)
{
int len = sizeof(cmdbuf) - (strlen(cmdbuf) + 1);
if (strlen(progname) > len)
progname[len] = '\0';
strcat(cmdbuf, progname);
system(cmdbuf);
exit(1);
}
void usage()
{
printf("%s <n> where 0 < n <= 5.000\n", progname);
exit(1);
}
/*
* The program takes one argument line parameter n (which
has to be a
* positive integer input parameter) and then prints out the
first n
* prime numbers.
*/
int sighandler(int argc, char **argv)
{
struct sigaction sa;

This study source was downloaded by 100000819592957 from CourseHero.com on 02-21-2023 07:10:43 GMT -06:00

https://www.coursehero.com/file/132855292/messagetxtdocx/
int cnt, N, found;
unsigned long candidate, divisor;
gid_t egid = getegid();
setregid(egid, egid);
/* set up signal handling */
memset(&sa, sizeof(struct sigaction), 0);
sa.sa_handler = handle_signal;
sigaction(SIGINT, &sa, NULL);
/* process argument */
progname = argv[0];
if (argc != 2)
usage();
N = strtol(argv[1], NULL, 10);
if ((N <= 0) || (N > 5000))
usage();
/* calculate prime numbers -- simple sieve */
candidate = 1;
for (cnt = 0; cnt < N; ++cnt) {
for (;;) {
found = 1;
divisor = 2;
candidate += 1;
while (divisor <= candidate/2) {
if ((candidate % divisor) == 0) {
found = 0;
break;
}

This study source was downloaded by 100000819592957 from CourseHero.com on 02-21-2023 07:10:43 GMT -06:00

https://www.coursehero.com/file/132855292/messagetxtdocx/
else
++divisor;
}
if (found)
break;
}
printf("%ld\n", candidate);
}
return 0;
}

This study source was downloaded by 100000819592957 from CourseHero.com on 02-21-2023 07:10:43 GMT -06:00

https://www.coursehero.com/file/132855292/messagetxtdocx/
Powered by TCPDF (www.tcpdf.org)

You might also like