Ovo su neke moje C vežbice. Možda će nekome koristiti.
sys_dir.c - bazirano na primeru iz GNU libC dokumentacije. ls ili dir, samo bez opcija.
Code: Select all
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv)
{
char *buffer;
size_t size = 2048;
DIR *wd;
struct dirent *distruct;
/* Try to allocate the memory for the buffer */
buffer = (char *) malloc (size);
if (buffer == NULL)
fprintf(stderr, "%s: %s: %d: Can't allocate memory!",
argv[0], "sys_dir.c", 13);
/* Get the current dir into the buffer */
buffer = getcwd(buffer, size);
/* Open the directory */
wd = opendir(buffer);
/* If successfull */
if (wd != NULL)
{
while ((distruct = readdir(wd)))
puts (distruct->d_name);
}
else
fprintf(stderr, "%s: %s: Can't stat directory!",
argv[0], buffer);
closedir(wd);
/* Free the buffer if needed */
if (buffer != NULL)
free(buffer);
return 0;
}
term1.c - mala vežba glibc terminal funkcija
Code: Select all
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdlib.h>
#define BUFFLEN 256
int main(int argc, char *argv[])
{
static char termid[L_ctermid];
struct termios *termstruct;
int filedesc = STDOUT_FILENO;
termstruct = (struct termios *) malloc (sizeof(struct termios));
ctermid(termid);
puts(termid);
printf("%d\n", isatty(filedesc));
printf("%s\n", ttyname(filedesc));
tcgetattr(filedesc, termstruct);
printf("%x\n", termstruct->c_cflag);
free(termstruct);
return 0;
}
Igranje sa glibc memorijskim funkcijama:
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
struct test {
int x;
int y;
};
struct test *ptr;
ptr = (struct test *) malloc (sizeof (struct test));
if (ptr == NULL) abort();
memset(ptr, 0, sizeof(struct test));
printf("Size of test: %d.\n", sizeof(struct test));
printf("Size of int: %d. \n", sizeof(int));
free(ptr);
return 0;
}
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *ptr;
ptr = (char *)malloc(sizeof(ptr));
memset(ptr, 0, sizeof(ptr));
printf("%d\n", sizeof(ptr));
printf("Allocated %d. \n", strlen(ptr) + 1);
free(ptr);
return 0;
}
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define strleng 6
int main(int argc, char *argv[])
{
char *ptr;
char *copy_string;
ptr = malloc(6);
copy_string = malloc(256);
memset(copy_string, 0, sizeof(copy_string));
memset(ptr, 0, sizeof(ptr));
strcpy(ptr, "proba");
printf("String: %s, size: %d.\n", ptr, strlen(ptr));
strcpy(copy_string, "veliki_string");
if( (strlen(ptr)) < (strlen(copy_string)))
ptr = realloc(ptr, (sizeof(copy_string) + 1));
strcpy(ptr, copy_string);
printf("String: %s, size: %d.\n", ptr, strlen(ptr));
free(ptr);
return 0;
}
glibc error funkcije:
Code: Select all
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main(int argc, char **argv)
{
FILE *test;
int err;
test = fopen("readme.txt", "r");
if (test == NULL)
{
err=errno;
puts(strerror(err)); // No such file or directory
perror("Error"); // Error: No such file or directory
}
else
fclose(test);
return 0;
}
Code: Select all
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <error.h>
int main(int argc, char **argv)
{
FILE *test;
int err;
test = fopen("readme.txt", "r");
if (test == NULL)
{
err=errno;
error(0, err, "The program %s failed", argv[0]);
error_at_line(1, err, __FILE__, __LINE__, "The program failed", argv[0]);
}
else
fclose(test);
return 0;
}
Mali standardni Unix alati bazirani na Unix V7 manual stranama:
sleep
Code: Select all
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
/* V7 limit */
#define MAXSEC 65536
/* Print the usage info and exit unsuccessfully */
void usage(void)
{
puts("Usage: sleep SECONDS");
exit(-1);
}
int main(int argc, char **argv)
{
if (argc != 2)
{
/* We accept only 1 argument - number of seconds */
/* So if more or less, print usage info & bail out. */
usage();
}
else
{
/* This part executes only if argc == 2 */
int k = 0;
/* Check atoi output */
if ((k = atoi(argv[1])) == 0)
usage();
else
{
/* Check for the maximal value */
if (k < MAXSEC)
sleep(k);
else
{
fprintf(stderr, "Error: number of seconds too large!\n");
usage();
}
}
}
return 0;
}
cwd
Code: Select all
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXPATHNAME 1024
int main(int argc, char **argv)
{
char currpath[MAXPATHNAME];
if ((getcwd(currpath, MAXPATHNAME)) == NULL)
perror("Error");
puts(currpath);
return 0;
}
echo
Code: Select all
#include <stdio.h>
int main(int argc, char **argv)
{
int nnl = 0;
int cnt = 0;
/* process the arguments */
for(cnt = 1; cnt < argc; cnt++)
{
/* in case of '-n', set nnl to > 0 */
if (argv[cnt][0] == '-' && argv[cnt][1] == 'n' && argv[cnt][2] == '\0')
nnl++;
else
{
/* print out user arguments */
fputs(argv[cnt], stdout);
if (cnt != argc - 1)
fputs(" ", stdout);
}
}
/* if not '-n' */
if (nnl == 0)
fputs("\n", stdout);
return(0);
}
cat
Code: Select all
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main(int argc, char **argv)
{
int cnt = 0, temp = 0, status = 0;
FILE *istream;
/*
* If no arguments, check for input from
* stdin, than display every character
* to stdout. Otherwise, display files
* specified in command line arguments.
*/
if (argc == 1)
{
while((temp = fgetc(stdin)) != EOF)
fputc(temp, stdout);
}
else
{
/*
* Browse through all the arguments
*/
for (cnt = 1; cnt < argc; cnt++)
{
/*
* Try to open the file, otherwise
* exit with an error message
*/
istream = fopen(argv[cnt], "r");
status = errno;
if (istream == NULL)
{
fprintf(stderr, "%s: %s: %s\n",
argv[0], argv[cnt], strerror(status));
break;
}
/*
* get a character at the time from specified
* file and display it to stdout.
*/
while((temp = fgetc(istream)) != EOF)
fputc(temp, stdout);
fclose(istream);
}
}
return (0);
}
sleep
Code: Select all
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
/* V7 limit */
#define MAXSEC 65536
/* Print the usage info and exit unsuccessfully */
void usage(void)
{
puts("Usage: sleep SECONDS");
exit(-1);
}
int main(int argc, char **argv)
{
if (argc != 2)
{
/* We accept only 1 argument - number of seconds */
/* So if more or less, print usage info & bail out. */
usage();
}
else
{
/* This part executes only if argc == 2 */
int k = 0;
/* Check atoi output */
if ((k = atoi(argv[1])) == 0)
usage();
else
{
/* Check for the maximal value */
if (k < MAXSEC)
sleep(k);
else
{
fprintf(stderr, "Error: number of seconds too large!\n");
usage();
}
}
}
return 0;
}
Jednostavno.