Last active
August 29, 2015 14:06
-
-
Save jsuwo/d65eb6184157739dd2a8 to your computer and use it in GitHub Desktop.
Code samples from Computer Networks I Lab Manual, Chapter 3 - Program I/O with the Getopt and Syslog Libraries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#define NAME_LENGTH 20 | |
int main() | |
{ | |
char name[NAME_LENGTH]; | |
printf("Please enter your name: "); | |
if (fgets(name, NAME_LENGTH, stdin) != NULL) | |
{ | |
printf("Nice to meet you, %s", name); | |
} | |
exit(EXIT_SUCCESS); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <syslog.h> | |
#include <stdlib.h> | |
int main() | |
{ | |
int i = 42; | |
char* str = "universe"; | |
openlog("fig3-10", LOG_PERROR | LOG_PID | LOG_NDELAY, LOG_USER); | |
syslog(LOG_INFO, "The answer to the %s is %d", str, i); | |
closelog(); | |
exit(EXIT_SUCCESS); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <syslog.h> | |
int main(int argc, char** argv) | |
{ | |
FILE* file; | |
openlog("fig3-11", LOG_PERROR | LOG_PID | LOG_NDELAY, LOG_USER); | |
syslog(LOG_DEBUG, "Program started"); | |
syslog(LOG_DEBUG, "Testing arguments"); | |
syslog(LOG_DEBUG, "Arguments passed to the program: %d", argc); | |
// Check if a filename was passed as an argument | |
if (argc != 2) | |
{ | |
syslog(LOG_ERR, "No filename specified"); | |
closelog(); | |
exit(EXIT_FAILURE); | |
} | |
// If so, open the file | |
syslog(LOG_DEBUG, "Opening file '%s'", argv[1]); | |
file = fopen(argv[1], "r"); | |
// Ensure the file was opened successfully | |
if (file == NULL) | |
{ | |
syslog(LOG_ERR, "The specified file could not be opened"); | |
closelog(); | |
exit(EXIT_FAILURE); | |
} | |
syslog(LOG_INFO, "The file '%s' exists and was successfully opened!", argv[1]); | |
close(file); | |
closelog(); | |
exit(EXIT_SUCCESS); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <syslog.h> | |
int main(int argc, char** argv) | |
{ | |
FILE* file; | |
setlogmask(LOG_UPTO(LOG_INFO)); | |
openlog("fig3-12", LOG_PERROR | LOG_PID | LOG_NDELAY, LOG_USER); | |
syslog(LOG_DEBUG, "Program started"); | |
syslog(LOG_DEBUG, "Testing arguments"); | |
syslog(LOG_DEBUG, "Arguments passed to the program: %d", argc); | |
if (argc != 2) | |
{ | |
syslog(LOG_ERR, "No filename specified"); | |
closelog(); | |
exit(EXIT_FAILURE); | |
} | |
syslog(LOG_DEBUG, "Opening file '%s'", argv[1]); | |
file = fopen(argv[1], "r"); | |
if (file == NULL) | |
{ | |
syslog(LOG_ERR, "The specified file could not be opened"); | |
closelog(); | |
exit(EXIT_FAILURE); | |
} | |
syslog(LOG_INFO, "The file '%s' exists and was successfully opened!", argv[1]); | |
close(file); | |
closelog(); | |
exit(EXIT_SUCCESS); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdbool.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int main(int argc, char** argv) | |
{ | |
bool verbose = false; | |
char* log_filename; | |
int i; | |
// Iterate over the command line arguments | |
for (i = 1; i < argc; ++i) | |
{ | |
// Check for the --verbose / -v flag | |
if (strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) | |
{ | |
verbose = true; | |
} | |
// Check for the --log / -l flag | |
else if (strcmp(argv[i], "--log") == 0 || strcmp(argv[i], "-l") == 0) | |
{ | |
// Ensure that an argument was provided | |
if (i + 1 == argc) { | |
fprintf(stderr, "The --log / -l option requires an argument\n"); | |
exit(EXIT_FAILURE); | |
} | |
else { | |
log_filename = argv[++i]; | |
} | |
} | |
} | |
printf("Verbose : %s\n", verbose ? "true" : "false"); | |
printf("Log : %s\n", log_filename); | |
exit(EXIT_SUCCESS); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <getopt.h> | |
#include <stdbool.h> | |
int main(int argc, char** argv) | |
{ | |
int c; | |
bool verbose = false; | |
char* log_filename = NULL; | |
while ((c = getopt(argc, argv, "vl:")) != -1) | |
{ | |
switch(c) | |
{ | |
case 'v': | |
verbose = true; | |
break; | |
case 'l': | |
printf("-l called -- ignoring for now\n"); | |
break; | |
case '?': | |
// Error message already printed by getopt -- we'll just exit | |
exit(EXIT_FAILURE); | |
break; | |
} | |
} | |
printf("Verbose : %s\n", verbose ? "true" : "false"); | |
printf("Log : %s\n", log_filename); | |
exit(EXIT_SUCCESS); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <getopt.h> | |
#include <stdbool.h> | |
int main(int argc, char** argv) | |
{ | |
int c; | |
bool verbose = false; | |
char* log_filename = NULL; | |
while ((c = getopt(argc, argv, "vl:")) != -1) | |
{ | |
switch(c) | |
{ | |
case 'v': | |
verbose = true; | |
break; | |
case 'l': | |
log_filename = optarg; | |
break; | |
case '?': | |
// Error message already printed by getopt -- we'll just exit | |
exit(EXIT_FAILURE); | |
break; | |
} | |
} | |
printf("Verbose : %s\n", verbose ? "true" : "false"); | |
printf("Log : %s\n", log_filename); | |
exit(EXIT_SUCCESS); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <getopt.h> | |
#include <stdbool.h> | |
int main(int argc, char** argv) | |
{ | |
int c, i; | |
bool verbose = false; | |
char* log_filename = NULL; | |
while ((c = getopt(argc, argv, "vl:")) != -1) | |
{ | |
switch(c) | |
{ | |
case 'v': | |
verbose = true; | |
break; | |
case 'l': | |
log_filename = optarg; | |
break; | |
case '?': | |
// Error message already printed by getopt -- we'll just exit | |
exit(EXIT_FAILURE); | |
break; | |
} | |
} | |
printf("Verbose : %s\n", verbose ? "true" : "false"); | |
printf("Log : %s\n", log_filename); | |
for (i = optind; i < argc; ++i) | |
{ | |
printf("Processing file %s\n", argv[i]); | |
} | |
exit(EXIT_SUCCESS); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <getopt.h> | |
int main(int argc, char** argv) | |
{ | |
int c, i; | |
static int verbose_flag = 0; | |
char* log_filename = NULL; | |
while (1) | |
{ | |
static struct option long_options[] = | |
{ | |
{"verbose", no_argument, &verbose_flag, 1 }, | |
{"log", required_argument, 0, 'l'}, | |
{0, 0, 0, 0} | |
}; | |
int option_index = 0; | |
c = getopt_long(argc, argv, "vl:", long_options, &option_index); | |
// If we've reached the end of the options, stop iterating | |
if (c == -1) | |
break; | |
switch (c) | |
{ | |
case 'l': | |
log_filename = optarg; | |
break; | |
case 'v': | |
verbose_flag = 1; | |
break; | |
case '?': | |
// Error message already printed by getopt_long -- we'll just exit | |
exit(EXIT_FAILURE); | |
break; | |
} | |
} | |
printf("Verbose : %s\n", verbose_flag ? "true" : "false"); | |
printf("Log : %s\n", log_filename); | |
for (i = optind; i < argc; ++i) | |
{ | |
printf("Processing file %s\n", argv[i]); | |
} | |
exit(EXIT_SUCCESS); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <syslog.h> | |
#include <stdlib.h> | |
int main() | |
{ | |
openlog("fig3-7", LOG_PID | LOG_NDELAY, LOG_USER); | |
syslog(LOG_NOTICE, "Hello Syslog!"); | |
syslog(LOG_INFO, "Here's an informational message."); | |
syslog(LOG_WARNING, "Here's a warning."); | |
syslog(LOG_ERR, "Here's an error."); | |
syslog(LOG_DEBUG, "Here's some debugging information.\n"); | |
closelog(); | |
exit(EXIT_SUCCESS); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <syslog.h> | |
#include <stdlib.h> | |
int main() | |
{ | |
openlog("fig3-8", LOG_PERROR | LOG_PID | LOG_NDELAY, LOG_USER); | |
syslog(LOG_NOTICE, "Hello Syslog!"); | |
syslog(LOG_INFO, "Here's an informational message."); | |
syslog(LOG_WARNING, "Here's a warning."); | |
syslog(LOG_ERR, "Here's an error."); | |
syslog(LOG_DEBUG, "Here's some debugging information.\n"); | |
closelog(); | |
exit(EXIT_SUCCESS); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <syslog.h> | |
#include <stdlib.h> | |
void b() | |
{ | |
syslog(LOG_DEBUG, "Entering b()"); | |
syslog(LOG_DEBUG, "Leaving b()"); | |
} | |
void a() | |
{ | |
syslog(LOG_DEBUG, "Entering a()"); | |
b(); | |
syslog(LOG_DEBUG, "Leaving a()"); | |
} | |
int main() | |
{ | |
openlog("fig3-9", LOG_PERROR | LOG_PID | LOG_NDELAY, LOG_USER); | |
a(); | |
closelog(); | |
exit(EXIT_SUCCESS); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment