NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>






int runcommand(char *tok[], int counter){

int pid = fork();

if(pid == 0) // Filho.
execvp(tok[0], tok);
if (pid > 0 && strcmp(tok[counter-1], "&" )) // Pai.
waitpid(pid,NULL,0);
if (pid < 0)
perror("fork");

}

void listDir(char *dirName)
{
DIR* dir;
struct dirent *dirEntry;
struct stat inode;
char name[1000];
dir = opendir(dirName);
if (dir == 0) {
perror ("Erro");
exit(1);
}
while ((dirEntry=readdir(dir)) != 0) {
sprintf(name,"%s/%s",dirName,dirEntry->d_name);
lstat (name, &inode);

// verificar o tipo de ficheiro
if (S_ISDIR(inode.st_mode))
printf("dir ");
else if (S_ISREG(inode.st_mode))
printf ("fis ");
else
if (S_ISLNK(inode.st_mode))
printf ("lnk ");
else;
printf(" %sn", dirEntry->d_name);
}
}


int main() {

char line[100];
char *tok[10];
int counter = 0;
char path[100];

printf("> ");

fgets(line, 100, stdin);

tok[0] = strtok(line, " tn");

// Gravar todas as palavras.
while(tok[counter] != ''){
counter++;
tok[counter] = strtok(NULL, " tn");
}


// Interpretador de comandos.
do{


// Comando "mostra"
if (tok[0] =='mostra') {

int fd, leitura;
char buffer[21];

if (tok < 2) {
fputs("nao tem nome ficheiro", stderr);
exit(1);
}

fd = open(tok[1], O_RDONLY);

if (fd < 0) {
perror("erro na abertura do ficheiro");
exit(1);
}

// ficheiro disponivel no descritor fd
leitura = read( fd, buffer, 20);

while (leitura > 0) {
printf("lidos: %dn", leitura);
buffer[leitura] = '';

puts(buffer);

leitura = read( fd, buffer, 20);
}

// -1 houve erro
if (leitura == -1) {
perror("erro na leitura do ficheiro");
exit(1);
} else { // 0 EOF
printf("Fimn");
}

close(fd);
}

// Comando "cria"
else if (tok[0] =='cria') {

int fd, leitura, mod;
char buffer[21];

if (tok < 2) {
fputs("nao tem nome ficheiro", stderr);
exit(1);
}

fd = open(tok[1], O_RDONLY);

if (fd < 0) {
close(fd);
fd = open(tok[1], O_WRONLY | O_CREAT | O_TRUNC, mod);
} else { printf("O ficheiro não pode ser criado (já existe?)"); }


close(fd);

}

// Comando "apaga"
else if (tok[0] =='apaga') {

int fd;
if (tok < 2) {
fputs("nao tem nome ficheiro", stderr);
exit(1);
}

fd = open(tok[1], O_RDONLY);

if (fd < 0) {

printf("O ficheiro não pode ser criado (já existe?)");
} else { remove(tok[1]);}


close(fd);
}


// Comando "conta"
else if (tok[0] =='conta') {
FILE *fp; int c, ct_lines;
ct_lines = 0;
fp = fopen(tok[1], "rb");
do {
c = fgetc(fp);
if (c == 'n')
ct_lines++;
} while (c != EOF);
fclose(fp);
printf("Numero de linhas: %d", ct_lines);
getch();
}

// Comando "informa"
else if (tok[0] =='informa') {
if (tok < 2) {
fputs("nao tem nome ficheiro", stderr);
exit(1);
}

struct stat fileStat;
if(stat(tok[1],&fileStat) < 0)
return 1;

printf("Informação para %sn",tok[1]);
printf("---------------------------n");
printf("Tamanho do ficheiro: tt%d bytesn",fileStat.st_size);
printf("Numro de links: t%dn",fileStat.st_nlink);
printf("Inode: tt%dn",fileStat.st_ino);

printf("Permissões do ficheiro: t");
printf( (S_ISDIR(fileStat.st_mode)) ? "d" : "-");
printf( (fileStat.st_mode & S_IRUSR) ? "r" : "-");
printf( (fileStat.st_mode & S_IWUSR) ? "w" : "-");
printf( (fileStat.st_mode & S_IXUSR) ? "x" : "-");
printf( (fileStat.st_mode & S_IRGRP) ? "r" : "-");
printf( (fileStat.st_mode & S_IWGRP) ? "w" : "-");
printf( (fileStat.st_mode & S_IXGRP) ? "x" : "-");
printf( (fileStat.st_mode & S_IROTH) ? "r" : "-");
printf( (fileStat.st_mode & S_IWOTH) ? "w" : "-");
printf( (fileStat.st_mode & S_IXOTH) ? "x" : "-");
printf("nn");

return 0;
}


// Comando "lista"
else if (tok[0] =='lista') {
if (tok != 2) {
printf ("Erro na inserção do comando ", tok[0]);
exit(0);
}
printf("Lista de diretorias:n");
listDir(tok[1]);
}

// Comando "vaipara"
else if (tok[0] =='vaipara') {
if (tok < 2)
{ printf("Erro no uso do comando");
exit(1);
}
if (chdir(tok[1]) != 0)
{ printf("Erro a mudar de directoria");
exit(1);
};
}

// Comando externo.
else
runcommand(tok, counter);

// Recomecar a leitura.
printf("> ");
fgets(line, 100, stdin);
counter = 0;
tok[0] = strtok(line, " tn");

while(tok[counter] != ''){
counter++;
tok[counter] = strtok(NULL, " tn");
}


} while(strcmp(tok[0], "exit") != 0);
}
// Terminar programa.
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.